From da4ddf827350610c0855500c2998e380354032be Mon Sep 17 00:00:00 2001 From: Deepak Tiwari Date: Fri, 4 Oct 2024 15:55:16 +0530 Subject: [PATCH 1/4] Add src_layout option to cookiecutter --- cookiecutter.json | 1 + hooks/post_gen_project.py | 11 +++++++++++ {{cookiecutter.project_name}}/pyproject.toml | 8 +++++--- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/cookiecutter.json b/cookiecutter.json index 8adb8b4..8624a6e 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -5,6 +5,7 @@ "project_name": "example-project", "project_slug": "{{cookiecutter.project_name|lower|replace('-', '_')}}", "project_description": "This is a template repository for Python projects that use Poetry for their dependency management.", + "src_layout": ["y", "n"], "include_github_actions": ["y", "n"], "publish_to": ["pypi", "artifactory", "none"], "typechecking": ["mypy", "pyright"], diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index 0fef2aa..9f939ff 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -3,6 +3,7 @@ import os import shutil +from pathlib import Path PROJECT_DIRECTORY = os.path.realpath(os.path.curdir) @@ -15,6 +16,14 @@ def remove_dir(filepath: str) -> None: shutil.rmtree(os.path.join(PROJECT_DIRECTORY, filepath)) +def move_to_src_layout() -> None: + package_dir = "{{cookiecutter.project_slug}}" + src_dir = Path(PROJECT_DIRECTORY) / package_dir + tgt_dir = Path(PROJECT_DIRECTORY) / "src" + tgt_dir.mkdir(parents=True, exist_ok=True) + src_dir.rename(tgt_dir / src_dir.name) + + if __name__ == "__main__": if "{{cookiecutter.include_github_actions}}" != "y": remove_dir(".github") @@ -36,3 +45,5 @@ def remove_dir(filepath: str) -> None: if "{{cookiecutter.devcontainer}}" != "y": remove_dir(".devcontainer") + if "{{cookiecutter.src_layout}}" == "y": + move_to_src_layout() diff --git a/{{cookiecutter.project_name}}/pyproject.toml b/{{cookiecutter.project_name}}/pyproject.toml index 7283173..cfa3d28 100644 --- a/{{cookiecutter.project_name}}/pyproject.toml +++ b/{{cookiecutter.project_name}}/pyproject.toml @@ -6,9 +6,11 @@ authors = ["{{cookiecutter.author}} <{{cookiecutter.email}}>"] repository = "https://github.com/{{cookiecutter.author_github_handle}}/{{cookiecutter.project_name}}" documentation = "https://{{cookiecutter.author_github_handle}}.github.io/{{cookiecutter.project_name}}/" readme = "README.md" -packages = [ - {include = "{{cookiecutter.project_slug}}"} -] +{% if cookiecutter.src_layout == 'y' -%} +packages = [{include = "{{cookiecutter.project_slug}}", from = "src"}] +{% else %} +packages = [{include = "{{cookiecutter.project_slug}}"}] +{% endif %} [tool.poetry.dependencies] python = ">=3.8,<4.0" From a0676b5de6605742d3a139e8f543f0df1239ffb1 Mon Sep 17 00:00:00 2001 From: Ramu Kamath Date: Fri, 4 Oct 2024 15:58:45 +0530 Subject: [PATCH 2/4] Add src_layout option to cookiecutter --- cookiecutter.json | 1 + hooks/post_gen_project.py | 11 +++++++++++ {{cookiecutter.project_name}}/pyproject.toml | 8 +++++--- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/cookiecutter.json b/cookiecutter.json index 8adb8b4..8624a6e 100644 --- a/cookiecutter.json +++ b/cookiecutter.json @@ -5,6 +5,7 @@ "project_name": "example-project", "project_slug": "{{cookiecutter.project_name|lower|replace('-', '_')}}", "project_description": "This is a template repository for Python projects that use Poetry for their dependency management.", + "src_layout": ["y", "n"], "include_github_actions": ["y", "n"], "publish_to": ["pypi", "artifactory", "none"], "typechecking": ["mypy", "pyright"], diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index 0fef2aa..9f939ff 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -3,6 +3,7 @@ import os import shutil +from pathlib import Path PROJECT_DIRECTORY = os.path.realpath(os.path.curdir) @@ -15,6 +16,14 @@ def remove_dir(filepath: str) -> None: shutil.rmtree(os.path.join(PROJECT_DIRECTORY, filepath)) +def move_to_src_layout() -> None: + package_dir = "{{cookiecutter.project_slug}}" + src_dir = Path(PROJECT_DIRECTORY) / package_dir + tgt_dir = Path(PROJECT_DIRECTORY) / "src" + tgt_dir.mkdir(parents=True, exist_ok=True) + src_dir.rename(tgt_dir / src_dir.name) + + if __name__ == "__main__": if "{{cookiecutter.include_github_actions}}" != "y": remove_dir(".github") @@ -36,3 +45,5 @@ def remove_dir(filepath: str) -> None: if "{{cookiecutter.devcontainer}}" != "y": remove_dir(".devcontainer") + if "{{cookiecutter.src_layout}}" == "y": + move_to_src_layout() diff --git a/{{cookiecutter.project_name}}/pyproject.toml b/{{cookiecutter.project_name}}/pyproject.toml index 7283173..cfa3d28 100644 --- a/{{cookiecutter.project_name}}/pyproject.toml +++ b/{{cookiecutter.project_name}}/pyproject.toml @@ -6,9 +6,11 @@ authors = ["{{cookiecutter.author}} <{{cookiecutter.email}}>"] repository = "https://github.com/{{cookiecutter.author_github_handle}}/{{cookiecutter.project_name}}" documentation = "https://{{cookiecutter.author_github_handle}}.github.io/{{cookiecutter.project_name}}/" readme = "README.md" -packages = [ - {include = "{{cookiecutter.project_slug}}"} -] +{% if cookiecutter.src_layout == 'y' -%} +packages = [{include = "{{cookiecutter.project_slug}}", from = "src"}] +{% else %} +packages = [{include = "{{cookiecutter.project_slug}}"}] +{% endif %} [tool.poetry.dependencies] python = ">=3.8,<4.0" From 4d6c00edceef971d5cceb0e6f973d13a3e45d93c Mon Sep 17 00:00:00 2001 From: Ramu Kamath Date: Fri, 4 Oct 2024 16:17:02 +0530 Subject: [PATCH 3/4] update documentation --- docs/prompt_arguments.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/prompt_arguments.md b/docs/prompt_arguments.md index 129fd43..e6031d7 100644 --- a/docs/prompt_arguments.md +++ b/docs/prompt_arguments.md @@ -35,6 +35,10 @@ from import foo A short description of your project. +**src_layout** +`"y"` or `"n"`. Adds [src_layout](https://packaging.python.org/en/latest/discussions/src-layout-vs-flat-layout/) structure to the project + + **include_github_actions** `"y"` or `"n"`. Adds a `.github` directory with various actions and From 9e67729ceb97ee88a638e6d2cbc77168d45aef7c Mon Sep 17 00:00:00 2001 From: deepak Date: Sun, 6 Oct 2024 15:24:14 +0530 Subject: [PATCH 4/4] run prettier --- docs/prompt_arguments.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/prompt_arguments.md b/docs/prompt_arguments.md index e6031d7..047517f 100644 --- a/docs/prompt_arguments.md +++ b/docs/prompt_arguments.md @@ -38,7 +38,6 @@ A short description of your project. **src_layout** `"y"` or `"n"`. Adds [src_layout](https://packaging.python.org/en/latest/discussions/src-layout-vs-flat-layout/) structure to the project - **include_github_actions** `"y"` or `"n"`. Adds a `.github` directory with various actions and