doxtr-cli 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- doxtr_cli/__init__.py +48 -0
- doxtr_cli/__main__.py +8 -0
- doxtr_cli/builders.py +596 -0
- doxtr_cli/cli.py +301 -0
- doxtr_cli/config.py +330 -0
- doxtr_cli/confluence.py +166 -0
- doxtr_cli/errors.py +49 -0
- doxtr_cli/git_meta.py +118 -0
- doxtr_cli/prompts.py +89 -0
- doxtr_cli/server.py +800 -0
- doxtr_cli-0.1.0.dist-info/METADATA +110 -0
- doxtr_cli-0.1.0.dist-info/RECORD +16 -0
- doxtr_cli-0.1.0.dist-info/WHEEL +5 -0
- doxtr_cli-0.1.0.dist-info/entry_points.txt +2 -0
- doxtr_cli-0.1.0.dist-info/licenses/LICENSE +21 -0
- doxtr_cli-0.1.0.dist-info/top_level.txt +1 -0
doxtr_cli/cli.py
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
"""Click CLI entry point for the ``doxtr`` documentation tool."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import sys
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
import click
|
|
11
|
+
|
|
12
|
+
from . import __version__
|
|
13
|
+
from .builders import run_release, run_test
|
|
14
|
+
from .config import load_config
|
|
15
|
+
from .errors import EXIT_INTERRUPTED, DoxtrError
|
|
16
|
+
from .git_meta import collect_git_metadata, format_test_stamp, resolve_release_version
|
|
17
|
+
from .server import discover_versions, resolve_default_doc_root, serve_documentation
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class DoxtrGroup(click.Group):
|
|
21
|
+
"""Click command group mapping :class:`DoxtrError` and ``Ctrl-C`` to standardized exit codes."""
|
|
22
|
+
|
|
23
|
+
def invoke(self, ctx: click.Context) -> Any:
|
|
24
|
+
try:
|
|
25
|
+
return super().invoke(ctx)
|
|
26
|
+
except DoxtrError as exc:
|
|
27
|
+
click.echo(f"Error: {exc}", err=True)
|
|
28
|
+
sys.exit(exc.exit_code)
|
|
29
|
+
except KeyboardInterrupt:
|
|
30
|
+
click.echo("Interrupted.", err=True)
|
|
31
|
+
sys.exit(EXIT_INTERRUPTED)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@click.group(cls=DoxtrGroup)
|
|
35
|
+
@click.version_option(version=__version__, prog_name="doxtr")
|
|
36
|
+
def cli() -> None:
|
|
37
|
+
"""Doxtr command-line tool for releasing, testing, and serving Sphinx documentation."""
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@cli.command()
|
|
41
|
+
@click.option(
|
|
42
|
+
"--config",
|
|
43
|
+
"config_path",
|
|
44
|
+
type=click.Path(path_type=Path),
|
|
45
|
+
default=None,
|
|
46
|
+
help="Path to doxtr.toml.",
|
|
47
|
+
)
|
|
48
|
+
@click.option(
|
|
49
|
+
"--project-dir",
|
|
50
|
+
type=click.Path(path_type=Path),
|
|
51
|
+
default=None,
|
|
52
|
+
help="Documentation project root.",
|
|
53
|
+
)
|
|
54
|
+
@click.option("--json", "as_json", is_flag=True, help="Output project metadata and config as JSON.")
|
|
55
|
+
def info(config_path: Path | None, project_dir: Path | None, as_json: bool) -> None:
|
|
56
|
+
"""Show resolved project configuration and Git metadata."""
|
|
57
|
+
cfg = load_config(
|
|
58
|
+
project_dir=project_dir, config_path=config_path, allow_missing=config_path is None
|
|
59
|
+
)
|
|
60
|
+
meta = collect_git_metadata(cfg.project_root)
|
|
61
|
+
version = resolve_release_version(meta, fallback=cfg.project.version)
|
|
62
|
+
stamp = format_test_stamp(meta, base_version=version)
|
|
63
|
+
default_doc = resolve_default_doc_root(cfg)
|
|
64
|
+
versions = discover_versions(cfg.versions_path)
|
|
65
|
+
|
|
66
|
+
payload = {
|
|
67
|
+
"project": {
|
|
68
|
+
"name": cfg.project.name,
|
|
69
|
+
"version": version,
|
|
70
|
+
"test_stamp": stamp,
|
|
71
|
+
"root": str(cfg.project_root),
|
|
72
|
+
"config_path": str(cfg.config_path) if cfg.config_path else None,
|
|
73
|
+
"source_dir": str(cfg.source_path),
|
|
74
|
+
"build_dir": str(cfg.build_path),
|
|
75
|
+
"html_theme": cfg.html.theme,
|
|
76
|
+
"default_doc_root": str(default_doc),
|
|
77
|
+
"versions": versions,
|
|
78
|
+
},
|
|
79
|
+
"git": {
|
|
80
|
+
"commit_sha": meta.commit_sha,
|
|
81
|
+
"short_sha": meta.short_sha,
|
|
82
|
+
"branch": meta.branch,
|
|
83
|
+
"tag": meta.tag,
|
|
84
|
+
"dirty": meta.dirty,
|
|
85
|
+
"utc_timestamp": meta.utc_timestamp,
|
|
86
|
+
},
|
|
87
|
+
}
|
|
88
|
+
if as_json:
|
|
89
|
+
click.echo(json.dumps(payload, indent=2))
|
|
90
|
+
return
|
|
91
|
+
|
|
92
|
+
click.echo(f"Project: {cfg.project.name}")
|
|
93
|
+
click.echo(f"Config: {cfg.config_path or '(defaults)'}")
|
|
94
|
+
click.echo(f"Version: {version}")
|
|
95
|
+
click.echo(f"Test Stamp: {stamp}")
|
|
96
|
+
click.echo(f"Git Commit: {meta.short_sha} ({meta.branch}, dirty={meta.dirty})")
|
|
97
|
+
click.echo(f"Source Dir: {cfg.source_path}")
|
|
98
|
+
click.echo(f"Build Dir: {cfg.build_path}")
|
|
99
|
+
click.echo(f"HTML Theme: {cfg.html.theme}")
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
@cli.command()
|
|
103
|
+
@click.option(
|
|
104
|
+
"--config",
|
|
105
|
+
"config_path",
|
|
106
|
+
type=click.Path(path_type=Path),
|
|
107
|
+
default=None,
|
|
108
|
+
help="Path to doxtr.toml.",
|
|
109
|
+
)
|
|
110
|
+
@click.option(
|
|
111
|
+
"--project-dir",
|
|
112
|
+
type=click.Path(path_type=Path),
|
|
113
|
+
default=None,
|
|
114
|
+
help="Documentation project root.",
|
|
115
|
+
)
|
|
116
|
+
@click.option(
|
|
117
|
+
"--dry-run",
|
|
118
|
+
is_flag=True,
|
|
119
|
+
help="Print planned build/publish steps without confirmation or execution.",
|
|
120
|
+
)
|
|
121
|
+
@click.option("--skip-pdf", is_flag=True, help="Skip building light/dark PDFs.")
|
|
122
|
+
@click.option("--skip-html", is_flag=True, help="Skip building HTML output.")
|
|
123
|
+
@click.option(
|
|
124
|
+
"--skip-confluence", is_flag=True, help="Skip Confluence export and attachment upload."
|
|
125
|
+
)
|
|
126
|
+
@click.option(
|
|
127
|
+
"--output-dir",
|
|
128
|
+
type=click.Path(path_type=Path),
|
|
129
|
+
default=None,
|
|
130
|
+
help="Override build output directory.",
|
|
131
|
+
)
|
|
132
|
+
def release(
|
|
133
|
+
config_path: Path | None,
|
|
134
|
+
project_dir: Path | None,
|
|
135
|
+
dry_run: bool,
|
|
136
|
+
skip_pdf: bool,
|
|
137
|
+
skip_html: bool,
|
|
138
|
+
skip_confluence: bool,
|
|
139
|
+
output_dir: Path | None,
|
|
140
|
+
) -> None:
|
|
141
|
+
"""Run production release build & publish (requires literal 'yes' + Enter)."""
|
|
142
|
+
cfg = load_config(project_dir=project_dir, config_path=config_path)
|
|
143
|
+
report = run_release(
|
|
144
|
+
cfg,
|
|
145
|
+
dry_run=dry_run,
|
|
146
|
+
skip_pdf=skip_pdf,
|
|
147
|
+
skip_html=skip_html,
|
|
148
|
+
skip_confluence=skip_confluence,
|
|
149
|
+
output_dir=output_dir,
|
|
150
|
+
input_fn=input,
|
|
151
|
+
output_fn=click.echo,
|
|
152
|
+
)
|
|
153
|
+
if not dry_run:
|
|
154
|
+
click.echo(f"Release completed for {cfg.project.name} v{report.version}.")
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
@cli.command()
|
|
158
|
+
@click.option(
|
|
159
|
+
"--config",
|
|
160
|
+
"config_path",
|
|
161
|
+
type=click.Path(path_type=Path),
|
|
162
|
+
default=None,
|
|
163
|
+
help="Path to doxtr.toml.",
|
|
164
|
+
)
|
|
165
|
+
@click.option(
|
|
166
|
+
"--project-dir",
|
|
167
|
+
type=click.Path(path_type=Path),
|
|
168
|
+
default=None,
|
|
169
|
+
help="Documentation project root.",
|
|
170
|
+
)
|
|
171
|
+
@click.option("--dry-run", is_flag=True, help="Print planned test build steps without executing.")
|
|
172
|
+
@click.option("--skip-pdf", is_flag=True, help="Skip building light/dark test PDFs.")
|
|
173
|
+
@click.option("--skip-html", is_flag=True, help="Skip building HTML test site.")
|
|
174
|
+
@click.option("--skip-confluence", is_flag=True, help="Skip Confluence test export.")
|
|
175
|
+
@click.option("--confluence", is_flag=True, help="Force enabling [confluence.test] export.")
|
|
176
|
+
@click.option("--watermark", is_flag=True, help="Apply default 'TEST' PDF watermark.")
|
|
177
|
+
@click.option("--watermark-text", default=None, help="Custom PDF watermark text.")
|
|
178
|
+
@click.option(
|
|
179
|
+
"--output-dir",
|
|
180
|
+
type=click.Path(path_type=Path),
|
|
181
|
+
default=None,
|
|
182
|
+
help="Override build output directory.",
|
|
183
|
+
)
|
|
184
|
+
def test(
|
|
185
|
+
config_path: Path | None,
|
|
186
|
+
project_dir: Path | None,
|
|
187
|
+
dry_run: bool,
|
|
188
|
+
skip_pdf: bool,
|
|
189
|
+
skip_html: bool,
|
|
190
|
+
skip_confluence: bool,
|
|
191
|
+
confluence: bool,
|
|
192
|
+
watermark: bool,
|
|
193
|
+
watermark_text: str | None,
|
|
194
|
+
output_dir: Path | None,
|
|
195
|
+
) -> None:
|
|
196
|
+
"""Build stamped staging HTML/PDFs and optional [confluence.test] preview."""
|
|
197
|
+
cfg = load_config(project_dir=project_dir, config_path=config_path)
|
|
198
|
+
wm = watermark_text if watermark_text is not None else ("TEST" if watermark else None)
|
|
199
|
+
report = run_test(
|
|
200
|
+
cfg,
|
|
201
|
+
dry_run=dry_run,
|
|
202
|
+
skip_pdf=skip_pdf,
|
|
203
|
+
skip_html=skip_html,
|
|
204
|
+
skip_confluence=skip_confluence,
|
|
205
|
+
enable_confluence=confluence,
|
|
206
|
+
watermark=wm,
|
|
207
|
+
output_dir=output_dir,
|
|
208
|
+
output_fn=click.echo,
|
|
209
|
+
)
|
|
210
|
+
if not dry_run:
|
|
211
|
+
click.echo(f"Test build completed: {report.stamp}")
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
@cli.command()
|
|
215
|
+
@click.option(
|
|
216
|
+
"--config",
|
|
217
|
+
"config_path",
|
|
218
|
+
type=click.Path(path_type=Path),
|
|
219
|
+
default=None,
|
|
220
|
+
help="Path to doxtr.toml.",
|
|
221
|
+
)
|
|
222
|
+
@click.option(
|
|
223
|
+
"--project-dir",
|
|
224
|
+
type=click.Path(path_type=Path),
|
|
225
|
+
default=None,
|
|
226
|
+
help="Documentation project root.",
|
|
227
|
+
)
|
|
228
|
+
@click.option(
|
|
229
|
+
"--dir",
|
|
230
|
+
"doc_dir",
|
|
231
|
+
type=click.Path(path_type=Path),
|
|
232
|
+
default=None,
|
|
233
|
+
help="Built HTML directory to serve.",
|
|
234
|
+
)
|
|
235
|
+
@click.option(
|
|
236
|
+
"--backend",
|
|
237
|
+
type=click.Choice(["auto", "nginx", "python"]),
|
|
238
|
+
default=None,
|
|
239
|
+
help="Server backend.",
|
|
240
|
+
)
|
|
241
|
+
@click.option("--host", default=None, help="Bind host address.")
|
|
242
|
+
@click.option("--port", type=int, default=None, help="Listen port.")
|
|
243
|
+
@click.option(
|
|
244
|
+
"--versions-dir",
|
|
245
|
+
type=click.Path(path_type=Path),
|
|
246
|
+
default=None,
|
|
247
|
+
help="Directory holding multi-version HTML builds.",
|
|
248
|
+
)
|
|
249
|
+
@click.option(
|
|
250
|
+
"--nginx-template",
|
|
251
|
+
type=click.Path(path_type=Path),
|
|
252
|
+
default=None,
|
|
253
|
+
help="Nginx server config template.",
|
|
254
|
+
)
|
|
255
|
+
@click.option("--theme", default=None, help="Rebuild HTML preview into isolated cache using theme.")
|
|
256
|
+
@click.option(
|
|
257
|
+
"--scheme",
|
|
258
|
+
type=click.Choice(["light", "dark"]),
|
|
259
|
+
default=None,
|
|
260
|
+
help="Preview color scheme override.",
|
|
261
|
+
)
|
|
262
|
+
@click.option(
|
|
263
|
+
"--watch",
|
|
264
|
+
is_flag=True,
|
|
265
|
+
help="Watch documentation sources and rebuild on edit (forces Python backend).",
|
|
266
|
+
)
|
|
267
|
+
def serve(
|
|
268
|
+
config_path: Path | None,
|
|
269
|
+
project_dir: Path | None,
|
|
270
|
+
doc_dir: Path | None,
|
|
271
|
+
backend: str | None,
|
|
272
|
+
host: str | None,
|
|
273
|
+
port: int | None,
|
|
274
|
+
versions_dir: Path | None,
|
|
275
|
+
nginx_template: Path | None,
|
|
276
|
+
theme: str | None,
|
|
277
|
+
scheme: str | None,
|
|
278
|
+
watch: bool,
|
|
279
|
+
) -> None:
|
|
280
|
+
"""Serve built documentation using Nginx or multi-version Python preview server."""
|
|
281
|
+
cfg = load_config(
|
|
282
|
+
project_dir=project_dir, config_path=config_path, allow_missing=config_path is None
|
|
283
|
+
)
|
|
284
|
+
serve_documentation(
|
|
285
|
+
cfg,
|
|
286
|
+
doc_dir=doc_dir,
|
|
287
|
+
backend=backend,
|
|
288
|
+
host=host,
|
|
289
|
+
port=port,
|
|
290
|
+
versions_dir=versions_dir,
|
|
291
|
+
nginx_template=nginx_template,
|
|
292
|
+
theme=theme,
|
|
293
|
+
scheme=scheme,
|
|
294
|
+
watch=watch,
|
|
295
|
+
output_fn=click.echo,
|
|
296
|
+
)
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
def main() -> None:
|
|
300
|
+
"""Console script entry point."""
|
|
301
|
+
cli(prog_name="doxtr")
|
doxtr_cli/config.py
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
"""Configuration loading and validation for ``doxtr.toml`` and ``pyproject.toml``."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import re
|
|
7
|
+
import sys
|
|
8
|
+
from collections.abc import Mapping
|
|
9
|
+
from dataclasses import dataclass, field
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
from .errors import ConfigError
|
|
14
|
+
|
|
15
|
+
if sys.version_info >= (3, 11):
|
|
16
|
+
import tomllib
|
|
17
|
+
else:
|
|
18
|
+
import tomli as tomllib
|
|
19
|
+
|
|
20
|
+
_ENV_PATTERN = re.compile(r"\$\{([A-Za-z_][A-Za-z0-9_]*)(?::-([^}]*))?\}")
|
|
21
|
+
_VALID_BACKENDS = {"auto", "nginx", "python"}
|
|
22
|
+
_VALID_SCHEMES = {"light", "dark"}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass(frozen=True)
|
|
26
|
+
class ProjectConfig:
|
|
27
|
+
"""Project source/build layout settings."""
|
|
28
|
+
|
|
29
|
+
name: str = "doxtr-docs"
|
|
30
|
+
version: str = "0.1.0"
|
|
31
|
+
source_dir: str = "source"
|
|
32
|
+
build_dir: str = "build"
|
|
33
|
+
latex_name: str = "doxtr"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass(frozen=True)
|
|
37
|
+
class HtmlConfig:
|
|
38
|
+
"""HTML build & download bundling settings."""
|
|
39
|
+
|
|
40
|
+
theme: str = "doxtr_aubergine"
|
|
41
|
+
downloads_subdir: str = "_static/downloads"
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@dataclass(frozen=True)
|
|
45
|
+
class PdfConfig:
|
|
46
|
+
"""Light/dark LaTeX PDF build settings."""
|
|
47
|
+
|
|
48
|
+
light: bool = True
|
|
49
|
+
dark: bool = True
|
|
50
|
+
light_filename: str = "doxtr-light.pdf"
|
|
51
|
+
dark_filename: str = "doxtr-dark.pdf"
|
|
52
|
+
copy_to_html_downloads: bool = True
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@dataclass(frozen=True)
|
|
56
|
+
class ConfluenceTargetConfig:
|
|
57
|
+
"""Confluence target settings for either ``release`` or ``test``."""
|
|
58
|
+
|
|
59
|
+
enabled: bool = False
|
|
60
|
+
space_key: str = ""
|
|
61
|
+
parent_page: str = ""
|
|
62
|
+
server_url: str = ""
|
|
63
|
+
attach_pdfs: bool = True
|
|
64
|
+
page_title_prefix: str = ""
|
|
65
|
+
root_page_title: str = ""
|
|
66
|
+
token: str = ""
|
|
67
|
+
username: str = ""
|
|
68
|
+
password: str = ""
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@dataclass(frozen=True)
|
|
72
|
+
class ConfluenceConfig:
|
|
73
|
+
"""Separated release and test Confluence targets."""
|
|
74
|
+
|
|
75
|
+
release: ConfluenceTargetConfig = field(default_factory=ConfluenceTargetConfig)
|
|
76
|
+
test: ConfluenceTargetConfig = field(
|
|
77
|
+
default_factory=lambda: ConfluenceTargetConfig(page_title_prefix="[TEST] ")
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@dataclass(frozen=True)
|
|
82
|
+
class ServeConfig:
|
|
83
|
+
"""Settings for ``doxtr serve``."""
|
|
84
|
+
|
|
85
|
+
default_backend: str = "auto"
|
|
86
|
+
port: int = 8008
|
|
87
|
+
host: str = "0.0.0.0"
|
|
88
|
+
versions_dir: str = "build/versions"
|
|
89
|
+
nginx_template: str = "/workspaces/docs/reactor/default.template"
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
@dataclass(frozen=True)
|
|
93
|
+
class DoxtrConfig:
|
|
94
|
+
"""Complete resolved configuration for a Doxtr documentation project."""
|
|
95
|
+
|
|
96
|
+
project_root: Path
|
|
97
|
+
config_path: Path | None = None
|
|
98
|
+
project: ProjectConfig = field(default_factory=ProjectConfig)
|
|
99
|
+
html: HtmlConfig = field(default_factory=HtmlConfig)
|
|
100
|
+
pdf: PdfConfig = field(default_factory=PdfConfig)
|
|
101
|
+
confluence: ConfluenceConfig = field(default_factory=ConfluenceConfig)
|
|
102
|
+
serve: ServeConfig = field(default_factory=ServeConfig)
|
|
103
|
+
|
|
104
|
+
@property
|
|
105
|
+
def source_path(self) -> Path:
|
|
106
|
+
return (self.project_root / self.project.source_dir).resolve()
|
|
107
|
+
|
|
108
|
+
@property
|
|
109
|
+
def build_path(self) -> Path:
|
|
110
|
+
return (self.project_root / self.project.build_dir).resolve()
|
|
111
|
+
|
|
112
|
+
@property
|
|
113
|
+
def versions_path(self) -> Path:
|
|
114
|
+
return (self.project_root / self.serve.versions_dir).resolve()
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def expand_env_vars(value: str, env: Mapping[str, str] | None = None) -> str:
|
|
118
|
+
"""Expand ``${VAR}`` and ``${VAR:-default}`` references using *env*."""
|
|
119
|
+
lookup = env if env is not None else os.environ
|
|
120
|
+
|
|
121
|
+
def _replace(match: re.Match[str]) -> str:
|
|
122
|
+
name = match.group(1)
|
|
123
|
+
default = match.group(2)
|
|
124
|
+
if name in lookup and lookup[name] != "":
|
|
125
|
+
return lookup[name]
|
|
126
|
+
return default if default is not None else lookup.get(name, "")
|
|
127
|
+
|
|
128
|
+
return _ENV_PATTERN.sub(_replace, value)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _as_table(raw: Any, section: str) -> dict[str, Any]:
|
|
132
|
+
if raw is None:
|
|
133
|
+
return {}
|
|
134
|
+
if not isinstance(raw, dict):
|
|
135
|
+
raise ConfigError(f"Configuration section [{section}] must be a TOML table.")
|
|
136
|
+
return raw
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _as_str(
|
|
140
|
+
table: Mapping[str, Any],
|
|
141
|
+
key: str,
|
|
142
|
+
default: str,
|
|
143
|
+
section: str,
|
|
144
|
+
*,
|
|
145
|
+
expand: bool = False,
|
|
146
|
+
env: Mapping[str, str] | None = None,
|
|
147
|
+
) -> str:
|
|
148
|
+
val = table.get(key, default)
|
|
149
|
+
if not isinstance(val, str):
|
|
150
|
+
raise ConfigError(f"Configuration key '{section}.{key}' must be a string.")
|
|
151
|
+
return expand_env_vars(val, env=env) if expand else val
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _as_bool(table: Mapping[str, Any], key: str, default: bool, section: str) -> bool:
|
|
155
|
+
val = table.get(key, default)
|
|
156
|
+
if not isinstance(val, bool):
|
|
157
|
+
raise ConfigError(f"Configuration key '{section}.{key}' must be a boolean.")
|
|
158
|
+
return val
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _as_int(table: Mapping[str, Any], key: str, default: int, section: str) -> int:
|
|
162
|
+
val = table.get(key, default)
|
|
163
|
+
if isinstance(val, bool) or not isinstance(val, int):
|
|
164
|
+
raise ConfigError(f"Configuration key '{section}.{key}' must be an integer.")
|
|
165
|
+
return val
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _parse_confluence_target(
|
|
169
|
+
raw: Mapping[str, Any],
|
|
170
|
+
section: str,
|
|
171
|
+
*,
|
|
172
|
+
default_prefix: str = "",
|
|
173
|
+
env: Mapping[str, str] | None = None,
|
|
174
|
+
) -> ConfluenceTargetConfig:
|
|
175
|
+
return ConfluenceTargetConfig(
|
|
176
|
+
enabled=_as_bool(raw, "enabled", False, section),
|
|
177
|
+
space_key=_as_str(raw, "space_key", "", section, expand=True, env=env),
|
|
178
|
+
parent_page=_as_str(raw, "parent_page", "", section, expand=True, env=env),
|
|
179
|
+
server_url=_as_str(raw, "server_url", "", section, expand=True, env=env),
|
|
180
|
+
attach_pdfs=_as_bool(raw, "attach_pdfs", True, section),
|
|
181
|
+
page_title_prefix=_as_str(
|
|
182
|
+
raw, "page_title_prefix", default_prefix, section, expand=True, env=env
|
|
183
|
+
),
|
|
184
|
+
root_page_title=_as_str(raw, "root_page_title", "", section, expand=True, env=env),
|
|
185
|
+
token=_as_str(raw, "token", "", section, expand=True, env=env),
|
|
186
|
+
username=_as_str(raw, "username", "", section, expand=True, env=env),
|
|
187
|
+
password=_as_str(raw, "password", "", section, expand=True, env=env),
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def _discover_config_file(root: Path, explicit: Path | None) -> tuple[Path | None, dict[str, Any]]:
|
|
192
|
+
if explicit is not None:
|
|
193
|
+
resolved = explicit if explicit.is_absolute() else (root / explicit)
|
|
194
|
+
if not resolved.is_file():
|
|
195
|
+
raise ConfigError(f"Configuration file not found: {resolved}")
|
|
196
|
+
data = _read_toml_file(resolved)
|
|
197
|
+
if resolved.name == "pyproject.toml":
|
|
198
|
+
tool_section = _as_table(data.get("tool"), "tool")
|
|
199
|
+
doxtr_section = _as_table(tool_section.get("doxtr"), "tool.doxtr")
|
|
200
|
+
return resolved, doxtr_section
|
|
201
|
+
return resolved, data
|
|
202
|
+
|
|
203
|
+
doxtr_toml = root / "doxtr.toml"
|
|
204
|
+
if doxtr_toml.is_file():
|
|
205
|
+
return doxtr_toml, _read_toml_file(doxtr_toml)
|
|
206
|
+
|
|
207
|
+
pyproject_toml = root / "pyproject.toml"
|
|
208
|
+
if pyproject_toml.is_file():
|
|
209
|
+
data = _read_toml_file(pyproject_toml)
|
|
210
|
+
tool_section = _as_table(data.get("tool"), "tool")
|
|
211
|
+
if "doxtr" in tool_section:
|
|
212
|
+
return pyproject_toml, _as_table(tool_section.get("doxtr"), "tool.doxtr")
|
|
213
|
+
|
|
214
|
+
return None, {}
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def _read_toml_file(path: Path) -> dict[str, Any]:
|
|
218
|
+
try:
|
|
219
|
+
with path.open("rb") as fh:
|
|
220
|
+
parsed = tomllib.load(fh)
|
|
221
|
+
except OSError as exc:
|
|
222
|
+
raise ConfigError(f"Unable to read configuration file {path}: {exc}") from exc
|
|
223
|
+
except Exception as exc:
|
|
224
|
+
raise ConfigError(f"Invalid TOML syntax in {path}: {exc}") from exc
|
|
225
|
+
if not isinstance(parsed, dict):
|
|
226
|
+
raise ConfigError(f"Configuration root in {path} must be a TOML table.")
|
|
227
|
+
return parsed
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def load_config(
|
|
231
|
+
project_dir: Path | None = None,
|
|
232
|
+
config_path: Path | None = None,
|
|
233
|
+
*,
|
|
234
|
+
allow_missing: bool = False,
|
|
235
|
+
env: Mapping[str, str] | None = None,
|
|
236
|
+
) -> DoxtrConfig:
|
|
237
|
+
"""Load and validate configuration from ``doxtr.toml`` or ``[tool.doxtr]``."""
|
|
238
|
+
base_dir = (project_dir or Path.cwd()).resolve()
|
|
239
|
+
if not base_dir.exists() or not base_dir.is_dir():
|
|
240
|
+
raise ConfigError(f"Project directory does not exist: {base_dir}")
|
|
241
|
+
|
|
242
|
+
resolved_path, raw = _discover_config_file(base_dir, config_path)
|
|
243
|
+
if resolved_path is None and not allow_missing:
|
|
244
|
+
raise ConfigError(
|
|
245
|
+
f"No doxtr.toml or [tool.doxtr] configuration found in {base_dir}. "
|
|
246
|
+
"Pass --config to specify one explicitly."
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
project_root = (
|
|
250
|
+
resolved_path.parent.resolve()
|
|
251
|
+
if resolved_path is not None and project_dir is None
|
|
252
|
+
else base_dir
|
|
253
|
+
)
|
|
254
|
+
|
|
255
|
+
project_tbl = _as_table(raw.get("project"), "project")
|
|
256
|
+
html_tbl = _as_table(raw.get("html"), "html")
|
|
257
|
+
pdf_tbl = _as_table(raw.get("pdf"), "pdf")
|
|
258
|
+
conf_tbl = _as_table(raw.get("confluence"), "confluence")
|
|
259
|
+
conf_release_tbl = _as_table(conf_tbl.get("release"), "confluence.release")
|
|
260
|
+
conf_test_tbl = _as_table(conf_tbl.get("test"), "confluence.test")
|
|
261
|
+
serve_tbl = _as_table(raw.get("serve"), "serve")
|
|
262
|
+
|
|
263
|
+
project = ProjectConfig(
|
|
264
|
+
name=_as_str(project_tbl, "name", base_dir.name or "doxtr-docs", "project"),
|
|
265
|
+
version=_as_str(project_tbl, "version", "0.1.0", "project"),
|
|
266
|
+
source_dir=_as_str(project_tbl, "source_dir", "source", "project"),
|
|
267
|
+
build_dir=_as_str(project_tbl, "build_dir", "build", "project"),
|
|
268
|
+
latex_name=_as_str(project_tbl, "latex_name", "doxtr", "project"),
|
|
269
|
+
)
|
|
270
|
+
|
|
271
|
+
html = HtmlConfig(
|
|
272
|
+
theme=_as_str(html_tbl, "theme", "doxtr_aubergine", "html"),
|
|
273
|
+
downloads_subdir=_as_str(html_tbl, "downloads_subdir", "_static/downloads", "html").strip(
|
|
274
|
+
"/"
|
|
275
|
+
),
|
|
276
|
+
)
|
|
277
|
+
|
|
278
|
+
pdf = PdfConfig(
|
|
279
|
+
light=_as_bool(pdf_tbl, "light", True, "pdf"),
|
|
280
|
+
dark=_as_bool(pdf_tbl, "dark", True, "pdf"),
|
|
281
|
+
light_filename=_as_str(pdf_tbl, "light_filename", "doxtr-light.pdf", "pdf"),
|
|
282
|
+
dark_filename=_as_str(pdf_tbl, "dark_filename", "doxtr-dark.pdf", "pdf"),
|
|
283
|
+
copy_to_html_downloads=_as_bool(pdf_tbl, "copy_to_html_downloads", True, "pdf"),
|
|
284
|
+
)
|
|
285
|
+
|
|
286
|
+
confluence = ConfluenceConfig(
|
|
287
|
+
release=_parse_confluence_target(conf_release_tbl, "confluence.release", env=env),
|
|
288
|
+
test=_parse_confluence_target(
|
|
289
|
+
conf_test_tbl, "confluence.test", default_prefix="[TEST] ", env=env
|
|
290
|
+
),
|
|
291
|
+
)
|
|
292
|
+
|
|
293
|
+
backend = _as_str(serve_tbl, "default_backend", "auto", "serve")
|
|
294
|
+
if backend not in _VALID_BACKENDS:
|
|
295
|
+
raise ConfigError(
|
|
296
|
+
f"Invalid serve.default_backend '{backend}'. Expected one of: {', '.join(sorted(_VALID_BACKENDS))}."
|
|
297
|
+
)
|
|
298
|
+
|
|
299
|
+
port = _as_int(serve_tbl, "port", 8008, "serve")
|
|
300
|
+
if port < 1 or port > 65535:
|
|
301
|
+
raise ConfigError(f"Invalid serve.port {port}. Expected 1..65535.")
|
|
302
|
+
|
|
303
|
+
serve = ServeConfig(
|
|
304
|
+
default_backend=backend,
|
|
305
|
+
port=port,
|
|
306
|
+
host=_as_str(serve_tbl, "host", "0.0.0.0", "serve"),
|
|
307
|
+
versions_dir=_as_str(serve_tbl, "versions_dir", "build/versions", "serve"),
|
|
308
|
+
nginx_template=_as_str(
|
|
309
|
+
serve_tbl, "nginx_template", "/workspaces/docs/reactor/default.template", "serve"
|
|
310
|
+
),
|
|
311
|
+
)
|
|
312
|
+
|
|
313
|
+
return DoxtrConfig(
|
|
314
|
+
project_root=project_root,
|
|
315
|
+
config_path=resolved_path,
|
|
316
|
+
project=project,
|
|
317
|
+
html=html,
|
|
318
|
+
pdf=pdf,
|
|
319
|
+
confluence=confluence,
|
|
320
|
+
serve=serve,
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def validate_scheme_name(scheme: str | None) -> str | None:
|
|
325
|
+
"""Validate optional color scheme override (``light`` or ``dark``)."""
|
|
326
|
+
if scheme is None:
|
|
327
|
+
return None
|
|
328
|
+
if scheme not in _VALID_SCHEMES:
|
|
329
|
+
raise ConfigError(f"Invalid color scheme '{scheme}'. Expected 'light' or 'dark'.")
|
|
330
|
+
return scheme
|