Skip to content

Commit b38c6f0

Browse files
committed
Blacken code and fix flake8 issues
1 parent b529282 commit b38c6f0

File tree

5 files changed

+64
-23
lines changed

5 files changed

+64
-23
lines changed

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[flake8]
2-
ignore = E501,E722
2+
ignore = E501,E722,D104

mkdocs_git_revision_date_localized_plugin/ci.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1+
"""
2+
Helper functions related to continuous integration (CI).
3+
4+
This is because often CI runners do not have access to full git history.
5+
"""
6+
17
import os
28
import logging
39

410

511
def raise_ci_warnings(repo) -> None:
612
"""
7-
Raise warnings when users use mkdocs-git-revision-date-localized-plugin
8-
on CI build runners.
13+
Raise warnings when users use plugin on CI build runners.
914
1015
Args:
1116
repo (GitPython.git.repo): The Git repo object.
1217
"""
13-
1418
if not is_shallow_clone(repo):
1519
return None
1620

@@ -72,7 +76,7 @@ def raise_ci_warnings(repo) -> None:
7276

7377
def commit_count(repo) -> int:
7478
"""
75-
Helper function to determine the number of commits in a repository.
79+
Determine the number of commits in a repository.
7680
7781
Args:
7882
repo (GitPython.Repo.git): Repository.
@@ -89,8 +93,7 @@ def commit_count(repo) -> int:
8993

9094
def is_shallow_clone(repo) -> bool:
9195
"""
92-
Helper function to determine if repository
93-
is a shallow clone.
96+
Determine if repository is a shallow clone.
9497
9598
References & Context:
9699
https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/issues/10

mkdocs_git_revision_date_localized_plugin/plugin.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
"""
2+
MkDocs Plugin.
3+
4+
https://www.mkdocs.org/
5+
https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/
6+
"""
17
# standard lib
28
import logging
39
import re
@@ -18,12 +24,18 @@
1824

1925

2026
class GitRevisionDateLocalizedPlugin(BasePlugin):
27+
"""
28+
Mkdocs plugin to add revision date from Git.
29+
30+
See https://www.mkdocs.org/user-guide/plugins
31+
"""
32+
2133
config_scheme = (
2234
("fallback_to_build_date", config_options.Type(bool, default=False)),
2335
("locale", config_options.Type(str, default=None)),
2436
("type", config_options.Type(str, default="date")),
2537
("timezone", config_options.Type(str, default="UTC")),
26-
("excluded_page_titles", config_options.Type(list, default=[]))
38+
("excluded_page_titles", config_options.Type(list, default=[])),
2739
)
2840

2941
def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
@@ -96,8 +108,12 @@ def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
96108

97109
# Add pointers to support files for timeago.js
98110
if self.config.get("type") == "timeago":
99-
config["extra_javascript"] = ["js/timeago_mkdocs_material.js"] + config["extra_javascript"]
100-
config["extra_javascript"] = ["js/timeago.min.js"] + config["extra_javascript"]
111+
config["extra_javascript"] = ["js/timeago_mkdocs_material.js"] + config[
112+
"extra_javascript"
113+
]
114+
config["extra_javascript"] = ["js/timeago.min.js"] + config[
115+
"extra_javascript"
116+
]
101117
config["extra_css"] = ["css/timeago.css"] + config["extra_css"]
102118

103119
return config
@@ -106,7 +122,7 @@ def on_page_markdown(
106122
self, markdown: str, page: Page, config: config_options.Config, files, **kwargs
107123
) -> str:
108124
"""
109-
Replace jinja2 tags in markdown and templates with the localized dates
125+
Replace jinja2 tags in markdown and templates with the localized dates.
110126
111127
The page_markdown event is called after the page's markdown is loaded
112128
from file and can be used to alter the Markdown source text.
@@ -124,7 +140,6 @@ def on_page_markdown(
124140
Returns:
125141
str: Markdown source text of page as string
126142
"""
127-
128143
excluded_titles = self.config.get("excluded_page_titles", [])
129144
if page.title in excluded_titles:
130145
logging.debug("Excluding page " + page.url)
@@ -156,11 +171,16 @@ def on_page_markdown(
156171
def on_post_build(self, config: Dict[str, Any], **kwargs) -> None:
157172
"""
158173
Run on post build.
174+
159175
Adds the timeago assets to the build.
160176
"""
161177
# Add timeago files:
162178
if self.config.get("type") == "timeago":
163-
files = ["js/timeago.min.js", "js/timeago_mkdocs_material.js", "css/timeago.css"]
179+
files = [
180+
"js/timeago.min.js",
181+
"js/timeago_mkdocs_material.js",
182+
"css/timeago.css",
183+
]
164184
for file in files:
165185
dest_file_path = os.path.join(config["site_dir"], file)
166186
src_file_path = os.path.join(HERE, file)

mkdocs_git_revision_date_localized_plugin/util.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Utility class for mkdocs plugin."""
12
import logging
23
import os
34
import time
@@ -6,16 +7,27 @@
67
from mkdocs_git_revision_date_localized_plugin.ci import raise_ci_warnings
78

89
from babel.dates import format_date, get_timezone
9-
from git import Repo, GitCommandError, GitCommandNotFound, InvalidGitRepositoryError, NoSuchPathError
10+
from git import (
11+
Repo,
12+
GitCommandError,
13+
GitCommandNotFound,
14+
InvalidGitRepositoryError,
15+
NoSuchPathError,
16+
)
1017

1118
from typing import Any, Dict
1219

1320
logger = logging.getLogger("mkdocs.plugins")
1421

1522

1623
class Util:
17-
def __init__(self, config={}):
24+
"""Utility class.
25+
26+
This helps find git and calculate relevant dates.
27+
"""
1828

29+
def __init__(self, config={}):
30+
"""Initialize utility class."""
1931
self.fallback_enabled = False
2032
self.config = config
2133
self.repo_cache = {}
@@ -37,7 +49,7 @@ def _date_formats(
3749
unix_timestamp: float, locale: str = "en", time_zone: str = "UTC"
3850
) -> Dict[str, Any]:
3951
"""
40-
Returns different date formats / types.
52+
Calculate different date formats / types.
4153
4254
Args:
4355
unix_timestamp (float): A timestamp in seconds since 1970.
@@ -54,10 +66,12 @@ def _date_formats(
5466

5567
return {
5668
"date": format_date(loc_revision_date, format="long", locale=locale),
57-
"datetime": " ".join([
58-
format_date(loc_revision_date, format="long", locale=locale),
59-
loc_revision_date.strftime("%H:%M:%S"),
60-
]),
69+
"datetime": " ".join(
70+
[
71+
format_date(loc_revision_date, format="long", locale=locale),
72+
loc_revision_date.strftime("%H:%M:%S"),
73+
]
74+
),
6175
"iso_date": loc_revision_date.strftime("%Y-%m-%d"),
6276
"iso_datetime": loc_revision_date.strftime("%Y-%m-%d %H:%M:%S"),
6377
"timeago": '<span class="timeago" datetime="%s" locale="%s"></span>'
@@ -72,7 +86,7 @@ def get_revision_date_for_file(
7286
fallback_to_build_date: bool = False,
7387
) -> Dict[str, str]:
7488
"""
75-
Determine localized date variants for a given file
89+
Determine localized date variants for a given file.
7690
7791
Args:
7892
path (str): Location of a markdown file that is part of a Git repository.
@@ -82,7 +96,6 @@ def get_revision_date_for_file(
8296
Returns:
8397
dict: Localized date variants.
8498
"""
85-
8699
unix_timestamp = None
87100

88101
# perform git log operation
@@ -91,7 +104,9 @@ def get_revision_date_for_file(
91104
# Retrieve author date in UNIX format (%at)
92105
# https://git-scm.com/docs/git-log#Documentation/git-log.txt-ematem
93106
realpath = os.path.realpath(path)
94-
unix_timestamp = self._get_repo(realpath).log(realpath, n=1, date="short", format="%at")
107+
unix_timestamp = self._get_repo(realpath).log(
108+
realpath, n=1, date="short", format="%at"
109+
)
95110
except (InvalidGitRepositoryError, NoSuchPathError) as err:
96111
if fallback_to_build_date:
97112
logger.warning(
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# page
2+
3+
in subfolder

0 commit comments

Comments
 (0)