aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorQrius <[email protected]>2025-03-20 09:17:55 +0100
committerQrius <[email protected]>2025-03-20 09:17:59 +0100
commit9a3ce865e64d496cb835ece3e5a84a80361480ab (patch)
tree686ec33939bedbbbf9ca8f31c91ff16646874534 /src
parent44fecdfe41596acba584cdc9a93b67e92a3f1f0d (diff)
downloadskaldpress-9a3ce865e64d496cb835ece3e5a84a80361480ab.tar.gz
skaldpress-9a3ce865e64d496cb835ece3e5a84a80361480ab.zip
Clean up things
Diffstat (limited to 'src')
-rw-r--r--src/skaldpress/file_metadata_extract.py50
-rw-r--r--src/skaldpress/filelist.py20
-rw-r--r--src/skaldpress/main.py46
-rw-r--r--src/skaldpress/metadata_parser.py52
-rw-r--r--src/skaldpress/smp_macros.py0
5 files changed, 71 insertions, 97 deletions
diff --git a/src/skaldpress/file_metadata_extract.py b/src/skaldpress/file_metadata_extract.py
deleted file mode 100644
index b2da095..0000000
--- a/src/skaldpress/file_metadata_extract.py
+++ /dev/null
@@ -1,50 +0,0 @@
-import os.path
-from pathlib import Path
-from typing import Any
-from copy import deepcopy
-from datetime import datetime
-from skaldpress.metadata_parser import extract_parse_yaml_metadata
-
-
-def get_template_path(template: str, template_dir: str):
- return Path(f"{template_dir}{template}")
-
-
-def get_all_meta(
- file_path: Path, template_dir: str, meta: dict[str, Any]
-) -> tuple[dict[str, Any], str, datetime]:
- filename, extension = os.path.splitext(file_path)
-
- fs_metadata = file_path.stat()
- fs_modified = datetime.fromtimestamp(fs_metadata.st_mtime)
-
- try:
- with open(file_path, "r") as f:
- file_content = f.read()
- except Exception:
- file_content = ""
-
- map_with_meta = extract_parse_yaml_metadata(file_content)[0]
-
- map_base = deepcopy(meta)
- map_base.update(map_with_meta)
-
- template = map_base.get("template")
- if not template:
- return map_base, extension, fs_modified
-
- template_file = get_template_path(str(template), template_dir)
-
- try:
- map_templated, extension, template_fs_modified = get_all_meta(
- template_file, template_dir, {}
- )
- except Exception as e:
- # raise Exception(f"MetadataError: {e}")
- return map_base, extension, fs_modified
-
- map_templated.update(map_base)
- # Shuld really add a cutsom extend function to the hashmap,
- # so lists can be merged and such
-
- return map_templated, extension, max(fs_modified, template_fs_modified)
diff --git a/src/skaldpress/filelist.py b/src/skaldpress/filelist.py
index b4fc843..285efb0 100644
--- a/src/skaldpress/filelist.py
+++ b/src/skaldpress/filelist.py
@@ -4,8 +4,8 @@ from dataclasses import dataclass
from datetime import datetime
from pathlib import Path
from enum import Enum
-from typing import Any, Generator, Iterable
-from skaldpress.file_metadata_extract import get_all_meta
+from typing import Any, Generator
+from skaldpress.metadata_parser import get_all_meta
class FileListFileTargetAction(Enum):
@@ -130,16 +130,16 @@ def make_filelist(
template_dir: str,
additional_metadata: dict[str, Any],
) -> Generator[FileListFile, None, None]:
- filelist_gen = (
+ filelist_gen1 = (
(x, x.path.replace(base_dir, "", 1)) for x in walk_filetree(directory)
)
- filelist_gen = (
- x for x in filelist_gen if not file_filtered(x[1], include, exclude)
+ filelist_gen2 = (
+ x for x in filelist_gen1 if not file_filtered(x[1], include, exclude)
)
- filelist_gen = enrich_with_metadata(
- filelist_gen, template_dir, additional_metadata, read_metadata
+ filelist_gen3 = enrich_with_metadata(
+ filelist_gen2, template_dir, additional_metadata, read_metadata
)
- filelist_gen = (
+ filelist_gen4 = (
FileListFile(
file_path=x[0],
file_rel=x[1],
@@ -148,7 +148,7 @@ def make_filelist(
target_action=target_action,
metadata=x[2],
)
- for x in filelist_gen
+ for x in filelist_gen3
)
- return filelist_gen
+ return filelist_gen4
diff --git a/src/skaldpress/main.py b/src/skaldpress/main.py
index 5dbb196..57da9e5 100644
--- a/src/skaldpress/main.py
+++ b/src/skaldpress/main.py
@@ -1,6 +1,5 @@
import os
import shutil
-from pathlib import Path
from argparse import ArgumentParser, ArgumentTypeError
from dataclasses import dataclass
from functools import partial
@@ -9,7 +8,12 @@ from collections import deque
import smp.macro_processor
from copy import deepcopy
from skaldpress.metadata_parser import extract_parse_yaml_metadata
-from skaldpress.filelist import make_filelist, FileList, FileListFileTargetAction
+from skaldpress.filelist import (
+ make_filelist,
+ FileList,
+ FileListFileTargetAction,
+ file_filtered,
+)
from time import perf_counter
@@ -77,10 +81,6 @@ class SkaldpressError(Exception):
self.path = path
-def get_template_path(template: str, opts):
- return f"{opts.template_dir}{template}"
-
-
def cached_file_id_by_path(source_path: str) -> int | None:
global COMPILED_FILES
for i in range(len(COMPILED_FILES)):
@@ -95,28 +95,6 @@ def print_warnings(macro_processor):
print(f" \u001b[33m{warning}\u001b[0m")
-def file_pat_match(file: str, pat: str) -> bool:
- if file == pat:
- return True
- if pat.startswith("*") and file.endswith(pat.removeprefix("*")):
- return True
- if pat.startswith("*") and file.endswith(pat.removeprefix("*")):
- return True
- return False
-
-
-def file_filtered(file: str, filters: list[str], exclude: list[str]) -> bool:
- for filter in exclude:
- if file_pat_match(file, filter):
- return True
- if len(filters) == 0:
- return False
- for filter in filters:
- if file_pat_match(file, filter):
- return False
- return True
-
-
def macro_processor_initialize(metadata, old_macro_processor, additional_state=None):
macro_processor = old_macro_processor
macro_processor.define_macro("all_tagged_by", sp_all_tagged_by)
@@ -135,6 +113,10 @@ def macro_processor_initialize(metadata, old_macro_processor, additional_state=N
for key, value in additional_state.items():
macro_processor.define_macro(key, value)
+ global COMPILED_FILES, COMPILED_FILES_BY_TAG
+ macro_processor.py_global_env["compiled_files"] = COMPILED_FILES
+ macro_processor.py_global_env["compiled_files_by_tag"] = COMPILED_FILES_BY_TAG
+
def extract_requested_macro_processor_state(macro_processor):
requested_keys = macro_processor.macros.get("METADATA_keep_states")
@@ -195,7 +177,7 @@ def wrap_template(macro_processor, template_file, file_content, opts):
template_parent = str(template_parent)
print(f" Wrapping in template {template_parent}")
return wrap_template(
- macro_processor, get_template_path(template_parent, opts), content, opts
+ macro_processor, f"{opts.template_dir}{template_parent}", content, opts
)
@@ -254,7 +236,7 @@ def compile_file(file_path, opts):
needs_recompilation=needs_recompilation(macro_processor),
)
- template_file = get_template_path(map["template"], opts)
+ template_file = f"{opts.template_dir}{map['template']}"
content, template_extension = wrap_template(
macro_processor, template_file, file_content, opts
)
@@ -393,7 +375,9 @@ def main():
)
parser.add_argument("-f", "--filter", metavar="filter", default=[], type=comma_arg)
parser.add_argument("-e", "--exclude", metavar="filter", default=[], type=comma_arg)
- parser.add_argument("-m", "--metadata", nargs="+", metavar="key=value", default=[], action="extend")
+ parser.add_argument(
+ "-m", "--metadata", nargs="+", metavar="key=value", default=[], action="extend"
+ )
parser.add_argument(
"-c", "--compilefilter", metavar="filter", default=[], type=comma_arg
)
diff --git a/src/skaldpress/metadata_parser.py b/src/skaldpress/metadata_parser.py
index 28cab31..85e6a65 100644
--- a/src/skaldpress/metadata_parser.py
+++ b/src/skaldpress/metadata_parser.py
@@ -1,5 +1,48 @@
-import datetime
+import os.path
+from pathlib import Path
from typing import Any
+from copy import deepcopy
+from datetime import datetime
+
+
+def get_all_meta(
+ file_path: Path, template_dir: str, meta: dict[str, Any]
+) -> tuple[dict[str, Any], str, datetime]:
+ extension = os.path.splitext(file_path)[1]
+
+ fs_metadata = file_path.stat()
+ fs_modified = datetime.fromtimestamp(fs_metadata.st_mtime)
+
+ try:
+ with open(file_path, "r") as f:
+ file_content = f.read()
+ except Exception:
+ file_content = ""
+
+ map_with_meta = extract_parse_yaml_metadata(file_content)[0]
+
+ map_base = deepcopy(meta)
+ map_base.update(map_with_meta)
+
+ template = map_base.get("template")
+ if not template:
+ return map_base, extension, fs_modified
+
+ template_file = Path(f"{template_dir}{str(template)}")
+
+ try:
+ map_templated, extension, template_fs_modified = get_all_meta(
+ template_file, template_dir, {}
+ )
+ except Exception:
+ # raise Exception(f"MetadataError: {e}")
+ return map_base, extension, fs_modified
+
+ map_templated.update(map_base)
+ # Shuld really add a cutsom extend function to the hashmap,
+ # so lists can be merged and such
+
+ return map_templated, extension, max(fs_modified, template_fs_modified)
def str_to_yaml_value(in_str: str) -> Any:
@@ -11,7 +54,7 @@ def str_to_yaml_value(in_str: str) -> Any:
pass
try:
- return datetime.datetime.strptime(in_str, "%Y-%m-%dT%H:%M:%S%z")
+ return datetime.strptime(in_str, "%Y-%m-%dT%H:%M:%S%z")
except:
pass
@@ -20,10 +63,7 @@ def str_to_yaml_value(in_str: str) -> Any:
def extract_parse_yaml_metadata(file_content, newline="\n") -> tuple[dict, str]:
file_lines = file_content.split(newline)
- if len(file_lines) < 1:
- return {}, file_content
-
- if next(iter(file_lines)).strip() != "---":
+ if len(file_lines) < 1 or next(iter(file_lines)).strip() != "---":
return {}, file_content
yaml_map: dict[str, Any] = {}
diff --git a/src/skaldpress/smp_macros.py b/src/skaldpress/smp_macros.py
deleted file mode 100644
index e69de29..0000000
--- a/src/skaldpress/smp_macros.py
+++ /dev/null