diff options
Diffstat (limited to 'src/smp/builtins.py')
-rw-r--r-- | src/smp/builtins.py | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/src/smp/builtins.py b/src/smp/builtins.py index 0997165..afa153d 100644 --- a/src/smp/builtins.py +++ b/src/smp/builtins.py @@ -79,7 +79,7 @@ def smp_builtin_add_metadata(macro_processor, metadata: dict[str, Any], overwrit ): macro_name = f"{macro_processor._get_macro_with_prefix('metadata_prefix')}{macro_name}" - macro_value = str(value) + macro_value: str | list[str] = str(value) if isinstance(value, list): macro_value = [str(el) for el in value] @@ -222,12 +222,13 @@ def smp_builtin_read(macro_processor, filename, template_content=None): return content -global LINK_CACHE -LINK_CACHE: dict[str, tuple[bool, int, str]] = dict() - - def smp_builtin_wodl(macro_processor, link, timeout_seconds=5): + if (macro_processor._get_macro_with_prefix("wodl_cache")) is None: + macro_processor._define_macro_with_prefix("wodl_cache", {}) + cache = macro_processor._get_macro_with_prefix("wodl_cache") + url = urllib.parse.urlparse(link) + link = ( url.scheme + "://" @@ -235,13 +236,13 @@ def smp_builtin_wodl(macro_processor, link, timeout_seconds=5): + urllib.parse.quote(url.path) ) - if link in LINK_CACHE: - return LINK_CACHE[link] + if link in cache: + return cache[link] try: r = urllib.request.urlopen(link, timeout=timeout_seconds) working_link = (r.status == 200) and (r.reason == "OK") - LINK_CACHE[link] = (working_link, r.status, r.reason) + cache[link] = (working_link, r.status, r.reason) if not working_link: macro_processor.log_warning(f"Dead link {link} ({r.status} {r.reason})!") except urllib.error.URLError as e: |