Skip to content

Commit

Permalink
Add two new time filters to jinja (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidHuber-NOAA authored Apr 22, 2024
1 parent c237f19 commit 2c48c56
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/wxflow/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import jinja2
from markupsafe import Markup

from .timetools import (strftime, to_fv3time, to_isotime, to_julian, to_YMD,
to_YMDH)
from .timetools import (add_to_datetime, strftime, to_fv3time, to_isotime,
to_julian, to_timedelta, to_YMD, to_YMDH)

__all__ = ['Jinja']

Expand Down Expand Up @@ -108,6 +108,8 @@ def get_set_env(self, loader: jinja2.BaseLoader, filters: Dict[str, callable] =
to_f90bool: convert a boolean to a fortran boolean
relpath: convert a full path to a relative path based on an input root_path
getenv: read variable from environment if defined, else UNDEFINED
to_timedelta: convert a string to a timedelta object
add_to_datetime: add time to a datetime, return new datetime object
Parameters
----------
Expand All @@ -131,6 +133,9 @@ def get_set_env(self, loader: jinja2.BaseLoader, filters: Dict[str, callable] =
env.filters["to_f90bool"] = lambda bool: ".true." if bool else ".false."
env.filters['getenv'] = lambda name, default='UNDEFINED': os.environ.get(name, default)
env.filters["relpath"] = lambda pathname, start=os.curdir: os.path.relpath(pathname, start)
env.filters["add_to_datetime"] = (lambda dt, delta: add_to_datetime(dt, delta)
if not (isinstance(dt, SilentUndefined) or isinstance(delta, SilentUndefined)) else dt)
env.filters["to_timedelta"] = lambda delta_str: to_timedelta(delta_str) if not isinstance(delta_str, SilentUndefined) else delta_str

# Add any additional filters
if filters is not None:
Expand Down

0 comments on commit 2c48c56

Please sign in to comment.