kolypto / j2cli

Jinja2 Command-Line Tool, reworked

Home Page:http://pypi.python.org/pypi/j2cli

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Feature Request] Load custom filters from Python modules

michalismeng opened this issue · comments

The current implementation allows loading custom filters from Python files by using the --filters argument.

My problem is I have compiled Python files, i.e Python bytecode (.pyc files) and passing them to --filters fails with the following error:

ValueError: source code string cannot contain null bytes

I understand the main reason for the above behavior is using Python's imp.load_source function, which can't read bytecode:

j2cli/j2cli/cli.py

Lines 72 to 74 in 26a67e9

def _import_functions(self, filename):
m = imp.load_source('imported-funcs', filename)
return dict((name, func) for name, func in inspect.getmembers(m) if inspect.isfunction(func))

A solution to the above would be to introduce a new argument, e.g --filters-modules MODULE, which will accept Python modules and load them using importlib.import_module.