Move to utils function

This commit is contained in:
frostming
2018-11-09 14:18:04 +08:00
parent de78c1efce
commit accd0ea4ab
3 changed files with 34 additions and 37 deletions
+2 -29
View File
@@ -25,6 +25,7 @@ from .utils import (
find_requirements,
is_editable,
cleanup_toml,
convert_toml_outline_tables,
is_installable_file,
is_valid_url,
normalize_drive,
@@ -572,34 +573,6 @@ class Project(object):
"""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 Project._is_tomlkit_parsed_result(parsed):
empty_inline_table = tomlkit.inline_table
else:
empty_inline_table = toml.TomlDecoder().get_empty_inline_table
for section in ("packages", "dev-packages"):
has_outline_table = False
table_data = parsed.get(section, {}).copy()
for package, value in table_data.items():
if hasattr(value, "keys") and not isinstance(
value, (tomlkit.items.InlineTable, toml.decoder.InlineTableDict)
):
has_outline_table = True
table = empty_inline_table()
table.update(value)
table_data[package] = table
if has_outline_table:
# We'll lose comments here, only update when necessary
parsed[section] = table_data
return parsed
def _parse_pipfile(self, contents):
try:
return tomlkit.parse(contents)
@@ -860,7 +833,7 @@ class Project(object):
"""Writes the given data structure out as TOML."""
if path is None:
path = self.pipfile_location
data = self.convert_outline_table(data)
data = convert_toml_outline_tables(data)
try:
formatted_data = tomlkit.dumps(data).rstrip()
except Exception:
+25
View File
@@ -7,6 +7,8 @@ import re
import shutil
import stat
import sys
import toml
import tomlkit
import warnings
import crayons
@@ -93,6 +95,29 @@ def cleanup_toml(tml):
return toml
def convert_toml_outline_tables(parsed):
"""Converts all outline tables to inline tables."""
if isinstance(parsed, tomlkit.container.Container):
empty_inline_table = tomlkit.inline_table
else:
empty_inline_table = toml.TomlDecoder().get_empty_inline_table
for section in ("packages", "dev-packages"):
has_outline_table = False
table_data = parsed.get(section, {}).copy()
for package, value in table_data.items():
if hasattr(value, "keys") and not isinstance(
value, (tomlkit.items.InlineTable, toml.decoder.InlineTableDict)
):
has_outline_table = True
table = empty_inline_table()
table.update(value)
table_data[package] = table
if has_outline_table:
# We'll lose comments here, only update when necessary
parsed[section] = table_data
return parsed
def parse_python_version(output):
"""Parse a Python version output returned by `python --version`.
+7 -8
View File
@@ -1,5 +1,5 @@
diff --git a/pipenv/vendor/tomlkit/container.py b/pipenv/vendor/tomlkit/container.py
index 37014921..5ddd72e7 100644
index 37014921..987a0790 100644
--- a/pipenv/vendor/tomlkit/container.py
+++ b/pipenv/vendor/tomlkit/container.py
@@ -9,6 +9,7 @@ from .items import Item
@@ -9,20 +9,19 @@ index 37014921..5ddd72e7 100644
+from .items import Trivia
from .items import Whitespace
from .items import item as _item
@@ -189,9 +190,14 @@ class Container(dict):
@@ -189,8 +190,12 @@ class Container(dict):
if idx is None:
raise NonExistentKey(key)
- self._body[idx] = (None, Null())
-
+ old_data = self._body[idx][1]
+ trivia = getattr(old_data, "trivia", None)
+ if trivia and getattr(trivia, "comment", None):
+ self._body[idx] = (None, Comment(Trivia(comment_ws="", comment=trivia.comment)))
+ else:
+ self._body[idx] = (None, Null())
+ super(Container, self).__delitem__(key.key)
- super(Container, self).__delitem__(key.key)
super(Container, self).__delitem__(key.key)
return self