Skip to content

Commit 324277f

Browse files
committed
🎨 style(duties.py): change jinja2 templating in duties.py
1 parent a0cf2d6 commit 324277f

3 files changed

Lines changed: 85 additions & 28 deletions

File tree

‎.pre-commit-config.yaml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,19 @@ repos:
1414
- pre-push
1515
repo: https://github.com/commitizen-tools/commitizen
1616
rev: v4.5.1
17+
18+
- repo: https://github.com/pre-commit/pre-commit-hooks
19+
rev: "v5.0.0"
20+
hooks:
21+
- id: check-case-conflict
22+
- id: check-merge-conflict
23+
- id: check-toml
24+
- id: check-yaml
25+
exclude: ^mkdocs\.yml$
26+
- id: check-json
27+
- id: check-added-large-files
28+
- id: detect-private-key
29+
- id: pretty-format-json
30+
args: [--autofix]
31+
- id: end-of-file-fixer
32+
- id: trailing-whitespace

‎template/Makefile.jinja‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ actions = \
3939
{%- endif %}
4040
setup \
4141
setup-dev \
42-
test {% if tox %}\
42+
test{% if tox %}\
4343
tox
4444
{%- endif %}
4545

‎template/duties.py.jinja‎

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ PTY = not WINDOWS and not CI
2828

2929
# -----------------------------------------------------------------------------
3030
# Helper functions ------------------------------------------------------------
31-
# -----------------------------------------------------------------------------
31+
# -----------------------------------------------------------------------------{% if dockerfile %}
32+
3233

33-
{% if dockerfile -%}
3434
def _is_running_in_docker() -> bool:
3535
"""Check if the code is running inside a Docker container."""
3636
return Path("/.dockerenv").exists()
@@ -56,10 +56,9 @@ def _verify_uv_installed(ctx: Context, duty_name: str) -> None:
5656
else:
5757
raise ValueError(
5858
f"make: {duty_name}: uv must be installed, see https://docs.astral.sh/uv/getting-started/installation/"
59-
)
59+
){% if dockerfile %}
6060

6161

62-
{% if dockerfile -%}
6362
def _pick_env(ctx: Context, env: str = "dev") -> str:
6463
while True:
6564
ctx.run(
@@ -224,10 +223,9 @@ def setup_dev(ctx: Context) -> None:
224223
ctx.run(
225224
["uv", "sync"], title="Creating virtual environment using uv and installing the package"
226225
)
227-
ctx.run(["uv", "run", "pre-commit", "install", "-f"], title="Setting up pre-commit")
226+
ctx.run(["uv", "run", "pre-commit", "install", "-f"], title="Setting up pre-commit"){% if dockerfile %}
228227

229228

230-
{% if dockerfile -%}
231229
@duty(
232230
skip_if=_is_running_in_docker(),
233231
skip_reason="docker-build: skipped => Running inside a Docker container",
@@ -306,9 +304,14 @@ def docker_stop(ctx: Context, env: str | None = None) -> None:
306304
capture=False,
307305
)
308306
{%- endif %}
307+
{%- if dockerfile %}
308+
309309

310+
@dockerized_duty()
311+
{% else %}
310312

311-
{% if dockerfile %}@dockerized_duty(){% endif -%}
313+
314+
{% endif -%}
312315
@duty(pre=["check-quality", "check-types", "check-docs", "check-api"])
313316
def check(ctx: Context) -> None:
314317
"""Check it all!
@@ -324,9 +327,14 @@ def check(ctx: Context) -> None:
324327
- [`check-docs`][], to check the docs on all Python versions
325328
- [`check-api`][], to check for API breaking changes
326329
"""
330+
{%- if dockerfile %}
331+
332+
333+
@dockerized_duty()
334+
{% else %}
327335

328336

329-
{% if dockerfile %}@dockerized_duty(){% endif -%}
337+
{% endif -%}
330338
@duty
331339
def check_quality(ctx: Context) -> None:
332340
"""Check the code quality.
@@ -341,9 +349,14 @@ def check_quality(ctx: Context) -> None:
341349
tools.ruff.check(*PY_SRC_LIST, config="pyproject.toml"),
342350
title="Checking code quality [ruff]",
343351
)
352+
{%- if dockerfile %}
353+
354+
355+
@dockerized_duty()
356+
{% else %}
344357

345358

346-
{% if dockerfile %}@dockerized_duty(){% endif -%}
359+
{% endif -%}
347360
@duty
348361
def check_types(ctx: Context) -> None:
349362
"""Check that the code is correctly typed.
@@ -359,9 +372,14 @@ def check_types(ctx: Context) -> None:
359372
tools.mypy(*PY_SRC_LIST, config_file="pyproject.toml"),
360373
title="Type-checking [mypy]",
361374
)
375+
{%- if dockerfile %}
376+
362377

378+
@dockerized_duty()
379+
{% else %}
363380

364-
{% if dockerfile %}@dockerized_duty(){% endif -%}
381+
382+
{% endif -%}
365383
@duty
366384
def check_docs(ctx: Context) -> None:
367385
"""Check if the documentation builds correctly.
@@ -383,9 +401,14 @@ def check_docs(ctx: Context) -> None:
383401
tools.mkdocs.build(strict=True, verbose=True),
384402
title="Building documentation [mkdocs]",
385403
)
404+
{%- if dockerfile %}
405+
406+
407+
@dockerized_duty()
408+
{% else %}
386409

387410

388-
{% if dockerfile %}@dockerized_duty(){% endif -%}
411+
{% endif -%}
389412
@duty{% if dockerfile %}(
390413
skip_if=_is_running_in_docker() and not shutil.which("git"),
391414
skip_reason="check-api: skipped => Running inside a Docker container that does not have git",
@@ -412,9 +435,14 @@ def check_api(ctx: Context, *cli_args: str) -> None:
412435
title="Checking for API breaking changes [griffe]",
413436
nofail=True,
414437
)
438+
{%- if dockerfile %}
439+
440+
441+
@dockerized_duty()
442+
{% else %}
415443

416444

417-
{% if dockerfile %}@dockerized_duty(host="0.0.0.0") # noqa: S104{% endif -%}
445+
{% endif -%}
418446
@duty
419447
def docs(ctx: Context, *cli_args: str, host: str = "127.0.0.1", port: int = 8080) -> None:
420448
"""Serve the documentation (localhost:8080).
@@ -434,10 +462,9 @@ def docs(ctx: Context, *cli_args: str, host: str = "127.0.0.1", port: int = 8080
434462
tools.mkdocs.serve(dev_addr=f"{host}:{port}").add_args(*cli_args),
435463
title="Serving documentation",
436464
capture=False,
437-
)
465+
){% if repository_provider == 'github' %}
438466

439467

440-
{% if repository_provider == 'github' -%}
441468
@duty{% if dockerfile %}(
442469
skip_if=_is_running_in_docker(),
443470
skip_reason="docs-deploy: skipped => Running inside a Docker container",
@@ -499,10 +526,9 @@ def build(ctx: Context) -> None:
499526
["uv", "build"],
500527
title="Building source and wheel distributions",
501528
pty=PTY,
502-
)
529+
){% if publish_to_pypi %}
503530

504531

505-
{% if publish_to_pypi -%}
506532
@duty
507533
def publish(ctx: Context) -> None:
508534
"""Publish source and wheel distributions to PyPI.
@@ -560,10 +586,9 @@ def clean(ctx: Context) -> None:
560586
This command deletes build artifacts such as `dist/`, `site/`, etc.
561587
"""
562588
paths_to_clean = ["build", "dist", "htmlcov", "site", ".coverage*", "coverage.xml"]
563-
ctx.run(f"rm -rf {' '.join(paths_to_clean)}", title="Deleting build artifacts")
589+
ctx.run(f"rm -rf {' '.join(paths_to_clean)}", title="Deleting build artifacts"){% if with_conventional_commits %}
564590

565591

566-
{% if with_conventional_commits -%}
567592
@duty{% if dockerfile %}(
568593
skip_if=_is_running_in_docker(),
569594
skip_reason="release: skipped => Running inside a Docker container",
@@ -613,7 +638,11 @@ def release(ctx: Context, *cli_args: str) -> None:
613638

614639
if dry_run:
615640
ctx.run("false", title="Dry-run: no new commits and tags to push", nofail=True)
616-
ctx.run("false", title="Dry-run: skipping clean, build{% if publish_to_pypi %}, publish{% endif %}{% if repository_provider == 'github' %}, and docs deployment{% endif %}", nofail=True)
641+
ctx.run(
642+
"false",
643+
title="Dry-run: skipping clean, build{% if publish_to_pypi %}, publish{% endif %}{% if repository_provider == 'github' %}, and docs deployment{% endif %}",
644+
nofail=True,
645+
)
617646
return
618647

619648
ctx.run(["git", "push"], title="Pushing commits", pty=False)
@@ -628,9 +657,14 @@ def release(ctx: Context, *cli_args: str) -> None:
628657
docs_deploy.run()
629658
{%- endif %}
630659
{%- endif %}
660+
{%- if dockerfile %}
631661

632662

633-
{% if dockerfile %}@dockerized_duty(){% endif -%}
663+
@dockerized_duty()
664+
{% else %}
665+
666+
667+
{% endif -%}
634668
@duty(aliases=["cov"])
635669
def coverage(ctx: Context) -> None:
636670
"""Report coverage as text, HTML and XML.
@@ -655,9 +689,14 @@ def coverage(ctx: Context) -> None:
655689
capture=False,
656690
title="Report coverage statistics",
657691
)
692+
{%- if dockerfile %}
693+
694+
695+
@dockerized_duty()
696+
{% else %}
658697

659698

660-
{% if dockerfile %}@dockerized_duty(){% endif -%}
699+
{% endif -%}
661700
@duty(post=["cov"])
662701
def test(ctx: Context, *cli_args: str, match: str | None = None) -> None: # noqa: PT028
663702
"""Run the test suite with doctests and coverage tracking.
@@ -669,7 +708,7 @@ def test(ctx: Context, *cli_args: str, match: str | None = None) -> None: # noq
669708
This task runs the `tests/` suite using [Pytest](https://docs.pytest.org/), with support for:
670709
- Embedded doctests via `--doctest-modules`
671710
- Coverage tracking using [Coverage.py](https://coverage.readthedocs.io/), configured through `pyproject.toml`
672-
- Optional test selection using `match=EXPR`, which maps to `-k EXPR`
711+
- Optional test selection using `match=EXPR`, which maps to `-k EXPR`
673712
{%- if pytest_xdist %}
674713
- Optional: Parallel execution via `-n auto` (with pytest-xdist)
675714
{%- endif %}
@@ -702,11 +741,14 @@ def test(ctx: Context, *cli_args: str, match: str | None = None) -> None: # noq
702741
*cli_args,
703742
),
704743
title="Running tests",
705-
)
744+
){% if tox %}{% if dockerfile %}
745+
706746

747+
@dockerized_duty()
748+
{% else %}
707749

708-
{% if tox -%}
709-
{% if dockerfile %}@dockerized_duty(){% endif -%}
750+
751+
{% endif -%}
710752
@duty(post=["cov"])
711753
def tox(ctx: Context) -> None:
712754
"""Run the test suite in multiple Python environments using Tox.
@@ -721,5 +763,4 @@ def tox(ctx: Context) -> None:
721763
Once all environments have finished, the `cov` task is automatically triggered
722764
to combine and report test coverage from all runs.
723765
"""
724-
ctx.run(["tox", "run-parallel"], title="Running Tox", capture=False)
725-
{%- endif %}
766+
ctx.run(["tox", "run-parallel"], title="Running Tox", capture=False){%- endif %}

0 commit comments

Comments
 (0)