Skip to content

Commit de5f9f0

Browse files
authored
use more pathlib
1 parent 07f0cb7 commit de5f9f0

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

src/mkdocs_git_revision_date_localized_plugin/plugin.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import os
1111
import time
1212
import multiprocessing
13+
from pathlib import Path
1314

1415
from mkdocs import __version__ as mkdocs_version
1516
from mkdocs.config import config_options
@@ -28,7 +29,7 @@
2829

2930
from packaging.version import Version
3031

31-
HERE = os.path.dirname(os.path.abspath(__file__))
32+
HERE = Path(__file__).parent.absolute()
3233

3334

3435
class GitRevisionDateLocalizedPlugin(BasePlugin):
@@ -147,12 +148,13 @@ def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
147148
def parallel_compute_commit_timestamps(self, files, original_source: Optional[Dict] = None, is_first_commit=False):
148149
pool = multiprocessing.Pool(processes=min(10, multiprocessing.cpu_count()))
149150
results = []
150-
for file in files:
151-
if file.is_documentation_page():
152-
abs_src_path = file.abs_src_path
151+
for f in files:
152+
if f.is_documentation_page():
153+
abs_src_path = f.abs_src_path
153154
# Support plugins like monorep that might have moved the files from the original source that is under git
154155
if original_source and abs_src_path in original_source:
155156
abs_src_path = original_source[abs_src_path]
157+
abs_src_path = Path(abs_src_path).absolute()
156158
result = pool.apply_async(self.util.get_git_commit_timestamp, args=(abs_src_path, is_first_commit))
157159
results.append((abs_src_path, result))
158160
pool.close()
@@ -374,8 +376,8 @@ def on_post_build(self, config: Dict[str, Any], **kwargs) -> None:
374376
"js/timeago_mkdocs_material.js",
375377
"css/timeago.css",
376378
]
377-
for file in files:
378-
dest_file_path = os.path.join(config["site_dir"], file)
379-
src_file_path = os.path.join(HERE, file)
380-
assert os.path.exists(src_file_path)
381-
copy_file(src_file_path, dest_file_path)
379+
for f in files:
380+
dest_file_path = Path(config["site_dir"]) / f
381+
src_file_path = HERE / f
382+
assert src_file_path.exists()
383+
copy_file(str(src_file_path), str(dest_file_path))

src/mkdocs_git_revision_date_localized_plugin/util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
import os
55
import time
6+
from pathlib import Path
67

78
from mkdocs_git_revision_date_localized_plugin.ci import raise_ci_warnings
89
from mkdocs_git_revision_date_localized_plugin.dates import get_date_formats
@@ -34,7 +35,7 @@ def __init__(self, config: Dict, mkdocs_dir: str):
3435
self.repo_cache = {}
3536

3637
ignore_commits_file = self.config.get("ignored_commits_file")
37-
ignore_commits_filepath = os.path.join(mkdocs_dir, ignore_commits_file) if ignore_commits_file else None
38+
ignore_commits_filepath = Path(mkdocs_dir) / ignore_commits_file if ignore_commits_file else None
3839
self.ignored_commits = self.parse_git_ignore_revs(ignore_commits_filepath) if ignore_commits_file else []
3940

4041
def _get_repo(self, path: str) -> Git:

tests/test_builds.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ def setup_commit_history(testproject_path):
144144
Returns:
145145
repo (repo): git.Repo object
146146
"""
147-
assert not os.path.exists(str(testproject_path / ".git"))
148-
testproject_path = str(testproject_path)
147+
assert not (testproject_path / ".git").exists()
149148

150149
repo = git.Repo.init(testproject_path, bare=False)
151150
repo.git.checkout("-b", "master")
@@ -159,7 +158,7 @@ def setup_commit_history(testproject_path):
159158
message="add homepage", author=author, date="1500854705"
160159
) # Mon Jul 24 2017 00:05:05 GMT+0000
161160

162-
file_name = os.path.join(testproject_path, "docs/page_with_tag.md")
161+
file_name = testproject_path / "docs/page_with_tag.md"
163162
with open(file_name, "a") as the_file:
164163
the_file.write("test\n")
165164
repo.git.add("docs/page_with_tag.md")
@@ -175,10 +174,10 @@ def setup_commit_history(testproject_path):
175174
) # Sun Jan 23 2022 04:10:26 GMT+0000
176175

177176
if os.path.exists("docs/page_with_renamed.md"):
178-
bf_file_name = os.path.join(testproject_path, "docs/page_with_renamed.md")
179-
af_file_name = os.path.join(testproject_path, "docs/subfolder/page_with_renamed.md")
177+
bf_file_name = testproject_path / "docs/page_with_renamed.md"
178+
af_file_name = testproject_path / "docs/subfolder/page_with_renamed.md"
180179
# Since git.mv would actually remove the file, move page_with_renamed.md back to docs if it has been moved
181-
if os.path.exists(af_file_name):
180+
if af_file_name.exists():
182181
os.replace(af_file_name, bf_file_name)
183182
repo.git.add("docs/page_with_renamed.md")
184183
repo.git.commit(
@@ -192,7 +191,7 @@ def setup_commit_history(testproject_path):
192191
if os.path.exists("docs/first_page.md"):
193192
repo.git.add("docs/first_page.md")
194193
repo.git.commit(message="first page", author=author, date="1500854705") # Mon Jul 24 2017 00:05:05 GMT+0000
195-
file_name = os.path.join(testproject_path, "docs/first_page.md")
194+
file_name = testproject_path / "docs/first_page.md"
196195
with open(file_name, "w+") as the_file:
197196
the_file.write("Hello\n")
198197
repo.git.add("docs/first_page.md")
@@ -211,7 +210,7 @@ def setup_commit_history(testproject_path):
211210
message="add mkdocs", author=author, date="1500854705 -0700"
212211
) # Mon Jul 24 2017 00:05:05 GMT+0000
213212

214-
if os.path.exists("docs/second_page.md"):
213+
if Path("docs/second_page.md").exists():
215214
repo.git.add("docs/second_page.md")
216215
repo.git.commit(
217216
message="second page", author=author, date="1643911026"

0 commit comments

Comments
 (0)