Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit 1318b24

Browse files
authored
feat: grab repository metadata (#364)
* feat: grab repository metadata * feat: do not generate summary pages if metadata not found * feat: address review comments
1 parent e3ae026 commit 1318b24

1 file changed

Lines changed: 47 additions & 25 deletions

File tree

docfx_yaml/extension.py

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,22 @@
1818
1919
This extension allows you to automagically generate DocFX YAML from your Python AutoAPI docs.
2020
"""
21-
import ast
22-
import os
23-
import inspect
24-
import re
25-
import copy
26-
import shutil
27-
import black
28-
import logging
2921

22+
import ast
3023
from collections import defaultdict
31-
from collections.abc import MutableSet, Mapping, Sequence
32-
from pathlib import Path
24+
from collections.abc import Mapping, MutableSet, Sequence
25+
import copy
3326
from functools import partial
27+
import inspect
3428
from itertools import zip_longest
29+
import json
30+
import logging
31+
import os
32+
from pathlib import Path
33+
import re
34+
import shutil
3535
from typing import Any, Dict, Iterable, List, Optional
36+
import black
3637
from black import InvalidInput
3738

3839
try:
@@ -161,7 +162,25 @@ class Bcolors:
161162
logging.getLogger("blib2to3").setLevel(logging.ERROR)
162163

163164

165+
def _grab_repo_metadata() -> Mapping[str, str] | None:
166+
"""Retrieves the repository's metadata info if found."""
167+
try:
168+
with open('.repo-metadata.json', 'r') as metadata_file:
169+
json_content = json.load(metadata_file)
170+
# Return outside of context manager for safe close
171+
return json_content
172+
except Exception:
173+
return None
174+
175+
164176
def build_init(app):
177+
print("Retrieving repository metadata.")
178+
if not (repo_metadata := _grab_repo_metadata()):
179+
print("Failed to retrieve repository metadata.")
180+
app.env.library_shortname = ""
181+
else:
182+
print("Successfully retrieved repository metadata.")
183+
app.env.library_shortname = repo_metadata["name"]
165184
print("Running sphinx-build with Markdown first...")
166185
markdown_utils.run_sphinx_markdown()
167186
print("Completed running sphinx-build with Markdown files.")
@@ -2013,18 +2032,19 @@ def convert_module_to_package_if_needed(obj):
20132032
markdown_utils.remove_unused_pages(
20142033
added_pages, app.env.moved_markdown_pages, normalized_outdir)
20152034

2016-
# Add summary pages as the second entry into the table of contents.
2017-
pkg_toc_yaml.insert(
2018-
1,
2019-
{
2020-
"name": f"{app.config.project} APIs",
2021-
"items": [
2022-
{"name": "Classes", "href": "summary_class.yml"},
2023-
{"name": "Methods", "href": "summary_method.yml"},
2024-
{"name": "Properties and Attributes", "href": "summary_property.yml"},
2025-
],
2026-
}
2027-
)
2035+
if app.env.library_shortname:
2036+
# Add summary pages as the second entry into the table of contents.
2037+
pkg_toc_yaml.insert(
2038+
1,
2039+
{
2040+
"name": f"{app.env.library_shortname} APIs",
2041+
"items": [
2042+
{"name": "Classes", "href": "summary_class.yml"},
2043+
{"name": "Methods", "href": "summary_method.yml"},
2044+
{"name": "Properties and Attributes", "href": "summary_property.yml"},
2045+
],
2046+
}
2047+
)
20282048

20292049
toc_file = os.path.join(normalized_outdir, 'toc.yml')
20302050
with open(toc_file, 'w') as writable:
@@ -2040,7 +2060,7 @@ def convert_module_to_package_if_needed(obj):
20402060

20412061
cgc_url = (
20422062
"https://cloud.google.com/python/docs/reference/"
2043-
f"{app.config.project}/latest/"
2063+
f"{app.env.library_shortname}/latest/"
20442064
)
20452065
yaml_entry_line = "### YamlMime:UniversalReference\n"
20462066
# Output files
@@ -2086,9 +2106,11 @@ def convert_module_to_package_if_needed(obj):
20862106
file_name_set.add(filename)
20872107

20882108
for entry in yaml_data:
2109+
if not app.env.library_shortname:
2110+
break
20892111
summary_type = _SUMMARY_TYPE_BY_ITEM_TYPE.get(entry.get("type"))
20902112
if not (summary_type):
2091-
continue
2113+
continue
20922114

20932115
_find_and_add_summary_details(entry, summary_type, cgc_url)
20942116

@@ -2105,7 +2127,7 @@ def convert_module_to_package_if_needed(obj):
21052127
children_names_and_content,
21062128
entry_name,
21072129
summary_type,
2108-
app.config.project,
2130+
app.env.library_shortname,
21092131
)
21102132

21112133
file_path_to_use = os.path.join(normalized_outdir, file_name)

0 commit comments

Comments
 (0)