Python jinja2.ext.Extension() Examples

The following are 8 code examples of jinja2.ext.Extension(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module jinja2.ext , or try the search function .
Example #1
Source File: tests.py    From jinja2-django-tags with MIT License 5 votes vote down vote up
def test_existing_finalize(self):
        finalize_mock = mock.Mock(side_effect=lambda s: s)

        class TestExtension(Extension):
            def __init__(self, environment):
                environment.finalize = finalize_mock

        env = Environment(extensions=[TestExtension, DjangoL10n])
        template = env.from_string("{{ foo }}")

        translation.activate('de')
        self.assertEqual('1,23', template.render({'foo': 1.23}))
        finalize_mock.assert_called_with(1.23) 
Example #2
Source File: jinja_extensions.py    From daf-recipes with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, environment):
        ext.Extension.__init__(self, environment)
        try:
            self.searchpath = environment.loader.searchpath[:]
        except AttributeError:
            # this isn't available on message extraction
            pass 
Example #3
Source File: extensions_test.py    From rdm with MIT License 5 votes vote down vote up
def test_dynamic_class_loader():
    extensions = dynamic_class_loader(['rdm.md_extensions.audit_notes.AuditNoteExclusionExtension'])
    assert extensions is not None
    assert len(extensions) == 1
    extension = extensions[0]
    assert issubclass(extension, Extension) 
Example #4
Source File: extensions.py    From historical with Apache License 2.0 5 votes vote down vote up
def __init__(self, environment):
        """Instantiates the Historical Extension

        :param environment:
        """
        super(HistoricalExtension, self).__init__(environment)
        environment.filters['titlecase'] = titlecase 
Example #5
Source File: ext.py    From Flask-P2P with MIT License 5 votes vote down vote up
def test_extension_ordering(self):
        class T1(Extension):
            priority = 1
        class T2(Extension):
            priority = 2
        env = Environment(extensions=[T1, T2])
        ext = list(env.iter_extensions())
        assert ext[0].__class__ is T1
        assert ext[1].__class__ is T2 
Example #6
Source File: ext.py    From Flask with Apache License 2.0 5 votes vote down vote up
def test_extension_ordering(self):
        class T1(Extension):
            priority = 1
        class T2(Extension):
            priority = 2
        env = Environment(extensions=[T1, T2])
        ext = list(env.iter_extensions())
        assert ext[0].__class__ is T1
        assert ext[1].__class__ is T2 
Example #7
Source File: ext.py    From Flask with Apache License 2.0 5 votes vote down vote up
def test_extension_ordering(self):
        class T1(Extension):
            priority = 1
        class T2(Extension):
            priority = 2
        env = Environment(extensions=[T1, T2])
        ext = list(env.iter_extensions())
        assert ext[0].__class__ is T1
        assert ext[1].__class__ is T2 
Example #8
Source File: cli.py    From yasha with MIT License 4 votes vote down vote up
def load_extensions(file):
    from jinja2.ext import Extension
    import inspect

    tests   = dict()
    filters = dict()
    parsers = dict()
    classes = []

    try:
        module = load_python_module(file)
    except NameError as e:
        msg = 'Unable to load extensions, {}'
        raise ClickException(msg.format(e))
    except SyntaxError as e:
        msg = "Unable to load extensions\n{} ({}, line {})"
        error = e.msg[0].upper() + e.msg[1:]
        filename = os.path.relpath(e.filename)
        raise ClickException(msg.format(error, filename, e.lineno))

    for attr in [getattr(module, x) for x in dir(module)]:
        if inspect.isfunction(attr):
            if attr.__name__.startswith('test_'):
                name = attr.__name__[5:]
                tests[name] = attr
            if attr.__name__.startswith('filter_'):
                name = attr.__name__[7:]
                filters[name] = attr
            if attr.__name__.startswith('parse_'):
                name = attr.__name__[6:]
                parsers['.' + name] = attr
        if inspect.isclass(attr):
            if issubclass(attr, Extension):
                classes.append(attr)

    import jinja2.defaults
    for name, obj in inspect.getmembers(module):
        if name in jinja2.defaults.__all__:
            setattr(jinja2.defaults, name, obj)

    try:
        TESTS.update(module.TESTS)
    except AttributeError:
        TESTS.update(tests)

    try:
        FILTERS.update(module.FILTERS)
    except AttributeError:
        FILTERS.update(filters)

    try:
        PARSERS.update(module.PARSERS)
    except AttributeError:
        PARSERS.update(parsers)

    try:
        CLASSES.extend(module.CLASSES)
    except AttributeError:
        CLASSES.extend(classes)