Skip to content

Commit

Permalink
make tomlkit dump toml's inline table
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming committed Nov 8, 2018
1 parent 53b073c commit b3aa66b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
13 changes: 7 additions & 6 deletions pipenv/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,10 +572,15 @@ def clear_pipfile_cache(self):
"""Clear pipfile cache (e.g., so we can mutate parsed pipfile)"""
_pipfile_cache.clear()

@staticmethod
def _is_tomlkit_parsed_result(parsed):
"""Check by duck typing of tomlkit.api.Container"""
return hasattr(parsed, "_body")

@staticmethod
def convert_outline_table(parsed):
"""Converts all outline to inline tables"""
if hasattr(parsed, "_body"): # Duck-type that implies tomlkit.api.Container.
if Project._istomlkit_parsed_result(parsed):
empty_inline_table = tomlkit.inline_table
else:
empty_inline_table = toml.TomlDecoder().get_empty_inline_table
Expand Down Expand Up @@ -852,11 +857,7 @@ def write_toml(self, data, path=None):
if path is None:
path = self.pipfile_location
try:
if hasattr(data, "_body"):
formatted_data = tomlkit.dumps(data).rstrip()
else:
encoder = toml.encoder.TomlPreserveInlineDictEncoder()
formatted_data = toml.dumps(data, encoder=encoder).rstrip()
formatted_data = tomlkit.dumps(data).rstrip()
except Exception:
document = tomlkit.document()
for section in ("packages", "dev-packages"):
Expand Down
6 changes: 5 additions & 1 deletion pipenv/vendor/tomlkit/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pipenv.vendor.backports.functools_lru_cache import lru_cache
else:
from functools import lru_cache
from toml.decoder import InlineTableDict


def item(value, _parent=None):
Expand All @@ -36,7 +37,10 @@ def item(value, _parent=None):
elif isinstance(value, float):
return Float(value, Trivia(), str(value))
elif isinstance(value, dict):
val = Table(Container(), Trivia(), False)
if isinstance(value, InlineTableDict):
val = InlineTable(Container(), Trivia())
else:
val = Table(Container(), Trivia(), False)
for k, v in sorted(value.items(), key=lambda i: (isinstance(i[1], dict), i[0])):
val[k] = item(v, _parent=val)

Expand Down
24 changes: 24 additions & 0 deletions tasks/vendoring/patches/vendor/tomlkit-dump-inline-table.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
diff --git a/pipenv/vendor/tomlkit/items.py b/pipenv/vendor/tomlkit/items.py
index 781e2e98..80e029d9 100644
--- a/pipenv/vendor/tomlkit/items.py
+++ b/pipenv/vendor/tomlkit/items.py
@@ -21,6 +21,7 @@ if PY2:
from pipenv.vendor.backports.functools_lru_cache import lru_cache
else:
from functools import lru_cache
+from toml.decoder import InlineTableDict


def item(value, _parent=None):
@@ -36,7 +37,10 @@ def item(value, _parent=None):
elif isinstance(value, float):
return Float(value, Trivia(), str(value))
elif isinstance(value, dict):
- val = Table(Container(), Trivia(), False)
+ if isinstance(value, InlineTableDict):
+ val = InlineTable(Container(), Trivia())
+ else:
+ val = Table(Container(), Trivia(), False)
for k, v in sorted(value.items(), key=lambda i: (isinstance(i[1], dict), i[0])):
val[k] = item(v, _parent=val)

0 comments on commit b3aa66b

Please sign in to comment.