Skip to content

Commit

Permalink
👽 Apply changes from cookiecutter-python-cli-app
Browse files Browse the repository at this point in the history
  • Loading branch information
sgraaf committed Jan 3, 2024
1 parent 8b5d8eb commit ab9c8cc
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 35 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ cookiecutter gh:sgraaf/cookiecutter-python-package

## Features

<!-- TODO: mention optional choice of licenses, git and venv initialization -->

- Linting with autofix (i.e. removing unused imports, formatting and Python syntax upgrades) with [ruff](https://beta.ruff.rs/docs/)
- Code formatting with [Black](https://black.readthedocs.io/en/stable/) and [Prettier](https://prettier.io/)
- Code formatting with [ruff](https://beta.ruff.rs/docs/) and [Prettier](https://prettier.io/)
- Static type-checking with [mypy](http://www.mypy-lang.org/)
- Checks and fixes before every commit with [pre-commit](https://pre-commit.com/)
- Testing with [pytest](https://docs.pytest.org/en/stable/index.html)
Expand Down
48 changes: 32 additions & 16 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,27 @@ def possibly_install_pipx() -> None:
subprocess.run([sys.executable, "-m", "pipx", "ensurepath"], check=False)


# possibly install nox (and pipx)
if shutil.which("nox") is None:
# possibly install pipx
possibly_install_pipx()
def possibly_install_nox() -> None:
if shutil.which("nox") is None:
# possibly install pipx
possibly_install_pipx()

# install nox
subprocess.run([sys.executable, "-m", "pipx", "install", "nox"], check=False)
# install nox
subprocess.run([sys.executable, "-m", "pipx", "install", "nox"], check=False)

# possibly install pre-commit (and pipx)
if shutil.which("pre-commit") is None:
# possibly install pipx
possibly_install_pipx()

# install pre-commit
subprocess.run([sys.executable, "-m", "pipx", "install", "pre-commit"], check=False)
def possibly_install_pre_commit() -> None:
if shutil.which("pre-commit") is None:
# possibly install pipx
possibly_install_pipx()

# perform git initialization
if "{{ cookiecutter.init_git }}" == "True":
# install pre-commit
subprocess.run(
[sys.executable, "-m", "pipx", "install", "pre-commit"], check=False
)


def initialize_git_repository() -> None:
# initialize Git repository
subprocess.run(["git", "init", "-b", "main"], check=False)

Expand Down Expand Up @@ -58,7 +61,20 @@ def possibly_install_pipx() -> None:
check=False,
)

# create venv and install dependencies
if "{{ cookiecutter.init_venv }}" == "True":

def initialize_venv() -> None:
# run nox "dev" session
subprocess.run(["nox", "--session", "dev"], check=False)


if __name__ == "__main__":
# possibly install nox (and pipx)
possibly_install_nox()
# possibly install pre-commit (and pipx)
possibly_install_pre_commit()
# perform git initialization
if "{{ cookiecutter.init_git }}" == "True":
initialize_git_repository()
# create venv and install dependencies
if "{{ cookiecutter.init_venv }}" == "True":
initialize_venv()
19 changes: 15 additions & 4 deletions hooks/pre_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@

PACKAGE_PATTERN = re.compile(r"^[_a-zA-Z][_a-zA-Z0-9]+$")

package_name = "{{ cookiecutter.package_name }}"

if PACKAGE_PATTERN.match(package_name) is None:
msg = f"Invalid package name: `{package_name}`. Please only use alphanumerics and underscores (_)."
raise ValueError(msg)
def validate_package_name(package_name: str) -> None:
if PACKAGE_PATTERN.match(package_name) is None:
msg = f"Invalid package name: `{package_name}`. Please only use alphanumerics and underscores (_)."
raise ValueError(msg)


def validate_description(description: str) -> None:
if not description.endswith("."):
msg = f"Invalid description: `{description}`. It should end with a single period (.)."
raise ValueError(msg)


if __name__ == "__main__":
validate_package_name("{{ cookiecutter.package_name }}")
validate_description("{{ cookiecutter.short_description }}")
28 changes: 14 additions & 14 deletions {{ cookiecutter.project_name }}/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ docs = [
]
dev = [
"{{ cookiecutter.project_name }}[tests,docs]",
"black",
"mypy",
"pre-commit",
"ruff",
]

[project.urls]
Expand All @@ -73,32 +69,36 @@ name = "{{ cookiecutter.package_name }}"
[tool.ruff]
select = ["ALL"]
ignore = [
"E501", # line too long, handled by black
"E501", # line too long, handled by black (pycodestyle)
"ANN003", # missing-type-kwargs (flake8-annotations)
"ANN101", # missing-type-self (flake8-annotations)
"ANN102", # missing-type-cls (flake8-annotations)
"ANN401", # any-type (flake8-annotations)
"COM", # flake8-commas, handled by black
"CPY", # flake8-copyright
"ISC001", # single-line-implicit-string-concatenation
"ISC001", # single-line-implicit-string-concatenation (flake8-implicit-str-concat)
"INP", # flake8-no-pep420
]
src = ["src", "tests", "docs"]
target-version = "py38" # the minimum Python version supported, used by pyupgrade

[tool.ruff.per-file-ignores]
"__init__.py" = [
"F401", # unused import
"F401", # unused-import (Pyflakes)
]
"tests/**/test_*.py" = [
"D", # pydocstyle
"ANN201", # missing-return-type-undocumented-public-function
"S101", # assert
"SIM300", # yoda-conditions
"PLR2004", # magic-value-comparison
"ANN201", # missing-return-type-undocumented-public-function (flake8-annotations)
"S101", # assert (flake8-bandit)
"SIM300", # yoda-conditions (flake8-simplify)
"PLR2004", # magic-value-comparison (Pylint)
]
"docs/conf.py" = [
"A001", # builtin-variable-shadowing
"A001", # builtin-variable-shadowing (flake8-builtins)
]
"noxfile.py" = [
"D100", # undocumented-public-module
"ERA001", # commented-out-code
"D100", # undocumented-public-module (pydocstyle)
"ERA001", # commented-out-code (eradicate)
]

[tool.ruff.isort]
Expand Down

1 comment on commit ab9c8cc

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.