Skip to content

Commit

Permalink
allow adding filters
Browse files Browse the repository at this point in the history
  • Loading branch information
grizz committed Jan 18, 2024
1 parent 0996e70 commit eff28b9
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/netom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from confu import schema
from pkg_resources import get_distribution

from . import filters
import netom.filters

from .exception import NetomValidationError

# TODO move out of this namespace
Expand All @@ -25,10 +26,8 @@ class NetomTemplar(Templar):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.environment.filters["address_to_mask"] = filters.address_to_mask

for name in filters.__all__:
self.environment.filters[name] = getattr(filters, name)
for name in netom.filters.__all__:
self.environment.filters[name] = getattr(netom.filters, name)


class PathLoader(DataLoader):
Expand Down Expand Up @@ -64,7 +63,7 @@ class Render:
Renders data to defined type.
"""

def __init__(self, model_version, model_type, search_path=None, engine="jinja2"):
def __init__(self, model_version, model_type, search_path=None, engine="jinja2", filters=None):
"""
Create a render object.
Expand All @@ -75,8 +74,15 @@ def __init__(self, model_version, model_type, search_path=None, engine="jinja2")

self.version = model_version
self.type = model_type
self.filters = {}
self._engine_type = engine

for name in netom.filters.__all__:
self.filters[name] = getattr(netom.filters, name)

if filters:
self.filters.update(filters)

if not search_path:
# FIXME use pkg resources
search_path = os.path.join(
Expand All @@ -91,10 +97,7 @@ def set_search_path(self, search_path):
self.engine = TemplarEngine(search_path=search_path)
else:
self.engine = tmpl.get_engine(self._engine_type)(search_path=search_path)

for name in filters.__all__:
self.engine.engine.filters[name] = getattr(filters, name)

self.engine.engine.filters.update(self.filters)

def _render(self, filename, data, fobj):
# engine.engine.undefined = IgnoreUndefined
Expand Down

0 comments on commit eff28b9

Please sign in to comment.