From 08f490eda49a95bf7e042b1f136d6e428496e86d Mon Sep 17 00:00:00 2001 From: finswimmer Date: Sun, 24 May 2020 15:09:15 +0200 Subject: [PATCH] append original toml during creation of poetry_content --- poetry/console/commands/init.py | 19 ++++++------------- poetry/layouts/layout.py | 9 +++++++-- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/poetry/console/commands/init.py b/poetry/console/commands/init.py index 6d0b1f3d8d6..3c66c46a837 100644 --- a/poetry/console/commands/init.py +++ b/poetry/console/commands/init.py @@ -74,14 +74,11 @@ def handle(self): with (Path.cwd() / "pyproject.toml").open() as toml_file: original_toml = tomlkit.loads(toml_file.read()) - try: - if original_toml["tool"]["poetry"]: - self.line( - "A pyproject.toml file with a poetry section already exists." - ) - return 1 - except KeyError: - pass + if original_toml.get("tool", {}).get("poetry"): + self.line( + "A pyproject.toml file with a poetry section already exists." + ) + return 1 if original_toml.get("build-system"): self.line( @@ -216,7 +213,7 @@ def handle(self): dev_dependencies=dev_requirements, ) - content = layout_.generate_poetry_content() + content = layout_.generate_poetry_content(original_toml) if self.io.is_interactive(): self.line("Generated file") self.line("") @@ -231,10 +228,6 @@ def handle(self): with (Path.cwd() / "pyproject.toml").open("w", encoding="utf-8") as f: f.write(content) - if original_toml: - f.write("\n") - f.write(tomlkit.dumps(original_toml)) - def _determine_requirements( self, requires, allow_prereleases=False, source=None ): # type: (List[str], bool) -> List[Dict[str, str]] diff --git a/poetry/layouts/layout.py b/poetry/layouts/layout.py index a7378c67c4c..dfe0db330ca 100644 --- a/poetry/layouts/layout.py +++ b/poetry/layouts/layout.py @@ -81,7 +81,7 @@ def create(self, path, with_tests=True): self._write_poetry(path) - def generate_poetry_content(self): + def generate_poetry_content(self, original_toml): template = POETRY_DEFAULT if self._license: template = POETRY_WITH_LICENSE @@ -114,7 +114,12 @@ def generate_poetry_content(self): content.add("build-system", build_system) - return dumps(content) + content = dumps(content) + + if original_toml: + content += "\n" + dumps(original_toml) + + return content def _create_default(self, path, src=True): raise NotImplementedError()