From fc0508c3b9c509a7494e392dff6a0588c792625f Mon Sep 17 00:00:00 2001 From: GeoJulien Date: Sun, 9 Jun 2024 21:04:22 +0200 Subject: [PATCH] feature: allow multiple instances --- mkdocs_rss_plugin/plugin.py | 3 ++ tests/base.py | 25 ++++++++--- tests/dev/dev_load_config.py | 20 +++++++++ .../docs/blog/posts/sample_blog_post.md | 3 +- tests/fixtures/mkdocs_multiple_instances.yml | 16 +++++++ tests/test_build.py | 45 +++++++++++++++++++ 6 files changed, 104 insertions(+), 8 deletions(-) create mode 100644 tests/dev/dev_load_config.py create mode 100644 tests/fixtures/mkdocs_multiple_instances.yml diff --git a/mkdocs_rss_plugin/plugin.py b/mkdocs_rss_plugin/plugin.py index 715ba62d..a0f43566 100644 --- a/mkdocs_rss_plugin/plugin.py +++ b/mkdocs_rss_plugin/plugin.py @@ -51,6 +51,9 @@ class GitRssPlugin(BasePlugin[RssPluginConfig]): """Main class for MkDocs plugin.""" + # allow to set the plugin multiple times in the same mkdocs config + supports_multiple_instances = True + def __init__(self): """Instanciation.""" # dates source diff --git a/tests/base.py b/tests/base.py index 1c133099..a5b51515 100644 --- a/tests/base.py +++ b/tests/base.py @@ -20,6 +20,9 @@ from mkdocs.config import load_config from mkdocs.config.base import Config +# package +from mkdocs_rss_plugin.plugin import GitRssPlugin + # ############################################################################# # ########## Classes ############### # ################################## @@ -45,19 +48,27 @@ def get_plugin_config_from_mkdocs( # instanciate plugin cfg_mkdocs = load_config(str(mkdocs_yml_filepath.resolve())) - plugins = cfg_mkdocs.get("plugins") - if "rss" not in plugins: + plugins = cfg_mkdocs.plugins + rss_plugin_instances = [ + plg for plg in plugins.items() if isinstance(plg[1], GitRssPlugin) + ] + if not len(rss_plugin_instances): logging.warning( f"Plugin {plugin_name} is not part of enabled plugin in the MkDocs " "configuration file: {mkdocs_yml_filepath}" ) - return {} - plugin_loaded = plugins.get("rss") + return cfg_mkdocs + + if len(rss_plugin_instances) == 1: + plugin = rss_plugin_instances[0][1] + self.assertIsInstance(plugin, GitRssPlugin) + elif len(rss_plugin_instances) >= 1: + plugin = rss_plugin_instances[1][1] + self.assertIsInstance(plugin, GitRssPlugin) - cfg = plugin_loaded.on_config(cfg_mkdocs) - logging.info("Fixture configuration loaded: " + str(cfg)) + logging.info(f"Fixture configuration loaded: {plugin.on_config(cfg_mkdocs)}") - return plugin_loaded.config + return plugin.config def build_docs_setup( self, diff --git a/tests/dev/dev_load_config.py b/tests/dev/dev_load_config.py new file mode 100644 index 00000000..3f132fc5 --- /dev/null +++ b/tests/dev/dev_load_config.py @@ -0,0 +1,20 @@ +from mkdocs.config import load_config + +from mkdocs_rss_plugin.plugin import GitRssPlugin + +mkdocs_cfg = load_config(config_file="tests/fixtures/mkdocs_multiple_instances.yml") + +print(mkdocs_cfg.plugins.keys()) +rss_instances = [ + plg for plg in mkdocs_cfg.plugins.items() if isinstance(plg[1], GitRssPlugin) +] +print(len(rss_instances)) + +for plg in mkdocs_cfg.plugins.items(): + print(plg) + print(isinstance(plg[1], GitRssPlugin)) + print(type(plg)) + +rss_instance_1 = plg[1] +print(dir(rss_instance_1)) +print(rss_instance_1.on_config(mkdocs_cfg)) diff --git a/tests/fixtures/docs/blog/posts/sample_blog_post.md b/tests/fixtures/docs/blog/posts/sample_blog_post.md index cf51740b..c8c42bce 100644 --- a/tests/fixtures/docs/blog/posts/sample_blog_post.md +++ b/tests/fixtures/docs/blog/posts/sample_blog_post.md @@ -1,6 +1,7 @@ --- date: 2023-02-12 -authors: [guts] +authors: + - guts categories: - Blog --- diff --git a/tests/fixtures/mkdocs_multiple_instances.yml b/tests/fixtures/mkdocs_multiple_instances.yml new file mode 100644 index 00000000..ad6e1fa0 --- /dev/null +++ b/tests/fixtures/mkdocs_multiple_instances.yml @@ -0,0 +1,16 @@ +site_name: Test Mkdocs with multiple RSS plugin instances +site_description: Multiple RSS plugin in a single mkdocs +site_url: https://guts.github.io/mkdocs-rss-plugin + +plugins: + - rss + - rss: + feeds_filenames: + json_created: blog.json + json_updated: blog-updated.json + rss_created: blog.xml + rss_updated: blog-updated.xml + match_path: "blog/.*" + +theme: + name: material diff --git a/tests/test_build.py b/tests/test_build.py index 75467e8d..a888d018 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -564,6 +564,51 @@ def test_simple_build_custom_output_basename(self): ) self.assertEqual(feed_parsed.bozo, 0) + def test_simple_build_multiple_instances(self): + config = self.get_plugin_config_from_mkdocs( + mkdocs_yml_filepath=Path("tests/fixtures/mkdocs_multiple_instances.yml"), + plugin_name="rss", + ) + + with tempfile.TemporaryDirectory() as tmpdirname: + cli_result = self.build_docs_setup( + testproject_path="docs", + mkdocs_yml_filepath=Path( + "tests/fixtures/mkdocs_multiple_instances.yml" + ), + output_path=tmpdirname, + strict=True, + ) + + if cli_result.exception is not None: + e = cli_result.exception + logger.debug(format_exception(type(e), e, e.__traceback__)) + + self.assertEqual(cli_result.exit_code, 0) + self.assertIsNone(cli_result.exception) + + # created items + feed_parsed = feedparser.parse( + Path(tmpdirname) / config.feeds_filenames.rss_created + ) + self.assertEqual(feed_parsed.bozo, 0) + + # updated items + feed_parsed = feedparser.parse( + Path(tmpdirname) / config.feeds_filenames.rss_updated + ) + self.assertEqual(feed_parsed.bozo, 0) + + # created items - blog + feed_parsed = feedparser.parse(Path(tmpdirname).joinpath("blog.xml")) + self.assertEqual(feed_parsed.bozo, 0) + + # updated items - blog + feed_parsed = feedparser.parse( + Path(tmpdirname).joinpath("blog-updated.xml") + ) + self.assertEqual(feed_parsed.bozo, 0) + def test_simple_build_pretty_print_enabled(self): with tempfile.TemporaryDirectory() as tmpdirname: cli_result = self.build_docs_setup(