summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2021-08-21 22:12:45 +0200
committerDaniel Schadt <kingdread@gmx.de>2021-08-21 22:12:45 +0200
commit662fcdc06012efacad53125ee4a449b632fad187 (patch)
tree2f0223e4d4cf1d22c4d397f069ab7a6a92249b89
parent01f52aa2481c314511f43560757c5bd63ca05ac2 (diff)
downloadwikimini-662fcdc06012efacad53125ee4a449b632fad187.tar.gz
wikimini-662fcdc06012efacad53125ee4a449b632fad187.tar.bz2
wikimini-662fcdc06012efacad53125ee4a449b632fad187.zip
Allow passing a custom template registry
-rw-r--r--wikimini/__init__.py23
-rw-r--r--wikimini/templates/__init__.py14
2 files changed, 32 insertions, 5 deletions
diff --git a/wikimini/__init__.py b/wikimini/__init__.py
index 876c7a7..00179cc 100644
--- a/wikimini/__init__.py
+++ b/wikimini/__init__.py
@@ -25,13 +25,26 @@ class Wikimini:
"""The main object for Wikipedia/Gemini access and conversion.
Attributes:
- api_url (str): The base URL of the API.
- table_format (str): The name of the table style, see
- :const:`TABLE_FORMAT`.
+ api_url: The base URL of the API.
+ table_format: The name of the table style, see :const:`TABLE_FORMAT`.
+ template_registry: The template registry to use. If :any:`None`, the
+ global registry will be used.
"""
- def __init__(self, api_url=API_URL, table_format=TABLE_FORMAT):
+ api_url: str
+ table_format: str
+ template_registry: "templates.Registry"
+
+ def __init__(
+ self,
+ api_url=API_URL,
+ table_format=TABLE_FORMAT,
+ template_registry=None,
+ ):
self.api_url = api_url
self.table_format = table_format
+ if template_registry is None:
+ template_registry = templates.registry
+ self.template_registry = template_registry
def page_url(self, title: str) -> str:
"""Returns the link for page with the given title.
@@ -180,7 +193,7 @@ class Wikimini:
# however, some of them are useful and provide some output that we
# should mimic (for example, the convert template).
name = str(obj.name)
- template = templates.registry.get(name)
+ template = self.template_registry.get(name)
if template is None:
return default(obj)
else:
diff --git a/wikimini/templates/__init__.py b/wikimini/templates/__init__.py
index 360b3fa..ebe3bbc 100644
--- a/wikimini/templates/__init__.py
+++ b/wikimini/templates/__init__.py
@@ -6,6 +6,7 @@ A template is a function that takes the :class:`~wikimini.Wikimini` instance
and the :class:`~mwparserfromhell.nodes.template.Template` node to convert, and
returns a string with the template output (see :const:`Template`).
"""
+import copy
from typing import Callable, Optional
import mwparserfromhell as mwp
@@ -49,6 +50,19 @@ class Registry:
"""
self.templates[name] = template
+ def copy(self) -> "Registry":
+ """Returns a copy of the registry.
+
+ This is useful if you want to use the global registry as a starting
+ point to add your own templates, without modifying the global instance.
+
+ Returns:
+ A copy of the registry.
+ """
+ copied = Registry()
+ copied.templates = copy.copy(self.templates)
+ return copied
+
#: The global template registry.
registry = Registry()