Skip to content

Commit

Permalink
Refactor to use new static type checking features (#180)
Browse files Browse the repository at this point in the history
* Use new static type checking features

* Use class-based config and attributes from MkDocs 1.4
* Use dataclass from Python 3.8
* Refactor: put runtime attributes onto the plugin rather than onto the config - also statically typed

Extra: actually depend on MkDocs, it is a dependency
  • Loading branch information
oprypin committed Oct 14, 2023
1 parent 6108b10 commit 065c890
Show file tree
Hide file tree
Showing 22 changed files with 242 additions and 248 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@ Includes the content of a file or a group of files.
[license-image]: https://img.shields.io/pypi/l/mkdocs-include-markdown-plugin?color=light-green&logo=apache&logoColor=white
[license-link]: https://github.com/mondeja/mkdocs-include-markdown-plugin/blob/master/LICENSE
[platformdirs]: https://pypi.org/project/platformdirs/
[cibuildwheel-470]: https://github.com/joerick/cibuildwheel/issues/470
[cibuildwheel-repo-link]: https://github.com/joerick/cibuildwheel
[cibuildwheel-470]: https://github.com/pypa/cibuildwheel/issues/470
[cibuildwheel-repo-link]: https://github.com/pypa/cibuildwheel
[es-readme-link]: https://github.com/mondeja/mkdocs-include-markdown-plugin/blob/master/locale/es/README.md
[fr-readme-link]: https://github.com/mondeja/mkdocs-include-markdown-plugin/blob/master/locale/fr/README.md
[Bash wildcard globs]: https://facelessuser.github.io/wcmatch/glob/#syntax
4 changes: 2 additions & 2 deletions locale/es/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ separar este plugin de la documentación de
[license-image]: https://img.shields.io/pypi/l/mkdocs-include-markdown-plugin?color=light-green&logo=apache&logoColor=white
[license-link]: https://github.com/mondeja/mkdocs-include-markdown-plugin/blob/master/LICENSE
[platformdirs]: https://pypi.org/project/platformdirs/
[cibuildwheel-470]: https://github.com/joerick/cibuildwheel/issues/470
[cibuildwheel-repo-link]: https://github.com/joerick/cibuildwheel
[cibuildwheel-470]: https://github.com/pypa/cibuildwheel/issues/470
[cibuildwheel-repo-link]: https://github.com/pypa/cibuildwheel
[es-readme-link]: https://github.com/mondeja/mkdocs-include-markdown-plugin/blob/master/locale/es/README.md
[fr-readme-link]: https://github.com/mondeja/mkdocs-include-markdown-plugin/blob/master/locale/fr/README.md
[Bash wildcard globs]: https://facelessuser.github.io/wcmatch/glob/#syntax
9 changes: 4 additions & 5 deletions locale/es/README.md.po
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,11 @@ msgstr ""
"[tests-link]: https://github.com/mondeja/mkdocs-include-markdown-"
"plugin/actions?query=workflow%3ACI"

msgid "[cibuildwheel-470]: https://github.com/joerick/cibuildwheel/issues/470"
msgstr ""
"[cibuildwheel-470]: https://github.com/joerick/cibuildwheel/issues/470"
msgid "[cibuildwheel-470]: https://github.com/pypa/cibuildwheel/issues/470"
msgstr "[cibuildwheel-470]: https://github.com/pypa/cibuildwheel/issues/470"

msgid "[cibuildwheel-repo-link]: https://github.com/joerick/cibuildwheel"
msgstr "[cibuildwheel-repo-link]: https://github.com/joerick/cibuildwheel"
msgid "[cibuildwheel-repo-link]: https://github.com/pypa/cibuildwheel"
msgstr "[cibuildwheel-repo-link]: https://github.com/pypa/cibuildwheel"

msgid "Read this document in other languages:"
msgstr "Lee este documento en otros idiomas:"
Expand Down
4 changes: 2 additions & 2 deletions locale/fr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ autorisations][cibuildwheel-470] pour séparer ce plugin de la documentation de
[license-image]: https://img.shields.io/pypi/l/mkdocs-include-markdown-plugin?color=light-green&logo=apache&logoColor=white
[license-link]: https://github.com/mondeja/mkdocs-include-markdown-plugin/blob/master/LICENSE
[platformdirs]: https://pypi.org/project/platformdirs/
[cibuildwheel-470]: https://github.com/joerick/cibuildwheel/issues/470
[cibuildwheel-repo-link]: https://github.com/joerick/cibuildwheel
[cibuildwheel-470]: https://github.com/pypa/cibuildwheel/issues/470
[cibuildwheel-repo-link]: https://github.com/pypa/cibuildwheel
[es-readme-link]: https://github.com/mondeja/mkdocs-include-markdown-plugin/blob/master/locale/es/README.md
[fr-readme-link]: https://github.com/mondeja/mkdocs-include-markdown-plugin/blob/master/locale/fr/README.md
[Bash wildcard globs]: https://facelessuser.github.io/wcmatch/glob/#syntax
9 changes: 4 additions & 5 deletions locale/fr/README.md.po
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,11 @@ msgstr ""
"[tests-link]: https://github.com/mondeja/mkdocs-include-markdown-"
"plugin/actions?query=workflow%3ACI"

msgid "[cibuildwheel-470]: https://github.com/joerick/cibuildwheel/issues/470"
msgstr ""
"[cibuildwheel-470]: https://github.com/joerick/cibuildwheel/issues/470"
msgid "[cibuildwheel-470]: https://github.com/pypa/cibuildwheel/issues/470"
msgstr "[cibuildwheel-470]: https://github.com/pypa/cibuildwheel/issues/470"

msgid "[cibuildwheel-repo-link]: https://github.com/joerick/cibuildwheel"
msgstr "[cibuildwheel-repo-link]: https://github.com/joerick/cibuildwheel"
msgid "[cibuildwheel-repo-link]: https://github.com/pypa/cibuildwheel"
msgstr "[cibuildwheel-repo-link]: https://github.com/pypa/cibuildwheel"

msgid ""
"[es-readme-link]: https://github.com/mondeja/mkdocs-include-markdown-"
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ classifiers = [
"Programming Language :: Python :: 3.12"
]
keywords = ["markdown", "mkdocs", "includer", "plugin"]
dependencies = ["wcmatch>=8,<9"]
dependencies = [
"mkdocs>=1.4",
"wcmatch>=8,<9"
]

[[project.authors]]
name = "Joe Rickerby"
Expand Down
85 changes: 15 additions & 70 deletions src/mkdocs_include_markdown_plugin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,25 @@

from __future__ import annotations

from mkdocs.config.base import Config
from mkdocs.config.config_options import (
ListOfItems,
Optional,
Type as MkType,
)


CONFIG_DEFAULTS = {
'opening-tag': '{%',
'closing-tag': '%}',
'encoding': 'utf-8',
'preserve-includer-indent': True,
'dedent': False,
'trailing-newlines': True,
'comments': True,
'rewrite-relative-urls': True,
'heading-offset': 0,
'start': None,
'end': None,
'exclude': None,
'cache': 0,
}

CONFIG_SCHEME = (
(
'opening_tag',
MkType(str, default=CONFIG_DEFAULTS['opening-tag']),
),
(
'closing_tag',
MkType(str, default=CONFIG_DEFAULTS['closing-tag']),
),
(
'encoding',
MkType(str, default=CONFIG_DEFAULTS['encoding']),
),
(
'preserve_includer_indent',
MkType(bool, default=CONFIG_DEFAULTS['preserve-includer-indent']),
),
(
'dedent',
MkType(bool, default=CONFIG_DEFAULTS['dedent']),
),
(
'trailing_newlines',
MkType(bool, default=CONFIG_DEFAULTS['trailing-newlines']),
),
(
'comments',
MkType(bool, default=CONFIG_DEFAULTS['comments']),
),
(
'rewrite_relative_urls',
MkType(bool, default=CONFIG_DEFAULTS['rewrite-relative-urls']),
),
(
'heading_offset',
MkType(int, default=CONFIG_DEFAULTS['heading-offset']),
),
(
'start',
MkType(str, default=CONFIG_DEFAULTS['start']),
),
(
'end',
MkType(str, default=CONFIG_DEFAULTS['end']),
),
(
'exclude',
Optional(ListOfItems(MkType(str))),
),
(
'cache',
MkType(int, default=CONFIG_DEFAULTS['cache']),
),
)
class PluginConfig(Config): # noqa: D101
opening_tag = MkType(str, default='{%')
closing_tag = MkType(str, default='%}')
encoding = MkType(str, default='utf-8')
preserve_includer_indent = MkType(bool, default=True)
dedent = MkType(bool, default=False)
trailing_newlines = MkType(bool, default=True)
comments = MkType(bool, default=True)
rewrite_relative_urls = MkType(bool, default=True)
heading_offset = MkType(int, default=0)
start = Optional(MkType(str))
end = Optional(MkType(str))
exclude = ListOfItems(MkType(str), default=[])
cache = MkType(int, default=0)
39 changes: 20 additions & 19 deletions src/mkdocs_include_markdown_plugin/directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@
import os
import re
import string
from dataclasses import dataclass
from typing import TYPE_CHECKING

from wcmatch import glob

from mkdocs_include_markdown_plugin import process


@dataclass
class DirectiveBoolArgument: # noqa: D101
value: bool
regex: re.Pattern[str]


if TYPE_CHECKING:
from typing import TypedDict

class DirectiveBoolArgument(TypedDict): # noqa: D101
value: bool
regex: re.Pattern[str]

DirectiveBoolArgumentsDict = dict[str, DirectiveBoolArgument]

DefaultValues = TypedDict(
Expand All @@ -30,8 +33,8 @@ class DirectiveBoolArgument(TypedDict): # noqa: D101
'comments': bool,
'rewrite-relative-urls': bool,
'heading-offset': int,
'start': str,
'end': str,
'start': str | None,
'end': str | None,
},
)

Expand Down Expand Up @@ -102,9 +105,9 @@ def parse_filename_argument(
match: re.Match[str],
) -> tuple[str | None, str | None]:
"""Return filename argument matched by ``match``."""
raw_filename = match.group('double_quoted_filename')
raw_filename = match['double_quoted_filename']
if raw_filename is None:
raw_filename = match.group('single_quoted_filename')
raw_filename = match['single_quoted_filename']
if raw_filename is None:
filename = None
else:
Expand All @@ -116,9 +119,9 @@ def parse_filename_argument(

def parse_string_argument(match: re.Match[str]) -> str | None:
"""Return the string argument matched by ``match``."""
value = match.group(1)
value = match[1]
if value is None:
value = match.group(3)
value = match[3]
if value is not None:
value = value.replace("\\'", "'")
else:
Expand Down Expand Up @@ -152,20 +155,18 @@ def parse_bool_options(

bool_options: dict[str, DirectiveBoolArgument] = {}
for option_name in option_names:
bool_options[option_name] = {
'value': defaults[option_name], # type: ignore
'regex': ARGUMENT_REGEXES[option_name],
}
bool_options[option_name] = DirectiveBoolArgument(
value=defaults[option_name], # type: ignore
regex=ARGUMENT_REGEXES[option_name],
)

for arg_name, arg in bool_options.items():
bool_arg_match = arg['regex'].search(arguments_string)
bool_arg_match = arg.regex.search(arguments_string)
if bool_arg_match is None:
continue
try:
bool_options[arg_name]['value'] = TRUE_FALSE_STR_BOOL[
bool_arg_match.group(
1,
) or TRUE_FALSE_BOOL_STR[arg['value']]
bool_options[arg_name].value = TRUE_FALSE_STR_BOOL[
bool_arg_match[1] or TRUE_FALSE_BOOL_STR[arg.value]
]
except KeyError:
invalid_args.append(arg_name)
Expand Down
Loading

0 comments on commit 065c890

Please sign in to comment.