Ejemplo n.º 1
0
def environment(**options):
    from django.utils.translation import ugettext

    env = Environment(**options)
    env.add_extension(
        'project.main.middleware.pagination.AutopaginateExtension')
    env.globals.update({
        'static': staticfiles_storage.url,
        'url': reverse,
        'get_site_name': get_site_name,
        'get_const_module': get_const_module,
        'slugify': slugify,
        'format_float': format_float,
        'format_float_thousands': format_float_thousands,
        'format_int_thousands': format_int_thousands,
        'format_datetime': format_datetime,
        'format_date': format_date,
        'format_time': format_time,
        'format_weekday': format_weekday,
        'format_weekday_date': format_weekday_date,
        'gettext': ugettext,
        'urlencode': urlencode,
        'ago': ago,
        'format_compact_datetime': format_compact_datetime,
        'get_year_today': get_year_today,
        'is_deploy': lambda: project.settings.local.Deploy,
        'is_debug': lambda: project.settings.local.Debug,
        'channels_enabled': lambda: project.settings.local.ChannelsEnabled,
        'get_timestamp_inc': get_timestamp_inc,
    })
    return env
Ejemplo n.º 2
0
class TestTemplateTag(object):

    def setup(self):
        # Setup the assets environment.
        assets_env = AssetsEnvironment('', '')
        self.foo_bundle = Bundle()
        self.bar_bundle = Bundle()
        assets_env.register('foo_bundle', self.foo_bundle)
        assets_env.register('bar_bundle', self.bar_bundle)

        # Inject a mock bundle class into the Jinja2 extension, so we
        # can check on what exactly it does.
        test_instance = self
        class MockBundle(Bundle):
            urls_to_fake = ['foo']
            def __init__(self, *a, **kw):
                Bundle.__init__(self, *a, **kw)
                self.env = assets_env
                # Kind of hacky, but gives us access to the last Bundle
                # instance used by the extension.
                test_instance.the_bundle = self
            def urls(self, *a, **kw):
                return self.urls_to_fake
        self._old_bundle_class = AssetsExtension.BundleClass
        AssetsExtension.BundleClass = self.BundleClass = MockBundle

        # Setup the Jinja2 environment.
        self.jinja_env = JinjaEnvironment()
        self.jinja_env.add_extension(AssetsExtension)
        self.jinja_env.assets_environment = assets_env

    def teardown(self):
        AssetsExtension.BundleClass = self._old_bundle_class
        del self._old_bundle_class

    def render_template(self, args, ctx={}):
        return self.jinja_env.from_string(
            '{% assets '+args+' %}{{ ASSET_URL }};{% endassets %}').render(ctx)

    def test_reference_bundles(self):
        self.render_template('"foo_bundle", "bar_bundle"')
        assert self.the_bundle.contents == (self.foo_bundle, self.bar_bundle)

    def test_reference_files(self):
        self.render_template('"file1", "file2", "file3"')
        assert self.the_bundle.contents == ('file1', 'file2', 'file3',)

    def test_reference_mixed(self):
        self.render_template('"foo_bundle", "file2", "file3"')
        assert self.the_bundle.contents == (self.foo_bundle, 'file2', 'file3',)

    def test_with_vars(self):
        self.render_template('var1, var2', {'var1': self.foo_bundle, 'var2': 'a_file'})
        assert self.the_bundle.contents == (self.foo_bundle, 'a_file',)

    def test_output_urls(self):
        """Ensure the tag correcly spits out the urls the bundle returns.
        """
        self.BundleClass.urls_to_fake = ['foo', 'bar']
        assert self.render_template('"file1" "file2" "file3"') == 'foo;bar;'
Ejemplo n.º 3
0
class TestLoader(TempEnvironmentHelper):

    default_files = {
        'template.html':
        """
            <h1>Test</h1>
            {% if foo %}
                {% assets "A", "B", "C", output="output.html" %}
                    {{ ASSET_URL }}
                {% endassets %}
            {% endif %}
            """
    }

    def setup(self):
        TempEnvironmentHelper.setup(self)
        self.jinja_env = JinjaEnvironment()
        self.jinja_env.add_extension(AssetsExtension)
        self.jinja_env.assets_environment = self.env

    def test(self):
        loader = Jinja2Loader(self.env, [self.tempdir], [self.jinja_env])
        bundles = loader.load_bundles()
        assert len(bundles) == 1
        assert bundles[0].output == "output.html"
def prepare_environment(env=None):
    try:
        loader = PackageLoader("glycresoft_sqlalchemy.web_app", "html")
        loader.list_templates()
    except:
        loader = FileSystemLoader(os.path.join(os.path.dirname(__file__), 'html'))
    if env is None:
        env = Environment(loader=loader, extensions=[FragmentCacheExtension])
    else:
        env.loader = loader
    env.add_extension(FragmentCacheExtension)
    env.fragment_cache = dict()
    env.filters["n_per_row"] = n_per_row
    env.filters['highlight_sequence_site'] = highlight_sequence_site
    env.filters['plot_glycoforms'] = plot_glycoforms
    env.filters['chromatogram'] = plot_chromatogram
    env.filters['svg_plot'] = svg_plot
    env.filters['png_plot'] = png_plot
    env.filters['fsort'] = fsort
    env.filters['glycopeptide_string'] = glycopeptide_string
    env.filters['glycan_composition_string'] = glycan_composition_string
    env.filters["glycopeptide_match_logo"] = glycopeptide_match_logo
    env.filters["formula"] = formula
    env.globals
    return env
Ejemplo n.º 5
0
class Template():
    """Class to handle templates."""
    def __init__(self, logya_inst):
        """Initialize template environment."""

        self.vars = {}
        self.dir_templates = logya_inst.dir_templates
        self.env = Environment(loader=TemplateLoader(self.dir_templates))

        # Enable break and continue in templates.
        self.env.add_extension('jinja2.ext.loopcontrols')

        # Enable with statement for nested variable scopes.
        self.env.add_extension('jinja2.ext.with_')

        # Trim whitespace around template tags if configured.
        tpl_settings = logya_inst.config.get('template')
        if tpl_settings and tpl_settings.get('trim_whitespace'):
            self.env.lstrip_blocks = True
            self.env.trim_blocks = True

        # Add filesource global to allow for including the source of a file.
        self.env.globals[
            'filesource'] = lambda x, lines=None, raw=False: filesource(
                logya_inst, x, lines=lines, raw=raw)

        self.env.globals['get_doc'] = lambda x: get_doc(logya_inst, x)

    def get_page(self, doc, template):
        try:
            page = self.env.get_template(template)
        except TemplateNotFound as err:
            raise TemplateNotFound('Doc: {}\n{}'.format(doc, err))
        return page
Ejemplo n.º 6
0
def prepare_environment(env=None):
    try:
        loader = PackageLoader("glypy", "search/results_template")
        loader.list_templates()
    except:
        loader = FileSystemLoader(os.path.join(os.path.dirname(__file__), 'results_template'))
    if env is None:
        env = Environment(loader=loader, extensions=[FragmentCacheExtension])
    else:
        env.loader = loader
        env.add_extension(FragmentCacheExtension)
    env.filters["collect_fragments"] = collect_fragments
    env.filters["all_matches"] = fetch_all_matches
    env.filters["strip_derivatize"] = strip_derivatize_glycoct
    env.filters["scientific_notation"] = scientific_notation
    env.filters["cfg_plot"] = cfg_plot
    env.filters["simple_cfg_plot"] = simple_plot
    env.filters["min"] = min
    env.filters["max"] = max
    env.filters["unique"] = unique
    env.filters["limit_sigfig"] = limit_sigfig
    env.filters['css_escape'] = css_escape
    env.filters['greek_fragment_name'] = greek_fragment_names
    env.fragment_cache = dict()
    return env
Ejemplo n.º 7
0
def render(menu, template):
    module_dir = os.path.dirname(os.path.abspath(__file__))
    templates_dir = os.path.join(module_dir, 'templates')
    j2_env = Environment(loader=FileSystemLoader(templates_dir),
                         trim_blocks=True,
                         undefined=StrictUndefined)

    j2_env.add_extension('jinja2.ext.loopcontrols')
    j2_env.filters['toDecimal'] = toDecimal
    j2_env.filters['toCharge'] = toCharge
    j2_env.filters['getCentralityBitmask'] = getCentralityBitmask
    j2_env.filters['hasCorrelationCuts'] = hasCorrelationCuts
    j2_env.filters['sortObjects'] = sortObjects
    j2_env.filters['toCpp'] = toCpp
    j2_env.filters['getDeltaLut'] = getDeltaLut
    j2_env.filters['getPrecisionByName'] = getPrecisionByName
    j2_env.filters['getEtLut'] = getEtLut
    j2_env.filters['getMathLut'] = getMathLut
    j2_env.filters['getMathUptLut'] = getMathUptLut
    j2_env.filters['getCaloMuonConversionLut'] = getCaloMuonConversionLut
    j2_env.filters['warning'] = warning
    j2_env.filters['toMass'] = toMass
    j2_env.filters['toDeltaR'] = toDeltaR
    j2_env.filters['chkChgCor'] = chkChgCor
    j2_env.filters['getPrefix'] = getPrefix
    j2_env.filters['isMuon'] = isMuon
    j2_env.filters['isTau'] = isTau
    j2_env.filters['hasIndexCut'] = hasIndexCut
    j2_env.filters['getIndexCut'] = getIndexCut
    j2_env.filters['getScale'] = getScale
    j2_env.filters['getLookUpTable'] = getLookUpTable
    data = {"tmGrammar": tmGrammar, "tmEventSetup": tmEventSetup, "menu": menu}

    return j2_env.get_template(template).render(data)
Ejemplo n.º 8
0
class JinjaLoader(object):
    def __init__(self, root_path, **kwargs):
        super(JinjaLoader, self).__init__()
        auto_reload = kwargs.get('debug', True)
        self.env = Environment(loader=FileSystemLoader(root_path),
                               extensions=['jinja2.ext.autoescape'],
                               trim_blocks=True,
                               lstrip_blocks=True,
                               cache_size=-1,  # no clean-up
                               auto_reload=auto_reload)

        additional_globals = {
            'ord': ord,
            'chr': chr,
            'unichr': unichr,
        }
        self.env.globals.update(additional_globals)
        self.env.add_extension('jinja2.ext.loopcontrols')

    def load(self, name, parent_path=None):
        return JinjaTemplate(self.env.get_template(name))

    def reset(self):
        '''Reset the cache of compiled templates, required
           in debug mode.
        '''
        self.env.cache.clear()
Ejemplo n.º 9
0
    def render(self, variables):
        """
        Render the template using Jinja2.
        """
        environment = Environment(
            loader=PeeringManagerLoader(),
            trim_blocks=self.jinja2_trim,
            lstrip_blocks=self.jinja2_lstrip,
        )
        environment.add_extension(IncludeTemplateExtension)
        for extension in settings.JINJA2_TEMPLATE_EXTENSIONS:
            environment.add_extension(extension)

        # Add custom filters to our environment
        environment.filters.update(FILTER_DICT)

        # Try rendering the template, return a message about syntax issues if there
        # are any
        try:
            jinja2_template = environment.from_string(self.template)
            return jinja2_template.render(variables)
        except TemplateSyntaxError as e:
            return f"Syntax error in template at line {e.lineno}: {e.message}"
        except Exception:
            return traceback.format_exc()
Ejemplo n.º 10
0
def environment(**options):
    env = Environment(**options)
    env.globals.update({
        'static': staticfiles_storage.url,
        'url': reverse,
    })
    env.add_extension('jinja2.ext.i18n')
    return env
Ejemplo n.º 11
0
def environment(**options):
    env = Environment(**options)
    env.globals.update({
        'url': reverse,
    })
    env.add_extension(ActiveUrl)

    return env
Ejemplo n.º 12
0
 def render_output(self, jinja_file, params):
     env = Environment(loader=FileSystemLoader(searchpath=self.src_path),
                       trim_blocks=True,
                       lstrip_blocks=True)
     env.add_extension('jinja2.ext.loopcontrols')
     env.add_extension('jinja2.ext.do')
     env.filters['todict'] = todict
     env.filters['islist'] = is_list
     jinja = env.get_template(jinja_file)
     return jinja.render(params=params)
Ejemplo n.º 13
0
def render(template):
    j2_env = Environment(loader=FileSystemLoader(THIS_DIR), trim_blocks=True)
    j2_env.add_extension('jinja2.ext.loopcontrols')

    association = {
        "info": info,
        "loop": loop,
        "link": link,
    }

    return j2_env.get_template(template).render(association)
Ejemplo n.º 14
0
 def _render_from_settings(self, setting_name):
     setting = getattr(self._settings, setting_name)
     env = Environment()
     env.add_extension('jinja2.ext.autoescape')
     result = setting
     while True:
         t = env.from_string(result)
         newresult = t.render(self._template_vars())
         if newresult == result:
             break
         result = newresult
     return result
Ejemplo n.º 15
0
 def _render_from_settings(self, setting_name):
     setting = getattr(self._settings, setting_name)
     env = Environment()
     env.add_extension('jinja2.ext.autoescape')
     result = setting
     while True:
         t = env.from_string(result)
         newresult = t.render(self._template_vars())
         if newresult == result:
             break
         result = newresult
     return result
Ejemplo n.º 16
0
    def test_python_tpl_with_autoescape(self):
        # See: https://github.com/tlatsas/jinja2-highlight/pull/1
        env = Environment(extensions=['jinja2_highlight.HighlightExtension'])
        env.add_extension('jinja2.ext.autoescape')
        tpl = env.from_string('''
            {% autoescape true %}
            {% highlight "python" %}
               print("Hello world")
            {% endhighlight %}
            {% endautoescape %}
        ''')

        self.assertHtmlListEqual(tpl.render().split(), self.rendered)
    def test_python_tpl_with_autoescape(self):
        # See: https://github.com/tlatsas/jinja2-highlight/pull/1
        env = Environment(extensions=['jinja2_highlight.HighlightExtension'])
        env.add_extension('jinja2.ext.autoescape')
        tpl = env.from_string('''
            {% autoescape true %}
            {% highlight "python" %}
               print("Hello world")
            {% endhighlight %}
            {% endautoescape %}
        ''')

        assert tpl.render().split() == self.rendered
Ejemplo n.º 18
0
def write_keeper(keeper_league):
    env = Environment(loader=FileSystemLoader(constants.templates_dir), trim_blocks=True)
    env.add_extension('jinja2.ext.do')

    keeper_template = env.get_template(constants.keeper_template)
    keeper_output = keeper_template.render(teams=keeper_league.teams)

    print("Writing HTML to %s" % constants.keepers_file_path)
    with open(constants.keepers_file_path, 'w+') as f1:
        f1.write(keeper_output)

    print("Writing keeper to spreadsheet")
    write_keeper_to_spreadsheet(keeper_league)
class JinjaMacroTagTestCase(unittest.TestCase):
    def setUp(self):
        self.env = Environment(loader=FileSystemLoader(templates_path))
        self.env.add_extension(LoadMacroExtension)
        self.env.add_extension(CallMacroTagExtension)
        self.env.add_extension(JinjaMacroTagsExtension)
        self.env.add_extension(HtmlMacroTagsExtension)
        self.env.autoload_macro_tags = True

    def test_register_macro_from_template(self):
        self.env.macros.register_from_template("macros.html")
        self.assertTrue(self.env.macros.exists("panel"))

    def test_register_macro_from_file(self):
        def register_file():
            self.env.macros.register_file(os.path.join(templates_path, "macros.html"))
        self.assertRaises(Exception, register_file)
        self.env.loader = MacroLoader(self.env.loader)
        register_file()
        self.assertTrue(self.env.macros.exists("panel"))

    def test_jinja_macro_tag(self):
        self.env.macros.register_from_template("macros.html")
        tpl = self.env.get_template("jinja_style.html")
        self.assert_html(tpl.render())

    def test_html_macro_tag(self):
        self.env.macros.register_from_template("macros.html")
        tpl = self.env.get_template("html_style.html")
        self.assert_html(tpl.render())

    def assert_html(self, html):
        self.assertIn('<div class="panel-title">test panel</div>', html)
        self.assertIn('<div><button type="button" class="btn btn-default">click me</button></div>', html)
        self.assertIn('<button type="button" class="btn btn-primary">click me</button>', html)
Ejemplo n.º 20
0
def write_output(leagues_data, week, pro_data, nfl_data):
    env = Environment(loader=FileSystemLoader(constants.templates_dir), trim_blocks=True)
    env.add_extension('jinja2.ext.do')

    start_week_template = env.get_template(constants.main_template)
    start_week_output = start_week_template.render(leagues=leagues_data, current_week=week, pro_data=pro_data,
                                                   legend=constants.team_notes, nfl_data=nfl_data)
    with open(constants.start_week_file_path % week, 'w+') as f1:
        f1.write(start_week_output)

    if constants.current_week > 1:
        end_week_template = env.get_template(constants.week_end_scores_template)
        end_week_output = end_week_template.render(leagues=leagues_data)
        with open(constants.end_week_file_path % (week - 1), 'w+') as f1:
            f1.write(end_week_output)
Ejemplo n.º 21
0
def environment(**options):
    env = Environment(**options)
    env.globals.update({
        'static': staticfiles_storage.url,
        'url': url,
        'assets': assets,
        # 'gravatar_url': get_avatar_url,
        'get_current_language': get_language,
        'get_available_languages': get_available_languages,
        'get_language_info_list': get_language_info_list,
        # 'switch': gargoyle.is_active,
        # 'avatar_url': avatar_url
    })

    env.filters.update(
        datetimeformat=babel.datetimefmt,
        dateformat=babel.datefmt,
        timeformat=babel.timefmt,
        natural_period=natural_period,
        # timedeltaformat=format_timedelta,
        numberformat=babel.numberfmt,
        decimalformat=babel.decimalfmt,
        currencyformat=babel.currencyfmt,
        percentformat=babel.percentfmt,
        scientificformat=babel.scientificfmt,
        append_get=do_append_get,
        class_name=do_class_name,
    )

    env.add_extension('jinja2.ext.loopcontrols')
    env.add_extension('jinja2.ext.with_')
    env.add_extension('dinja2.ext.active')
    # from ext.relative_templates import RelativeInclude
    # env.add_extension(RelativeInclude)

    # env.add_extension('jinja2_highlight.HighlightExtension')
    # env.add_extension('jinja2htmlcompress.HTMLCompress')
    env.add_extension('webassets.ext.jinja2.AssetsExtension')
    env.assets_environment = get_env()

    env.add_extension('jinja2.ext.i18n')

    if settings.USE_I18N:
        from django.utils import translation
        env.install_gettext_translations(translation, newstyle=True)
    else:  # pragma: no cover
        env.install_null_translations(newstyle=True)
    return env
Ejemplo n.º 22
0
def load(template, env, kwargs):
    if env:
        env.add_extension(EvalExtension)
    else:
        try:
            path = os.path.dirname(os.path.abspath(template))
            template = os.path.split(os.path.abspath(template))[-1]
            env = Environment(loader=FileSystemLoader(path))
            env.add_extension(EvalExtension)
            template = env.get_template(template).render(kwargs)
        except:
            env = Environment(loader=BaseLoader())
            env.add_extension(EvalExtension)
            template = env.from_string(template).render(kwargs)
    replace = [item.strip() for item in re.findall('[ :]{{.+}}', template)]
    for item in replace:
        template = template.replace(item, '\'' + item + '\'')
    return template
Ejemplo n.º 23
0
class Jinja2Engine(templates.TemplateEngine):
    extension = 'jinja2'
    def get_template(self, name, **kw):
        self._lookup = Environment(loader=FileSystemLoader(self.directories, **kw))
        self._lookup.filters.update(
            #如果你想要在模板里添加filter,可以在这里添加
            field_label = field_label,
            field_name = field_name,
        )
        #增加这个扩展可以让我们在模板的循环块里使用break,continue
        self._lookup.add_extension('jinja2.ext.loopcontrols')
        #增加这个扩展可以让我们在模板里使用do关键字,用来进行一些python语句操作
        self._lookup.add_extension('jinja2.ext.do')
        return self._lookup.get_template("%s.%s" % (name, self.extension))

    def render(self, template_name, **kwargs):
        template = self._lookup.get_template("%s.%s" % (template_name, self.extension))
        return template.render(**kwargs)
Ejemplo n.º 24
0
def create_report_environment():
    # prepare jinja2 environment
    from orun.reports.engines.chrome.filters import localize, linebreaks, groupby
    from orun.reports.engines.chrome.extension import ReportExtension
    from orun.reports.engines.chrome.utils import avg, total, to_list, COUNT, AVG, SUM
    env = Environment()
    env.autoescape = False
    env.add_extension(ReportExtension)
    env.finalize = localize
    # env.undefined = SilentUndefined
    env.filters['localize'] = localize
    env.filters['linebreaks'] = linebreaks
    # env.globals['static_fs'] = self.static_fs
    env.filters['total'] = total
    env.globals['avg'] = avg
    env.globals['sum'] = sum
    env.globals['count'] = len
    env.globals['COUNT'] = COUNT
    env.globals['SUM'] = SUM
    env.globals['AVG'] = AVG
    from orun.reports.data import DataSource
    env.globals['create_datasource'] = lambda sql: DataSource(sql)
Ejemplo n.º 25
0
def environment(**options):
    env = Environment(**options)
    env.globals.update({
        "static": staticfiles_storage.url,
        "url": reverse,
        "curr_year": datetime.today().year,
    })
    env.install_gettext_callables(gettext=gettext,
                                  ngettext=ngettext,
                                  newstyle=True)
    env.add_extension(LanguageExtension)

    env.policies["json.dumps_function"] = datetime_dumps

    env.filters.update({
        "datetime": datetime_filter,
        "date": date_filter,
        "number_format": number_format,
        "excerpt_markdown": excerpt_markdown,
        "markdown": markdown_filter,
    })

    return env
Ejemplo n.º 26
0
def render(filename, path, **kwargs):
    """Render template with some info variables.

    >>> from jinja2_template_info import render
    >>> render("test.html", "./", debug=True,
    ...        code_variable="Variable from code")
    ...        # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
    '<!DOCTYPE html>...</html>'
    """
    info_path = files('jinja2_template_info')
    if isinstance(path, (list, tuple)):
        path = tuple(path) + (info_path, )
    else:  # str
        path = (info_path, path)

    env = Environment(loader=FileSystemLoader(path))
    if kwargs.get("debug"):
        env.add_extension(TemplateInfoExtension)
        env.globals['template_info'].data = kwargs.copy()
        env.globals['template_info'].template = filename

    template = env.get_template(filename)
    return template.render(kwargs)
Ejemplo n.º 27
0
class TestLoader(TempEnvironmentHelper):

    default_files = {
        'template.html': """
            <h1>Test</h1>
            {% if foo %}
                {% assets "A", "B", "C", output="output.html" %}
                    {{ ASSET_URL }}
                {% endassets %}
            {% endif %}
            """
    }

    def setup(self):
        TempEnvironmentHelper.setup(self)
        self.jinja_env = JinjaEnvironment()
        self.jinja_env.add_extension(AssetsExtension)
        self.jinja_env.assets_environment = self.env

    def test(self):
        loader = Jinja2Loader(self.env, [self.tempdir], [self.jinja_env])
        bundles = loader.load_bundles()
        assert len(bundles) == 1
        assert bundles[0].output == "output.html"
Ejemplo n.º 28
0
class JinjaLayoutTestCase(unittest.TestCase):
    def setUp(self):
        self.env = Environment(loader=FileSystemLoader(templates_path))
        self.env.add_extension(LayoutExtension)
        self.env.default_layout = "base.html"

    def test_blocks(self):
        html = self.env.get_template("blocks.html").render()
        self.assertIn("title=foobar", html)
        self.assertIn("content=hello world", html)

    def test_mixed(self):
        html = self.env.get_template("mixed.html").render()
        self.assertIn("title=foobar", html)
        self.assertIn("content=\n\nhello world", html)

    def test_noblock(self):
        html = self.env.get_template("noblock.html").render()
        self.assertIn("data=\nhello world", html)

    def test_disable_layout(self):
        self.env.disable_layout = True
        html = self.env.get_template("blocks.html").render()
        self.assertEquals("hello world", html)
Ejemplo n.º 29
0
class JinjaLayoutTestCase(unittest.TestCase):
    def setUp(self):
        self.env = Environment(loader=FileSystemLoader(templates_path))
        self.env.add_extension(LayoutExtension)
        self.env.default_layout = "base.html"

    def test_blocks(self):
        html = self.env.get_template("blocks.html").render()
        self.assertIn("title=foobar", html)
        self.assertIn("content=hello world", html)

    def test_mixed(self):
        html = self.env.get_template("mixed.html").render()
        self.assertIn("title=foobar", html)
        self.assertIn("content=\n\nhello world", html)

    def test_noblock(self):
        html = self.env.get_template("noblock.html").render()
        self.assertIn("data=\nhello world", html)

    def test_disable_layout(self):
        self.env.disable_layout = True
        html = self.env.get_template("blocks.html").render()
        self.assertEquals("hello world", html)
Ejemplo n.º 30
0
    def render(self, variables):
        """
        Render the template using Jinja2.
        """
        subject, body = "", ""
        environment = Environment(
            loader=PeeringManagerLoader(),
            trim_blocks=self.jinja2_trim,
            lstrip_blocks=self.jinja2_lstrip,
        )
        environment.add_extension(IncludeTemplateExtension)
        for extension in settings.JINJA2_TEMPLATE_EXTENSIONS:
            environment.add_extension(extension)

        # Add custom filters to our environment
        environment.filters.update(FILTER_DICT)

        try:
            jinja2_template = environment.from_string(self.subject)
            subject = jinja2_template.render(variables)
        except TemplateSyntaxError as e:
            subject = (
                f"Syntax error in subject template at line {e.lineno}: {e.message}"
            )
        except Exception as e:
            subject = str(e)

        try:
            jinja2_template = environment.from_string(self.template)
            body = jinja2_template.render(variables)
        except TemplateSyntaxError as e:
            body = f"Syntax error in body template at line {e.lineno}: {e.message}"
        except Exception:
            body = traceback.format_exc()

        return subject, body
Ejemplo n.º 31
0
class JinjaMacroTagTestCase(unittest.TestCase):
    def setUp(self):
        self.env = Environment(loader=FileSystemLoader(templates_path))
        self.env.add_extension(LoadMacroExtension)
        self.env.add_extension(CallMacroTagExtension)
        self.env.add_extension(JinjaMacroTagsExtension)
        self.env.add_extension(HtmlMacroTagsExtension)
        self.env.autoload_macro_tags = True

    def test_register_macro_from_template(self):
        self.env.macros.register_from_template("macros.html")
        self.assertTrue(self.env.macros.exists("panel"))

    def test_register_macro_from_file(self):
        def register_file():
            self.env.macros.register_file(
                os.path.join(templates_path, "macros.html"))

        self.assertRaises(Exception, register_file)
        self.env.loader = MacroLoader(self.env.loader)
        register_file()
        self.assertTrue(self.env.macros.exists("panel"))

    def test_jinja_macro_tag(self):
        self.env.macros.register_from_template("macros.html")
        tpl = self.env.get_template("jinja_style.html")
        self.assert_html(tpl.render())

    def test_html_macro_tag(self):
        self.env.macros.register_from_template("macros.html")
        tpl = self.env.get_template("html_style.html")
        self.assert_html(tpl.render())

    def assert_html(self, html):
        self.assertIn('<div class="panel-title">test panel</div>', html)
        self.assertIn(
            '<div><button type="button" class="btn btn-default">click me</button></div>',
            html)
        self.assertIn(
            '<button type="button" class="btn btn-primary">click me</button>',
            html)
Ejemplo n.º 32
0
class Template():
    """Class to handle templates."""
    def __init__(self, logya_inst):
        """Initialize template environment."""

        self.vars = {}
        self.dir_templates = logya_inst.dir_templates
        self.env = Environment(loader=TemplateLoader(self.dir_templates))

        # Enable break and continue in templates.
        self.env.add_extension('jinja2.ext.loopcontrols')

        # Enable with statement for nested variable scopes.
        self.env.add_extension('jinja2.ext.with_')

        # Enable expression-statement extension that adds the do tag.
        self.env.add_extension('jinja2.ext.do')

        # Trim whitespace around template tags if configured.
        tpl_settings = logya_inst.config.get('template')
        if tpl_settings and tpl_settings.get('trim_whitespace'):
            self.env.lstrip_blocks = True
            self.env.trim_blocks = True

        # Return an alphabetical index for a list of collection tuples.
        self.env.globals[
            'collection_index'] = lambda collection, non_ascii_key='_': collection_index(
                collection, non_ascii_key)

        # Return an alphabetical index for a list of doc objects.
        self.env.globals[
            'doc_index'] = lambda docs, attr='title', non_ascii_key='_': doc_index(
                docs, attr, non_ascii_key)

        # Include the source of a file.
        self.env.globals[
            'filesource'] = lambda x, lines=None, raw=False: filesource(
                logya_inst, x, lines=lines, raw=raw)

        # Get a document from its URL.
        self.env.globals['get_doc'] = lambda x: get_doc(logya_inst, x)

        # Filter docs list where the given attribute contains the given value.
        self.env.filters['attr_contains'] = lambda docs, attr, val: [
            doc for doc in docs if attr in doc and val in doc[attr]
        ]

        # Create slugs for use in URLs
        self.env.filters['slugify'] = slugify
Ejemplo n.º 33
0
class Template():
    """Class to handle templates."""
    def __init__(self, logya_inst):
        """Initialize template environment."""

        self.vars = {}
        self.dir_templates = logya_inst.dir_templates
        self.env = Environment(loader=TemplateLoader(self.dir_templates))

        # Enable break and continue in templates.
        self.env.add_extension('jinja2.ext.loopcontrols')

        # Enable with statement for nested variable scopes.
        self.env.add_extension('jinja2.ext.with_')

        # Enable expression-statement extension that adds the do tag.
        self.env.add_extension('jinja2.ext.do')

        # Trim whitespace around template tags if configured.
        tpl_settings = logya_inst.config.get('template')
        if tpl_settings and tpl_settings.get('trim_whitespace'):
            self.env.lstrip_blocks = True
            self.env.trim_blocks = True

        # Include the source of a file.
        self.env.globals[
            'filesource'] = lambda x, lines=None, raw=False: filesource(
                logya_inst, x, lines=lines, raw=raw)

        # Get a document from its URL.
        self.env.globals['get_doc'] = lambda x: get_doc(logya_inst, x)

        # Filter docs list where the given attribute contains the given value.
        self.env.filters['attr_contains'] = lambda docs, attr, val: [
            doc for doc in docs if attr in doc and val in doc[attr]
        ]

        # Create slugs for use in URLs
        self.env.filters['slugify'] = slugify

    def get_page(self, doc, template):
        try:
            page = self.env.get_template(template)
        except TemplateNotFound as err:
            raise TemplateNotFound('Doc: {}\n{}'.format(doc, err))
        return page
Ejemplo n.º 34
0
class Template():
    """Class to handle templates."""

    def __init__(self, logya_inst):
        """Initialize template environment."""

        self.vars = {}
        self.dir_templates = logya_inst.dir_templates
        self.env = Environment(loader=TemplateLoader(self.dir_templates))

        # Enable break and continue in templates.
        self.env.add_extension('jinja2.ext.loopcontrols')

        # Enable with statement for nested variable scopes.
        self.env.add_extension('jinja2.ext.with_')

        # Enable expression-statement extension that adds the do tag.
        self.env.add_extension('jinja2.ext.do')

        # Trim whitespace around template tags if configured.
        tpl_settings = logya_inst.config.get('template')
        if tpl_settings and tpl_settings.get('trim_whitespace'):
            self.env.lstrip_blocks = True
            self.env.trim_blocks = True

        # Include the source of a file.
        self.env.globals['filesource'] = lambda x, lines=None, raw=False: filesource(
            logya_inst, x, lines=lines, raw=raw)

        # Get a document from its URL.
        self.env.globals['get_doc'] = lambda x: get_doc(logya_inst, x)

        # Filter docs list where the given attribute contains the given value.
        self.env.filters['attr_contains'] = lambda docs, attr, val: [
            doc for doc in docs if attr in doc and val in doc[attr]]

        # Create slugs for use in URLs
        self.env.filters['slugify'] = slugify

    def get_page(self, doc, template):
        try:
            page = self.env.get_template(template)
        except TemplateNotFound as err:
            raise TemplateNotFound('Doc: {}\n{}'.format(doc, err))
        return page
Ejemplo n.º 35
0
def environment(**options):
    from django.conf import settings
    from django.urls import translate_url
    from django.utils import translation

    env = Environment(extensions=["jinja2.ext.i18n"], **options)
    env.install_gettext_translations(translation)

    env.globals.update(
        {
            "_": gettext,
            "get_messages": messages.get_messages,
            "settings": settings,
            "static": staticfiles_storage.url,
            "url": reverse,
            "get_language": translation.get_language,
            "translate_url": translate_url,
        }
    )

    from django_zero.config.settings import features

    if features.is_webpack_enabled():
        env.globals.update(
            {
                "assets": assets.get_helper(),
            }
        )

    env.add_extension(DjangoCsrfExtension)
    try:
        from django_includes.jinja2 import DjangoIncludesExtension

        env.add_extension(DjangoIncludesExtension)
    except ImportError:
        pass
    env.add_extension(DjangoUrlExtension)
    env.add_extension(SpacelessExtension)

    return env
Ejemplo n.º 36
0
 def test_extend_late(self):
     env = Environment()
     env.add_extension('jinja2.ext.autoescape')
     t = env.from_string('{% autoescape true %}{{ "<test>" }}{% endautoescape %}')
Ejemplo n.º 37
0
        "tabs": tabs,
        "css_blocks": css_blocks,
        "js_blocks": js_blocks
    }


# Jinja settings

code_dir = os.path.dirname(os.path.abspath(__file__))
html_dir = os.path.join(code_dir, "templates")
loader = FileSystemLoader(html_dir)
env = Environment(loader=loader, autoescape=select_autoescape(['html', 'xml']))
# This add an include option that do not treat the content of the file as a jinja template
env.globals['include_static'] = include_static
# Add {% do <statement> %}
env.add_extension("jinja2.ext.do")

# Parsing fonts, audio and bestiary

params = {}
for root, dirs, filenames in os.walk(os.path.join(html_dir, "../font")):
    for filename in filenames:
        if filename[-4:] == ".ttf":
            with open(os.path.join(root, filename), "rb") as extern_file:
                params[filename.split(".")[0] + "_font"] = base64.b64encode(
                    extern_file.read()).decode()

for root, dirs, filenames in os.walk(os.path.join(html_dir, "../audio")):
    for filename in filenames:
        if filename[-4:] == ".wav":
            with open(os.path.join(root, filename), "rb") as extern_file:
Ejemplo n.º 38
0
    return decorate


def url_for(endpoint, _external=False, **values):
    return local.url_adapter.build(endpoint, values, force_external=_external)


def render_template(template, **context):
    return Response(jinja_env.get_template(template).render(**context),
                    mimetype='text/html')


def scheduler_uptime():
    time_diff = datetime.utcnow() - settings.settings['process_start_time']
    d = {'days': time_diff.days}
    d['hours'], rem = divmod(time_diff.seconds, 3600)
    d['minutes'], d['seconds'] = divmod(rem, 60)
    return '{days:02d}:{hours:02d}:{minutes:02d}:{seconds:02d}'.format(**d)


jinja_env = Environment(loader=FileSystemLoader(TEMPLATE_PATH), autoescape=True)
jinja_env.add_extension('jinja2.ext.do')
jinja_env.globals['url_for'] = url_for
jinja_env.globals['local'] = local
jinja_env.globals['get_current_time'] = lambda: datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S %Z')
jinja_env.globals['scheduler_version'] = lambda: settings.settings['version']
jinja_env.globals['scheduler_uptime'] = scheduler_uptime
jinja_env.globals['mx_processing_context'] = mx_page_context
jinja_env.globals['synergy_process_context'] = context.process_context
jinja_env.filters['jsonify'] = json.dumps
Ejemplo n.º 39
0
else:
    if tuple(map(int, np.__version__.split('.')[:2])) < (1, 8):
        raise ImportError('Numpy v1.8 or later is required.')


_PY3K = PY3K = sys.version_info[0] > 2
PY2K = not PY3K
VERSION = __version__


def is_float(n):
    return isinstance(n, float)

TPLPATH = "{}/templates/".format(os.path.dirname(__file__))
jinja2_ENV = Environment(loader=FileSystemLoader(TPLPATH))
jinja2_ENV.add_extension('jinja2.ext.loopcontrols')
jinja2_ENV.tests['Float'] = is_float
MODELPATH = "{}/models/".format(os.path.dirname(__file__))

data = "{}{}{}{}{}".format(
            sysconfig.get_path('data'),
            os.sep, 'share', os.sep, 'tappm', os.sep, 'data')

__all__ = []

from .dataset_maker import *
from .method_hmm import *

from .utils import *

from .hmm import *
    def render_page(self, root, file):
        input_file = os.path.join(root, file)
        output_file = self.__get_page_filename(input_file)
        partial = yaml.load(open(input_file, "r").read())
        url_root = os.getenv("ROOT_DIRECTORY") or ""
        # if main index page, add version number from VERSION.txt
        if self.__is_main_index(output_file):
            partial['content'] = pystache.render(
                partial['content'], {"version": self.get_version()}
            )
        else:
            partial['pageHeading'] = partial['pageTitle']
            partial['pageTitle'] = (
                partial['pageTitle'] +
                " - Digital Marketplace frontend toolkit"
            )
            if "examples" in partial:
                template_name, template_extension = os.path.splitext(file)
                template_subfolder = root.replace(self.pages_dirname, "").strip("/")
                env = Environment(
                    loader=FileSystemLoader(os.path.join(self.repo_root, "toolkit/templates"))
                )
                env.filters.update({
                    'markdown': markdown_filter,
                })
                # used in `toolkit/templates/summary-table.html` for a conditional import statement
                env.globals['PAGES_BUILDER'] = True
                env.add_extension('jinja2.ext.with_')

                template_file = os.path.join(template_subfolder, template_name + ".html")
                template = None
                # not all of our examples have template files (`_lists.scss`, for example)
                if os.path.isfile(os.path.join(self.repo_root, "toolkit/templates", template_file)):
                    template = env.get_template(template_file)
                examples = []
                for index, example in enumerate(partial["examples"]):
                    grid = partial.get('grid')
                    if isinstance(example, dict):
                        example_template = self.parameters_example(template_subfolder, template_name, example)
                        example_markup = template.render(example)
                        # set a grid if specified. Example-level grids will overwrite the one for the page
                        grid = example.get('grid', partial.get('grid'))
                    else:
                        example_template = example
                        example_markup = env.from_string(example_template).render({})
              
                    examples.append({
                        "markup": example_markup,
                        "highlighted_markup": highlight(example_markup, HtmlLexer(), HtmlFormatter(noclasses=True)),
                        "grid": grid
                    })
                    if template:
                        examples[-1].update(
                            {"parameters": highlight(example_template, DjangoLexer(), HtmlFormatter(noclasses=True))}
                        )
                partial_data = {
                    "examples": examples,
                    "pageTitle": partial['pageTitle'],
                    "pageDescription": partial.get('pageDescription'),
                    "pageHeading": partial['pageHeading'],
                    "templateFile": template_file,
                    "urlRoot": url_root
                }
                if "grid" in partial:
                    partial_data['grid'] = partial['grid']

                partial['content'] = self.render_include(
                    os.path.join(self.repo_root, "pages_builder", "includes", "content.html"),
                    partial_data
                )

        partial['head'] = self.render_include(
            os.path.join(self.repo_root, "pages_builder", "includes", "head.html"),
            {"url_root": url_root}
        )
        bodyEnd = partial['bodyEnd'] if "bodyEnd" in partial else ""
        partial['bodyEnd'] = self.render_include(
            os.path.join(self.repo_root, "pages_builder", "includes", "bodyEnd.html"),
            {
                "url_root": url_root,
                "bodyEnd": bodyEnd
            }
        )
        page_render = pystache.render(self.template_view, partial)
        print "\n  " + input_file
        print "▸ " + output_file
        open(output_file, "w+").write(page_render.encode('utf-8'))
Ejemplo n.º 41
0
        # Memorydata
        host_data['mem_data'] = data.ix[:, 20].apply(lambda x: x / 1024000).to_json(date_format='iso')

        # Disk data
        host_data['disk_read'] = data.ix[:, 66].apply(lambda x: x / 1024).to_json(date_format='iso')
        host_data['disk_write'] = data.ix[:, 67].apply(lambda x: x / 1024).to_json(date_format='iso')

        # Network Data
        host_data['net_rx'] = data.ix[:, 57].to_json(date_format='iso')
        host_data['net_tx'] = data.ix[:, 58].to_json(date_format='iso')

        hosts.append(host_data)

    env = Environment(loader=FileSystemLoader('templates'))
    env.add_extension("chartkick.ext.charts")

    cpu_template = env.get_template('cpu_template.html')
    memory_template = env.get_template('memory_template.html')
    disk_template = env.get_template('disk_template.html')
    network_template = env.get_template('network_template.html')

    cpu_output = cpu_template.render(
            hosts=sorted(hosts, key=itemgetter('name'), reverse=True),
    )
    memory_output = memory_template.render(
            hosts=sorted(hosts, key=itemgetter('name'), reverse=True),
    )
    disk_output = disk_template.render(
            hosts=sorted(hosts, key=itemgetter('name'), reverse=True),
    )
Ejemplo n.º 42
0
class Site:
    def __init__(self):
        self.cfg = Config()
        self.url = self.cfg.url
        self.title = self.cfg.title
        self.author = self.cfg.author
        self.loader = FileSystemLoader(ROOT_PATH)
        self.env = Environment(loader=self.loader)
        for ttag in templatetags:
            self.env.add_extension(ttag)
        self.env.add_extension(SkipBlockExtension)
        for tfunc in functions:
            self.env.globals[tfunc.__name__] = tfunc
        for tfilter in filters:
            self.env.filters[tfilter.__name__] = tfilter

        self.env.globals['site'] = self
        self.entries = []
        self.url_lookup = {}
        self.url_loopup_reverse = {}
        self.dest = os.path.join(os.path.abspath(ROOT_PATH), '_build')

    def exclude_path(self, path):
        for exc in self.cfg.exclude:
            if exc.search(path):
                return True
        return False

    def process_path(self, path):
        for rule, repl in self.cfg.rules:
            match = rule.match(path)
            if match:
                return rule.sub(repl, path), match
        return path, None

    def traverse(self):
        for root, _, files in os.walk(ROOT_PATH):
            for f in files:
                path = os.path.join(root, f)
                path = os.path.relpath(path, ROOT_PATH)

                if self.exclude_path(path):
                    continue

                url, match = self.process_path(path)
                print(path, '->', url)

                self.add_page(path, url, match)

    def add_page(self, path, url, match):
        entryclass = match and Entry or StaticEntry
        entry = entryclass(self, path, url)
        self.entries.append(entry)
        self.url_lookup[path] = entry
        self.url_loopup_reverse[url] = entry
        if match:
            url_settings = match.groupdict()
            entry.settings.update(url_settings)
        return entry

    @property
    def posts(self):
        posts = [p for p in self.entries if p.path == 'blog/index.html']
        if not posts:
            return None
        return posts[0].children

    def render_meta(self):
        for entry in self.entries:
            entry.patch()
        for entry in self.entries:
            print('Render meta', entry)
            entry.render_meta()

    def render(self):
        self.env.cache.clear()
        self.env.skip_blocks.append('meta')
        for entry in self.entries:
            print('Render', entry)
            entry.render()
Ejemplo n.º 43
0
class TestTemplateTag(object):
    def setup(self):
        # Setup the assets environment.
        assets_env = AssetsEnvironment('', '')
        self.foo_bundle = Bundle()
        self.bar_bundle = Bundle()
        assets_env.register('foo_bundle', self.foo_bundle)
        assets_env.register('bar_bundle', self.bar_bundle)

        # Inject a mock bundle class into the Jinja2 extension, so we
        # can check on what exactly it does.
        test_instance = self

        class MockBundle(Bundle):
            urls_to_fake = ['foo']

            def __init__(self, *a, **kw):
                Bundle.__init__(self, *a, **kw)
                self.env = assets_env
                # Kind of hacky, but gives us access to the last Bundle
                # instance used by the extension.
                test_instance.the_bundle = self

            def urls(self, *a, **kw):
                return self.urls_to_fake

        self._old_bundle_class = AssetsExtension.BundleClass
        AssetsExtension.BundleClass = self.BundleClass = MockBundle

        # Setup the Jinja2 environment.
        self.jinja_env = JinjaEnvironment()
        self.jinja_env.add_extension(AssetsExtension)
        self.jinja_env.assets_environment = assets_env

    def teardown(self):
        AssetsExtension.BundleClass = self._old_bundle_class
        del self._old_bundle_class

    def render_template(self, args, ctx={}):
        return self.jinja_env.from_string(
            '{% assets ' + args +
            ' %}{{ ASSET_URL }};{% endassets %}').render(ctx)

    def test_reference_bundles(self):
        self.render_template('"foo_bundle", "bar_bundle"')
        assert self.the_bundle.contents == (self.foo_bundle, self.bar_bundle)

    def test_reference_files(self):
        self.render_template('"file1", "file2", "file3"')
        assert self.the_bundle.contents == (
            'file1',
            'file2',
            'file3',
        )

    def test_reference_mixed(self):
        self.render_template('"foo_bundle", "file2", "file3"')
        assert self.the_bundle.contents == (
            self.foo_bundle,
            'file2',
            'file3',
        )

    def test_with_vars(self):
        self.render_template('var1, var2', {
            'var1': self.foo_bundle,
            'var2': 'a_file'
        })
        assert self.the_bundle.contents == (
            self.foo_bundle,
            'a_file',
        )

    def test_output_urls(self):
        """Ensure the tag correcly spits out the urls the bundle returns.
        """
        self.BundleClass.urls_to_fake = ['foo', 'bar']
        assert self.render_template('"file1" "file2" "file3"') == 'foo;bar;'
Ejemplo n.º 44
0
class Render(object):
    """
		The core jinja2 render.

		This is the bridge between your ViUR modules and your templates.
		First, the default jinja2-api is exposed to your templates. See http://jinja.pocoo.org/ for
		more information. Second, we'll pass data das global variables to templates depending on the
		current action.

			- For list() we'll pass `skellist` - a :py:class:`server.render.jinja2.default.SkelListWrapper` instance
			- For view(): skel - a dictionary with values from the skeleton prepared for use inside html
			- For add()/edit: a dictionary as `skel` with `values`, `structure` and `errors` as keys.

		Third, a bunch of global filters (like urlencode) and functions (getEntry, ..) are available  to templates.

		See the ViUR Documentation for more information about functions and data available to jinja2 templates.

		Its possible for modules to extend the list of filters/functions available to templates by defining
		a function called `jinjaEnv`. Its called from the render when the environment is first created and
		can extend/override the functionality exposed to templates.

	"""
    kind = "html"
    listTemplate = "list"
    viewTemplate = "view"
    addTemplate = "add"
    editTemplate = "edit"
    addSuccessTemplate = "add_success"
    editSuccessTemplate = "edit_success"
    deleteSuccessTemplate = "delete_success"
    listRepositoriesTemplate = "list_repositories"
    listRootNodeContentsTemplate = "list_rootNode_contents"
    addDirSuccessTemplate = "add_dir_success"
    renameSuccessTemplate = "rename_success"
    copySuccessTemplate = "copy_success"

    reparentSuccessTemplate = "reparent_success"
    setIndexSuccessTemplate = "setindex_success"
    cloneSuccessTemplate = "clone_success"

    __haveEnvImported_ = False

    def __init__(self, parent=None, *args, **kwargs):
        super(Render, self).__init__(*args, **kwargs)
        if not Render.__haveEnvImported_:
            # We defer loading our plugins to this point to avoid circular imports
            # noinspection PyUnresolvedReferences
            from . import env
            Render.__haveEnvImported_ = True
        self.parent = parent

    def getTemplateFileName(self, template, ignoreStyle=False):
        """
			Returns the filename of the template.

			This function decides in which language and which style a given template is rendered.
			The style is provided as get-parameters for special-case templates that differ from
			their usual way.

			It is advised to override this function in case that
			:func:`server.render.jinja2.default.Render.getLoaders` is redefined.

			:param template: The basename of the template to use.
			:type template: str

			:param ignoreStyle: Ignore any maybe given style hints.
			:type ignoreStyle: bool

			:returns: Filename of the template
			:rtype: str
		"""
        validChars = "abcdefghijklmnopqrstuvwxyz1234567890-"
        if "htmlpath" in dir(self):
            htmlpath = self.htmlpath
        else:
            htmlpath = "html"
        currReq = currentRequest.get()
        if not ignoreStyle \
         and "style" in currReq.kwargs \
         and all([x in validChars for x in currReq.kwargs["style"].lower()]):
            stylePostfix = "_" + currReq.kwargs["style"]
        else:
            stylePostfix = ""
        lang = currentLanguage.get()  # session.current.getLanguage()
        fnames = [template + stylePostfix + ".html", template + ".html"]
        if lang:
            fnames = [
                os.path.join(lang, template + stylePostfix + ".html"),
                template + stylePostfix + ".html",
                os.path.join(lang, template + ".html"), template + ".html"
            ]
        for fn in fnames:  # check subfolders
            prefix = template.split("_")[0]
            if os.path.isfile(
                    os.path.join(utils.projectBasePath, htmlpath, prefix, fn)):
                return ("%s/%s" % (prefix, fn))
        for fn in fnames:  # Check the templatefolder of the application
            if os.path.isfile(os.path.join(utils.projectBasePath, htmlpath,
                                           fn)):
                return fn
        for fn in fnames:  # Check the fallback
            if os.path.isfile(
                    os.path.join(utils.projectBasePath, "viur", "core",
                                 "template", fn)):
                return fn
        raise errors.NotFound("Template %s not found." % template)

    def getLoaders(self):
        """
			Return the list of Jinja2 loaders which should be used.

			May be overridden to provide an alternative loader
			(e.g. for fetching templates from the datastore).
		"""
        if "htmlpath" in dir(self):
            htmlpath = self.htmlpath
        else:
            htmlpath = "html/"

        return ChoiceLoader([
            FileSystemLoader(htmlpath),
            FileSystemLoader("viur/core/template/")
        ])

    def renderBoneStructure(self, bone):
        """
		Renders the structure of a bone.

		This function is used by :func:`renderSkelStructure`.
		can be overridden and super-called from a custom renderer.

		:param bone: The bone which structure should be rendered.
		:type bone: Any bone that inherits from :class:`server.bones.base.baseBone`.

		:return: A dict containing the rendered attributes.
		:rtype: dict
		"""

        # Base bone contents.
        ret = {
            "descr": str(bone.descr),
            "type": bone.type,
            "required": bone.required,
            "multiple": bone.multiple,
            "params": bone.params,
            "visible": bone.visible,
            "readOnly": bone.readOnly
        }

        if bone.type == "relational" or bone.type.startswith("relational."):
            ret.update({
                "module":
                bone.module,
                "format":
                bone.format,
                "using":
                self.renderSkelStructure(bone.using()) if bone.using else None,
                "relskel":
                self.renderSkelStructure(bone._refSkelCache())
            })

        elif bone.type == "record" or bone.type.startswith("record."):
            ret.update({
                "format": bone.format,
                "using": self.renderSkelStructure(bone.using())
            })

        elif bone.type == "select" or bone.type.startswith("select."):
            ret.update({
                "values": {k: str(v)
                           for (k, v) in bone.values.items()},
                "multiple": bone.multiple
            })

        elif bone.type == "date" or bone.type.startswith("date."):
            ret.update({"date": bone.date, "time": bone.time})

        elif bone.type == "numeric" or bone.type.startswith("numeric."):
            ret.update({
                "precision": bone.precision,
                "min": bone.min,
                "max": bone.max
            })

        elif bone.type == "text" or bone.type.startswith("text."):
            ret.update({
                "validHtml": bone.validHtml,
                "languages": bone.languages
            })

        elif bone.type == "str" or bone.type.startswith("str."):
            ret.update({
                "languages": bone.languages,
                "multiple": bone.multiple
            })
        elif bone.type == "captcha" or bone.type.startswith("captcha."):
            ret.update({
                "publicKey": bone.publicKey,
            })

        return ret

    def renderSkelStructure(self, skel):
        """
			Dumps the structure of a :class:`server.db.skeleton.Skeleton`.

			:param skel: Skeleton which structure will be processed.
			:type skel: server.db.skeleton.Skeleton

			:returns: The rendered dictionary.
			:rtype: dict
		"""
        res = OrderedDict()

        for key, bone in skel.items():
            if "__" in key or not isinstance(bone, baseBone):
                continue

            res[key] = self.renderBoneStructure(bone)

            if key in skel.errors:
                res[key]["error"] = skel.errors[key]
            else:
                res[key]["error"] = None

        return res

    def renderBoneValue(self,
                        bone,
                        skel,
                        key,
                        boneValue,
                        isLanguageWrapped: bool = False):
        """
		Renders the value of a bone.

		This function is used by :func:`collectSkelData`.
		It can be overridden and super-called from a custom renderer.

		:param bone: The bone which value should be rendered.
		:type bone: Any bone that inherits from :class:`server.bones.base.baseBone`.

		:return: A dict containing the rendered attributes.
		:rtype: dict
		"""
        if bone.languages and not isLanguageWrapped:
            res = LanguageWrapper(bone.languages)
            if isinstance(boneValue, dict):
                for language in bone.languages:
                    if language in boneValue:
                        res[language] = self.renderBoneValue(
                            bone, skel, key, boneValue[language], True)
            return res
        elif bone.type == "select" or bone.type.startswith("select."):
            skelValue = boneValue
            if isinstance(skelValue, list):
                return {val: bone.values.get(val, val) for val in boneValue}
            elif skelValue in bone.values:
                return KeyValueWrapper(skelValue, bone.values[skelValue])
            return KeyValueWrapper(skelValue, str(skelValue))

        elif bone.type == "relational" or bone.type.startswith("relational."):
            if isinstance(boneValue, list):
                tmpList = []
                for k in boneValue:
                    if not k:
                        continue
                    if bone.using is not None and k["rel"]:
                        k["rel"].renderPreparation = self.renderBoneValue
                        usingData = k["rel"]
                    else:
                        usingData = None
                    k["dest"].renderPreparation = self.renderBoneValue
                    tmpList.append({"dest": k["dest"], "rel": usingData})
                return tmpList
            elif isinstance(boneValue, dict):
                if bone.using is not None and boneValue["rel"]:
                    boneValue["rel"].renderPreparation = self.renderBoneValue
                    usingData = boneValue["rel"]
                else:
                    usingData = None
                boneValue["dest"].renderPreparation = self.renderBoneValue
                return {"dest": boneValue["dest"], "rel": usingData}
        elif bone.type == "record" or bone.type.startswith("record."):
            value = boneValue
            if value:
                if bone.multiple:
                    ret = []
                    for entry in value:
                        entry.renderPreparation = self.renderBoneValue
                        ret.append(entry)
                    return ret
                value.renderPreparation = self.renderBoneValue
                return value
        elif bone.type == "password":
            return ""
        elif bone.type == "key":
            return db.encodeKey(boneValue) if boneValue else None

        else:
            return boneValue

        return None

    def collectSkelData(self, skel):
        """
			Prepares values of one :class:`server.db.skeleton.Skeleton` or a list of skeletons for output.

			:param skel: Skeleton which contents will be processed.
			:type skel: server.db.skeleton.Skeleton

			:returns: A dictionary or list of dictionaries.
			:rtype: dict | list

			 .. deprecated:: 3.0.0
				This method is deprecated since ViUR 3.0. Instead, attach a renderPreparation method to the skeleton
				and pass the skeleton itself.
		"""
        # logging.error("collectSkelData %s", skel)
        if isinstance(skel, list):
            return [self.collectSkelData(x) for x in skel]
        res = {}
        for key, bone in skel.items():
            val = self.renderBoneValue(bone, skel, key, skel[key])
            res[key] = val
            if isinstance(res[key], list):
                res[key] = ListWrapper(res[key])
        return res

    def add(self, skel, tpl=None, params=None, *args, **kwargs):
        """
			Renders a page for adding an entry.

			The template must construct the HTML-form on itself; the required information
			are passed via skel.structure, skel.value and skel.errors.

			A jinja2-macro, which builds such kind of forms, is shipped with the server.

			Any data in \*\*kwargs is passed unmodified to the template.

			:param skel: Skeleton of the entry which should be created.
			:type skel: server.db.skeleton.Skeleton

			:param tpl: Name of a different template, which should be used instead of the default one.
			:type tpl: str

			:param params: Optional data that will be passed unmodified to the template
			:type params: object

			:return: Returns the emitted HTML response.
			:rtype: str
		"""
        if not tpl and "addTemplate" in dir(self.parent):
            tpl = self.parent.addTemplate

        tpl = tpl or self.addTemplate
        template = self.getEnv().get_template(self.getTemplateFileName(tpl))
        skeybone = baseBone(descr="SecurityKey", readOnly=True, visible=False)
        skel.skey = skeybone
        skel["skey"] = securitykey.create()
        if currentRequest.get().kwargs.get("nomissing") == "1":
            if isinstance(skel, SkeletonInstance):
                super(SkeletonInstance, skel).__setattr__("errors", [])
        skel.renderPreparation = self.renderBoneValue
        return template.render(skel={
            "structure": self.renderSkelStructure(skel),
            "errors": skel.errors,
            "value": skel
        },
                               params=params,
                               **kwargs)

    def edit(self, skel, tpl=None, params=None, **kwargs):
        """
			Renders a page for modifying an entry.

			The template must construct the HTML-form on itself; the required information
			are passed via skel.structure, skel.value and skel.errors.

			A jinja2-macro, which builds such kind of forms, is shipped with the server.

			Any data in \*\*kwargs is passed unmodified to the template.

			:param skel: Skeleton of the entry which should be modified.
			:type skel: server.db.skeleton.Skeleton

			:param tpl: Name of a different template, which should be used instead of the default one.
			:type tpl: str

			:param params: Optional data that will be passed unmodified to the template
			:type params: object

			:return: Returns the emitted HTML response.
			:rtype: str
		"""
        if not tpl and "editTemplate" in dir(self.parent):
            tpl = self.parent.editTemplate

        tpl = tpl or self.editTemplate
        template = self.getEnv().get_template(self.getTemplateFileName(tpl))
        skeybone = baseBone(descr="SecurityKey", readOnly=True, visible=False)
        skel.skey = skeybone
        skel["skey"] = securitykey.create()

        if currentRequest.get().kwargs.get("nomissing") == "1":
            if isinstance(skel, SkeletonInstance):
                super(SkeletonInstance, skel).__setattr__("errors", [])
        skel.renderPreparation = self.renderBoneValue
        return template.render(skel={
            "structure": self.renderSkelStructure(skel),
            "errors": skel.errors,
            "value": skel
        },
                               params=params,
                               **kwargs)

    def addSuccess(self, skel, tpl=None, params=None, *args, **kwargs):
        """
			Renders a page, informing that the entry has been successfully created.

			:param skel: Skeleton which contains the data of the new entity
			:type skel: server.db.skeleton.Skeleton

			:param tpl: Name of a different template, which should be used instead of the default one.
			:type tpl: str

			:param params: Optional data that will be passed unmodified to the template
			:type params: object

			:return: Returns the emitted HTML response.
			:rtype: str
		"""
        if not tpl:
            if "addSuccessTemplate" in dir(self.parent):
                tpl = self.parent.addSuccessTemplate
            else:
                tpl = self.addSuccessTemplate

        template = self.getEnv().get_template(self.getTemplateFileName(tpl))
        if isinstance(skel, SkeletonInstance):
            skel.renderPreparation = self.renderBoneValue
        return template.render({"skel": skel}, params=params, **kwargs)

    def editSuccess(self, skel, tpl=None, params=None, *args, **kwargs):
        """
			Renders a page, informing that the entry has been successfully modified.

			:param skel: Skeleton which contains the data of the modified entity
			:type skel: server.db.skeleton.Skeleton

			:param tpl: Name of a different template, which should be used instead of the default one.
			:type tpl: str

			:param params: Optional data that will be passed unmodified to the template
			:type params: object

			:return: Returns the emitted HTML response.
			:rtype: str
		"""
        if not tpl:
            if "editSuccessTemplate" in dir(self.parent):
                tpl = self.parent.editSuccessTemplate
            else:
                tpl = self.editSuccessTemplate

        template = self.getEnv().get_template(self.getTemplateFileName(tpl))
        if isinstance(skel, SkeletonInstance):
            skel.renderPreparation = self.renderBoneValue
        return template.render(skel=skel, params=params, **kwargs)

    def deleteSuccess(self, skel, tpl=None, params=None, *args, **kwargs):
        """
			Renders a page, informing that the entry has been successfully deleted.

			The provided parameters depend on the application calling this:
			List and Hierarchy pass the id of the deleted entry, while Tree passes
			the rootNode and path.

			:param params: Optional data that will be passed unmodified to the template
			:type params: object

			:param tpl: Name of a different template, which should be used instead of the default one.
			:type tpl: str

			:return: Returns the emitted HTML response.
			:rtype: str
		"""
        if not tpl:
            if "deleteSuccessTemplate" in dir(self.parent):
                tpl = self.parent.deleteSuccessTemplate
            else:
                tpl = self.deleteSuccessTemplate

        template = self.getEnv().get_template(self.getTemplateFileName(tpl))
        return template.render(params=params, **kwargs)

    def list(self, skellist, tpl=None, params=None, **kwargs):
        """
			Renders a list of entries.

			Any data in \*\*kwargs is passed unmodified to the template.

			:param skellist: List of Skeletons with entries to display.
			:type skellist: server.db.skeleton.SkelList

			:param tpl: Name of a different template, which should be used instead of the default one.
			:param: tpl: str

			:param params: Optional data that will be passed unmodified to the template
			:type params: object

			:return: Returns the emitted HTML response.
			:rtype: str
		"""
        if not tpl and "listTemplate" in dir(self.parent):
            tpl = self.parent.listTemplate
        tpl = tpl or self.listTemplate
        try:
            fn = self.getTemplateFileName(tpl)
        except errors.HTTPException as e:  # Not found - try default fallbacks
            tpl = "list"
        template = self.getEnv().get_template(self.getTemplateFileName(tpl))
        for skel in skellist:
            skel.renderPreparation = self.renderBoneValue
        return template.render(skellist=skellist, params=params,
                               **kwargs)  # SkelListWrapper(resList, skellist)

    def listRootNodes(self, repos, tpl=None, params=None, **kwargs):
        """
			Renders a list of available repositories.

			:param repos: List of repositories (dict with "key"=>Repo-Key and "name"=>Repo-Name)
			:type repos: list

			:param tpl: Name of a different template, which should be used instead of the default one.
			:param: tpl: str

			:param params: Optional data that will be passed unmodified to the template
			:type params: object

			:return: Returns the emitted HTML response.
			:rtype: str
		"""
        if "listRepositoriesTemplate" in dir(self.parent):
            tpl = tpl or self.parent.listTemplate
        if not tpl:
            tpl = self.listRepositoriesTemplate
        try:
            fn = self.getTemplateFileName(tpl)
        except errors.HTTPException as e:  # Not found - try default fallbacks
            tpl = "list"
        template = self.getEnv().get_template(self.getTemplateFileName(tpl))
        return template.render(repos=repos, params=params, **kwargs)

    def view(self, skel, tpl=None, params=None, **kwargs):
        """
			Renders a single entry.

			Any data in \*\*kwargs is passed unmodified to the template.

			:param skel: Skeleton to be displayed.
			:type skellist: server.db.skeleton.Skeleton

			:param tpl: Name of a different template, which should be used instead of the default one.
			:param: tpl: str

			:param params: Optional data that will be passed unmodified to the template
			:type params: object

			:return: Returns the emitted HTML response.
			:rtype: str
		"""
        if not tpl and "viewTemplate" in dir(self.parent):
            tpl = self.parent.viewTemplate

        tpl = tpl or self.viewTemplate
        template = self.getEnv().get_template(self.getTemplateFileName(tpl))

        if isinstance(skel, SkeletonInstance):
            skel.renderPreparation = self.renderBoneValue
        return template.render(skel=skel, params=params, **kwargs)

    ## Extended functionality for the Tree-Application ##
    def listRootNodeContents(self,
                             subdirs,
                             entries,
                             tpl=None,
                             params=None,
                             **kwargs):
        """
			Renders the contents of a given RootNode.

			This differs from list(), as one level in the tree-application may contains two different
			child-types: Entries and folders.

			:param subdirs: List of (sub-)directories on the current level
			:type repos: list

			:param entries: List of entries of the current level
			:type entries: server.db.skeleton.SkelList

			:param tpl: Name of a different template, which should be used instead of the default one
			:param: tpl: str

			:param params: Optional data that will be passed unmodified to the template
			:type params: object

			:return: Returns the emitted HTML response.
			:rtype: str
		"""
        if "listRootNodeContentsTemplate" in dir(self.parent):
            tpl = tpl or self.parent.listRootNodeContentsTemplate
        else:
            tpl = tpl or self.listRootNodeContentsTemplate
        template = self.getEnv().get_template(self.getTemplateFileName(tpl))
        return template.render(
            subdirs=subdirs,
            entries=[self.collectSkelData(x) for x in entries],
            params=params,
            **kwargs)

    def addDirSuccess(self,
                      rootNode,
                      path,
                      dirname,
                      params=None,
                      *args,
                      **kwargs):
        """
			Renders a page, informing that the directory has been successfully created.

			:param rootNode: RootNode-key in which the directory has been created
			:type rootNode: str

			:param path: Path in which the directory has been created
			:type path: str

			:param dirname: Name of the newly created directory
			:type dirname: str

			:param params: Optional data that will be passed unmodified to the template
			:type params: object

			:return: Returns the emitted HTML response.
			:rtype: str
		"""

        tpl = self.addDirSuccessTemplate
        if "addDirSuccessTemplate" in dir(self.parent):
            tpl = self.parent.addDirSuccessTemplate
        template = self.getEnv().get_template(self.getTemplateFileName(tpl))
        return template.render(rootNode=rootNode,
                               path=path,
                               dirname=dirname,
                               params=params)

    def renameSuccess(self,
                      rootNode,
                      path,
                      src,
                      dest,
                      params=None,
                      *args,
                      **kwargs):
        """
			Renders a page, informing that the entry has been successfully renamed.

			:param rootNode: RootNode-key in which the entry has been renamed
			:type rootNode: str

			:param path: Path in which the entry has been renamed
			:type path: str

			:param src: Old name of the entry
			:type src: str

			:param dest: New name of the entry
			:type dest: str

			:param params: Optional data that will be passed unmodified to the template
			:type params: object

			:return: Returns the emitted HTML response.
			:rtype: str
		"""
        tpl = self.renameSuccessTemplate
        if "renameSuccessTemplate" in dir(self.parent):
            tpl = self.parent.renameSuccessTemplate
        template = self.getEnv().get_template(self.getTemplateFileName(tpl))
        return template.render(rootNode=rootNode,
                               path=path,
                               src=src,
                               dest=dest,
                               params=params)

    def copySuccess(self,
                    srcrepo,
                    srcpath,
                    name,
                    destrepo,
                    destpath,
                    type,
                    deleteold,
                    params=None,
                    *args,
                    **kwargs):
        """
			Renders a page, informing that an entry has been successfully copied/moved.

			:param srcrepo: RootNode-key from which has been copied/moved
			:type srcrepo: str

			:param srcpath: Path from which the entry has been copied/moved
			:type srcpath: str

			:param name: Name of the entry which has been copied/moved
			:type name: str

			:param destrepo: RootNode-key to which has been copied/moved
			:type destrepo: str

			:param destpath: Path to which the entries has been copied/moved
			:type destpath: str

			:param type: "entry": Copy/Move an entry, everything else: Copy/Move an directory
			:type type: str

			:param deleteold: "0": Copy, "1": Move
			:type deleteold: str

			:param params: Optional data that will be passed unmodified to the template
			:type params: object

			:return: Returns the emitted HTML response.
			:rtype: str
		"""
        tpl = self.copySuccessTemplate
        if "copySuccessTemplate" in dir(self.parent):
            tpl = self.parent.copySuccessTemplate
        template = self.getEnv().get_template(self.getTemplateFileName(tpl))
        return template.render(srcrepo=srcrepo,
                               srcpath=srcpath,
                               name=name,
                               destrepo=destrepo,
                               destpath=destpath,
                               type=type,
                               deleteold=deleteold,
                               params=params)

    def reparentSuccess(self, obj, tpl=None, params=None, **kwargs):
        """
			Renders a page informing that the item was successfully moved.

			:param obj: ndb.Expando instance of the item that was moved.
			:type obj: ndb.Expando

			:param tpl: Name of a different template, which should be used instead of the default one
			:type tpl: str

			:param params: Optional data that will be passed unmodified to the template
			:type params: object
		"""
        if not tpl:
            if "reparentSuccessTemplate" in dir(self.parent):
                tpl = self.parent.reparentSuccessTemplate
            else:
                tpl = self.reparentSuccessTemplate

        template = self.getEnv().get_template(self.getTemplateFileName(tpl))
        return template.render(repoObj=obj, params=params, **kwargs)

    def setIndexSuccess(self, obj, tpl=None, params=None, *args, **kwargs):
        """
			Renders a page informing that the items sortindex was successfully changed.

			:param obj: ndb.Expando instance of the item that was changed
			:type obj: ndb.Expando

			:param tpl: Name of a different template, which should be used instead of the default one
			:type tpl: str

			:param params: Optional data that will be passed unmodified to the template
			:type params: object

			:return: Returns the emitted HTML response.
			:rtype: str
		"""
        if not tpl:
            if "setIndexSuccessTemplate" in dir(self.parent):
                tpl = self.parent.setIndexSuccessTemplate
            else:
                tpl = self.setIndexSuccessTemplate

        template = self.getEnv().get_template(self.getTemplateFileName(tpl))
        return template.render(skel=obj, repoObj=obj, params=params, **kwargs)

    def cloneSuccess(self, tpl=None, params=None, *args, **kwargs):
        """
			Renders a page informing that the items sortindex was successfully changed.

			:param obj: ndb.Expando instance of the item that was changed
			:type obj: ndb.Expando

			:param tpl: Name of a different template, which should be used instead of the default one
			:type tpl: str

			:param params: Optional data that will be passed unmodified to the template
			:type params: object

			:return: Returns the emitted HTML response.
			:rtype: str
		"""
        if not tpl:
            if "cloneSuccessTemplate" in dir(self.parent):
                tpl = self.parent.cloneSuccessTemplate
            else:
                tpl = self.cloneSuccessTemplate

        template = self.getEnv().get_template(self.getTemplateFileName(tpl))
        return template.render(params=params, **kwargs)

    def renderEmail(self,
                    dests: List[str],
                    file: str = None,
                    template: str = None,
                    skel: Union[None, Dict, SkeletonInstance,
                                List[SkeletonInstance]] = None,
                    **kwargs) -> Tuple[str, str]:
        """
			Renders an email.
			Uses the first not-empty line as subject and the remaining template as body.

			:param dests: Destination recipients.
			:param file: The name of a template from the deploy/emails directory.
			:param template: This string is interpreted as the template contents. Alternative to load from template file.
			:param skel: Skeleton or dict which data to supply to the template.
			:return: Returns the rendered email subject and body.
		"""
        if isinstance(skel, SkeletonInstance):
            skel.renderPreparation = self.renderBoneValue
        elif isinstance(skel, list) and all(
            [isinstance(x, SkeletonInstance) for x in skel]):
            for x in skel:
                x.renderPreparation = self.renderBoneValue
        if file is not None:
            try:
                tpl = self.getEnv().from_string(
                    codecs.open("emails/" + file + ".email", "r",
                                "utf-8").read())
            except Exception as err:
                logging.exception(err)
                tpl = self.getEnv().get_template(file + ".email")
        else:
            tpl = self.getEnv().from_string(template)
        content = tpl.render(skel=skel, dests=dests,
                             **kwargs).lstrip().splitlines()
        if len(content) == 1:
            content.insert(0, "")  # add empty subject
        return content[0], os.linesep.join(content[1:]).lstrip()

    def getEnv(self):
        """
			Constucts the Jinja2 environment.

			If an application specifies an jinja2Env function, this function
			can alter the environment before its used to parse any template.

			:returns: Extended Jinja2 environment.
			:rtype: jinja2.Environment
		"""
        def mkLambda(func, s):
            return lambda *args, **kwargs: func(s, *args, **kwargs)

        if not "env" in dir(self):
            loaders = self.getLoaders()
            self.env = Environment(loader=loaders,
                                   extensions=[
                                       "jinja2.ext.do",
                                       "jinja2.ext.loopcontrols",
                                       TranslationExtension
                                   ])
            self.env.trCache = {}

            # Import functions.
            for name, func in jinjaUtils.getGlobalFunctions().items():
                self.env.globals[name] = mkLambda(func, self)

            # Import filters.
            for name, func in jinjaUtils.getGlobalFilters().items():
                self.env.filters[name] = mkLambda(func, self)

            # Import extensions.
            for ext in jinjaUtils.getGlobalExtensions():
                self.env.add_extension(ext)

            # Import module-specific environment, if available.
            if "jinjaEnv" in dir(self.parent):
                self.env = self.parent.jinjaEnv(self.env)

        return self.env
Ejemplo n.º 45
0
class TestJinjaExtension(TempdirHelper):

    default_files = {
        'static/buttons.css': """
            /*
            A classic form button.

            :hover - Highlights on hover.
            :disabled - Mute inactive buttons.

            Styleguide 1.1
            */
            .button {}
        """,
        'static/minibuttons.css': """
            /*
            A mini button.

            :hover - Highlights on hover.
            :disabled - Mute inactive buttons.

            Styleguide 1.2
            */
            .minibutton {}
        """,
        'templates/custom.html': "{{ section.section }}{{ section.example }}\n\n",
    }

    def setup(self):
        super(TestJinjaExtension, self).setup()

        self.jinja_env = Environment()
        self.jinja_env.add_extension(StyleguideExtension)
        self.jinja_env.loader = FileSystemLoader('%s/templates' % self.tempdir)
        self.jinja_env.styleguide_kss_parser = Parser('%s/static' % self.tempdir)
        self.jinja_env.styleguide_template_name = 'custom.html'

    def render_template(self, ref, example=''):
        template_string = (
            "{%% styleguide %(ref)s %%}"
            "%(example)s"
            "{%% endstyleguide %%}"
        ) % dict(ref=ref, example=example)

        template = self.jinja_env.from_string(template_string)
        return template.render()

    def test_render_section(self):
        assert self.render_template('1.1') == '1.1\n'

    def test_render_subsections_as_well(self):
        assert self.render_template('1') == '1.1\n1.2\n'

    def test_render_when_section_does_not_exist(self):
        assert self.render_template('0') == ''

    def test_kss_parser_caches_output(self):
        assert self.render_template('1') == '1.1\n1.2\n'

        self.create_files(('static/buttons.css', 'static/minibuttons.css'))
        assert self.render_template('1') == '1.1\n1.2\n'

    def test_dedent_example(self):
        example_html = """
        <button>
            <i></i>
        </button>
        """
        expected_html = textwrap.dedent(example_html)
        assert self.render_template('1.1', example_html) == '1.1%s\n' % expected_html
Ejemplo n.º 46
0
 def _render_from_settings(self, setting_name):
     setting = getattr(self._settings, setting_name)
     env = Environment()
     env.add_extension('jinja2.ext.autoescape')
     t = env.from_string(setting)
     return t.render(self._template_vars())
Ejemplo n.º 47
0
import os
from jinja2 import Environment, FileSystemLoader

#-------------
# Environment
#-------------
template_root = '/var/www/webapps/templates'
JLOADER = FileSystemLoader(template_root, encoding='utf-8')
JENV = Environment(block_start_string='[[',
    block_end_string=']]',
    variable_start_string='[-',
    variable_end_string='-]',
    comment_start_string='[#',
    comment_end_string='#]',
    loader=JLOADER,
    extensions=[],
    cache_size=50,
)

JENV.add_extension('jinja2.ext.i18n')
JENV.add_extension('jinja2.ext.do')

def jrender(template_name, params=None):
    if params is None:
        params = {}

    return JENV.get_template(template_name).render(params)
        
Ejemplo n.º 48
0
    def generateJavaApplication(name, changesets):
        # i is the number to be transform into byte array, n is the number of bytes to use (little endian)
        def bytestring(i, n):
            return ["(byte)" + str(ord(b)) for b in pack("H", i)][:n]

        def nodeinjava(node):
            return str(node.id)

        def wuobjectinjava(wuobject):
            return ", ".join([str(wuobject.wunode().id), str(wuobject.port_number)])

        def linkinjava(link):
            return ", ".join(
                bytestring(link.from_component_index, 2)
                + bytestring(link.from_property_id, 1)
                + bytestring(link.to_component_index, 2)
                + bytestring(link.to_property_id, 1)
                + bytestring(link.to_wuclass_id, 2)
            )

        def wuclassname(wuclass):
            return wuclass.name

        def wuclassvirtualclassname(wuclass):
            return "Virtual" + Convert.to_java(wuclass.name) + "WuObject"

        def wuclassid(wuclass):
            return str(wuclass.id)

        def wuclassgenclassname(wuclass):
            return "GENERATEDVirtual" + Convert.to_java(wuclass.name) + "WuObject"

        def propertyconstname(property):
            print "propertyconstname"
            return (
                "PROPERTY_"
                + Convert.to_constant(property.wuobject().wuclass().wuclassdef().name)
                + "_"
                + Convert.to_constant(property.wupropertydef().name)
            )

        # doesn't really matter to check since basic types are being take care of in application.java
        def propertyconstantvalue(property):
            wutype = WuTypeDef.where(name=property.datatype)
            if wutype:
                return (
                    wutype[0].type.upper()
                    + "_"
                    + Convert.to_constant(property.datatype)
                    + "_"
                    + Convert.to_constant(property.value)
                )
            else:
                return "ENUM" + "_" + Convert.to_constant(property.datatype) + "_" + Convert.to_constant(property.value)

        def generateProperties(wuobject_properties, component_properties):
            properties = wuobject_properties

            for property in properties:
                if property.wupropertydef().name in component_properties:
                    property.value = component_properties[property.wupropertydef().name]
            return properties

        # Generate the Java code
        print "[generator] generating", os.path.join(JAVA_OUTPUT_DIR, "WKDeploy.java")
        jinja2_env = Environment(loader=FileSystemLoader([os.path.join(os.path.dirname(__file__), "jinja_templates")]))
        jinja2_env.filters["nodeinjava"] = nodeinjava
        jinja2_env.filters["wuclassname"] = wuclassname
        jinja2_env.filters["wuclassvirtualclassname"] = wuclassvirtualclassname
        jinja2_env.filters["wuclassid"] = wuclassid
        jinja2_env.filters["wuclassgenclassname"] = wuclassgenclassname
        jinja2_env.filters["propertyconstname"] = propertyconstname
        jinja2_env.filters["propertyconstantvalue"] = propertyconstantvalue
        jinja2_env.filters["generateProperties"] = generateProperties
        jinja2_env.add_extension("jinja2.ext.do")
        output = open(os.path.join(JAVA_OUTPUT_DIR, "WKDeploy.java"), "w")
        output.write(jinja2_env.get_template("application2.java").render(name=name, changesets=changesets))
        output.close()
Ejemplo n.º 49
0
class Renderer(object):
    """Renders a template."""

    def __init__(self, path=None, loader=None, filters=None):
        """Constructs a new Renderer object.

        Either path or loader has to be specified.

        Keyword arguments:
        path -- list or str which represents template locations
        loader -- a jinja2 template loader instance (default: None)
        filters -- dict containing filters (default: {})

        """
        if (path is None and loader is None
                or path is not None and loader is not None):
            raise ValueError('Either specify path oder loader')
        if path is not None:
            loader = FileSystemLoader(path)
        self._env = Environment(loader=loader)
        self._env.add_extension('jinja2.ext.do')
        self._add_filters(filters)

    def _add_filters(self, filters):
        """Adds new filters to the jinja2 environment.

        filters is a dict of filters.

        """
        self._env.filters['dateformat'] = dateformat
        self._env.filters.update(filters or {})

    def _custom_template_names(self, template):
        """Returns a list of custom template names.

        template is the name of the original template.

        """
        splitted = template.rsplit('/', 1)
        name = 'custom_' + splitted[-1]
        ret = [name]
        if len(splitted) == 2:
            ret.append(splitted[0] + '/' + name)
        return ret

    def render_only(self, template, *args, **kwargs):
        """Renders template template.

        The rendered template is returned (no data will be written
        or printed).
        *args and **kwargs are passed to jinja2 Template's render
        method.

        """
        names = self._custom_template_names(template)
        names.append(template)
        tmpl = self._env.select_template(names)
        return tmpl.render(*args, **kwargs)

    def _render(self, template, out, *args, **kwargs):
        """Renders template template.

        out is a file or file-like object to which the rendered
        template should be written to.
        *args and **kwargs are passed to the render_only method.

        """
        text = self.render_only(template, *args, **kwargs)
        try:
            out.write(text)
        except UnicodeEncodeError:
            text = text.encode('utf-8')
            out.write(text)

    def render(self, template, *args, **kwargs):
        """Renders template template.

        Writes the rendered template to sys.stdout.
        *args and **kwargs are passed to jinja2 Template's render
        method.

        """
        self._render(template, sys.stdout, *args, **kwargs)

    def render_text(self, text, *args, **kwargs):
        """Renders text.

        *args and **kwargs are passed to jinja2 Template's render
        method.

        """
        global TEXT_TEMPLATE
        self.render(TEXT_TEMPLATE, text=text, *args, **kwargs)

    def render_error(self, template, *args, **kwargs):
        """Renders template template.

        Writes the rendered template to sys.stderr.
        *args and **kwargs are passed to jinja2 Template's render
        method.

        """
        self._render(template, sys.stderr, *args, **kwargs)
Ejemplo n.º 50
0
 def test_extend_late(self):
     env = Environment()
     env.add_extension('jinja2.ext.autoescape')
     t = env.from_string('{% autoescape true %}{{ "<test>" }}{% endautoescape %}')
     assert t.render() == '&lt;test&gt;'
Ejemplo n.º 51
0
class HTMLPage(Page, metaclass=Singleton):
    """
    Base class for HTML UI pages.

    Class Attributes:
        _tpl (SharedData): Descriptor object that handles access to Jinja2 template environment.
        Also see `Page.__doc__`

    """

    _tpl = SharedData('_tpl')
    _tpl_setup_ran = SharedData('_tpl_setup_running')
    _top_level_tabs = SharedData('_top_level_tabs')

    def __init__(self, name='HTMLPage', index=0, tpl_engine='jinja', *args, **kwargs):
        """
        Attributes:
            _tpl (Environment): The Jinja2 template environment.
            Also see `Page.__doc__`.

        Args:
            name (str): A name for this widget.

        """

        super().__init__(name=name, tpl_engine=tpl_engine, *args, **kwargs)

        self.signals = ['go-to-next-page', '--trigger-event']
        self.tabs = []
        self.can_go_to_next_page = False
        self.index = index
        self.logger.debug('page index is %s', index)

        if self._tpl is None and self._tpl_setup_ran is None:
            self._tpl_setup_ran = True
            self._initialize_template_engine()

        if self._pages_data is None:
            self.logger.debug('creating data object in _pages_data for %s..', name)
            self._pages_data = DataObject()

        if self._top_level_tabs is None:
            self.logger.debug('Generating main navigation tabs list..')
            self._generate_tabs_list()

        self._initialize_page_data()

    def _create_and_connect_signals(self):
        """
        Creates the page's signals and connects them to their callbacks (handlers).
        Signals should be appended to `self.signals` prior to calling this method.

        A corresponding result signal will be created for each signal and added to the
        allowed signals list automatically. Signal names will have 'do-' prepended to them.
        Their corresponding result signal will have '-result' appended to it.

        The name of the callback method that will be registered for each signal will be the
        signal name as it appears in `self.signals` at the time of calling this method with
        hyphens replaced by underscores and should end with '_cb'.

        A callback is not automatically connected for signals that start with two hyphens.

        Example:
            >>> self.signals = ['some-action', 'some-other-action', '--private-action']
            >>> self._create_and_connect_signals()
            >>> self.signals
                ['do-some-action', 'some-action-result', 'do-other-action',
                 'other-action-result', 'private-action']

            With the above, these callback methods will have been registered (they must exist):
                'do-some-action':  `self.some_action_cb`
                'do-other-action': `self.other_action_cb`

        """

        signals = []

        for _signal in self.signals:
            result_signal_name = '{}-result'.format(_signal)
            signal_name = 'do-{}'.format(_signal) if not _signal.startswith('--') else _signal[2:]

            signals_to_add = [
                s for s in [signal_name, result_signal_name]
                if s not in self._allowed_signals
            ]

            for name in signals_to_add:
                self._allowed_signals.append(name)
                self._main_window.create_custom_signal(name)
                signals.append(name)

            if not _signal.startswith('--'):
                callback_name = '{}_cb'.format(_signal.replace('-', '_'))
                callback = getattr(self, callback_name)

                self._main_window.connect(signal_name, callback)

        self.signals = signals

    def _generate_tabs_list(self):
        tabs = self._pages_helper.get_page_names()
        excluded = ['language', 'welcome']
        self._top_level_tabs = [t for t in tabs if t not in excluded]

    def _get_default_template_vars(self):
        return {
            'page_name': self.name,
            'top_level_tabs': self._get_top_level_tabs(),
            'page_index': self.index
        }

    def _get_initial_page_data(self):
        return dict()

    def _get_top_level_tabs(self):
        return [(t, self.name == t) for t in self._top_level_tabs]

    def _initialize_template_engine(self):
        resources_path = 'cnchi://{}'.format(os.path.join(self.PAGES_DIR, 'resources'))
        tpl_map = {
            pdir: FileSystemLoader(os.path.join(self.PAGES_DIR, pdir)) for pdir in self._page_dirs
        }
        tpl_map['pages'] = FileSystemLoader(self.PAGES_DIR)
        self._tpl = Environment(loader=PrefixLoader(tpl_map), lstrip_blocks=True, trim_blocks=True)
        self._tpl.globals['RESOURCES_DIR'] = resources_path
        self._tpl.add_extension('jinja2.ext.do')
        self._tpl.add_extension('jinja2.ext.i18n')
        self._tpl.install_null_translations(newstyle=True)

    def _initialize_page_data(self):
        if getattr(self._pages_data, self.name) is None:
            from_dict = self._get_initial_page_data()
            setattr(self._pages_data, self.name, DataObject(from_dict=from_dict))

    def emit_js(self, name, *args):
        """ See `Controller.emit_js.__doc__` """
        self._controller.emit_js(name, *args)

    def get_next_page_index(self):
        return self._pages_helper.page_names.index(self.name) + 1

    def get_previous_page_index(self):
        return self._pages_helper.page_names.index(self.name) - 1

    def go_to_next_page(self, obj=None, next_plus=0):
        if self.name != self._controller.current_page:
            return

        self.store_values()
        self._controller.set_current_page(self.get_next_page_index() + next_plus)

        return True

    def go_to_next_page_cb(self, obj=None, next_plus=0):
        self.go_to_next_page(obj, next_plus)

    def prepare(self):
        """ This must be implemented by subclasses """
        pass

    def render_template(self, name=None, tpl_vars=None):
        name = name if name is not None else self.template
        default_tpl_vars = self._get_default_template_vars()
        tpl = self._tpl.get_template(name)

        if tpl_vars is not None:
            tpl_vars = tpl_vars.update(default_tpl_vars)
        else:
            tpl_vars = default_tpl_vars

        return tpl.render(tpl_vars)

    def render_template_as_bytes(self, name=None, tpl_vars=None):
        tpl = self.render_template(name=name, tpl_vars=tpl_vars)
        return tpl.encode('UTF-8')

    def store_values(self):
        """ This must be implemented by subclasses """
        self._pages_data.has_data = True
Ejemplo n.º 52
0
    def generateJavaApplication(name, changesets):
        # i is the number to be transform into byte array, n is the number of bytes to use (little endian)
        def bytestring(i, n): 
            return ['(byte)' + str(ord(b)) for b in pack("H", i)][:n]

        def nodeinjava(node):
            return str(node.id)

        def wuobjectinjava(wuobject):
            return ', '.join([str(wuobject.node_id),
                            str(wuobject.port_number)])
            
        def linkinjava(link):
            return ', '.join(bytestring(link.from_component_index, 2)
                    + bytestring(link.from_property_id, 1)
                    + bytestring(link.to_component_index, 2)
                    + bytestring(link.to_property_id, 1)
                    + bytestring(link.to_wuclass_id, 2))

        def wuclassname(wuclass):
            return wuclass.name

        def wuclassvirtualclassname(wuclass):
            return "Virtual" + Convert.to_java(wuclass.name) + "WuObject"

        def wuclassconstname(wuclass):
            return "WUCLASS_" + Convert.to_constant(wuclass.name)

        def wuclassgenclassname(wuclass):
            return "GENERATEDVirtual" + Convert.to_java(wuclass.name) + "WuObject"

        def propertyconstname(property):
            print 'propertyconstname'
            return "PROPERTY_" + Convert.to_constant(property.wuclass.name) + "_" + Convert.to_constant(property.name)

        # doesn't really matter to check since basic types are being take care of in application.java
        def propertyconstantvalue(property):
            wutype = WuType.where(name=property.datatype)
            if wutype:
                return wutype[0].type.upper() + '_' + Convert.to_constant(property.datatype) + "_" + Convert.to_constant(property.value)
            else:
                return 'ENUM' + '_' + Convert.to_constant(property.datatype) + "_" + Convert.to_constant(property.value)

        def generateProperties(wuclass_properties, component_properties):
            properties = []
            for property in wuclass_properties:
                if property.value.strip() != "" and (not property.name in [x.name for x in properties]):
                    properties.append(property)

            for property in properties:
                if property.name in component_properties:
                    if component_properties[property.name].strip() != "":
                        property.value = component_properties[property.name]
            return properties

        # Generate the Java code
        print 'generating', os.path.join(JAVA_OUTPUT_DIR, "WKDeploy.java")
        jinja2_env = Environment(loader=FileSystemLoader([os.path.join(os.path.dirname(__file__), 'jinja_templates')]))
        jinja2_env.filters['nodeinjava'] = nodeinjava
        jinja2_env.filters['wuclassname'] = wuclassname
        jinja2_env.filters['wuclassvirtualclassname'] = wuclassvirtualclassname
        jinja2_env.filters['wuclassconstname'] = wuclassconstname
        jinja2_env.filters['wuclassgenclassname'] = wuclassgenclassname
        jinja2_env.filters['propertyconstname'] = propertyconstname
        jinja2_env.filters['propertyconstantvalue'] = propertyconstantvalue
        jinja2_env.filters['generateProperties'] = generateProperties
        jinja2_env.add_extension('jinja2.ext.do')
        output = open(os.path.join(JAVA_OUTPUT_DIR, "WKDeploy.java"), 'w')
        output.write(jinja2_env.get_template('application2.java').render(name=name, changesets=changesets))
        output.close()
Ejemplo n.º 53
0
class Kit(object):
    """Magic wrapper for jinja environment
    """
    def __init__(self, search_path, loader=FileSystemLoader,
                 auto_reload=False, cache_size=50, extensions=(),
                 filters={}, globals={}):
        """Construct wrapper
        """
        self.search_path = search_path
        self.env = Environment(loader=loader(self.search_path),
                               auto_reload=auto_reload,
                               cache_size=cache_size,
                               extensions=extensions)
        self.filters = filters
        self.globals = globals

    def filter(self, name=None):
        def wrapper(func):
            return self.add_filter(func, name)
        return wrapper

    def tglobal(self, name=None):
        def wrapper(func):
            return self.add_global(func, name)
        return wrapper

    def add_filter(self, func, name=None):
        """Add filter to environment
        :param func:
        :param name:
        """
        self.env.filters.update({name or func.func_name: func})
        return func

    def add_global(self, func, name=None):
        """Add global function to context

        :param func:
        :param name:
        """
        self.env.globals.update({name or func.func_name: func})
        return func

    def load_filters(self, paths=None):
        """Load filter to environment
        """
        result = {}
        paths = paths or self.filters
        for m in paths:
            f = __import__(m, fromlist=['filters'])
            result.update(f.filters)
        return result

    def load_globals(self, paths=None, key='globals'):
        """Load global function to environment
        """
        result = {}
        paths = paths or self.globals
        for m in paths:
            f = __import__(m, fromlist=[key])
            result.update(f.globals)
            for name, func in f.globals.iteritems():
                self.add_global(func, name)
        return result

    def load_extensions(self, extensions):
        """Load extensions
        """
        for extension in extensions:
            if isinstance(extension, basestring):
                extension = import_string(extension)
            self.add_extension(extension)

    def add_extension(self, ext):
        """Load extension to environment

        :param ext: extension object
        """
        self.env.add_extension(ext)

    def get_template(self, template_name, global_functions={}):
        """Get template
        """
        return self.env.get_template(template_name, globals=global_functions)

    def select_template(self, templates):
        """Search templates
        """
        if isinstance(templates, (list, tuple)):
            for template in templates:
                try:
                    return self.get_template(template,
                                             global_functions=globals())
                except TemplateNotFound:
                    continue
        elif isinstance(templates, (str, unicode, Template)):
            return self.get_template(templates, globals())


        raise TemplateNotFound

    def render_to_string(self, template_name, context=None,
                         processors=None, processor_arg=None):
        """Render template into string

        :param template_name:
        :param context:
        :param processors:
        :param proccessor_arg:
        """
        created_context = self.get_context(context=context, processors=processors,
                                           processor_arg=processor_arg)
        return self.select_template(template_name).render(created_context)

    def get_standard_processors(self):
        return ()

    def get_context(self, context=None, processors=None, processor_arg=None):
        context = dict(context or {})
        context['processor_arg'] = processor_arg
        standard_processors = self.get_standard_processors()

        for processor in chain(standard_processors, processors or ()):
            try:
                context.update(processor(processor_arg))
            except Exception, e:
                print(e)

        return context
Ejemplo n.º 54
0
    def generateJavaApplication(name, changesets):
        # i is the number to be transform into byte array, n is the number of bytes to use (little endian)
        def bytestring(i, n):
            return ['(byte)' + str(ord(b)) for b in pack("H", i)][:n]

        def nodeinjava(node):
            return str(node.id)

        def wuobjectinjava(wuobject):
            return ', '.join(
                [str(wuobject.wunode().id),
                 str(wuobject.port_number)])

        def linkinjava(link):
            return ', '.join(
                bytestring(link.from_component_index, 2) +
                bytestring(link.from_property_id, 1) +
                bytestring(link.to_component_index, 2) +
                bytestring(link.to_property_id, 1) +
                bytestring(link.to_wuclass_id, 2))

        def wuclassname(wuclass):
            return wuclass.name

        def wuclassvirtualclassname(wuclass):
            return "Virtual" + Convert.to_java(wuclass.name) + "WuObject"

        def wuclassid(wuclass):
            return str(wuclass.id)

        def wuclassgenclassname(wuclass):
            return "GENERATEDVirtual" + Convert.to_java(
                wuclass.name) + "WuObject"

        def propertyconstname(property):
            print 'propertyconstname'
            return "PROPERTY_" + Convert.to_constant(
                property.wuobject.wuclassdef.name) + "_" + Convert.to_constant(
                    property.name)

        # doesn't really matter to check since basic types are being take care of in application.java
        def propertyconstantvalue(property):
            wutype = WuTypeDef.where(name=property.wutype)
            if wutype:
                return wutype[0].type.upper() + '_' + Convert.to_constant(
                    property.wutype) + "_" + Convert.to_constant(
                        property.value)
            else:
                return 'ENUM' + '_' + Convert.to_constant(
                    property.wutype) + "_" + Convert.to_constant(
                        property.value)

        def generateProperties(wuobject_properties, component_properties):
            properties = wuobject_properties
            for property in properties:
                if property.name in component_properties:
                    property.value = component_properties[property.name]
            return properties

        # Generate the Java code
        print '[generator] generating', os.path.join(JAVA_OUTPUT_DIR,
                                                     "WKDeploy.java")
        jinja2_env = Environment(loader=FileSystemLoader(
            [os.path.join(os.path.dirname(__file__), 'jinja_templates')]))
        jinja2_env.filters['nodeinjava'] = nodeinjava
        jinja2_env.filters['wuclassname'] = wuclassname
        jinja2_env.filters['wuclassvirtualclassname'] = wuclassvirtualclassname
        jinja2_env.filters['wuclassid'] = wuclassid
        jinja2_env.filters['wuclassgenclassname'] = wuclassgenclassname
        jinja2_env.filters['propertyconstname'] = propertyconstname
        jinja2_env.filters['propertyconstantvalue'] = propertyconstantvalue
        jinja2_env.filters['generateProperties'] = generateProperties
        jinja2_env.add_extension('jinja2.ext.do')
        output = open(os.path.join(JAVA_OUTPUT_DIR, "WKDeploy.java"), 'w')
        output.write(
            jinja2_env.get_template('application2.java').render(
                name=name, changesets=changesets))
        output.close()
Ejemplo n.º 55
0
 def test_extend_late(self):
     env = Environment()
     env.add_extension("jinja2.ext.autoescape")
     t = env.from_string(
         '{% autoescape true %}{{ "<test>" }}{% endautoescape %}')
     assert t.render() == "&lt;test&gt;"
Ejemplo n.º 56
0
class Renderer(object):
    """Renders a template."""
    def __init__(self, path=None, loader=None, filters=None):
        """Constructs a new Renderer object.

        Either path or loader has to be specified.

        Keyword arguments:
        path -- list or str which represents template locations
        loader -- a jinja2 template loader instance (default: None)
        filters -- dict containing filters (default: {})

        """
        if (path is None and loader is None
                or path is not None and loader is not None):
            raise ValueError('Either specify path oder loader')
        if path is not None:
            loader = FileSystemLoader(path)
        self._env = Environment(loader=loader)
        self._env.add_extension('jinja2.ext.do')
        self._add_filters(filters)

    def _add_filters(self, filters):
        """Adds new filters to the jinja2 environment.

        filters is a dict of filters.

        """
        self._env.filters['dateformat'] = dateformat
        self._env.filters.update(filters or {})

    def _custom_template_names(self, template):
        """Returns a list of custom template names.

        template is the name of the original template.

        """
        splitted = template.rsplit('/', 1)
        name = 'custom_' + splitted[-1]
        ret = [name]
        if len(splitted) == 2:
            ret.append(splitted[0] + '/' + name)
        return ret

    def render_only(self, template, *args, **kwargs):
        """Renders template template.

        The rendered template is returned (no data will be written
        or printed).
        *args and **kwargs are passed to jinja2 Template's render
        method.

        """
        names = self._custom_template_names(template)
        names.append(template)
        tmpl = self._env.select_template(names)
        return tmpl.render(*args, **kwargs)

    def _render(self, template, out, *args, **kwargs):
        """Renders template template.

        out is a file or file-like object to which the rendered
        template should be written to.
        *args and **kwargs are passed to the render_only method.

        """
        text = self.render_only(template, *args, **kwargs)
        try:
            out.write(text)
        except UnicodeEncodeError:
            text = text.encode('utf-8')
            out.write(text)

    def render(self, template, *args, **kwargs):
        """Renders template template.

        Writes the rendered template to sys.stdout.
        *args and **kwargs are passed to jinja2 Template's render
        method.

        """
        self._render(template, sys.stdout, *args, **kwargs)

    def render_text(self, text, *args, **kwargs):
        """Renders text.

        *args and **kwargs are passed to jinja2 Template's render
        method.

        """
        global TEXT_TEMPLATE
        self.render(TEXT_TEMPLATE, text=text, *args, **kwargs)

    def render_error(self, template, *args, **kwargs):
        """Renders template template.

        Writes the rendered template to sys.stderr.
        *args and **kwargs are passed to jinja2 Template's render
        method.

        """
        self._render(template, sys.stderr, *args, **kwargs)