NDOJ/judge/jinja2/registry.py

56 lines
1.2 KiB
Python
Raw Permalink Normal View History

2020-01-21 06:35:58 +00:00
from django_jinja.library import render_with
globals = {}
tests = {}
filters = {}
extensions = []
2022-05-14 17:57:27 +00:00
__all__ = ["render_with", "function", "filter", "test", "extension"]
2020-01-21 06:35:58 +00:00
def _store_function(store, func, name=None):
if name is None:
name = func.__name__
store[name] = func
def _register_function(store, name, func):
if name is None and func is None:
2022-05-14 17:57:27 +00:00
2020-01-21 06:35:58 +00:00
def decorator(func):
_store_function(store, func)
return func
return decorator
elif name is not None and func is None:
if callable(name):
_store_function(store, name)
return name
else:
2022-05-14 17:57:27 +00:00
2020-01-21 06:35:58 +00:00
def decorator(func):
_store_function(store, func, name)
return func
return decorator
else:
_store_function(store, func, name)
return func
def filter(name=None, func=None):
return _register_function(filters, name, func)
def function(name=None, func=None):
return _register_function(globals, name, func)
def test(name=None, func=None):
return _register_function(tests, name, func)
def extension(cls):
extensions.append(cls)
return cls