Example #1
1
File: cli.py Project: edk0/cms7
def compile_theme(theme, target, *, zip_=False):
    """
    Compile a theme (i.e. directory of Jinja2 templates)

    theme: Path to directory with templates in

    target: Directory in which to store compiled templates

    zip_: Target is a zip file, rather than a directory
    """
    from jinja2 import Environment, FileSystemLoader
    env = Environment(loader=FileSystemLoader(theme))
    env.compile_templates(target, zip='deflated' if zip_ else None)
Example #2
0
        def render(self, templ_path, templ_name, cached_path):
            templ_compiled_path = cached_path
            template = ''
            
            JINJA_ENVIRONMENT = Environment(
                loader=ModuleLoader(templ_compiled_path),
                autoescape=False
            )
            
            try:
                template = JINJA_ENVIRONMENT.get_template(templ_name)
            except exceptions.TemplateNotFound:
                JINJA_COMPILE_ENVIRONMENT = Environment(
                   loader=FileSystemLoader(templ_path),
                   autoescape=False
                )
                
                JINJA_COMPILE_ENVIRONMENT.compile_templates(
                    templ_compiled_path, 
                    zip=None
                )
                
                template = JINJA_COMPILE_ENVIRONMENT.get_template(templ_name)

            template_values = {
                'data': self.tableData,
                'maxInfo': self._maxInfo,
                'properties': self._property_names
            }

            print template.render(template_values)
Example #3
0
    def render(self, templ_path, templ_name, cached_path):
        templ_compiled_path = cached_path
        template = ''

        JINJA_ENVIRONMENT = Environment(
            loader=ModuleLoader(templ_compiled_path), autoescape=False)

        try:
            template = JINJA_ENVIRONMENT.get_template(templ_name)
        except exceptions.TemplateNotFound:
            JINJA_COMPILE_ENVIRONMENT = Environment(
                loader=FileSystemLoader(templ_path), autoescape=False)

            JINJA_COMPILE_ENVIRONMENT.compile_templates(templ_compiled_path,
                                                        zip=None)

            template = JINJA_COMPILE_ENVIRONMENT.get_template(templ_name)

        template_values = {
            'data': self.tableData,
            'maxInfo': self._maxInfo,
            'properties': self._property_names
        }

        print template.render(template_values)
Example #4
0
def compile_theme(theme, target, *, zip_=False):
    """
    Compile a theme (i.e. directory of Jinja2 templates)

    theme: Path to directory with templates in

    target: Directory in which to store compiled templates

    zip_: Target is a zip file, rather than a directory
    """
    from jinja2 import Environment, FileSystemLoader
    env = Environment(loader=FileSystemLoader(theme))
    env.compile_templates(target, zip='deflated' if zip_ else None)
Example #5
0
class ModuleLoaderTestCase(JinjaTestCase):
    archive = None

    def compile_down(self, zip='deflated', py_compile=False):
        super(ModuleLoaderTestCase, self).setup()
        log = []
        self.reg_env = Environment(loader=prefix_loader)
        if zip is not None:
            self.archive = tempfile.mkstemp(suffix='.zip')[1]
        else:
            self.archive = tempfile.mkdtemp()
        self.reg_env.compile_templates(self.archive, zip=zip,
                                       log_function=log.append,
                                       py_compile=py_compile)
        self.mod_env = Environment(loader=loaders.ModuleLoader(self.archive))
        return ''.join(log)

    def teardown(self):
        super(ModuleLoaderTestCase, self).teardown()
        if hasattr(self, 'mod_env'):
            if os.path.isfile(self.archive):
                os.remove(self.archive)
            else:
                shutil.rmtree(self.archive)
            self.archive = None

    def test_log(self):
        log = self.compile_down()
        assert 'Compiled "a/foo/test.html" as ' \
               'tmpl_a790caf9d669e39ea4d280d597ec891c4ef0404a' in log
        assert 'Finished compiling templates' in log
        assert 'Could not compile "a/syntaxerror.html": ' \
               'Encountered unknown tag \'endif\'' in log

    def _test_common(self):
        tmpl1 = self.reg_env.get_template('a/test.html')
        tmpl2 = self.mod_env.get_template('a/test.html')
        assert tmpl1.render() == tmpl2.render()

        tmpl1 = self.reg_env.get_template('b/justdict.html')
        tmpl2 = self.mod_env.get_template('b/justdict.html')
        assert tmpl1.render() == tmpl2.render()

    def test_deflated_zip_compile(self):
        self.compile_down(zip='deflated')
        self._test_common()

    def test_stored_zip_compile(self):
        self.compile_down(zip='stored')
        self._test_common()

    def test_filesystem_compile(self):
        self.compile_down(zip=None)
        self._test_common()

    def test_weak_references(self):
        self.compile_down()
        tmpl = self.mod_env.get_template('a/test.html')
        key = loaders.ModuleLoader.get_template_key('a/test.html')
        name = self.mod_env.loader.module.__name__

        assert hasattr(self.mod_env.loader.module, key)
        assert name in sys.modules

        # unset all, ensure the module is gone from sys.modules
        self.mod_env = tmpl = None

        try:
            import gc
            gc.collect()
        except:
            pass

        assert name not in sys.modules

    def test_byte_compilation(self):
        log = self.compile_down(py_compile=True)
        assert 'Byte-compiled "a/test.html"' in log
        tmpl1 = self.mod_env.get_template('a/test.html')
        mod = self.mod_env.loader.module. \
            tmpl_3c4ddf650c1a73df961a6d3d2ce2752f1b8fd490
        assert mod.__file__.endswith('.pyc')

    def test_choice_loader(self):
        log = self.compile_down(py_compile=True)
        assert 'Byte-compiled "a/test.html"' in log

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

        tmpl1 = self.mod_env.get_template('a/test.html')
        self.assert_equal(tmpl1.render(), 'BAR')
        tmpl2 = self.mod_env.get_template('DICT_SOURCE')
        self.assert_equal(tmpl2.render(), 'DICT_TEMPLATE')

    def test_prefix_loader(self):
        log = self.compile_down(py_compile=True)
        assert 'Byte-compiled "a/test.html"' in log

        self.mod_env.loader = loaders.PrefixLoader({
            'MOD':      self.mod_env.loader,
            'DICT':     loaders.DictLoader({'test.html': 'DICT_TEMPLATE'})
        })

        tmpl1 = self.mod_env.get_template('MOD/a/test.html')
        self.assert_equal(tmpl1.render(), 'BAR')
        tmpl2 = self.mod_env.get_template('DICT/test.html')
        self.assert_equal(tmpl2.render(), 'DICT_TEMPLATE')
class ModuleLoaderTestCase(JinjaTestCase):
    archive = None

    def compile_down(self, zip='deflated', py_compile=False):
        super(ModuleLoaderTestCase, self).setup()
        log = []
        self.reg_env = Environment(loader=prefix_loader)
        if zip is not None:
            self.archive = tempfile.mkstemp(suffix='.zip')[1]
        else:
            self.archive = tempfile.mkdtemp()
        self.reg_env.compile_templates(self.archive,
                                       zip=zip,
                                       log_function=log.append,
                                       py_compile=py_compile)
        self.mod_env = Environment(loader=loaders.ModuleLoader(self.archive))
        return ''.join(log)

    def teardown(self):
        super(ModuleLoaderTestCase, self).teardown()
        if hasattr(self, 'mod_env'):
            if os.path.isfile(self.archive):
                os.remove(self.archive)
            else:
                shutil.rmtree(self.archive)
            self.archive = None

    def test_log(self):
        log = self.compile_down()
        assert 'Compiled "a/foo/test.html" as ' \
               'tmpl_a790caf9d669e39ea4d280d597ec891c4ef0404a' in log
        assert 'Finished compiling templates' in log
        assert 'Could not compile "a/syntaxerror.html": ' \
               'Encountered unknown tag \'endif\'' in log

    def _test_common(self):
        tmpl1 = self.reg_env.get_template('a/test.html')
        tmpl2 = self.mod_env.get_template('a/test.html')
        assert tmpl1.render() == tmpl2.render()

        tmpl1 = self.reg_env.get_template('b/justdict.html')
        tmpl2 = self.mod_env.get_template('b/justdict.html')
        assert tmpl1.render() == tmpl2.render()

    def test_deflated_zip_compile(self):
        self.compile_down(zip='deflated')
        self._test_common()

    def test_stored_zip_compile(self):
        self.compile_down(zip='stored')
        self._test_common()

    def test_filesystem_compile(self):
        self.compile_down(zip=None)
        self._test_common()

    def test_weak_references(self):
        self.compile_down()
        tmpl = self.mod_env.get_template('a/test.html')
        key = loaders.ModuleLoader.get_template_key('a/test.html')
        name = self.mod_env.loader.module.__name__

        assert hasattr(self.mod_env.loader.module, key)
        assert name in sys.modules

        # unset all, ensure the module is gone from sys.modules
        self.mod_env = tmpl = None

        try:
            import gc
            gc.collect()
        except:
            pass

        assert name not in sys.modules

    # This test only makes sense on non-pypy python 2
    if PY2 and not PYPY:

        def test_byte_compilation(self):
            log = self.compile_down(py_compile=True)
            assert 'Byte-compiled "a/test.html"' in log
            tmpl1 = self.mod_env.get_template('a/test.html')
            mod = self.mod_env.loader.module. \
                tmpl_3c4ddf650c1a73df961a6d3d2ce2752f1b8fd490
            assert mod.__file__.endswith('.pyc')

    def test_choice_loader(self):
        log = self.compile_down()

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

        tmpl1 = self.mod_env.get_template('a/test.html')
        self.assert_equal(tmpl1.render(), 'BAR')
        tmpl2 = self.mod_env.get_template('DICT_SOURCE')
        self.assert_equal(tmpl2.render(), 'DICT_TEMPLATE')

    def test_prefix_loader(self):
        log = self.compile_down()

        self.mod_env.loader = loaders.PrefixLoader({
            'MOD':
            self.mod_env.loader,
            'DICT':
            loaders.DictLoader({'test.html': 'DICT_TEMPLATE'})
        })

        tmpl1 = self.mod_env.get_template('MOD/a/test.html')
        self.assert_equal(tmpl1.render(), 'BAR')
        tmpl2 = self.mod_env.get_template('DICT/test.html')
        self.assert_equal(tmpl2.render(), 'DICT_TEMPLATE')
Example #7
0
class TestModuleLoader:
    archive = None

    def compile_down(self, prefix_loader, zip="deflated"):
        log = []
        self.reg_env = Environment(loader=prefix_loader)
        if zip is not None:
            fd, self.archive = tempfile.mkstemp(suffix=".zip")
            os.close(fd)
        else:
            self.archive = tempfile.mkdtemp()
        self.reg_env.compile_templates(self.archive,
                                       zip=zip,
                                       log_function=log.append)
        self.mod_env = Environment(loader=loaders.ModuleLoader(self.archive))
        return "".join(log)

    def teardown(self):
        if hasattr(self, "mod_env"):
            if os.path.isfile(self.archive):
                os.remove(self.archive)
            else:
                shutil.rmtree(self.archive)
            self.archive = None

    def test_log(self, prefix_loader):
        log = self.compile_down(prefix_loader)
        assert ('Compiled "a/foo/test.html" as '
                "tmpl_a790caf9d669e39ea4d280d597ec891c4ef0404a" in log)
        assert "Finished compiling templates" in log
        assert ('Could not compile "a/syntaxerror.html": '
                "Encountered unknown tag 'endif'" in log)

    def _test_common(self):
        tmpl1 = self.reg_env.get_template("a/test.html")
        tmpl2 = self.mod_env.get_template("a/test.html")
        assert tmpl1.render() == tmpl2.render()

        tmpl1 = self.reg_env.get_template("b/justdict.html")
        tmpl2 = self.mod_env.get_template("b/justdict.html")
        assert tmpl1.render() == tmpl2.render()

    def test_deflated_zip_compile(self, prefix_loader):
        self.compile_down(prefix_loader, zip="deflated")
        self._test_common()

    def test_stored_zip_compile(self, prefix_loader):
        self.compile_down(prefix_loader, zip="stored")
        self._test_common()

    def test_filesystem_compile(self, prefix_loader):
        self.compile_down(prefix_loader, zip=None)
        self._test_common()

    def test_weak_references(self, prefix_loader):
        self.compile_down(prefix_loader)
        self.mod_env.get_template("a/test.html")
        key = loaders.ModuleLoader.get_template_key("a/test.html")
        name = self.mod_env.loader.module.__name__

        assert hasattr(self.mod_env.loader.module, key)
        assert name in sys.modules

        # unset all, ensure the module is gone from sys.modules
        self.mod_env = None

        try:
            import gc

            gc.collect()
        except BaseException:
            pass

        assert name not in sys.modules

    def test_choice_loader(self, prefix_loader):
        self.compile_down(prefix_loader)
        self.mod_env.loader = loaders.ChoiceLoader([
            self.mod_env.loader,
            loaders.DictLoader({"DICT_SOURCE": "DICT_TEMPLATE"})
        ])
        tmpl1 = self.mod_env.get_template("a/test.html")
        assert tmpl1.render() == "BAR"
        tmpl2 = self.mod_env.get_template("DICT_SOURCE")
        assert tmpl2.render() == "DICT_TEMPLATE"

    def test_prefix_loader(self, prefix_loader):
        self.compile_down(prefix_loader)
        self.mod_env.loader = loaders.PrefixLoader({
            "MOD":
            self.mod_env.loader,
            "DICT":
            loaders.DictLoader({"test.html": "DICT_TEMPLATE"}),
        })
        tmpl1 = self.mod_env.get_template("MOD/a/test.html")
        assert tmpl1.render() == "BAR"
        tmpl2 = self.mod_env.get_template("DICT/test.html")
        assert tmpl2.render() == "DICT_TEMPLATE"

    def test_path_as_pathlib(self, prefix_loader):
        self.compile_down(prefix_loader)

        mod_path = self.mod_env.loader.module.__path__[0]
        mod_loader = loaders.ModuleLoader(Path(mod_path))
        self.mod_env = Environment(loader=mod_loader)

        self._test_common()

    def test_supports_pathlib_in_list_of_paths(self, prefix_loader):
        self.compile_down(prefix_loader)

        mod_path = self.mod_env.loader.module.__path__[0]
        mod_loader = loaders.ModuleLoader([Path(mod_path), "/tmp/templates"])
        self.mod_env = Environment(loader=mod_loader)

        self._test_common()
class JinjaStaticRenderer(Core.Renderer):
    def init(self):

        # Setup the env
        self.env = Environment(undefined=SilentUndefined,
                               loader=FileSystemLoader(
                                   self.config['templates_dir']),
                               extensions=[MarkdownExtension],
                               trim_blocks=True,
                               lstrip_blocks=True)

        self.env.filters["markdown"] = markdown_filter
        self.env.filters['rst'] = rst_filter

        if not os.path.exists(self.config['templates_dir']):
            self.reporter.log(
                "Couldn't find %s. Check your templates exist, and your config in wrangler.yaml"
                % (self.config['templates_dir']), "red")
            exit()

        # Load up some custom, project specific filters
        # if "lib_path" in self.config and os.path.exists(self.config["lib_path"]):

        sig = signal("template_filter")
        results = sig.send("renderer", env=self.env, config=self.config)

        for filter_function in results:
            name = filter_function[1].__name__
            self.env.filters[name] = filter_function[1]

        self.template_trees = {}
        self.template_modified_times = {}

        var_path = os.path.dirname(self.config['compiled_templates_file'])

        self.reporter.verbose("Ensure directory exists: \033[34m%s\033[0m" %
                              (var_path))
        util.ensure_dir(var_path)

        self.reporter.verbose("Loading templates from: \033[34m%s\033[0m" %
                              (self.config['templates_dir']))
        self.env.compile_templates(self.config['compiled_templates_file'],
                                   ignore_errors=False,
                                   filter_func=self.filter_hidden_files)

        self.reporter.verbose("Compile templates to .zip: \033[32m%s\033[0m" %
                              (self.config['compiled_templates_file']))

    def filter_hidden_files(self, filename):
        _name = os.path.basename(filename)
        _path = os.path.dirname(filename)

        for _segment in _path.split(os.sep):
            if _segment.startswith("."):
                return False
        if _name.startswith("."):
            return False

        self.reporter.verbose("Loading template: %s" % (_name))

        return True

    def get_template_mtime(self, template):
        """
        Request the template's mtime. If we don't have it yet, cache it.
        """
        if not template in self.template_modified_times:
            self.template_modified_times[template] = os.path.getmtime(
                "%s/%s" % (self.config['templates_dir'], template))
        return self.template_modified_times[template]

    def get_referenced_templates(self, template):
        """
        Request the template's Abstract Syntax Tree so we can find other template references.
        Store the reference list in a dictionary
        """

        if not template in self.template_trees:
            try:
                ast = self.env.parse(
                    self.env.loader.get_source(self.env, template))
                self.template_trees[template] = list(
                    meta.find_referenced_templates(ast))
            except:
                self.template_trees[template] = list()

            self.reporter.verbose(
                "\033[34m%s\033[0m references templates \033[32m%s\033[0m" %
                (template, self.template_trees[template]))

        return self.template_trees[template]

    def render(self, item, **kwargs):
        template_name = item.get_template()
        template = self.load_template(template_name)

        if template and (self.should_render_item(item, template_name)):
            sig = signal("wranglerRenderItem")
            result = sig.send('item', item=item, template_name=template_name)
            return (template.render(kwargs), item)

        return (False, item)

    def load_template(self, template_name):
        template_object = False
        try:
            template_object = self.env.get_template(template_name)
        except:
            print "\033[1;91mCouldn't parse `%s`. Check the template for errors \033[0;37m" % (
                template)

            if self.config["verbose"] == True:
                traceback.print_exc()
            print "\033[0m"
            return False
        finally:
            return template_object

    def should_render_item(self, item, template_name):
        force_render = 0
        last_build_time = self.reporter.get_last_build_time()
        output_path = item.get_output_path()

        if last_build_time == 0:
            return True

        config_modified_time = self.reporter.get_config_modified_time()

        if last_build_time == 0 or config_modified_time >= last_build_time:
            return True

        if self.config["force"] == True:
            return True

        if output_path and not os.path.exists(output_path):
            return True

        if item.get_modified_time() >= last_build_time:
            return True

        template_modified_time = 0

        if template_name:

            referenced_templates = self.get_referenced_templates(template_name)
            template_modified_time = self.get_template_mtime(template_name)

            if template_modified_time >= last_build_time:
                return True

            ref_template_mtime = 0

            for t in referenced_templates:
                time = self.get_template_mtime(t)
                if time >= last_build_time:
                    ref_template_mtime = time
                    return True

        return False
Example #9
0
class ModuleLoaderTestCase(JinjaTestCase):
    archive = None

    def compile_down(self, zip = 'deflated', py_compile = False):
        super(ModuleLoaderTestCase, self).setup()
        log = []
        self.reg_env = Environment(loader=prefix_loader)
        if zip is not None:
            self.archive = tempfile.mkstemp(suffix='.zip')[1]
        else:
            self.archive = tempfile.mkdtemp()
        self.reg_env.compile_templates(self.archive, zip=zip, log_function=log.append, py_compile=py_compile)
        self.mod_env = Environment(loader=loaders.ModuleLoader(self.archive))
        return ''.join(log)

    def teardown(self):
        super(ModuleLoaderTestCase, self).teardown()
        if hasattr(self, 'mod_env'):
            if os.path.isfile(self.archive):
                os.remove(self.archive)
            else:
                shutil.rmtree(self.archive)
            self.archive = None

    def test_log(self):
        log = self.compile_down()

    def _test_common(self):
        tmpl1 = self.reg_env.get_template('a/test.html')
        tmpl2 = self.mod_env.get_template('a/test.html')
        tmpl1 = self.reg_env.get_template('b/justdict.html')
        tmpl2 = self.mod_env.get_template('b/justdict.html')

    def test_deflated_zip_compile(self):
        self.compile_down(zip='deflated')
        self._test_common()

    def test_stored_zip_compile(self):
        self.compile_down(zip='stored')
        self._test_common()

    def test_filesystem_compile(self):
        self.compile_down(zip=None)
        self._test_common()

    def test_weak_references(self):
        self.compile_down()
        tmpl = self.mod_env.get_template('a/test.html')
        key = loaders.ModuleLoader.get_template_key('a/test.html')
        name = self.mod_env.loader.module.__name__
        self.mod_env = tmpl = None
        try:
            import gc
            gc.collect()
        except:
            pass

    def test_byte_compilation(self):
        log = self.compile_down(py_compile=True)
        tmpl1 = self.mod_env.get_template('a/test.html')
        mod = self.mod_env.loader.module.tmpl_3c4ddf650c1a73df961a6d3d2ce2752f1b8fd490

    def test_choice_loader(self):
        log = self.compile_down(py_compile=True)
        self.mod_env.loader = loaders.ChoiceLoader([self.mod_env.loader, loaders.DictLoader({'DICT_SOURCE': 'DICT_TEMPLATE'})])
        tmpl1 = self.mod_env.get_template('a/test.html')
        self.assert_equal(tmpl1.render(), 'BAR')
        tmpl2 = self.mod_env.get_template('DICT_SOURCE')
        self.assert_equal(tmpl2.render(), 'DICT_TEMPLATE')

    def test_prefix_loader(self):
        log = self.compile_down(py_compile=True)
        self.mod_env.loader = loaders.PrefixLoader({'MOD': self.mod_env.loader,
         'DICT': loaders.DictLoader({'test.html': 'DICT_TEMPLATE'})})
        tmpl1 = self.mod_env.get_template('MOD/a/test.html')
        self.assert_equal(tmpl1.render(), 'BAR')
        tmpl2 = self.mod_env.get_template('DICT/test.html')
        self.assert_equal(tmpl2.render(), 'DICT_TEMPLATE')
class JinjaStaticRenderer(Core.Renderer):

    def init(self):

        # Setup the env
        self.env = Environment(
            undefined=SilentUndefined,
            loader=FileSystemLoader(self.config['templates_dir']),
            extensions=[
                MarkdownExtension
            ],
            trim_blocks = True,
            lstrip_blocks = True
            )
        
        self.env.filters["markdown"] = markdown_filter
        self.env.filters['rst'] = rst_filter

        if not os.path.exists(self.config['templates_dir']):
            self.reporter.log("Couldn't find %s. Check your templates exist, and your config in wrangler.yaml" % (self.config['templates_dir']), "red")
            exit()

        # Load up some custom, project specific filters
        # if "lib_path" in self.config and os.path.exists(self.config["lib_path"]):

        sig = signal("template_filter")
        results = sig.send("renderer", env=self.env, config=self.config)

        for filter_function in results:
            name = filter_function[1].__name__
            self.env.filters[name] = filter_function[1]


        self.template_trees = {}
        self.template_modified_times = {}
        
        var_path = os.path.dirname(self.config['compiled_templates_file'])

        self.reporter.verbose("Ensure directory exists: \033[34m%s\033[0m" % (var_path))
        util.ensure_dir(var_path)

        self.reporter.verbose("Loading templates from: \033[34m%s\033[0m" % (self.config['templates_dir']))
        self.env.compile_templates(
            self.config['compiled_templates_file'],
            ignore_errors=False,
            filter_func=self.filter_hidden_files 
            )

        self.reporter.verbose("Compile templates to .zip: \033[32m%s\033[0m" % (self.config['compiled_templates_file']))
    
    def filter_hidden_files(self, filename):
        _name = os.path.basename(filename)
        _path = os.path.dirname(filename)

        for _segment in _path.split(os.sep):
            if _segment.startswith("."):
                return False
        if _name.startswith("."):
            return False;
        
        self.reporter.verbose("Loading template: %s" % (_name))

        return True;


    def get_template_mtime(self, template):
        """
        Request the template's mtime. If we don't have it yet, cache it.
        """
        if not template in self.template_modified_times:
            self.template_modified_times[template] = os.path.getmtime("%s/%s" % (self.config['templates_dir'], template) )
        return self.template_modified_times[template]


    def get_referenced_templates(self, template):
        """
        Request the template's Abstract Syntax Tree so we can find other template references.
        Store the reference list in a dictionary
        """

        if not template in self.template_trees:
            try: 
                ast = self.env.parse(self.env.loader.get_source(self.env, template))
                self.template_trees[template] = list(meta.find_referenced_templates(ast))
            except:
                self.template_trees[template] = list()

            self.reporter.verbose("\033[34m%s\033[0m references templates \033[32m%s\033[0m" % (template, self.template_trees[template]))

        return self.template_trees[template]


    def render(self, item, **kwargs):
        template_name = item.get_template()
        template = self.load_template(template_name)

        if template and (self.should_render_item(item, template_name)):
            sig = signal("wranglerRenderItem")
            result = sig.send('item', item=item, template_name=template_name)
            return (template.render(kwargs), item)

        return (False, item)


    def load_template(self, template_name):
        template_object = False
        try:
            template_object = self.env.get_template(template_name)
        except:
            print "\033[1;91mCouldn't parse `%s`. Check the template for errors \033[0;37m" % (template)
            
            if self.config["verbose"] == True:
                traceback.print_exc()
            print "\033[0m"
            return False
        finally:
            return template_object


    def should_render_item(self, item, template_name):
        force_render = 0
        last_build_time = self.reporter.get_last_build_time()
        output_path = item.get_output_path()

        if last_build_time == 0:
            return True

        config_modified_time = self.reporter.get_config_modified_time()
        
        if last_build_time == 0 or config_modified_time >= last_build_time:
            return True

        if self.config["force"] == True:
            return True

        if output_path and not os.path.exists(output_path):
            return True
        
        if item.get_modified_time() >= last_build_time:
            return True

        template_modified_time = 0

        if template_name:

            referenced_templates = self.get_referenced_templates(template_name)
            template_modified_time = self.get_template_mtime(template_name)
            
            if template_modified_time >= last_build_time:
                return True

            ref_template_mtime = 0

            for t in referenced_templates:
                time = self.get_template_mtime(t)
                if time >= last_build_time:
                    ref_template_mtime = time
                    return True

        return False
class TestModuleLoader(object):
    archive = None

    def compile_down(self, prefix_loader, zip="deflated", py_compile=False):
        log = []
        self.reg_env = Environment(loader=prefix_loader)
        if zip is not None:
            fd, self.archive = tempfile.mkstemp(suffix=".zip")
            os.close(fd)
        else:
            self.archive = tempfile.mkdtemp()
        self.reg_env.compile_templates(
            self.archive, zip=zip, log_function=log.append, py_compile=py_compile
        )
        self.mod_env = Environment(loader=loaders.ModuleLoader(self.archive))
        return "".join(log)

    def teardown(self):
        if hasattr(self, "mod_env"):
            if os.path.isfile(self.archive):
                os.remove(self.archive)
            else:
                shutil.rmtree(self.archive)
            self.archive = None

    def test_log(self, prefix_loader):
        log = self.compile_down(prefix_loader)
        assert (
            'Compiled "a/foo/test.html" as '
            "tmpl_a790caf9d669e39ea4d280d597ec891c4ef0404a" in log
        )
        assert "Finished compiling templates" in log
        assert (
            'Could not compile "a/syntaxerror.html": '
            "Encountered unknown tag 'endif'" in log
        )

    def _test_common(self):
        tmpl1 = self.reg_env.get_template("a/test.html")
        tmpl2 = self.mod_env.get_template("a/test.html")
        assert tmpl1.render() == tmpl2.render()

        tmpl1 = self.reg_env.get_template("b/justdict.html")
        tmpl2 = self.mod_env.get_template("b/justdict.html")
        assert tmpl1.render() == tmpl2.render()

    def test_deflated_zip_compile(self, prefix_loader):
        self.compile_down(prefix_loader, zip="deflated")
        self._test_common()

    def test_stored_zip_compile(self, prefix_loader):
        self.compile_down(prefix_loader, zip="stored")
        self._test_common()

    def test_filesystem_compile(self, prefix_loader):
        self.compile_down(prefix_loader, zip=None)
        self._test_common()

    def test_weak_references(self, prefix_loader):
        self.compile_down(prefix_loader)
        self.mod_env.get_template("a/test.html")
        key = loaders.ModuleLoader.get_template_key("a/test.html")
        name = self.mod_env.loader.module.__name__

        assert hasattr(self.mod_env.loader.module, key)
        assert name in sys.modules

        # unset all, ensure the module is gone from sys.modules
        self.mod_env = None

        try:
            import gc

            gc.collect()
        except BaseException:
            pass

        assert name not in sys.modules

    # This test only makes sense on non-pypy python 2
    @pytest.mark.skipif(
        not (PY2 and not PYPY), reason="This test only makes sense on non-pypy python 2"
    )
    def test_byte_compilation(self, prefix_loader):
        log = self.compile_down(prefix_loader, py_compile=True)
        assert 'Byte-compiled "a/test.html"' in log
        self.mod_env.get_template("a/test.html")
        mod = self.mod_env.loader.module.tmpl_3c4ddf650c1a73df961a6d3d2ce2752f1b8fd490
        assert mod.__file__.endswith(".pyc")

    def test_choice_loader(self, prefix_loader):
        self.compile_down(prefix_loader)
        self.mod_env.loader = loaders.ChoiceLoader(
            [self.mod_env.loader, loaders.DictLoader({"DICT_SOURCE": "DICT_TEMPLATE"})]
        )
        tmpl1 = self.mod_env.get_template("a/test.html")
        assert tmpl1.render() == "BAR"
        tmpl2 = self.mod_env.get_template("DICT_SOURCE")
        assert tmpl2.render() == "DICT_TEMPLATE"

    def test_prefix_loader(self, prefix_loader):
        self.compile_down(prefix_loader)
        self.mod_env.loader = loaders.PrefixLoader(
            {
                "MOD": self.mod_env.loader,
                "DICT": loaders.DictLoader({"test.html": "DICT_TEMPLATE"}),
            }
        )
        tmpl1 = self.mod_env.get_template("MOD/a/test.html")
        assert tmpl1.render() == "BAR"
        tmpl2 = self.mod_env.get_template("DICT/test.html")
        assert tmpl2.render() == "DICT_TEMPLATE"

    @pytest.mark.skipif(PY2, reason="pathlib is not available in Python 2")
    def test_path_as_pathlib(self, prefix_loader):
        self.compile_down(prefix_loader)

        mod_path = self.mod_env.loader.module.__path__[0]

        import pathlib

        mod_loader = loaders.ModuleLoader(pathlib.Path(mod_path))
        self.mod_env = Environment(loader=mod_loader)

        self._test_common()

    @pytest.mark.skipif(PY2, reason="pathlib is not available in Python 2")
    def test_supports_pathlib_in_list_of_paths(self, prefix_loader):
        self.compile_down(prefix_loader)

        mod_path = self.mod_env.loader.module.__path__[0]

        import pathlib

        mod_loader = loaders.ModuleLoader([pathlib.Path(mod_path), "/tmp/templates"])
        self.mod_env = Environment(loader=mod_loader)

        self._test_common()
Example #12
0
import os.path
from jinja2 import Environment, FileSystemLoader

here = os.path.dirname(__file__)
e = Environment(loader=FileSystemLoader(here))
t = e.get_template("main.j2")
e.compile_templates(here, zip=None, log_function=print)
Example #13
0
class TestModuleLoader:
    archive = None

    def compile_down(self, prefix_loader, zip="deflated", py_compile=False):
        log = []
        self.reg_env = Environment(loader=prefix_loader)
        if zip is not None:
            fd, self.archive = tempfile.mkstemp(suffix=".zip")
            os.close(fd)
        else:
            self.archive = tempfile.mkdtemp()
        self.reg_env.compile_templates(self.archive, zip=zip, log_function=log.append, py_compile=py_compile)
        self.mod_env = Environment(loader=loaders.ModuleLoader(self.archive))
        return "".join(log)

    def teardown(self):
        if hasattr(self, "mod_env"):
            if os.path.isfile(self.archive):
                os.remove(self.archive)
            else:
                shutil.rmtree(self.archive)
            self.archive = None

    def test_log(self, prefix_loader):
        log = self.compile_down(prefix_loader)
        assert 'Compiled "a/foo/test.html" as ' "tmpl_a790caf9d669e39ea4d280d597ec891c4ef0404a" in log
        assert "Finished compiling templates" in log
        assert 'Could not compile "a/syntaxerror.html": ' "Encountered unknown tag 'endif'" in log

    def _test_common(self):
        tmpl1 = self.reg_env.get_template("a/test.html")
        tmpl2 = self.mod_env.get_template("a/test.html")
        assert tmpl1.render() == tmpl2.render()

        tmpl1 = self.reg_env.get_template("b/justdict.html")
        tmpl2 = self.mod_env.get_template("b/justdict.html")
        assert tmpl1.render() == tmpl2.render()

    def test_deflated_zip_compile(self, prefix_loader):
        self.compile_down(prefix_loader, zip="deflated")
        self._test_common()

    def test_stored_zip_compile(self, prefix_loader):
        self.compile_down(prefix_loader, zip="stored")
        self._test_common()

    def test_filesystem_compile(self, prefix_loader):
        self.compile_down(prefix_loader, zip=None)
        self._test_common()

    def test_weak_references(self, prefix_loader):
        self.compile_down(prefix_loader)
        tmpl = self.mod_env.get_template("a/test.html")
        key = loaders.ModuleLoader.get_template_key("a/test.html")
        name = self.mod_env.loader.module.__name__

        assert hasattr(self.mod_env.loader.module, key)
        assert name in sys.modules

        # unset all, ensure the module is gone from sys.modules
        self.mod_env = tmpl = None

        try:
            import gc

            gc.collect()
        except:
            pass

        assert name not in sys.modules

    # This test only makes sense on non-pypy python 2
    @pytest.mark.skipif(not (PY2 and not PYPY), reason="This test only makes sense on non-pypy python 2")
    def test_byte_compilation(self, prefix_loader):
        log = self.compile_down(prefix_loader, py_compile=True)
        assert 'Byte-compiled "a/test.html"' in log
        tmpl1 = self.mod_env.get_template("a/test.html")
        mod = self.mod_env.loader.module.tmpl_3c4ddf650c1a73df961a6d3d2ce2752f1b8fd490
        assert mod.__file__.endswith(".pyc")

    def test_choice_loader(self, prefix_loader):
        log = self.compile_down(prefix_loader)

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

        tmpl1 = self.mod_env.get_template("a/test.html")
        assert tmpl1.render() == "BAR"
        tmpl2 = self.mod_env.get_template("DICT_SOURCE")
        assert tmpl2.render() == "DICT_TEMPLATE"

    def test_prefix_loader(self, prefix_loader):
        log = self.compile_down(prefix_loader)

        self.mod_env.loader = loaders.PrefixLoader(
            {"MOD": self.mod_env.loader, "DICT": loaders.DictLoader({"test.html": "DICT_TEMPLATE"})}
        )

        tmpl1 = self.mod_env.get_template("MOD/a/test.html")
        assert tmpl1.render() == "BAR"
        tmpl2 = self.mod_env.get_template("DICT/test.html")
        assert tmpl2.render() == "DICT_TEMPLATE"
Example #14
0
class ModuleLoaderTestCase(JinjaTestCase):
    archive = None

    def compile_down(self, zip = 'deflated', py_compile = False):
        super(ModuleLoaderTestCase, self).setup()
        log = []
        self.reg_env = Environment(loader=prefix_loader)
        if zip is not None:
            self.archive = tempfile.mkstemp(suffix='.zip')[1]
        else:
            self.archive = tempfile.mkdtemp()
        self.reg_env.compile_templates(self.archive, zip=zip, log_function=log.append, py_compile=py_compile)
        self.mod_env = Environment(loader=loaders.ModuleLoader(self.archive))
        return ''.join(log)

    def teardown(self):
        super(ModuleLoaderTestCase, self).teardown()
        if hasattr(self, 'mod_env'):
            if os.path.isfile(self.archive):
                os.remove(self.archive)
            else:
                shutil.rmtree(self.archive)
            self.archive = None

    def test_log(self):
        log = self.compile_down()

    def _test_common(self):
        tmpl1 = self.reg_env.get_template('a/test.html')
        tmpl2 = self.mod_env.get_template('a/test.html')
        tmpl1 = self.reg_env.get_template('b/justdict.html')
        tmpl2 = self.mod_env.get_template('b/justdict.html')

    def test_deflated_zip_compile(self):
        self.compile_down(zip='deflated')
        self._test_common()

    def test_stored_zip_compile(self):
        self.compile_down(zip='stored')
        self._test_common()

    def test_filesystem_compile(self):
        self.compile_down(zip=None)
        self._test_common()

    def test_weak_references(self):
        self.compile_down()
        tmpl = self.mod_env.get_template('a/test.html')
        key = loaders.ModuleLoader.get_template_key('a/test.html')
        name = self.mod_env.loader.module.__name__
        self.mod_env = tmpl = None
        try:
            import gc
            gc.collect()
        except:
            pass

    def test_byte_compilation(self):
        log = self.compile_down(py_compile=True)
        tmpl1 = self.mod_env.get_template('a/test.html')
        mod = self.mod_env.loader.module.tmpl_3c4ddf650c1a73df961a6d3d2ce2752f1b8fd490

    def test_choice_loader(self):
        log = self.compile_down(py_compile=True)
        self.mod_env.loader = loaders.ChoiceLoader([self.mod_env.loader, loaders.DictLoader({'DICT_SOURCE': 'DICT_TEMPLATE'})])
        tmpl1 = self.mod_env.get_template('a/test.html')
        self.assert_equal(tmpl1.render(), 'BAR')
        tmpl2 = self.mod_env.get_template('DICT_SOURCE')
        self.assert_equal(tmpl2.render(), 'DICT_TEMPLATE')

    def test_prefix_loader(self):
        log = self.compile_down(py_compile=True)
        self.mod_env.loader = loaders.PrefixLoader({'MOD': self.mod_env.loader,
         'DICT': loaders.DictLoader({'test.html': 'DICT_TEMPLATE'})})
        tmpl1 = self.mod_env.get_template('MOD/a/test.html')
        self.assert_equal(tmpl1.render(), 'BAR')
        tmpl2 = self.mod_env.get_template('DICT/test.html')
        self.assert_equal(tmpl2.render(), 'DICT_TEMPLATE')