aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/skaldpress/main.py3
-rw-r--r--src/skaldpress/metadata_parser.py5
-rw-r--r--src/smp/builtins.py17
3 files changed, 15 insertions, 10 deletions
diff --git a/src/skaldpress/main.py b/src/skaldpress/main.py
index b58f4ad..cc6dc94 100644
--- a/src/skaldpress/main.py
+++ b/src/skaldpress/main.py
@@ -91,7 +91,8 @@ def compile_file(smps: smp.macro_processor.MacroProcessorState, file_path, opts)
macro_processor = smps.macro_processor()
macro_processor.define_macro_string(
- "METADATA_filename", os.path.splitext(os.path.relpath(file_path, opts.content_dir))[0]
+ "METADATA_filename",
+ os.path.splitext(os.path.relpath(file_path, opts.content_dir))[0],
)
macro_processor.source_file_path = file_path
diff --git a/src/skaldpress/metadata_parser.py b/src/skaldpress/metadata_parser.py
index 18ae56b..9ee4c8d 100644
--- a/src/skaldpress/metadata_parser.py
+++ b/src/skaldpress/metadata_parser.py
@@ -16,16 +16,18 @@ def dict_soft_extend(a: dict[Any, Any], b: dict[Any, Any]):
def get_all_meta_iter(
- file: Path, template_dir: str, meta: dict[str, Any]
+ file: Path | None, template_dir: str, meta: dict[str, Any]
) -> tuple[dict[str, Any], str, datetime]:
"""
Will extract all metadata,
weird things will happen if the first file doesn't exist
"""
+ assert file is not None, "file cannot be None"
meta = deepcopy(meta)
fs_modified = datetime.min
+ extension = None
while file is not None:
try:
extension = os.path.splitext(file)[1]
@@ -46,6 +48,7 @@ def get_all_meta_iter(
else:
file = None
+ assert extension is not None, "Extension is None, this should not be possible"
return meta, extension, fs_modified
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: