Пример #1
0
    def __init__(self,
                 filters={},
                 globals={},
                 tests={},
                 loader=None,
                 extensions=[],
                 **kwargs):
        if not loader:
            loader = loaders.ChoiceLoader(self._get_loaders())
        all_ext = self._get_all_extensions()

        extensions.extend(all_ext['extensions'])
        super(CoffinEnvironment, self).__init__(extensions=extensions,
                                                loader=loader,
                                                **kwargs)
        # Note: all_ext already includes Jinja2's own builtins (with
        # the proper priority), so we want to assign to these attributes.
        self.filters = all_ext['filters'].copy()
        self.filters.update(filters)
        self.globals.update(all_ext['globals'])
        self.globals.update(globals)
        self.tests = all_ext['tests'].copy()
        self.tests.update(tests)
        for key, value in all_ext['attrs'].items():
            setattr(self, key, value)

        from coffin.template import Template as CoffinTemplate
        self.template_class = CoffinTemplate
Пример #2
0
 def test_choice_loader(self):
     log = self.compile_down(py_compile=True)
     self.mod_env.loader = loaders.ChoiceLoader([self.mod_env.loader, loaders.DictLoader({'DICT_SOURCE': 'DICT_TEMPLATE'})])
     tmpl1 = self.mod_env.get_template('a/test.html')
     self.assert_equal(tmpl1.render(), 'BAR')
     tmpl2 = self.mod_env.get_template('DICT_SOURCE')
     self.assert_equal(tmpl2.render(), 'DICT_TEMPLATE')
Пример #3
0
    def __init__(self,
                 filters={},
                 globals={},
                 tests={},
                 loader=None,
                 extensions=[],
                 **kwargs):
        if not loader:
            loader = loaders.ChoiceLoader(self._get_loaders())
        all_ext = self._get_all_extensions()

        extensions.extend(all_ext['extensions'])
        super(CoffinEnvironment, self).__init__(extensions=extensions,
                                                loader=loader,
                                                **kwargs)
        self.filters.update(filters)
        self.filters.update(all_ext['filters'])
        self.globals.update(globals)
        self.globals.update(all_ext['globals'])
        self.tests.update(tests)
        self.tests.update(all_ext['tests'])
        for key, value in all_ext['attrs'].items():
            setattr(self, key, value)

        from coffin.template import Template as CoffinTemplate
        self.template_class = CoffinTemplate
Пример #4
0
def get_env():
    """
    :return: A Jinja2 environment singleton.
    """
    global _ENV
    if not _ENV:
        loaders_ = _get_loaders()
        filters = _get_filters()
        extensions = _get_extensions()
        arguments = {
            'autoescape': True,
        }

        need_env.send(sender=Environment,
                      arguments=arguments,
                      loaders=loaders_,
                      extensions=extensions,
                      filters=filters)

        if not _ENV:
            if not 'loader' in arguments:
                arguments['loader'] = loaders.ChoiceLoader(loaders_)
            if not 'extensions' in arguments:
                arguments['extensions'] = extensions

            _ENV = Environment(**arguments)
            _ENV.filters.update(filters)
            from coffin.template import Template as CoffinTemplate
            _ENV.template_class = CoffinTemplate
    return _ENV
Пример #5
0
 def test_choice_loader(self, prefix_loader):
     self.compile_down(prefix_loader)
     self.mod_env.loader = loaders.ChoiceLoader(
         [self.mod_env.loader, loaders.DictLoader({"DICT_SOURCE": "DICT_TEMPLATE"})]
     )
     tmpl1 = self.mod_env.get_template("a/test.html")
     assert tmpl1.render() == "BAR"
     tmpl2 = self.mod_env.get_template("DICT_SOURCE")
     assert tmpl2.render() == "DICT_TEMPLATE"
Пример #6
0
    def test_choice_loader(self, prefix_loader):
        log = self.compile_down(prefix_loader)

        self.mod_env.loader = loaders.ChoiceLoader([
            self.mod_env.loader,
            loaders.DictLoader({'DICT_SOURCE': 'DICT_TEMPLATE'})
        ])

        tmpl1 = self.mod_env.get_template('a/test.html')
        assert tmpl1.render() == 'BAR'
        tmpl2 = self.mod_env.get_template('DICT_SOURCE')
        assert tmpl2.render() == 'DICT_TEMPLATE'
Пример #7
0
def render_target(project_loader: loaders.BaseLoader,
                  project_variables: typing.Mapping[str, str],
                  target: TargetConfig):

    variables = dict(project_variables)
    variables.update(target.variables)

    layout = build_layout(target.transaction)
    layout_loader = loaders.DictLoader({"layout": layout})
    target_loader = loaders.ChoiceLoader([project_loader, layout_loader])

    render_text = render_abstract(target.schema_template,
                                  target_loader,
                                  variables)
    return render_text
"""
import os
import re
import sys
import unittest
from traceback import format_exception
from jinja2 import loaders
from jinja2._compat import PY2

here = os.path.dirname(os.path.abspath(__file__))

dict_loader = loaders.DictLoader({'justdict.html': 'FOO'})
package_loader = loaders.PackageLoader('jinja2.testsuite.res', 'templates')
filesystem_loader = loaders.FileSystemLoader(here + '/res/templates')
function_loader = loaders.FunctionLoader({'justfunction.html': 'FOO'}.get)
choice_loader = loaders.ChoiceLoader([dict_loader, package_loader])
prefix_loader = loaders.PrefixLoader({
    'a': filesystem_loader,
    'b': dict_loader
})


class JinjaTestCase(unittest.TestCase):

    ### use only these methods for testing.  If you need standard
    ### unittest method, wrap them!

    def setup(self):
        pass

    def teardown(self):
Пример #9
0
def choice_loader(dict_loader, package_loader):
    '''returns a ChoiceLoader
    '''
    return loaders.ChoiceLoader([dict_loader, package_loader])
Пример #10
0
 def __init__(self, subloaders):
     self.loader = loaders.ChoiceLoader(subloaders)
     self.template_cache = {}
Пример #11
0
def choice_loader(dict_loader, package_loader):
    """returns a ChoiceLoader"""
    return loaders.ChoiceLoader([dict_loader, package_loader])
Пример #12
0
def build_loader(paths, packages):
    fs_loaders = [loaders.FileSystemLoader(path) for path in paths]

    package_loaders = [loaders.PackageLoader(package.package_name, package.template_path)
                       for package in packages]
    return loaders.ChoiceLoader(fs_loaders + package_loaders)