diff --git a/poetry/console/commands/init.py b/poetry/console/commands/init.py
index 40388ff39f3..dbb1f153d4f 100644
--- a/poetry/console/commands/init.py
+++ b/poetry/console/commands/init.py
@@ -73,14 +73,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(
@@ -205,7 +202,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("")
@@ -220,10 +217,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()