Skip to content

Commit

Permalink
Switch to ruff's formatter and import sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin committed Nov 4, 2023
1 parent 3c54048 commit 3b36c72
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 27 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ jobs:
- name: Check formatting
if: always()
run: |
hatch run style:format
git diff --color --exit-code
- name: Check types
if: always()
Expand Down
2 changes: 1 addition & 1 deletion mkdocs_redirects/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.2.1'
__version__ = "1.2.1"
24 changes: 12 additions & 12 deletions mkdocs_redirects/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from mkdocs.plugins import BasePlugin
from mkdocs.structure.files import File

log = logging.getLogger('mkdocs.plugin.redirects')
log = logging.getLogger("mkdocs.plugin.redirects")


HTML_TEMPLATE = """
Expand Down Expand Up @@ -47,7 +47,7 @@ def write_html(site_dir, old_path, new_path):
# Write the HTML redirect file in place of the old file
log.debug("Creating redirect: '%s' -> '%s'", old_path, new_path)
content = HTML_TEMPLATE.format(url=new_path)
with open(old_path_abs, 'w', encoding='utf-8') as f:
with open(old_path_abs, "w", encoding="utf-8") as f:
f.write(content)


Expand All @@ -58,26 +58,26 @@ def get_relative_html_path(old_page, new_page, use_directory_urls):

relative_path = posixpath.relpath(new_path, start=posixpath.dirname(old_path))
if use_directory_urls:
relative_path = relative_path + '/'
relative_path = relative_path + "/"

return relative_path + new_hash_fragment


def get_html_path(path, use_directory_urls):
"""Return the HTML file path for a given markdown file"""
f = File(path, '', '', use_directory_urls)
return f.dest_path.replace(os.sep, '/')
f = File(path, "", "", use_directory_urls)
return f.dest_path.replace(os.sep, "/")


class RedirectPlugin(BasePlugin):
# Any options that this plugin supplies should go here.
config_scheme = (
('redirect_maps', config_options.Type(dict, default={})), # note the trailing comma
("redirect_maps", config_options.Type(dict, default={})), # note the trailing comma
)

# Build a list of redirects on file generation
def on_files(self, files, config, **kwargs):
self.redirects = self.config.get('redirect_maps', {})
self.redirects = self.config.get("redirect_maps", {})

# Validate user-provided redirect "old files"
for page_old in self.redirects.keys():
Expand All @@ -87,20 +87,20 @@ def on_files(self, files, config, **kwargs):
# Build a dict of known document pages to validate against later
self.doc_pages = {}
for page in files.documentation_pages(): # object type: mkdocs.structure.files.File
self.doc_pages[page.src_path.replace(os.sep, '/')] = page
self.doc_pages[page.src_path.replace(os.sep, "/")] = page

# Create HTML files for redirects after site dir has been built
def on_post_build(self, config, **kwargs):
# Determine if 'use_directory_urls' is set
use_directory_urls = config.get('use_directory_urls')
use_directory_urls = config.get("use_directory_urls")

# Walk through the redirect map and write their HTML files
for page_old, page_new in self.redirects.items():
# Need to remove hash fragment from new page to verify existence
page_new_without_hash, hash = _split_hash_fragment(str(page_new))

# External redirect targets are easy, just use it as the target path
if page_new.lower().startswith(('http://', 'https://')):
if page_new.lower().startswith(("http://", "https://")):
dest_path = page_new

elif page_new_without_hash in self.doc_pages:
Expand All @@ -115,7 +115,7 @@ def on_post_build(self, config, **kwargs):

# DO IT!
write_html(
config['site_dir'],
config["site_dir"],
get_html_path(page_old, use_directory_urls),
dest_path,
)
Expand All @@ -128,5 +128,5 @@ def _split_hash_fragment(path):
"path/to/file#hash" => ("/path/to/file", "#hash")
"path/to/file" => ("/path/to/file", "")
"""
path, hash, after = path.partition('#')
path, hash, after = path.partition("#")
return path, hash + after
17 changes: 4 additions & 13 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,29 +80,20 @@ check = [
skip-install = true
dependencies = [
"ruff",
"isort",
"black",
]
[tool.hatch.envs.style.scripts]
fix = [
"ruff check --fix mkdocs_redirects tests",
"format",
]
format = [
"isort -q mkdocs_redirects tests",
"black -q mkdocs_redirects tests",
"ruff format -q mkdocs_redirects tests",
]

[tool.black]
line-length = 100
skip-string-normalization = true

[tool.isort]
profile = "black"
line_length = 100

[tool.ruff]
line-length = 100
select = [
"I",
"F", "W", "E", "UP", "YTT", "C4", "DTZ", "FA", "ISC", "PIE", "T20", "RSE", "TCH",
"B002", "B003", "B005", "B007", "B009", "B012", "B013", "B014", "B015", "B018", "B020", "B021", "B023", "B026", "B033", "B034", "B905",
"COM818",
Expand All @@ -119,7 +110,7 @@ allow-dict-calls-with-keyword-arguments = true

[tool.mypy]
warn_unreachable = true
show_error_codes = true
allow_redefinition = true

[tool.pytest.ini_options]
addopts = "--tb=native"
Expand Down

0 comments on commit 3b36c72

Please sign in to comment.