From f16bd1f06277c578d2b328fa8cea7bda852c4962 Mon Sep 17 00:00:00 2001 From: Julien Date: Tue, 5 Dec 2023 22:08:49 +0100 Subject: [PATCH] Road to Mkdocs >= 1.4: plugin's configuration (#195) See: https://www.mkdocs.org/about/release-notes/#upgrades-for-plugin-developers_1 --- mkdocs_rss_plugin/config.py | 33 +++++++++++++++++++++++++++++++ mkdocs_rss_plugin/plugin.py | 39 ++++++++++++------------------------- requirements/base.txt | 2 +- tests/test_config.py | 11 +++++++++-- 4 files changed, 55 insertions(+), 30 deletions(-) create mode 100644 mkdocs_rss_plugin/config.py diff --git a/mkdocs_rss_plugin/config.py b/mkdocs_rss_plugin/config.py new file mode 100644 index 00000000..f4b57f1e --- /dev/null +++ b/mkdocs_rss_plugin/config.py @@ -0,0 +1,33 @@ +#! python3 # noqa: E265 + +# ############################################################################ +# ########## Libraries ############# +# ################################## + + +# 3rd party +from mkdocs.config import config_options +from mkdocs.config.base import Config + + +# ############################################################################ +# ########## Classes ############### +# ################################## +class RssPluginConfig(Config): + """Configuration for RSS plugin for Mkdocs.""" + + abstract_chars_count = config_options.Type(int, default=160) + abstract_delimiter = config_options.Type(str, default="") + categories = config_options.Optional( + config_options.ListOfItems(config_options.Type(str)) + ) + comments_path = config_options.Optional(config_options.Type(str)) + date_from_meta = config_options.Optional(config_options.Type(dict)) + enabled = config_options.Type(bool, default=True) + feed_ttl = config_options.Type(int, default=1440) + image = config_options.Optional(config_options.Type(str)) + length = config_options.Type(int, default=20) + match_path = config_options.Type(str, default=".*") + pretty_print = config_options.Type(bool, default=False) + url_parameters = config_options.Optional(config_options.Type(dict)) + use_git = config_options.Type(bool, default=True) diff --git a/mkdocs_rss_plugin/plugin.py b/mkdocs_rss_plugin/plugin.py index d78c0314..3a121e1b 100644 --- a/mkdocs_rss_plugin/plugin.py +++ b/mkdocs_rss_plugin/plugin.py @@ -21,9 +21,10 @@ from mkdocs.utils import get_build_timestamp # package modules -from .__about__ import __title__, __uri__, __version__ -from .customtypes import PageInformation -from .util import Util +from mkdocs_rss_plugin.__about__ import __title__, __uri__, __version__ +from mkdocs_rss_plugin.config import RssPluginConfig +from mkdocs_rss_plugin.customtypes import PageInformation +from mkdocs_rss_plugin.util import Util # ############################################################################ # ########## Globals ############# @@ -42,25 +43,9 @@ # ################################## -class GitRssPlugin(BasePlugin): +class GitRssPlugin(BasePlugin[RssPluginConfig]): """Main class for MkDocs plugin.""" - config_scheme = ( - ("abstract_chars_count", config_options.Type(int, default=160)), - ("abstract_delimiter", config_options.Type(str, default="")), - ("categories", config_options.Type(list, default=None)), - ("comments_path", config_options.Type(str, default=None)), - ("date_from_meta", config_options.Type(dict, default=None)), - ("enabled", config_options.Type(bool, default=True)), - ("feed_ttl", config_options.Type(int, default=1440)), - ("image", config_options.Type(str, default=None)), - ("length", config_options.Type(int, default=20)), - ("match_path", config_options.Type(str, default=".*")), - ("pretty_print", config_options.Type(bool, default=False)), - ("url_parameters", config_options.Type(dict, default=None)), - ("use_git", config_options.Type(bool, default=True)), - ) - def __init__(self): """Instanciation.""" # dates source @@ -104,23 +89,23 @@ def on_config(self, config: config_options.Config) -> dict: # start a feed dictionary using global config vars base_feed = { - "author": config.get("site_author", None), + "author": config.site_author or None, "buildDate": formatdate(get_build_timestamp()), - "copyright": config.get("copyright", None), - "description": config.get("site_description", None), + "copyright": config.copyright, + "description": config.site_description, "entries": [], "generator": f"{__title__} - v{__version__}", "html_url": self.util.get_site_url(config), "language": self.util.guess_locale(config), "pubDate": formatdate(get_build_timestamp()), - "repo_url": config.get("repo_url", config.get("site_url", None)), + "repo_url": config.repo_url, "title": config.get("site_name", None), - "ttl": self.config.get("feed_ttl", None), + "ttl": self.config.feed_ttl, } # feed image - if self.config.get("image"): - base_feed["logo_url"] = self.config.get("image") + if self.config.image: + base_feed["logo_url"] = self.config.image # pattern to match pages included in output self.match_path_pattern = compile(self.config.get("match_path")) diff --git a/requirements/base.txt b/requirements/base.txt index 74bc079b..4bdc69fb 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -3,6 +3,6 @@ GitPython>=3.1,<3.2 -mkdocs>=1.1,<2 +mkdocs>=1.4,<2 pytz==2022.* ; python_version < "3.9" tzdata==2023.* ; python_version >= "3.9" and sys_platform == "win32" diff --git a/tests/test_config.py b/tests/test_config.py index 56b9874f..52ed3597 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -17,8 +17,11 @@ import unittest from pathlib import Path +# 3rd party from mkdocs.config.base import Config +from mkdocs_rss_plugin.config import RssPluginConfig + # plugin target from mkdocs_rss_plugin.plugin import GitRssPlugin @@ -65,8 +68,8 @@ def test_plugin_config_defaults(self): "feed_ttl": 1440, "image": None, "length": 20, - "pretty_print": False, "match_path": ".*", + "pretty_print": False, "url_parameters": None, "use_git": True, } @@ -74,6 +77,8 @@ def test_plugin_config_defaults(self): # load plugin = GitRssPlugin() errors, warnings = plugin.load_config({}) + self.assertIsInstance(plugin.config, RssPluginConfig) + self.assertIsInstance(plugin.config, Config) self.assertEqual(plugin.config, expected) self.assertEqual(errors, []) self.assertEqual(warnings, []) @@ -90,8 +95,8 @@ def test_plugin_config_image(self): "feed_ttl": 1440, "image": self.feed_image, "length": 20, - "pretty_print": False, "match_path": ".*", + "pretty_print": False, "url_parameters": None, "use_git": True, } @@ -102,6 +107,8 @@ def test_plugin_config_image(self): # load plugin = GitRssPlugin() errors, warnings = plugin.load_config(custom_cfg) + self.assertIsInstance(plugin.config, RssPluginConfig) + self.assertIsInstance(plugin.config, Config) self.assertEqual(plugin.config, expected) self.assertEqual(errors, []) self.assertEqual(warnings, [])