Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: document attempt to keep only tomlkit #444

Merged
merged 4 commits into from
Jan 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/nitpick/blender.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,27 @@ def _inner_custom_splitter(flat_key) -> Tuple[str, ...]:
return _inner_custom_splitter


# TODO: use only tomlkit and remove uiri/toml
# - tomlkit preserves comments
# - uiri/toml looks abandoned https://github.com/uiri/toml/issues/361
# Code to be used with tomlkit when merging styles
# merged_dict = unflatten(self._merged_styles, toml_style_splitter)
# def toml_style_splitter(flat_key: str) -> Tuple[str, ...]:
# """Splitter for TOML style files, in an attempt to remove empty TOML tables."""
# original = flat_key.split(SEPARATOR_FLATTEN)
# quoted = [quote_if_dotted(k) for k in original]
#
# first = quoted.pop(0)
# last = quoted.pop() if quoted else None
#
# grouped = [first]
# if quoted:
# grouped.append(SEPARATOR_DOT.join(quoted))
# if last:
# grouped.append(last)
# return tuple(grouped)


def flatten_quotes(dict_: JsonDict, separator=SEPARATOR_DOT) -> JsonDict:
"""Flatten a dict keeping quotes in keys."""
dict_with_quoted_keys = flatten(dict_, reducer=quote_reducer(separator))
Expand Down Expand Up @@ -455,6 +476,8 @@ def get_empty_inline_table(self):
class TomlDoc(BaseDoc):
"""TOML configuration format."""

# TODO: use only tomlkit and remove uiri/toml
# remove __init__() completely
def __init__(
self,
*,
Expand All @@ -474,13 +497,42 @@ def load(self) -> bool:
if self._string is not None:
# TODO: I tried to replace toml by tomlkit, but lots of tests break.
if self.use_tomlkit:
# TODO: use only tomlkit and remove uiri/toml
# Removing empty tables on loads() didn't work.
# The empty tables are gone, but:
# 1. the output has 2 blank lines at the top
# 2. the underlying dict is different than expected, and tests fail:
# 'NIP001 has an incorrect style. Invalid config:',
# '"pyproject.toml".tool.black: Unknown file. See '
# 'https://nitpick.rtfd.io/en/latest/plugins.html.']

# toml_obj = tomlkit.loads(self._string)
# if "tool.black" in self._string:
# from tomlkit.items import KeyType, SingleKey
#
# black_dict = toml_obj["pyproject.toml"]["tool"]["black"]
# toml_obj["pyproject.toml"].remove("tool")
# toml_obj.remove("pyproject.toml")
# toml_obj.add(SingleKey('"pyproject.toml".tool.black', KeyType.Bare), black_dict)
# result = tomlkit.dumps(toml_obj)
# print(result)
self._object = tomlkit.loads(self._string)
else:
self._object = toml.loads(self._string, decoder=InlineTableTomlDecoder(dict)) # type: ignore[call-arg,assignment]
if self._object is not None:
# TODO: tomlkit.dumps() renders comments and I didn't find a way to turn this off,
# but comments are being lost when the TOML plugin does dict comparisons.
if self.use_tomlkit:
# TODO: use only tomlkit and remove uiri/toml
# Removing empty tables on dumps() didn't work.
# Another attempt would be to remove tables when dumping to TOML when setting self._reformatted:
# 1. load a dict normally with loads()
# 2. clean up TomlDocument and its empty tables recursively, reusing the code with SingleKey above
# 3. dump the cleaned TomlDocument
# It looks like some effort. I'll wait for https://github.com/sdispater/tomlkit/issues/166
# remove_empty_tables = unflatten(
# flatten(self._object, custom_reducer(SEPARATOR_FLATTEN)), toml_style_splitter
# )
self._reformatted = tomlkit.dumps(self._object, sort_keys=True)
else:
self._reformatted = toml.dumps(self._object)
Expand Down