summaryrefslogtreecommitdiff
path: root/searx/plugins/hostnames.py
diff options
context:
space:
mode:
authorBnyro <bnyro@tutanota.com>2024-05-15 16:30:49 +0200
committerMarkus Heiser <markus.heiser@darmarIT.de>2024-06-07 14:42:52 +0200
commitf5eb56b63f250c7804e5e1cf4426e550bc933906 (patch)
treec0267c726de3be65676d48d7e97dce052a789259 /searx/plugins/hostnames.py
parent845a0b678d0c94a68d0ec333aeaa7f7e6b7a18e8 (diff)
downloadsearxng-f5eb56b63f250c7804e5e1cf4426e550bc933906.tar.gz
searxng-f5eb56b63f250c7804e5e1cf4426e550bc933906.zip
[refactor] hostnames plugin: add fallback for old hostname_replace plugin
Co-authored-by: Markus Heiser <markus.heiser@darmarit.de>
Diffstat (limited to 'searx/plugins/hostnames.py')
-rw-r--r--searx/plugins/hostnames.py35
1 files changed, 33 insertions, 2 deletions
diff --git a/searx/plugins/hostnames.py b/searx/plugins/hostnames.py
index 0fafcbb6d..3c699c4fd 100644
--- a/searx/plugins/hostnames.py
+++ b/searx/plugins/hostnames.py
@@ -3,6 +3,16 @@
"""In addition to rewriting/replace reslut URLs, the *hoostnames* plugin offers
other features.
+.. attention::
+
+ The 'Hostnames plugin' from `PR-3463
+ <https://github.com/searxng/searxng/pull/3463>`_ is a rewrite of the
+ 'Hostname replace' plugin. Backwards compatibility is guaranteed for a
+ transitional period, but this will end soon.
+
+ **To maintainers of SearXNG instances, please modify your old plugin config
+ to the new.**
+
- ``hostnames.replace``: A mapping of regular expressions to hostnames to be
replaced by other hostnames.
@@ -86,8 +96,29 @@ def _load_regular_expressions(settings_key):
return {}
-replacements = _load_regular_expressions('replace')
-removables = _load_regular_expressions('remove')
+# compatibility fallback for old hostname replace plugin
+# TODO: remove in the future once most/all instance maintainers finished migrating # pylint: disable=fixme
+def _load_regular_expressions_with_fallback(settings_key):
+ expressions = _load_regular_expressions(settings_key)
+ if expressions:
+ return expressions
+
+ # fallback to the old `hostname_replace` settings format
+ # pylint: disable=import-outside-toplevel, cyclic-import
+ hostname_replace_config = settings.get('hostname_replace', {})
+ if hostname_replace_config:
+ from searx.plugins.hostname_replace import deprecated_msg
+
+ deprecated_msg()
+
+ if settings_key == 'replace':
+ return {re.compile(p): r for (p, r) in hostname_replace_config.items() if r}
+
+ return {re.compile(p) for (p, r) in hostname_replace_config.items() if not r}
+
+
+replacements = _load_regular_expressions_with_fallback('replace')
+removables = _load_regular_expressions_with_fallback('remove')
high_priority = _load_regular_expressions('high_priority')
low_priority = _load_regular_expressions('low_priority')