Skip to content

Commit 8838f05

Browse files
committed
Add repository finding mechanism similar to PATH
1 parent 520a2d5 commit 8838f05

File tree

3 files changed

+65
-8
lines changed

3 files changed

+65
-8
lines changed

src/pulp_docs/cli.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import click
22

33
from mkdocs.__main__ import cli as mkdocs_cli
4-
from pulp_docs.context import ctx_blog, ctx_docstrings, ctx_draft
4+
from pulp_docs.context import (
5+
ctx_blog,
6+
ctx_docstrings,
7+
ctx_draft,
8+
ctx_paths,
9+
)
510

611

712
def blog_callback(ctx: click.Context, param: click.Parameter, value: bool) -> bool:
@@ -21,13 +26,22 @@ def draft_callback(ctx: click.Context, param: click.Parameter, value: bool) -> b
2126
return value
2227

2328

29+
def find_paths_callback(
30+
ctx: click.Context, param: click.Parameter, value: bool
31+
) -> bool:
32+
result = [item.strip() for item in value.split(";") if item.strip()]
33+
ctx_paths.set(result)
34+
return result
35+
36+
2437
blog_option = click.option(
2538
"--blog/--no-blog",
2639
default=True,
2740
expose_value=False,
2841
callback=blog_callback,
2942
help="Build blog.",
3043
)
44+
3145
docstrings_option = click.option(
3246
"--docstrings/--no-docstrings",
3347
default=True,
@@ -43,13 +57,24 @@ def draft_callback(ctx: click.Context, param: click.Parameter, value: bool) -> b
4357
help="Don't fail if repositories are missing.",
4458
)
4559

60+
paths_option = click.option(
61+
"--paths",
62+
envvar="PULPDOCS_PATHS",
63+
expose_value=False,
64+
default="",
65+
callback=find_paths_callback,
66+
help="A semicolon separated list of repository paths. Accept glob patterns.",
67+
)
68+
69+
4670
main = mkdocs_cli
4771

4872
for command_name in ["build", "serve"]:
4973
sub_command = main.commands.get(command_name)
5074
draft_option(sub_command)
5175
blog_option(sub_command)
5276
docstrings_option(sub_command)
77+
paths_option(sub_command)
5378
serve_options = sub_command.params
5479
config_file_opt = next(filter(lambda opt: opt.name == "config_file", serve_options))
5580
config_file_opt.envvar = "PULPDOCS_DIR"

src/pulp_docs/context.py

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
ctx_blog = ContextVar("ctx_blog", default=True)
55
ctx_docstrings = ContextVar("ctx_docstrings", default=True)
66
ctx_draft = ContextVar("ctx_draft", default=False)
7+
ctx_paths = ContextVar("ctx_paths", default=None)

src/pulp_docs/plugin.py

+38-7
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
import json
44
import tomllib
55
import yaml
6+
import glob
67

78
import httpx
89
from git import Repo, GitCommandError
910
from mkdocs.config import Config, config_options
1011
from mkdocs.config.defaults import MkDocsConfig
1112
from mkdocs.exceptions import PluginError
12-
from mkdocs.plugins import event_priority, get_plugin_logger, BasePlugin
13+
from mkdocs.plugins import get_plugin_logger, BasePlugin
1314
from mkdocs.structure.files import File, Files
1415
from mkdocs.structure.nav import Navigation, Section, Link
1516
from mkdocs.structure.pages import Page
1617
from mkdocs.utils.templates import TemplateContext
1718

18-
from pulp_docs.context import ctx_blog, ctx_docstrings, ctx_draft
19+
from pulp_docs.context import ctx_blog, ctx_docstrings, ctx_draft, ctx_paths
1920

2021
log = get_plugin_logger(__name__)
2122

@@ -34,6 +35,33 @@ class ComponentOption(Config):
3435
git_url = config_options.Type(str, default="")
3536
rest_api = config_options.Type(str, default="")
3637

38+
_repository_dir = None
39+
40+
@property
41+
def repository_dir(self):
42+
if not self._repository_dir:
43+
raise RuntimeError(
44+
"Can't get repository/component path before resolving the dir in the filesystem."
45+
)
46+
return self._repository_dir
47+
48+
@property
49+
def component_dir(self):
50+
return self.repository_dir.parent / self.path
51+
52+
def resolve_location(self, find_paths: list[str]):
53+
repository_name = self.path.split("/")[0]
54+
expanded_paths = []
55+
for dir in find_paths:
56+
expanded_paths.extend(glob.glob(dir))
57+
for dir in expanded_paths:
58+
dir = Path(dir)
59+
component_dir = dir.parent / self.path
60+
if repository_name == dir.name and component_dir.exists():
61+
self._repository_dir = dir
62+
return self._repository_dir
63+
return None
64+
3765

3866
class PulpDocsPluginConfig(Config):
3967
components = config_options.ListOfItems(ComponentOption, default=[])
@@ -234,13 +262,15 @@ def on_config(self, config: MkDocsConfig) -> MkDocsConfig | None:
234262

235263
self.pulp_docs_dir = Path(config.docs_dir).parent
236264
self.repositories_dir = self.pulp_docs_dir.parent
265+
self.find_paths = ctx_paths.get() or [f"{self.repositories_dir}/*"]
237266

238267
mkdocstrings_config = config.plugins["mkdocstrings"].config
239268
components_var = []
240269
new_components = []
241270
for component in self.config.components:
242-
component_dir = self.repositories_dir / component.path
243-
if component_dir.exists():
271+
found_location = component.resolve_location(self.find_paths)
272+
if found_location:
273+
component_dir = component.component_dir
244274
components_var.append(component_data(component, component_dir))
245275
config.watch.append(str(component_dir / "docs"))
246276
mkdocstrings_config.handlers["python"]["paths"].append(
@@ -253,6 +283,7 @@ def on_config(self, config: MkDocsConfig) -> MkDocsConfig | None:
253283
else:
254284
raise PluginError(f"Component '{component.title}' missing.")
255285
self.config.components = new_components
286+
log.info(f"Using components={[str(c.component_dir) for c in new_components]}")
256287

257288
macros_plugin = config.plugins["macros"]
258289
macros_plugin.register_macros({"rss_items": rss_items})
@@ -273,10 +304,10 @@ def on_files(self, files: Files, /, *, config: MkDocsConfig) -> Files | None:
273304
user_nav: dict[str, t.Any] = {}
274305
dev_nav: dict[str, t.Any] = {}
275306
for component in self.config.components:
276-
component_dir = self.repositories_dir / component.path
307+
component_dir = component.component_dir
277308

278309
log.info(f"Fetching docs from '{component.title}'.")
279-
git_repository_dir = self.repositories_dir / Path(component.path).parts[0]
310+
git_repository_dir = component.repository_dir
280311
try:
281312
git_branch = Repo(git_repository_dir).active_branch.name
282313
except TypeError:
@@ -290,7 +321,7 @@ def on_files(self, files: Files, /, *, config: MkDocsConfig) -> Files | None:
290321
else:
291322
component_docs_dir = component_dir / "docs"
292323
component_slug = Path(component_dir.name)
293-
assert component_docs_dir.exists()
324+
assert component_docs_dir.exists(), component_docs_dir
294325

295326
component_nav = ComponentNav(config, component_slug)
296327

0 commit comments

Comments
 (0)