-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add insiders documentation features. (#48)
- Loading branch information
Showing
8 changed files
with
232 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
docs/_templates/mkdocstrings/python/readthedocs/class.html.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{# TODO: Drop Python 3.8: remove this file after updating to newer versions of mkdocstrings-python #} | ||
{% extends "_base/class.html.jinja" %} | ||
{% block inheritance_diagram %} | ||
{#- Inheritance diagram block. | ||
This block renders the inheritance diagram for the class, | ||
using Mermaid syntax and a bit of JavaScript to make the nodes clickable, | ||
linking to the corresponding class documentation. | ||
-#} | ||
{% if config.show_inheritance_diagram and class.bases %} | ||
{% macro edges(class) %} | ||
{% for base in class.resolved_bases %} | ||
{{ base.path }} --> {{ class.path }} | ||
{{ edges(base) }} | ||
{% endfor %} | ||
{% endmacro %} | ||
<div style="display: none"> | ||
<autoref identifier="{{ class.path }}" optional id="mermaid-link-{{ class.path }}"></autoref> | ||
{% for base in class.mro() %} | ||
<autoref identifier="{{ base.path }}" optional id="mermaid-link-{{ base.path }}"></autoref> | ||
{% endfor %} | ||
</div> | ||
<div class="mermaid" id="mermaid-diagram-{{ class.path }}"> | ||
flowchart LR | ||
{{ class.path }}[{{ class.name }}] | ||
{% for base in class.mro() %} | ||
{{ base.path }}[{{ base.name }}] | ||
{% endfor %} | ||
|
||
{{ edges(class) | safe }} | ||
|
||
click {{ class.path }} href "" "{{ class.path }}" | ||
{% for base in class.mro() %} | ||
click {{ base.path }} href "" "{{ base.path }}" | ||
{% endfor %} | ||
</div> | ||
<!--suppress ES6ConvertVarToLetConst --> | ||
<script> | ||
var diagram = document.getElementById('mermaid-diagram-{{ class.path }}'); | ||
diagram.innerHTML = diagram.innerHTML.replace(/click ([\w.]+) href "" "\1"/g, function(match, nodeID, offset) { | ||
try { | ||
const link = document.getElementById("mermaid-link-" + nodeID).href; | ||
return `click ${nodeID} href "${link}" "${nodeID}"` | ||
} catch (e) { | ||
return ''; | ||
} | ||
}); | ||
</script> | ||
{% endif %} | ||
{% endblock inheritance_diagram %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,7 +59,6 @@ markdown_extensions: | |
- def_list | ||
- pymdownx.snippets: | ||
check_paths: true | ||
dedent_subsections: true | ||
- pymdownx.highlight: | ||
use_pygments: true | ||
pygments_style: default | ||
|
@@ -90,36 +89,45 @@ plugins: | |
use_default: true | ||
inline_refs: none | ||
markdown_links: true | ||
- mermaid2 | ||
- mermaid2: | ||
javascript: https://unpkg.com/[email protected]/dist/mermaid.min.js | ||
- mkdocstrings: # additional customization takes place in docs/generate_api_pages.py | ||
enable_inventory: true | ||
custom_templates: docs/_templates/mkdocstrings | ||
handlers: | ||
python: # see https://mkdocstrings.github.io/python/usage/ | ||
paths: [src] | ||
options: | ||
docstring_options: | ||
ignore_init_summary: true | ||
trim_doctest_flags: true | ||
merge_init_into_class: true | ||
members_order: alphabetical | ||
show_inheritance_diagram: true | ||
inherited_members: true | ||
# General options | ||
show_inheritance_diagram: true # INSIDERS FEATURE | ||
show_source: false # a link is included at the top of each page | ||
docstring_section_style: list | ||
filters: ['!^_', ^__init__] | ||
summary: true | ||
show_signature_annotations: true | ||
separate_signature: true | ||
line_length: 100 | ||
signature_crossrefs: true | ||
show_labels: true | ||
# Headings options | ||
heading_level: 1 | ||
show_root_heading: true | ||
show_root_full_path: false | ||
show_category_heading: false | ||
show_symbol_type_heading: true | ||
show_symbol_type_toc: false | ||
# Members options | ||
inherited_members: true | ||
members_order: alphabetical | ||
filters: ['!^_', ^__init__] | ||
summary: false # Currently implemented with custom templates | ||
show_labels: true | ||
# Docstrings options | ||
docstring_options: | ||
ignore_init_summary: true | ||
trim_doctest_flags: true | ||
docstring_section_style: list | ||
merge_init_into_class: true | ||
relative_crossrefs: true # INSIDERS FEATURE | ||
scoped_crossrefs: true # INSIDERS FEATURE | ||
# Signature options | ||
line_length: 100 | ||
modernize_annotations: false # INSIDERS FEATURE (if the source code annotations format changes, update this to true) | ||
show_signature_annotations: true | ||
signature_crossrefs: true | ||
separate_signature: true | ||
import: | ||
- url: https://docs.python.org/3/objects.inv | ||
domains: [std, py] | ||
|
@@ -154,8 +162,8 @@ plugins: | |
strict_only: true | ||
nav: # The first entry in each section needs a hard-coded title to avoid being folded in as the section index | ||
- index.md | ||
- Usage Guide: | ||
- Basic usage: basic_usage.md | ||
- The Basics: | ||
- Basic Usage: basic_usage.md | ||
- glossary.md | ||
- CHANGELOG.md | ||
- Contributing: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
"""Install insiders packages corresponding to currently installed versions of specified packages.""" | ||
|
||
import importlib.metadata | ||
import logging | ||
import os | ||
import subprocess | ||
import sys | ||
|
||
from typing import List | ||
|
||
import requests | ||
|
||
from packaging.version import parse as parse_version | ||
|
||
logging.basicConfig( | ||
level=getattr(logging, os.getenv("LOGGING_LEVEL", "INFO")), | ||
format="[%(asctime)s] [%(levelname)8s] %(message)s", | ||
) | ||
logger = logging.getLogger(__name__) | ||
|
||
PACKAGE_LIST = { | ||
"mkdocstrings-python": "https://github.com/pawamoy-insiders/mkdocstrings-python", | ||
"griffe": "https://github.com/pawamoy-insiders/griffe", | ||
} | ||
|
||
|
||
def get_github_tags(repo_url_str: str, github_token: str) -> List[str]: | ||
"""Get the tags for a GitHub repository. | ||
Args: | ||
repo_url_str: The URL of the GitHub repository. | ||
github_token: The GitHub token to use for authentication. | ||
Returns: | ||
A list of tag names for the repository. | ||
""" | ||
# Extract the owner and repository name from the URL | ||
parts = repo_url_str.rstrip("/").split("/") | ||
owner, repo = parts[-2], parts[-1] | ||
|
||
try: | ||
response = requests.get( | ||
f"https://api.github.com/repos/{owner}/{repo}/tags", | ||
headers={"Authorization": f"token {github_token}"}, | ||
timeout=30, | ||
) | ||
response.raise_for_status() # Raise an exception for HTTP errors | ||
|
||
tags = response.json() | ||
return [tag["name"] for tag in tags] | ||
|
||
except requests.exceptions.RequestException: | ||
logger.error("Error getting tags for %s", repo_url_str) # noqa: TRY400 | ||
return [] | ||
|
||
|
||
def get_newest_matching_tag(current_version: str, tags: List[str]) -> str: | ||
"""Find the newest tag that starts with the given current version. | ||
Args: | ||
current_version: The current version of the package. | ||
tags: A list of tag names. | ||
Returns: | ||
The newest tag that starts with the current version, or an empty string if none are found. | ||
""" | ||
# Filter tags that start with the current version | ||
matching_tags = [tag for tag in tags if tag.startswith(current_version)] | ||
|
||
# Sort matching tags as version numbers in descending order | ||
matching_tags.sort(key=parse_version, reverse=True) | ||
|
||
return matching_tags[0] if matching_tags else "" | ||
|
||
|
||
def install_package(package_name: str, repo_url_str: str, tag: str, github_token: str) -> None: | ||
"""Install a package from a GitHub repository using a specific tag. | ||
Args: | ||
package_name: The name of the package to install. | ||
repo_url_str: The HTTPS URL of the GitHub repository. | ||
tag: The tag to install. | ||
github_token: The GitHub token to use for authentication. | ||
""" | ||
# Insert the token into the URL for authentication | ||
repo_url_str = repo_url_str.replace("https://", f"https://{github_token}@") | ||
|
||
install_url = f"{repo_url_str}.git@{tag}" | ||
try: | ||
logger.info("Installing %s from tag %s", package_name, tag) | ||
subprocess.check_call( # noqa: S603 | ||
[sys.executable, "-m", "pip", "install", f"git+{install_url}"], | ||
stdout=subprocess.DEVNULL, | ||
stderr=subprocess.DEVNULL, | ||
) | ||
logger.info("Successfully installed %s from tag %s", package_name, tag) | ||
except subprocess.CalledProcessError: | ||
logger.error("Failed to install %s from tag %s", package_name, tag) # noqa: TRY400 | ||
|
||
|
||
def main() -> None: | ||
"""Install insiders packages.""" | ||
if not (github_token := os.environ.get("GITHUB_PAT")): | ||
logger.info( | ||
"GITHUB_PAT environment variable is not set, no insiders packages will be installed." | ||
) | ||
return | ||
for package, repo_url in PACKAGE_LIST.items(): | ||
logger.info("Processing %s", package) | ||
try: | ||
version = importlib.metadata.version(package) | ||
logger.info("%s installed version is %s", package, version) | ||
except importlib.metadata.PackageNotFoundError: | ||
logger.warning("%s is not installed.", package) | ||
version = None | ||
|
||
if version and (package_tags := get_github_tags(repo_url, github_token)): | ||
logger.info("Tags for %s: %s", package, package_tags) | ||
|
||
if newest_tag := get_newest_matching_tag(version, package_tags): | ||
logger.info( | ||
'Newest matching tag for %s version %s is "%s"', | ||
package, | ||
version, | ||
newest_tag, | ||
) | ||
install_package(package, repo_url, newest_tag, github_token) | ||
else: | ||
logger.info("No matching tags found for %s with version %s", package, version) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |