Ejemplo n.º 1
0
    def __init__(self, templates, cache, cache_dir):

        # PERF: This function is simply constructing a Jinja environment and
        # would be trivial, except that we optimise re-execution of template
        # code by compiling the templates to Python bytecode the first time
        # they are seen. This happens when the compilation cache is enabled and
        # should speed the execution of the template code itself in future
        # runs.

        self.templates = templates

        # Directory in which to store and fetch pre-compiled Jinja2 templates.
        template_cache = os.path.join(cache_dir, version(),
                                      'precompiled-templates')

        loaders = []
        if cache and os.path.exists(template_cache):
            # Pre-compiled templates.
            loaders.append(jinja2.ModuleLoader(template_cache))

        # Source templates.
        loaders.extend(
            jinja2.FileSystemLoader(os.path.abspath(x))
            for x in templates.get_roots())

        self.env = jinja2.Environment(
            loader=jinja2.ChoiceLoader(loaders),
            extensions=["jinja2.ext.do", "jinja2.ext.loopcontrols"],
            block_start_string=START_BLOCK,
            block_end_string=END_BLOCK,
            variable_start_string=START_VARIABLE,
            variable_end_string=END_VARIABLE,
            comment_start_string=START_COMMENT,
            comment_end_string=END_COMMENT,
            auto_reload=False,
            undefined=jinja2.StrictUndefined)

        if cache and not os.path.exists(template_cache):
            # The pre-compiled template cache is enabled but does not exist.
            # We build it here for next time.

            # We filter the templates that Jinja compiles to only the ones we
            # know of in order to avoid errors or wasted time on other stray
            # garbage in the template directory (vim swp files, pycs, ...).
            templates = list(get_leaves(TEMPLATES))

            mkdirp(template_cache)

            # Compile the templates. Note that we only compile them to PYCs on
            # Python 2, because this has no effect on Python 3 or PyPy.
            self.env.compile_templates(
                template_cache,
                filter_func=(lambda x: x in templates),
                zip=None,
                ignore_errors=False,
                py_compile=platform.python_implementation() == 'CPython'
                and six.PY2)
Ejemplo n.º 2
0
    def __init__(self, templates, cache, cache_dir):

        # PERF: This function is simply constructing a Jinja environment and
        # would be trivial, except that we optimise re-execution of template
        # code by compiling the templates to Python bytecode the first time
        # they are seen. This happens when the compilation cache is enabled and
        # should speed the execution of the template code itself in future
        # runs.

        self.templates = templates

        # Directory in which to store and fetch pre-compiled Jinja2 templates.
        template_cache = os.path.join(cache_dir, version(),
            'precompiled-templates')

        loaders = []
        if cache and os.path.exists(template_cache):
            # Pre-compiled templates.
            loaders.append(jinja2.ModuleLoader(template_cache))

        # Source templates.
        loaders.extend(jinja2.FileSystemLoader(os.path.abspath(x)) for x in
            templates.get_roots())

        self.env = jinja2.Environment(
            loader=jinja2.ChoiceLoader(loaders),
            extensions=["jinja2.ext.do", "jinja2.ext.loopcontrols"],
            block_start_string=START_BLOCK,
            block_end_string=END_BLOCK,
            variable_start_string=START_VARIABLE,
            variable_end_string=END_VARIABLE,
            comment_start_string=START_COMMENT,
            comment_end_string=END_COMMENT,
            auto_reload=False,
            undefined=jinja2.StrictUndefined)

        if cache and not os.path.exists(template_cache):
            # The pre-compiled template cache is enabled but does not exist.
            # We build it here for next time.

            # We filter the templates that Jinja compiles to only the ones we
            # know of in order to avoid errors or wasted time on other stray
            # garbage in the template directory (vim swp files, pycs, ...).
            templates = list(get_leaves(TEMPLATES))

            mkdirp(template_cache)

            # Compile the templates. Note that we only compile them to PYCs on
            # Python 2, because this has no effect on Python 3 or PyPy.
            self.env.compile_templates(template_cache,
                filter_func=(lambda x: x in templates), zip=None,
                ignore_errors=False, py_compile=
                platform.python_implementation() == 'CPython' and six.PY2)
Ejemplo n.º 3
0
    def __init__(self, template_paths, options):

        # PERF: This function is simply constructing a Jinja environment and
        # would be trivial, except that we optimise re-execution of template
        # code by compiling the templates to Python bytecode the first time
        # they are seen. This happens when the compilation cache is enabled and
        # should speed the execution of the template code itself in future
        # runs.

        # Directory in which to store and fetch pre-compiled Jinja2 templates.
        template_cache = os.path.join(options.cache_dir, version_hash(),
                                      'precompiled-templates')

        loaders = []
        if options.cache in ['on', 'readonly'] and \
                os.path.exists(template_cache):
            # Pre-compiled templates.
            loaders.append(jinja2.ModuleLoader(template_cache))

        # Source templates.
        loaders.extend(
            map(jinja2.FileSystemLoader,
                [os.path.abspath(x) for x in template_paths]))

        self.env = jinja2.Environment(
            loader=jinja2.ChoiceLoader(loaders),
            extensions=["jinja2.ext.do", "jinja2.ext.loopcontrols"],
            block_start_string=START_BLOCK,
            block_end_string=END_BLOCK,
            variable_start_string=START_VARIABLE,
            variable_end_string=END_VARIABLE,
            comment_start_string=START_COMMENT,
            comment_end_string=END_COMMENT,
            auto_reload=False)

        if options.cache in ['on', 'writeonly'] and \
                not os.path.exists(template_cache):
            # The pre-compiled template cache is enabled but does not exist.
            # We build it here for next time.

            # We filter the templates that Jinja compiles to only the ones we
            # know of in order to avoid errors or wasted time on other stray
            # garbage in the template directory (vim swp files, pycs, ...).
            templates = list(get_leaves(TEMPLATES))

            mkdirp(template_cache)
            self.env.compile_templates(template_cache,
                                       filter_func=(lambda x: x in templates),
                                       zip=None,
                                       ignore_errors=False,
                                       py_compile=True)
Ejemplo n.º 4
0
    def __init__(self, template_paths, options):

        # PERF: This function is simply constructing a Jinja environment and
        # would be trivial, except that we optimise re-execution of template
        # code by compiling the templates to Python bytecode the first time
        # they are seen. This happens when the compilation cache is enabled and
        # should speed the execution of the template code itself in future
        # runs.

        # Directory in which to store and fetch pre-compiled Jinja2 templates.
        template_cache = os.path.join(options.cache_dir, version(),
            'precompiled-templates')

        loaders = []
        if options.cache in ['on', 'readonly'] and \
                os.path.exists(template_cache):
            # Pre-compiled templates.
            loaders.append(jinja2.ModuleLoader(template_cache))

        # Source templates.
        loaders.extend(map(jinja2.FileSystemLoader,
            [os.path.abspath(x) for x in template_paths]))

        self.env = jinja2.Environment(
            loader=jinja2.ChoiceLoader(loaders),
            extensions=["jinja2.ext.do", "jinja2.ext.loopcontrols"],
            block_start_string=START_BLOCK,
            block_end_string=END_BLOCK,
            variable_start_string=START_VARIABLE,
            variable_end_string=END_VARIABLE,
            comment_start_string=START_COMMENT,
            comment_end_string=END_COMMENT,
            auto_reload=False)

        if options.cache in ['on', 'writeonly'] and \
                not os.path.exists(template_cache):
            # The pre-compiled template cache is enabled but does not exist.
            # We build it here for next time.

            # We filter the templates that Jinja compiles to only the ones we
            # know of in order to avoid errors or wasted time on other stray
            # garbage in the template directory (vim swp files, pycs, ...).
            templates = list(get_leaves(TEMPLATES))

            mkdirp(template_cache)
            self.env.compile_templates(template_cache,
                filter_func=(lambda x: x in templates), zip=None,
                ignore_errors=False, py_compile=True)