Skip to content

Commit

Permalink
Fixed all ruff errors
Browse files Browse the repository at this point in the history
  • Loading branch information
MartenBE committed Nov 6, 2024
1 parent 64018d4 commit 646d505
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- run: pip install poetry
- run: poetry install --with dev
- run: poetry run ruff format --check
# - run: poetry run ruff check # TODO: Enable this line
- run: poetry run ruff check
# - run: poetry run mypy src/ tests/ # TODO: Enable this line
- run: poetry run pytest
- run: poetry run mkslides build docs/index.md
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ jobs:
- run: pip install poetry
- run: poetry install --with dev
- run: poetry run ruff format --check
# - run: poetry run ruff check # TODO: Enable this line
- run: poetry run ruff check
# - run: poetry run mypy src/ tests/ # TODO: Enable this line
- run: poetry run pytest
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ ignore = [
"D102", # Missing docstring in public method
"D104", # Missing docstring in public package"
"D107", # Missing docstring in `__init__`
"D203", # one-blank-line-before-class
"D212", # multi-line-summary-first-line
"E501", # Line too long
"FA102", # Missing `from __future__ import annotations`, but uses PEP 585 collection
"FBT001", # Boolean-typed positional argument in function definition
"G004", # Logging statement uses f-string"
"PLR0913", # Too many arguments in function definition
"RET504", # Unnecessary assignment to `...` before `return` statement
Expand Down
61 changes: 38 additions & 23 deletions src/mkslides/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def parse_ip_port(
return ip, port


def generate(config_file, input_path: Path, output_directory: Path) -> None:
def generate(config_file: str, input_path: Path, output_directory: Path) -> None:
config = read_config(config_file)
markup_generator = MarkupGenerator(config, output_directory)
markup_generator.create_output_directory()
Expand All @@ -95,8 +95,9 @@ def generate(config_file, input_path: Path, output_directory: Path) -> None:
metavar="PATH",
default=DEFAULT_OUTPUT_DIR,
)
def build(files, config_file, site_dir) -> None:
"""Build the MkDocs documentation.
def build(files: str, config_file: str, site_dir: str) -> None:
"""
Build the MkDocs documentation.
FILENAME|PATH is the path to the Markdown file, or the directory containing Markdown files.
"""
Expand Down Expand Up @@ -145,17 +146,18 @@ def build(files, config_file, site_dir) -> None:
is_flag=True,
)
@click.option("-f", "--config-file", **config_file_argument_data)
def serve(
files,
dev_addr,
open_in_browser,
watch_index_theme,
watch_index_template,
watch_slides_theme,
watch_slides_template,
config_file,
def serve( # noqa: C901
files: str,
dev_addr: str,
open_in_browser: bool,
watch_index_theme: bool,
watch_index_template: bool,
watch_slides_theme: bool,
watch_slides_template: bool,
config_file: str,
) -> None:
"""Run the builtin development server.
"""
Run the builtin development server.
FILENAME|PATH is the path to the Markdown file, or the directory containing Markdown files.
"""
Expand All @@ -176,29 +178,42 @@ def reload() -> None:

try:
server = livereload.Server()
server._setup_logging = (

# https://github.com/lepture/python-livereload/issues/232
server._setup_logging = ( # noqa: SLF001
lambda: None
) # https://github.com/lepture/python-livereload/issues/232
)

watched_paths = [
files,
config_file, # TODO: reload config
config_file,
]

if watch_index_theme:
watched_paths.append(config.get_index_theme())
index_theme = config.get_index_theme()
if index_theme is not None:
watched_paths.append(index_theme)

if watch_index_template:
watched_paths.append(config.get_index_template())
index_template = config.get_index_template()
if index_template is not None:
watched_paths.append(index_template)

if watch_slides_theme:
watched_paths.append(config.get_slides_theme())
slides_theme = config.get_slides_theme()
if slides_theme is not None:
watched_paths.append(slides_theme)

if watch_slides_template:
watched_paths.append(config.get_slides_template())
slides_template = config.get_slides_template()
if slides_template is not None:
watched_paths.append(slides_template)

for path in watched_paths:
if path:
path = Path(path).resolve(strict=True)
logger.info(f'Watching: "{path.absolute()}"')
server.watch(filepath=path.absolute().as_posix(), func=reload, delay=1)
resolved_path = Path(path).resolve(strict=True).absolute()
logger.info(f'Watching: "{resolved_path}"')
server.watch(filepath=resolved_path.as_posix(), func=reload, delay=1)

ip, port = parse_ip_port(dev_addr)

Expand Down
69 changes: 45 additions & 24 deletions src/mkslides/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from pathlib import Path
from typing import Any

import yaml

Expand All @@ -9,14 +10,6 @@


class Config:
schema = {
"type": "object",
"properties": {
"price": {"type": "number"},
"name": {"type": "string"},
},
}

def __init__(self) -> None:
with DEFAULT_CONFIG_RESOURCE.open() as f:
self.__config = yaml.safe_load(f)
Expand All @@ -25,46 +18,74 @@ def __init__(self) -> None:
logger.info(f"Default config: {self.__config}")

def get_index_title(self) -> str | None:
return self.__get("index", "title")
value = self.__get("index", "title")
assert isinstance(value, str) or value is None
return value

def get_index_favicon(self) -> str | None:
return self.__get("index", "favicon")
value = self.__get("index", "favicon")
assert isinstance(value, str) or value is None
return value

def get_index_theme(self) -> str | None:
return self.__get("index", "theme")
value = self.__get("index", "theme")
assert isinstance(value, str) or value is None
return value

def get_index_template(self) -> str | None:
return self.__get("index", "template")
value = self.__get("index", "template")
assert isinstance(value, str) or value is None
return value

def get_slides_favicon(self) -> str | None:
return self.__get("slides", "favicon")
value = self.__get("slides", "favicon")
assert isinstance(value, str) or value is None
return value

def get_slides_theme(self) -> str | None:
return self.__get("slides", "theme")
value = self.__get("slides", "theme")
assert isinstance(value, str) or value is None
return value

def get_slides_highlight_theme(self) -> str | None:
return self.__get("slides", "highlight_theme")
value = self.__get("slides", "highlight_theme")
assert isinstance(value, str) or value is None
return value

def get_slides_template(self) -> str | None:
return self.__get("slides", "template")
value = self.__get("slides", "template")
assert isinstance(value, str) or value is None
return value

def get_slides_separator(self) -> str | None:
return self.__get("slides", "separator")
value = self.__get("slides", "separator")
assert isinstance(value, str) or value is None
return value

def get_slides_separator_vertical(self) -> str | None:
return self.__get("slides", "separator_vertical")
value = self.__get("slides", "separator_vertical")
assert isinstance(value, str) or value is None
return value

def get_slides_separator_notes(self) -> str | None:
return self.__get("slides", "separator_notes")
value = self.__get("slides", "separator_notes")
assert isinstance(value, str) or value is None
return value

def get_slides_charset(self) -> str | None:
return self.__get("slides", "charset")
value = self.__get("slides", "charset")
assert isinstance(value, str) or value is None
return value

def get_revealjs_options(self) -> dict | None:
return self.__get("revealjs")
value = self.__get("revealjs")
assert isinstance(value, dict) or value is None
return value

def get_plugins(self) -> list | None:
return self.__get("plugins")
value = self.__get("plugins")
assert isinstance(value, list) or value is None
return value

def merge_config_from_file(self, config_path: Path) -> None:
with config_path.open(encoding="utf-8-sig") as f:
Expand All @@ -81,7 +102,7 @@ def merge_config_from_dict(self, new_config: dict) -> None:
logger.info("Config merged from dict")
logger.info(f"Config: {self.__config}")

def __get(self, *keys) -> str | dict | list | None:
def __get(self, *keys: str) -> str | dict | list | None:
current_value = self.__config
for key in keys:
if isinstance(current_value, dict) and key in current_value:
Expand All @@ -90,7 +111,7 @@ def __get(self, *keys) -> str | dict | list | None:
return None
return current_value

def __recursive_merge(self, current, new) -> dict:
def __recursive_merge(self, current: Any, new: dict) -> dict:
if new:
for key, value in new.items():
if isinstance(value, dict):
Expand Down
2 changes: 1 addition & 1 deletion src/mkslides/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
DEFAULT_SLIDESHOW_TEMPLATE = DEFAULT_JINJA2_ENVIRONMENT.get_template(
"slideshow.html.jinja",
)
LOCAL_JINJA2_ENVIRONMENT = Environment(loader=FileSystemLoader("."))
LOCAL_JINJA2_ENVIRONMENT = Environment(loader=FileSystemLoader("."), autoescape=True)

VERSION = metadata.version("mkslides")

Expand Down
33 changes: 17 additions & 16 deletions src/mkslides/markupgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import logging
import shutil
import time
from emoji import emojize
from importlib import resources
from importlib.resources.abc import Traversable
from pathlib import Path
from typing import Any
from urllib.parse import urlparse

import frontmatter
from emoji import emojize
from natsort import natsorted

from .config import Config
Expand Down Expand Up @@ -164,15 +164,16 @@ def __process_markdown_file(
slideshow_template = DEFAULT_SLIDESHOW_TEMPLATE

# https://revealjs.com/markdown/#external-markdown
markdown_data_options = {}
for key, value in {
"data-separator": self.config.get_slides_separator(),
"data-separator-vertical": self.config.get_slides_separator_vertical(),
"data-separator-notes": self.config.get_slides_separator_notes(),
"data-charset": self.config.get_slides_charset(),
}.items():
if value:
markdown_data_options[key] = value
markdown_data_options = {
key: value
for key, value in {
"data-separator": self.config.get_slides_separator(),
"data-separator-vertical": self.config.get_slides_separator_vertical(),
"data-separator-notes": self.config.get_slides_separator_notes(),
"data-charset": self.config.get_slides_charset(),
}.items()
if value
}

markup = slideshow_template.render(
favicon=relative_favicon_path,
Expand Down Expand Up @@ -241,7 +242,7 @@ def __process_markdown_directory(self, md_root_path: Path) -> None:
title=self.config.get_index_title(),
theme=relative_theme_path,
slideshows=slideshows,
build_datetime=datetime.datetime.now(),
build_datetime=datetime.datetime.now(tz=datetime.timezone.utc),
)
self.__create_file(index_path, content)

Expand Down Expand Up @@ -281,8 +282,8 @@ def __copy_theme(
assert default_theme_resource is not None
with resources.as_file(
default_theme_resource.joinpath(theme),
) as theme_path:
theme_path = theme_path.with_suffix(".css").resolve(strict=True)
) as builtin_theme_path:
theme_path = builtin_theme_path.with_suffix(".css").resolve(strict=True)
logger.info(
f'Using built-in theme "{theme}" from "{theme_path.absolute()}"',
)
Expand Down Expand Up @@ -355,13 +356,13 @@ def __copy_to_output_relative_to_md_root(

return relative_path

def __copy(self, source_path, destination_path) -> None:
def __copy(self, source_path: Path, destination_path: Path) -> None:
if source_path.is_dir():
self.__copy_tree(source_path, destination_path)
else:
self.__copy_file(source_path, destination_path)

def __copy_tree(self, source_path, destination_path) -> None:
def __copy_tree(self, source_path: Path, destination_path: Path) -> None:
overwrite = destination_path.exists()
destination_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copytree(source_path, destination_path, dirs_exist_ok=True)
Expand All @@ -371,7 +372,7 @@ def __copy_tree(self, source_path, destination_path) -> None:
f'{action} directory "{source_path.absolute()}" to "{destination_path.absolute()}"',
)

def __copy_file(self, source_path, destination_path) -> None:
def __copy_file(self, source_path: Path, destination_path: Path) -> None:
overwrite = destination_path.exists()
destination_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(source_path, destination_path)
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


@pytest.fixture
def setup_markup_generator() -> None:
def setup_markup_generator() -> tuple[MarkupGenerator, Path]:
config = Config()
output_path = Path("tests/site")
markup_generator = MarkupGenerator(config, output_path)
Expand Down

0 comments on commit 646d505

Please sign in to comment.