From 664f424d76ef2134af9513ece11d08263cff4e24 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Thu, 10 Dec 2020 22:36:32 +0100 Subject: Add splitpath and splitext filter to jinja2 engine --- patternutils/template.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/patternutils/template.py b/patternutils/template.py index 56bb0be..8dbc650 100644 --- a/patternutils/template.py +++ b/patternutils/template.py @@ -1,10 +1,11 @@ -from typing import Callable +from typing import Callable, Tuple try: import jinja2 except ImportError: jinja_feature = False else: + import os.path import shlex from datetime import datetime @@ -26,11 +27,19 @@ else: def quote(value: str) -> str: return shlex.quote(value) + def splitpath(value: str) -> Tuple[str, str]: + return os.path.split(value) + + def splitext(value: str) -> Tuple[str, str]: + 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 def get_render_function(template: str, *, engine: str = "python") -> Callable[..., str]: -- cgit v1.3.1