Skip to content

Commit

Permalink
refacto(python): apply git hooks with Python >= 3.10
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed Jun 10, 2024
1 parent a8138f9 commit 8ec6164
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

# standard library
from pathlib import Path
from typing import Optional

# 3rd party
from mkdocs.config.config_options import Config
Expand Down Expand Up @@ -174,7 +173,7 @@ def get_social_cards_dir(self, mkdocs_config: Config) -> str:
return social_plugin_cfg.config.cards_dir

def get_social_card_build_path_for_page(
self, mkdocs_page: Page, mkdocs_site_dir: Optional[str] = None
self, mkdocs_page: Page, mkdocs_site_dir: str | None = None
) -> Path:
"""Get social card URL for a specific page in documentation.
Expand All @@ -197,7 +196,7 @@ def get_social_card_build_path_for_page(
def get_social_card_url_for_page(
self,
mkdocs_page: Page,
mkdocs_site_url: Optional[str] = None,
mkdocs_site_url: str | None = None,
) -> str:
"""Get social card URL for a specific page in documentation.
Expand Down
24 changes: 12 additions & 12 deletions mkdocs_rss_plugin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# standard
from datetime import datetime
from pathlib import Path
from typing import NamedTuple, Optional
from typing import NamedTuple


# ############################################################################
Expand All @@ -16,14 +16,14 @@
class PageInformation(NamedTuple):
"""Data type to set and get page information in order to produce the RSS feed."""

abs_path: Optional[Path] = None
categories: Optional[list] = None
authors: Optional[tuple] = None
created: Optional[datetime] = None
description: Optional[str] = None
guid: Optional[str] = None
image: Optional[str] = None
title: Optional[str] = None
updated: Optional[datetime] = None
url_comments: Optional[str] = None
url_full: Optional[str] = None
abs_path: Path | None = None
categories: list | None = None
authors: tuple | None = None
created: datetime | None = None
description: str | None = None
guid: str | None = None
image: str | None = None
title: str | None = None
updated: datetime | None = None
url_comments: str | None = None
url_full: str | None = None
9 changes: 4 additions & 5 deletions mkdocs_rss_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from email.utils import formatdate
from pathlib import Path
from re import compile
from typing import Optional

# 3rd party
from jinja2 import Environment, FileSystemLoader, select_autoescape
Expand Down Expand Up @@ -58,9 +57,9 @@ def __init__(self):
"""Instanciation."""
# dates source
self.src_date_created = self.src_date_updated = "git"
self.meta_datetime_format: Optional[str] = None
self.meta_datetime_format: str | None = None
self.meta_default_timezone: str = "UTC"
self.meta_default_time: Optional[datetime.time] = None
self.meta_default_time: datetime.time | None = None
# pages storage
self.pages_to_filter: list = []
# prepare output feeds
Expand Down Expand Up @@ -208,7 +207,7 @@ def on_config(self, config: config_options.Config) -> dict:
@event_priority(priority=-75)
def on_page_content(
self, html: str, page: Page, config: config_options.Config, files
) -> Optional[str]:
) -> str | None:
"""The page_content event is called after the Markdown text is rendered
to HTML (but before being passed to a template) and can be used to alter
the HTML body of the page.
Expand Down Expand Up @@ -297,7 +296,7 @@ def on_page_content(
)
)

def on_post_build(self, config: config_options.Config) -> Optional[dict]:
def on_post_build(self, config: config_options.Config) -> dict | None:
"""The post_build event does not alter any variables. \
Use this event to call post-build scripts. \
See: <https://www.mkdocs.org/user-guide/plugins/#on_post_build>
Expand Down
29 changes: 13 additions & 16 deletions mkdocs_rss_plugin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
# standard library
import logging
import ssl
import sys
from collections.abc import Iterable
from datetime import date, datetime
from email.utils import format_datetime
from mimetypes import guess_type
from pathlib import Path
from typing import Any, Iterable, Optional, Tuple, Union
from typing import Any
from urllib import request
from urllib.error import HTTPError, URLError
from urllib.parse import urlencode, urlparse, urlunparse
Expand All @@ -33,10 +33,7 @@
)

# conditional imports
if sys.version_info < (3, 9):
from mkdocs_rss_plugin.timezoner_pre39 import set_datetime_zoneinfo
else:
from mkdocs_rss_plugin.timezoner_py39 import set_datetime_zoneinfo
from mkdocs_rss_plugin.timezoner_py39 import set_datetime_zoneinfo

# ############################################################################
# ########## Globals #############
Expand All @@ -58,9 +55,9 @@ def __init__(
self,
path: str = ".",
use_git: bool = True,
integration_material_social_cards: Optional[
integration_material_social_cards: None | (
IntegrationMaterialSocialCards
] = None,
) = None,
):
"""Class hosting the plugin logic.
Expand Down Expand Up @@ -139,7 +136,7 @@ def build_url(self, base_url: str, path: str, args_dict: dict = None) -> str:
url_parts[4] = urlencode(args_dict)
return urlunparse(url_parts)

def get_value_from_dot_key(self, data: dict, dot_key: Union[str, bool]) -> Any:
def get_value_from_dot_key(self, data: dict, dot_key: str | bool) -> Any:
"""
Retrieves a value from a dictionary using a dot notation key.
Expand Down Expand Up @@ -168,8 +165,8 @@ def get_file_dates(
source_date_update: str = "git",
meta_datetime_format: str = "%Y-%m-%d %H:%M",
meta_default_timezone: str = "UTC",
meta_default_time: Optional[datetime] = None,
) -> Tuple[datetime, datetime]:
meta_default_time: datetime | None = None,
) -> tuple[datetime, datetime]:
"""Extract creation and update dates from page metadata (yaml frontmatter) or
git log for given file.
Expand Down Expand Up @@ -325,7 +322,7 @@ def get_file_dates(
get_build_datetime(),
)

def get_authors_from_meta(self, in_page: Page) -> Optional[Tuple[str]]:
def get_authors_from_meta(self, in_page: Page) -> tuple[str] | None:
"""Returns authors from page meta. It handles 'author' and 'authors' for keys, \
str and iterable as values types.
Expand Down Expand Up @@ -508,7 +505,7 @@ def get_description_or_abstract(
)
return ""

def get_image(self, in_page: Page, base_url: str) -> Optional[Tuple[str, str, int]]:
def get_image(self, in_page: Page, base_url: str) -> tuple[str, str, int] | None:
"""Get page's image from page meta or social cards and returns properties.
Args:
Expand Down Expand Up @@ -610,7 +607,7 @@ def get_remote_image_length(
http_method: str = "HEAD",
attempt: int = 0,
ssl_context: ssl.SSLContext = None,
) -> Optional[int]:
) -> int | None:
"""Retrieve length for remote images (starting with 'http' \
in meta.image or meta.illustration). \
It tries to perform a HEAD request and get the length from the headers. \
Expand Down Expand Up @@ -662,7 +659,7 @@ def get_remote_image_length(
return int(img_length)

@staticmethod
def get_site_url(mkdocs_config: Config) -> Optional[str]:
def get_site_url(mkdocs_config: Config) -> str | None:
"""Extract site URL from MkDocs configuration and enforce the behavior to ensure \
returning a str with length > 0 or None. If exists, it adds an ending slash.
Expand All @@ -688,7 +685,7 @@ def get_site_url(mkdocs_config: Config) -> Optional[str]:

return site_url

def guess_locale(self, mkdocs_config: Config) -> Optional[str]:
def guess_locale(self, mkdocs_config: Config) -> str | None:
"""Extract language code from MkDocs or Theme configuration.
:param mkdocs_config: configuration object
Expand Down
16 changes: 10 additions & 6 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,16 +699,20 @@ def test_json_feed_validation(self):
self.assertIsNone(cli_result.exception)

# created items
with Path(tmpdirname).joinpath(OUTPUT_JSON_FEED_CREATED).open(
"r", encoding="UTF-8"
) as in_json:
with (
Path(tmpdirname)
.joinpath(OUTPUT_JSON_FEED_CREATED)
.open("r", encoding="UTF-8") as in_json
):
json_feed_created_data = json.load(in_json)
jsonfeed.Feed.parse(json_feed_created_data)

# updated items
with Path(tmpdirname).joinpath(OUTPUT_JSON_FEED_UPDATED).open(
"r", encoding="UTF-8"
) as in_json:
with (
Path(tmpdirname)
.joinpath(OUTPUT_JSON_FEED_UPDATED)
.open("r", encoding="UTF-8") as in_json
):
json_feed_updated_data = json.load(in_json)
jsonfeed.Feed.parse(json_feed_updated_data)

Expand Down

0 comments on commit 8ec6164

Please sign in to comment.