diff options
| author | Dennis Fink | 2025-07-14 16:34:38 +0200 |
|---|---|---|
| committer | Dennis Fink | 2025-07-14 16:34:38 +0200 |
| commit | 840ad324f1e6056be66faba3bdad636053332f70 (patch) | |
| tree | c3dbf2e4cf284846bbdbecdaded666d12216fd0c | |
| parent | 83e24b1e572c9755c1be69e520ea081fadac136c (diff) | |
| download | patternutils-840ad324f1e6056be66faba3bdad636053332f70.tar.gz patternutils-840ad324f1e6056be66faba3bdad636053332f70.zip | |
Make jina2 a required dependency
Diffstat (limited to '')
| -rw-r--r-- | patternutils/template.py | 101 | ||||
| -rw-r--r-- | pyproject.toml | 4 |
2 files changed, 50 insertions, 55 deletions
diff --git a/patternutils/template.py b/patternutils/template.py index ce09768..39cc3f3 100644 --- a/patternutils/template.py +++ b/patternutils/template.py @@ -1,56 +1,58 @@ import os.path +import shlex +from datetime import datetime from typing import Callable, cast +import jinja2 + from .config import CONFIG_PATH from .types import TemplateCommands, TemplateExtensions -try: - import jinja2 -except ImportError: - jinja_feature = False -else: - import os.path - import shlex - from datetime import datetime +jinja_env = jinja2.Environment() + + +def to_datetime(value: str, format: str = "%Y-%m-%d") -> datetime: + """Converts a string to a datetime object.""" + return datetime.strptime(value, format) + + +def format_datetime(value: datetime, format: str = "%Y-%m-%d") -> str: + """Formats a datetime object as a string.""" + return value.strftime(format) + + +def to_int(value: str, base: int = 10) -> int: + """Converts a string to an integer.""" + return int(value, base=base) + - jinja_feature = True - jinja_env = jinja2.Environment() +def to_float(value: str) -> float: + """Converts a string to a float.""" + return float(value) - def to_datetime(value: str, format: str = "%Y-%m-%d") -> datetime: - """Converts a string to a datetime object.""" - return datetime.strptime(value, format) - def format_datetime(value: datetime, format: str = "%Y-%m-%d") -> str: - """Formats a datetime object as a string.""" - return value.strftime(format) +def quote(value: str) -> str: + """Quotes a string safely for shell execution.""" + return shlex.quote(value) - def to_int(value: str, base: int = 10) -> int: - """Converts a string to an integer.""" - return int(value, base=base) - def to_float(value: str) -> float: - """Converts a string to a float.""" - return float(value) +def splitpath(value: str) -> tuple[str, str]: + """Splits a path into (directory, filename).""" + return os.path.split(value) - def quote(value: str) -> str: - """Quotes a string safely for shell execution.""" - return shlex.quote(value) - def splitpath(value: str) -> tuple[str, str]: - """Splits a path into (directory, filename).""" - return os.path.split(value) +def splitext(value: str) -> tuple[str, str]: + """Splits a file path into (root, extension).""" + return os.path.splitext(value) - def splitext(value: str) -> tuple[str, str]: - """Splits a file path into (root, extension).""" - return os.path.splitext(value) - jinja_env.filters["datetime"] = to_datetime - jinja_env.filters["datetimeformat"] = format_datetime - jinja_env.filters["int"] = to_int - jinja_env.filters["float"] = to_float - jinja_env.filters["quote"] = quote - jinja_env.filters["splitpath"] = splitpath - jinja_env.filters["splitext"] = splitext +jinja_env.filters["datetime"] = to_datetime +jinja_env.filters["datetimeformat"] = format_datetime +jinja_env.filters["int"] = to_int +jinja_env.filters["float"] = to_float +jinja_env.filters["quote"] = quote +jinja_env.filters["splitpath"] = splitpath +jinja_env.filters["splitext"] = splitext def get_render_function( @@ -72,20 +74,15 @@ def get_render_function( RuntimeError: If Jinja2 is selected but not installed. """ if engine == "jinja2": - if jinja_feature: - jinja_env_overlay = jinja_env.overlay( - loader=jinja2.ChoiceLoader( - [ - jinja2.FileSystemLoader( - os.path.join(CONFIG_PATH, command_name) - ), - jinja2.FileSystemLoader(os.path.join(CONFIG_PATH, "globals")), - ] - ) + jinja_env_overlay = jinja_env.overlay( + loader=jinja2.ChoiceLoader( + [ + jinja2.FileSystemLoader(os.path.join(CONFIG_PATH, command_name)), + jinja2.FileSystemLoader(os.path.join(CONFIG_PATH, "globals")), + ] ) - env_template = jinja_env_overlay.from_string(template) - return cast(Callable[..., str], env_template.render) - else: - raise RuntimeError("Jinja2 is required but not installed!") + ) + env_template = jinja_env_overlay.from_string(template) + return cast(Callable[..., str], env_template.render) else: return template.format diff --git a/pyproject.toml b/pyproject.toml index 9df713e..da0d5f7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,15 +9,13 @@ license = {text = "GPL-3.0-or-later"} requires-python = ">=3.12, <4" dependencies = [ "click >=8.1.8, <9.0.0", + "Jinja2 >=3.1.5, <4.0.0", ] [project.urls] repository = "https://projects.metalgamer.eu/patternutils" [project.optional-dependencies] -jinja2 = [ - "Jinja2 >=3.1.5, <4.0.0", -] pygments = [ "Pygments >=2.19.1, <3.0.0", ] |
