Skip to content

Commit 0beec0d

Browse files
committed
wip: refactor load_component logic
1 parent 5932612 commit 0beec0d

File tree

1 file changed

+44
-27
lines changed

1 file changed

+44
-27
lines changed

src/pulp_docs/plugin.py

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Component:
4545
git_url: str
4646
rest_api: str
4747

48+
version: str
4849
repository_dir: Path
4950
component_dir: Path
5051

@@ -58,11 +59,19 @@ def build(cls, find_path: list[str], component_opt: ComponentOption):
5859
for dir in expanded_path:
5960
dir = Path(dir)
6061
component_dir = dir.parent / component_opt.path
61-
if repository_name == dir.name and component_dir.exists():
62+
found_dir = repository_name == dir.name and component_dir.exists()
63+
if found_dir:
64+
version = "unknown"
65+
try:
66+
pyproject = component_dir / "pyproject.toml"
67+
version = tomllib.loads(pyproject.read_text())["project"]["version"]
68+
except Exception:
69+
pass
70+
body["version"] = version
6271
body["repository_dir"] = dir
6372
body["component_dir"] = component_dir
6473
return cls(**body)
65-
raise None
74+
return None
6675

6776

6877
class PulpDocsPluginConfig(Config):
@@ -211,12 +220,6 @@ def component_data(
211220
component_dir = component.component_dir
212221
path = component_dir.name
213222

214-
version = "unknown"
215-
try:
216-
pyproject = component_dir / "pyproject.toml"
217-
version = tomllib.loads(pyproject.read_text())["project"]["version"]
218-
except Exception:
219-
pass
220223
github_org = "pulp"
221224
try:
222225
template_config = component_dir / "template_config.yml"
@@ -234,7 +237,7 @@ def component_data(
234237
return {
235238
"title": f"[{component.title}](site:{path}/)",
236239
"kind": component.kind,
237-
"version": version,
240+
"version": component.version,
238241
"links": links,
239242
}
240243

@@ -255,35 +258,49 @@ def rss_items() -> list:
255258
return rss_feed["items"][:20]
256259

257260

261+
def load_components(find_path: list[str], config: MkDocsConfig, draft: bool):
262+
loaded_components = []
263+
for component_opt in config.components:
264+
component = Component.build(find_path, component_opt)
265+
if component:
266+
loaded_components.append(component)
267+
all_components = {o.path for o in config.components}
268+
not_loaded_components = all_components.difference(
269+
{o.path for o in loaded_components}
270+
)
271+
if not_loaded_components:
272+
if draft:
273+
log.warning(f"Skip missing components {not_loaded_components}.")
274+
else:
275+
raise PluginError(f"Components missing: {not_loaded_components}.")
276+
return loaded_components
277+
278+
258279
class PulpDocsPlugin(BasePlugin[PulpDocsPluginConfig]):
259280
def on_config(self, config: MkDocsConfig) -> MkDocsConfig | None:
260-
# Two directories up from docs is where we expect all the other repositories.
261281
self.blog = ctx_blog.get()
262282
self.docstrings = ctx_docstrings.get()
263283
self.draft = ctx_draft.get()
264284

265285
self.pulp_docs_dir = Path(config.docs_dir).parent
286+
# Two directories up from docs is where we expect all the other repositories by default.
266287
self.find_path = ctx_path.get() or [f"{self.pulp_docs_dir.parent}/*"]
267288

289+
loaded_components = load_components(self.find_path, self.config, self.draft)
290+
self.config.components = loaded_components
291+
292+
log.info(
293+
f"Using components={[str(c.component_dir) for c in loaded_components]}"
294+
)
295+
268296
mkdocstrings_config = config.plugins["mkdocstrings"].config
269297
components_var = []
270-
new_components = []
271-
for component_opt in self.config.components:
272-
component = Component.build(self.find_path, component_opt)
273-
if component:
274-
components_var.append(component_data(component))
275-
config.watch.append(str(component.component_dir / "docs"))
276-
mkdocstrings_config.handlers["python"]["paths"].append(
277-
str(component.component_dir)
278-
)
279-
new_components.append(component)
280-
else:
281-
if self.draft:
282-
log.warning(f"Skip missing component '{component.title}'.")
283-
else:
284-
raise PluginError(f"Component '{component.title}' missing.")
285-
self.config.components = new_components
286-
log.info(f"Using components={[str(c.component_dir) for c in new_components]}")
298+
for component in self.config.components:
299+
components_var.append(component_data(component))
300+
config.watch.append(str(component.component_dir / "docs"))
301+
mkdocstrings_config.handlers["python"]["paths"].append(
302+
str(component.component_dir)
303+
)
287304

288305
macros_plugin = config.plugins["macros"]
289306
macros_plugin.register_macros({"rss_items": rss_items})

0 commit comments

Comments
 (0)