Skip to content

Commit

Permalink
Fixed type annotation in config.
Browse files Browse the repository at this point in the history
  • Loading branch information
coordt committed Feb 24, 2024
1 parent 2850aa7 commit 2988ede
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 4 deletions.
4 changes: 2 additions & 2 deletions bumpversion/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Union
from typing import TYPE_CHECKING, Any, Union

from bumpversion.config.files import read_config_file
from bumpversion.config.models import Config
Expand Down Expand Up @@ -37,7 +37,7 @@
}


def get_configuration(config_file: Union[str, Path, None] = None, **overrides) -> Config:
def get_configuration(config_file: Union[str, Path, None] = None, **overrides: Any) -> Config:
"""
Return the configuration based on any configuration files and overrides.
Expand Down
2 changes: 1 addition & 1 deletion docsrc/reference/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
- [Formatting context](formatting-context.md)
- [Version parts](version-parts.md)
- [Search and replace configuration](search-and-replace-config.md)
- [API](api/bumpversion.md)
- [API](api/bumpversion/index.md)
3 changes: 3 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ plugins:
- git-revision-date-localized
- git-authors:
show_email_address: false
exclude:
- reference/api/*
- include-markdown
- drawio
- literate-nav:
Expand Down Expand Up @@ -98,6 +100,7 @@ plugins:
merge_init_into_class: true
separate_signature: true
show_docstring_parameters: true
show_root_toc_entry: true
show_signature_annotations: true
show_source: false
show_symbol_type_heading: true
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ dev = [
"pre-commit",
]
docs = [
"black",
"markdown-customblocks",
"mdx-truly-sane-lists",
"mkdocs",
Expand All @@ -69,7 +70,6 @@ docs = [
"mkdocs-include-markdown-plugin",
"mkdocs-literate-nav",
"mkdocs-material",
"mkdocstrings",
"mkdocstrings[python]",
"python-frontmatter",
]
Expand Down
76 changes: 76 additions & 0 deletions tools/update_frontmatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env python
"""Update frontmatter of markdown files."""

import argparse
from pathlib import Path
from typing import Any, Dict, Optional

import frontmatter


def extract_main_heading(markdown_content: str) -> Optional[str]:
"""
Extracts the first level 1 heading from the provided Markdown content.
Args:
markdown_content: A string containing Markdown text.
Returns:
The text of the first level 1 heading, or None if no such heading is found.
"""
lines = markdown_content.split("\n")

return next((line[2:] for line in lines if line.startswith("# ")), None)


def calc_title(post: frontmatter.Post) -> str:
"""Calculate the title of the post."""
return extract_main_heading(post.content) or post.get("title", "")


def calc_comment(post: frontmatter.Post) -> bool:
"""Calculate if the post has comments."""
return bool(post.get("comments", True))


def calculate_update(post: frontmatter.Post) -> dict:
"""Calculate if the frontmatter needs to be updated."""
expected_title = calc_title(post)
expected_comment = calc_comment(post)
update: Dict[str, Any] = {}
if expected_title and expected_title != post.get("title"):
update["title"] = expected_title
if expected_comment != post.get("comments"):
update["comments"] = expected_comment
return update


def process_file(markdown_path: Path) -> None:
"""Process a single file."""
if not (markdown_path.is_file() and markdown_path.suffix == ".md"):
return
raw_text = markdown_path.read_text()
post = frontmatter.loads(raw_text)

update = calculate_update(post)
if update:
for key, value in update.items():
post[key] = value
new_text = frontmatter.dumps(post)
print(f"Updating {markdown_path}") # noqa: T201
markdown_path.write_text(new_text)


def parse_args() -> argparse.Namespace:
"""Parse command line arguments."""
parser = argparse.ArgumentParser(description="Update frontmatter of markdown files")
parser.add_argument("markdown_path", type=str, nargs="+", help="Path or glob to markdown files")
return parser.parse_args()


if __name__ == "__main__":
args = parse_args()
documents = args.markdown_path
for document in documents:
for path in Path().glob(document):
process_file(path)

0 comments on commit 2988ede

Please sign in to comment.