From 2b5f9e9b43188426a61234d7cff3026dd5913355 Mon Sep 17 00:00:00 2001 From: Amy Gale Ruth Bowersox Date: Mon, 19 Aug 2024 21:55:42 -0600 Subject: [PATCH] worked out some bugs in extension loading --- doc/configuration.toml | 6 +++--- src/dragonglass/config.py | 17 +++++++---------- src/dragonglass/extensions/breadcrumbs.py | 10 ++++++++-- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/doc/configuration.toml b/doc/configuration.toml index 87cec92..f46d4ee 100644 --- a/doc/configuration.toml +++ b/doc/configuration.toml @@ -65,8 +65,8 @@ sitetitle = "" [extensions] # Full classnames of the extensions to be loaded. -load = ["dragonglass.extensions.breadcrumbs.BreadcrumbExtension"] +load = ["dragonglass.extensions.breadcrumbs.Breadcrumbs"] -[extensions.BreadcrumbExtension] -# Configuration data for BreadcrumbExtension. +[extensions.Breadcrumbs] +# Configuration data for Breadcrumbs. var = "value" diff --git a/src/dragonglass/config.py b/src/dragonglass/config.py index e3c6ec2..285f6b5 100644 --- a/src/dragonglass/config.py +++ b/src/dragonglass/config.py @@ -49,8 +49,6 @@ CALLOUT_ICONS = { 'todo': 'circle-check', 'warning': 'triangle-alert' } -"""Cache of extension module names.""" -EXTENSION_PKGS = {} def load_extension_class(name: str, sect: dict[str, Any]) -> Extension: @@ -65,15 +63,14 @@ def load_extension_class(name: str, sect: dict[str, Any]) -> Extension: Extension: Instance of the extension class. """ components = name.split('.') - classname = components.pop() - modname = ".".join(components) - if modname not in EXTENSION_PKGS: - EXTENSION_PKGS[modname] = __import__(modname) - extclass = getattr(EXTENSION_PKGS[modname], classname) - cfg_sect = sect.get(classname, {}) - rc = extclass(cfg_sect) + modname = ".".join(components[:-1]) + b = __import__(modname) + for c in components[1:]: + b = getattr(b, c) + cfg_sect = sect.get(components[-1], {}) + rc = b(cfg_sect) if not isinstance(rc, Extension): - raise RuntimeError(f"class {name} is not a valid Dragonglass extension") + raise RuntimeError(f"class {name} is not a valid dragonglass extension") return rc diff --git a/src/dragonglass/extensions/breadcrumbs.py b/src/dragonglass/extensions/breadcrumbs.py index 8a8a603..aaa87f5 100644 --- a/src/dragonglass/extensions/breadcrumbs.py +++ b/src/dragonglass/extensions/breadcrumbs.py @@ -9,11 +9,17 @@ from ..tree import SourceNode, SourceIndex """The extension for generating breadcrumb informatioon.""" -class BreadcrumbExtension(Extension): +class Breadcrumbs(Extension): LINK = re.compile(r'^\[\[(.+)(?:\|.+)?\]\]') def __init__(self, config: dict[str, Any]): - super(BreadcrumbExtension, self).__init__(config) + """ + Initialize the Breadcrumbs extension. + + Args: + config (dict[str, Any]): Configuration variables for this extension. + """ + super(Breadcrumbs, self).__init__(config) self._root_nodes: list[SourceNode] = [] self._raw_targets: list[tuple[SourceNode, str]] = [] self._parents: dict[SourceNode, SourceNode] = {}