From ece47e5261f3a042299186f1146ebd511b8a85fe Mon Sep 17 00:00:00 2001 From: Qrius Date: Fri, 11 Apr 2025 18:57:49 +0200 Subject: Make mypy/pyflake happy, remove global vars in favor of cache in a macro --- src/smp/builtins.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'src/smp') 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: -- cgit v1.2.3