From accd0ea4abdb1cbe35c094e01cc93b36147c44a3 Mon Sep 17 00:00:00 2001 From: frostming Date: Fri, 9 Nov 2018 14:18:04 +0800 Subject: [PATCH] Move to utils function --- pipenv/project.py | 31 ++----------------- pipenv/utils.py | 25 +++++++++++++++ .../patches/vendor/tomlkit-update-items.patch | 15 +++++---- 3 files changed, 34 insertions(+), 37 deletions(-) diff --git a/pipenv/project.py b/pipenv/project.py index bed430af..04b519ed 100644 --- a/pipenv/project.py +++ b/pipenv/project.py @@ -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: diff --git a/pipenv/utils.py b/pipenv/utils.py index 8674aee2..9d495fa3 100644 --- a/pipenv/utils.py +++ b/pipenv/utils.py @@ -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`. diff --git a/tasks/vendoring/patches/vendor/tomlkit-update-items.patch b/tasks/vendoring/patches/vendor/tomlkit-update-items.patch index c996cb96..ed2fb95e 100644 --- a/tasks/vendoring/patches/vendor/tomlkit-update-items.patch +++ b/tasks/vendoring/patches/vendor/tomlkit-update-items.patch @@ -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