Skip to content

Commit 3c41cee

Browse files
authored
Improve overall commands handling of envconfig (#181)
* Adjust commands due to Pydantic validation * As Pydantic takes care of validation the envconfig, we don't have to check that anymore * * Remove any checking of if context.envconfig is None * Adjust ManagedGitRepo to allow string or Repo as remote_url This makes it easier * Fix tests Most tests had a dict instead of a EnvConfig object. This fix introduces a fake EnvConfig. * Change DocBuildContext.envconfig to be a EnvConfig object and not a dict. * Remove obsolete tests for checking None envconfig We have already Pydantic validation, no need to check. * Test ManagedGitRepo with str and Repo * Add news fragment
1 parent ad033a3 commit 3c41cee

20 files changed

Lines changed: 217 additions & 198 deletions

File tree

changelog.d/181.doc.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Streamlined CLI subcommands by removing redundant environment configuration
2+
checks and implementing strict typing with EnvConfig models.
3+
Updated :class:`~docbuild.utils.git.ManagedGitRepo` to support both string
4+
and :class:`~docbuild.models.repo.Repo` model initialization.

docs/source/reference/_autoapi/docbuild/utils/git/ManagedGitRepo.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
docbuild.utils.git.ManagedGitRepo
22
=================================
33

4-
.. py:class:: docbuild.utils.git.ManagedGitRepo(remote_url: str, rootdir: pathlib.Path, gitconfig: pathlib.Path | None = None)
4+
.. py:class:: docbuild.utils.git.ManagedGitRepo(repo: str | docbuild.models.repo.Repo, rootdir: pathlib.Path, gitconfig: pathlib.Path | None = None)
55
66
Manages a bare repository and its temporary worktrees.
77

src/docbuild/cli/cmd_build/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def build(ctx: click.Context, doctypes: tuple[Doctype]) -> None:
5858
"""
5959
ctx.ensure_object(DocBuildContext)
6060
context: DocBuildContext = ctx.obj
61+
# env = context.envconfig
6162

6263
click.echo(f"[BUILD] Verbosity: {context.verbose}")
6364
click.echo(f"{context=}")

src/docbuild/cli/cmd_c14n/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ def c14n(ctx: click.Context) -> None:
1010
1111
:param ctx: The Click context object.
1212
"""
13+
# env = context.envconfig
1314
click.echo(f"[C17N] Verbosity: {ctx.obj.verbose}")

src/docbuild/cli/cmd_check/process.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
from collections.abc import Sequence
44
import logging
55
from pathlib import Path
6-
from typing import cast
76

87
from docbuild.cli.cmd_metadata.metaprocess import get_deliverable_from_doctype
98
from docbuild.cli.context import DocBuildContext
109
from docbuild.config.xml.stitch import create_stitchfile
1110
from docbuild.constants import DEFAULT_DELIVERABLES
12-
from docbuild.models.config.env import EnvConfig
1311
from docbuild.models.deliverable import Deliverable
1412
from docbuild.models.doctype import Doctype
1513
from docbuild.utils.git import ManagedGitRepo
@@ -56,7 +54,7 @@ async def process_check_files(
5654
"""Verify DC file existence using official Deliverable models."""
5755
log.info("Starting DC file availability check...")
5856

59-
env_config = cast(EnvConfig, ctx.envconfig)
57+
env_config = ctx.envconfig
6058
config_dir = env_config.paths.config_dir.expanduser()
6159
repo_root = env_config.paths.repo_dir.expanduser()
6260

src/docbuild/cli/cmd_metadata/metaprocess.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,13 @@ async def process(
407407
configured correctly.
408408
:return: 0 if all files passed validation, 1 if any failures occurred.
409409
"""
410-
configdir = Path(context.envconfig.paths.config_dir).expanduser()
410+
env = context.envconfig
411+
configdir = Path(env.paths.config_dir).expanduser()
411412
stdout.print(f"Config path: {configdir}")
412413
xmlconfigs = tuple(configdir.rglob("[a-z]*.xml"))
413414
stitchnode: etree._ElementTree = await create_stitchfile(xmlconfigs)
414415

415-
tmp_metadata_dir = context.envconfig.paths.tmp.tmp_metadata_dir
416+
tmp_metadata_dir = env.paths.tmp.tmp_metadata_dir
416417
# TODO: Is this necessary here?
417418
tmp_metadata_dir.mkdir(parents=True, exist_ok=True)
418419

src/docbuild/cli/cmd_repo/cmd_clone.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ def clone(ctx: click.Context, repos: tuple[str, ...]) -> None:
3535
:param ctx: The Click context object.
3636
"""
3737
context: DocBuildContext = ctx.obj
38-
if context.envconfig is None:
39-
raise ValueError("No envconfig found in context.")
40-
4138
result = asyncio.run(process(context, repos))
4239
log.info(f"Clone process completed with exit code: {result}")
4340
ctx.exit(result)

src/docbuild/cli/cmd_repo/cmd_dir.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Show the directory path for permanent repositories."""
22

3+
34
import click
45

56
from ...cli.context import DocBuildContext
@@ -16,9 +17,7 @@ def cmd_dir(ctx: click.Context) -> None:
1617
:param ctx: The Click context object.
1718
"""
1819
context: DocBuildContext = ctx.obj
19-
if context.envconfig is None:
20-
raise ValueError("No envconfig found in context.")
21-
22-
repo_dir = context.envconfig.get("paths", {}).get("repo_dir", None)
20+
env = context.envconfig
21+
repo_dir = env.paths.repo_dir
2322
print(repo_dir)
2423
ctx.exit(0)

src/docbuild/cli/cmd_repo/cmd_list.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,9 @@ def cmd_list(ctx: click.Context) -> None:
2424
:param ctx: The Click context object.
2525
"""
2626
context: DocBuildContext = ctx.obj
27-
if context.envconfig is None:
28-
raise ValueError("No envconfig found in context.")
29-
30-
repo_dir = context.envconfig.get("paths", {}).get("repo_dir", None)
31-
if repo_dir is None:
32-
raise ValueError(
33-
"No permanent repositories defined, neither with "
34-
"--env-config nor as default."
35-
)
27+
env = context.envconfig
28+
29+
repo_dir = env.paths.repo_dir
3630
repo_dir = Path(repo_dir).resolve()
3731
if not repo_dir.exists():
3832
console_err.print(

src/docbuild/cli/cmd_repo/process.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,9 @@ async def process(context: DocBuildContext, repos: tuple[str, ...]) -> int:
2323
:raises ValueError: If configuration paths are missing.
2424
"""
2525
# The calling command function is expected to have checked context.envconfig.
26-
paths = context.envconfig.get("paths", {})
27-
config_dir_str = paths.get("config_dir")
28-
repo_dir_str = paths.get("repo_dir")
29-
30-
if not config_dir_str:
31-
raise ValueError("Could not get a value from envconfig.paths.config_dir")
32-
if not repo_dir_str:
33-
raise ValueError("Could not get a value from envconfig.paths.repo_dir")
26+
envcfg = context.envconfig
27+
config_dir_str = envcfg.paths.config_dir
28+
repo_dir_str = envcfg.paths.repo_dir
3429

3530
configdir = Path(config_dir_str).expanduser()
3631
repo_dir = Path(repo_dir_str).expanduser()
@@ -52,6 +47,7 @@ async def process(context: DocBuildContext, repos: tuple[str, ...]) -> int:
5247
else:
5348
# Create a unique list from user input, preserving order
5449
unique_git_repos = list(dict.fromkeys(Repo(r) for r in repos))
50+
log.debug("User-specified repositories: %s", unique_git_repos)
5551

5652
if not unique_git_repos:
5753
log.info("No repositories found to clone.")

0 commit comments

Comments
 (0)