Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: grab repository metadata #364

Merged
merged 3 commits into from
Apr 1, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions docfx_yaml/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import shutil
import black
import logging
import json

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: import ordering

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I was able to sort through the first party libraries.


from collections import defaultdict
from collections.abc import MutableSet, Mapping, Sequence
Expand Down Expand Up @@ -161,7 +162,25 @@ class Bcolors:
logging.getLogger("blib2to3").setLevel(logging.ERROR)


def _grab_repo_metadata() -> Mapping[str, str] | None:
"""Retrieves the repository's metadata info if found."""
try:
with open('.repo-metadata.json', 'r') as metadata_file:
json_content = json.load(metadata_file)
# Return outside of context manager for safe close
return json_content
except Exception:
return None


def build_init(app):
print("Retrieving repository metadata.")
if not (repo_metadata := _grab_repo_metadata()):
print("Failed to retrieve repository metadata.")
app.env.library_shortname = ""
else:
print("Successfully retrieved repository metadata.")
app.env.library_shortname = repo_metadata["name"]
print("Running sphinx-build with Markdown first...")
markdown_utils.run_sphinx_markdown()
print("Completed running sphinx-build with Markdown files.")
Expand Down Expand Up @@ -2013,18 +2032,19 @@ def convert_module_to_package_if_needed(obj):
markdown_utils.remove_unused_pages(
added_pages, app.env.moved_markdown_pages, normalized_outdir)

# Add summary pages as the second entry into the table of contents.
pkg_toc_yaml.insert(
1,
{
"name": f"{app.config.project} APIs",
"items": [
{"name": "Classes", "href": "summary_class.yml"},
{"name": "Methods", "href": "summary_method.yml"},
{"name": "Properties and Attributes", "href": "summary_property.yml"},
],
}
)
if app.env.library_shortname:
# Add summary pages as the second entry into the table of contents.
pkg_toc_yaml.insert(
1,
{
"name": f"{app.env.library_shortname} APIs",
"items": [
{"name": "Classes", "href": "summary_class.yml"},
{"name": "Methods", "href": "summary_method.yml"},
{"name": "Properties and Attributes", "href": "summary_property.yml"},
],
}
)

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

cgc_url = (
"https://cloud.google.com/python/docs/reference/"
f"{app.config.project}/latest/"
f"{app.env.library_shortname}/latest/"
)
yaml_entry_line = "### YamlMime:UniversalReference\n"
# Output files
Expand Down Expand Up @@ -2086,9 +2106,11 @@ def convert_module_to_package_if_needed(obj):
file_name_set.add(filename)

for entry in yaml_data:
if not app.env.library_shortname:
continue

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would apply to the whole library, right? If so, I think you can just break instead of continue.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, done.

summary_type = _SUMMARY_TYPE_BY_ITEM_TYPE.get(entry.get("type"))
if not (summary_type):
continue
continue

_find_and_add_summary_details(entry, summary_type, cgc_url)

Expand All @@ -2105,7 +2127,7 @@ def convert_module_to_package_if_needed(obj):
children_names_and_content,
entry_name,
summary_type,
app.config.project,
app.env.library_shortname,
)

file_path_to_use = os.path.join(normalized_outdir, file_name)
Expand Down
Loading