Esempio n. 1
0
 def load_precompiled(
     self,
     template_name,
     source=None,
     source_ast=None,
     source_python_string=None,
     source_python_code=None,
     source_python_func=None,
 ):
     if source_python_func:
         template = ashes.Template.from_python_func(source_python_func,
                                                    name=template_name)
     elif source_python_code:
         template = ashes.Template.from_python_code(source_python_code,
                                                    name=template_name)
     elif source_python_string:
         template = ashes.Template.from_python_string(source_python_string,
                                                      name=template_name)
     elif source_ast:
         template = ashes.Template.from_ast(source_ast, name=template_name)
     elif source:
         template = ashes.Template(
             template_name,
             source,
         )
     else:
         raise ValueError("nothing to load")
     return template
Esempio n. 2
0
 def register_alt_templates(self, ashesEnv):
     """this function registers the non-json templates into an env
        this is needed for some tests, as we don't load them otherwise.
     """
     for fname in self.chert_files_html_alt:
         template = ashes.Template(fname, self.chert_files_html_alt[fname])
         ashesEnv.register(template, fname)
Esempio n. 3
0
 def load_all(self):
     """
     DEMO
     our load_all will just create template objects from
     the _template_source_dust files
     """
     for _template_name in self._template_source_dust.keys():
         if _template_name not in self._template_objects:
             self._template_objects[_template_name] = ashes.Template(
                 _template_name, self._template_source_dust[_template_name])
Esempio n. 4
0
 def test_template_init_source(self):
     """
     this tests a template can be initialized from source.
     this does not test the templates with dependencies, as they must be executed with a loader
     """
     for _fruit in self._SimpleFruitData.template_source.keys():
         if _fruit not in self._SimpleFruitData.template_dependencies:
             _template = ashes.Template(
                 _fruit, self._SimpleFruitData.template_source[_fruit])
             _rendered = _template.render({})
             self.assertEquals(
                 _rendered, self._SimpleFruitData.renders_expected[_fruit])
Esempio n. 5
0
 def test_template_init_source_file(self):
     """
     this tests a template can be initialized from a source_file argument.
     this does not test the templates with dependencies, as they must be executed with a loader
     """
     for _fruit in self._SimpleFruitData.template_source.keys():
         if _fruit not in self._SimpleFruitData.template_dependencies:
             _source_file = os.path.join(self._SimpleFruitData.directory,
                                         _fruit)
             _template = ashes.Template(_fruit,
                                        None,
                                        source_file=_source_file)
             _rendered = _template.render({})
             self.assertEquals(
                 _rendered, self._SimpleFruitData.renders_expected[_fruit])
Esempio n. 6
0
    def __init__(self):
        """load data"""
        _files_all = os.listdir(_simple_fruit_dir)
        _files_dust = [i for i in _files_all if i[-5:] == '.dust']
        _files_html = [i for i in _files_all if i[-5:] == '.html']

        _template_source = {}
        for _fname in _files_dust:
            _fpath = os.path.join(_simple_fruit_dir, _fname)
            _template_source[_fname] = codecs.open(_fpath, 'r', 'utf-8').read()

        _renders_expected = {}
        for _fname in _files_html:
            _fpath = os.path.join(_simple_fruit_dir, _fname)
            _fname_adjusted = _fname[:-5]  # strip off the .html
            _renders_expected[_fname_adjusted] = codecs.open(
                _fpath, 'r', 'utf-8').read()

        self.directory = _simple_fruit_dir
        self.template_source = _template_source
        self.renders_expected = _renders_expected
        self.template_dependencies = {
            'all.dust': [
                'bananas.dust',
                'citrus.dust',
                'dragonfruits.dust',
                'elderflower.dust',
            ],
            'oranges.dust': [
                'apples.dust',
            ],
        }

        compiled_template_data = {}
        for _fname in _files_dust:
            template_obj = ashes.Template(_fname, self.template_source[_fname])
            compiled_template_data[_fname] = {
                'obj': template_obj,
                'ast': template_obj.to_ast(),
                'python_string': template_obj.to_python_string(),
                'python_code': template_obj.to_python_code(),
                'python_func': template_obj.to_python_func(),
            }
        self.compiled_template_data = compiled_template_data
Esempio n. 7
0
    def load(self, template_name, env=None):
        """
        DEMO
        our load_all will just create template objects from
        the _template_source_dust files
        """
        # did we already compile this into a template object?
        if template_name in self._template_objects:
            return self._template_objects[template_name]

        # maybe we have the source?
        if template_name in self._template_source_dust:
            # build a template
            _templateObj = ashes.Template(
                template_name, self._template_source_dust[template_name])

            # stash the source
            self._template_objects[template_name] = _templateObj

            # return the template
            return _templateObj

        raise ashes.TemplateNotFound(template_name)
Esempio n. 8
0
def bench_cacheable_templates():
    """
    This just runs a few strategies of template generation to compare against 
    one another
    """
    print("running benchmarks: bench_cacheable_templates...")

    if utils.ChertDefaults is None:
        utils.ChertDefaults = utils._ChertDefaults()

    ashesDataLoader = utils.TemplatePathLoaderExtended(utils._chert_dir)
    ashesEnvLoader = ashes.AshesEnv(loaders=(ashesDataLoader, ))

    templateData = {
        'ast': {},
        'python_string': {},
        'python_code': {},
        'python_code-marshal': {},
        'python_func': {},
    }
    for (fname, fdata) in utils.ChertDefaults.chert_data.items():
        templateData['ast'][fname] = ashesEnvLoader.load(fname).to_ast()
        templateData['python_string'][fname] = ashesEnvLoader.load(
            fname).to_python_string()
        templateData['python_code'][fname] = ashes.python_string_to_code(
            templateData['python_string'][fname])
        templateData['python_code-marshal'][fname] = marshal.dumps(
            templateData['python_code'][fname])
        templateData['python_func'][fname] = ashes.python_string_to_function(
            templateData['python_string'][fname])

    # generate the inherited templates
    for (fname, fdata) in utils.ChertDefaults.chert_files_html_alt.items():
        template = ashes.Template(
            fname, utils.ChertDefaults.chert_files_html_alt[fname])
        ashesEnvLoader.register(template, fname)
        templateData['ast'][fname] = ashesEnvLoader.load(fname).to_ast()
        templateData['python_string'][fname] = ashesEnvLoader.load(
            fname).to_python_string()
        templateData['python_code'][fname] = ashes.python_string_to_code(
            templateData['python_string'][fname])
        templateData['python_code-marshal'][fname] = marshal.dumps(
            templateData['python_code'][fname])
        templateData['python_func'][fname] = ashes.python_string_to_function(
            templateData['python_string'][fname])

    def test_persisted():
        """
        test_persisted
        this is the normal way to use ashes.  a single persistent env/template.
        """
        renders = {}
        for (fname, fdata) in utils.ChertDefaults.chert_data.items():
            rendered = ashesEnvLoader.render(fname, fdata)
            renders[fname] = fdata

    def test_baseline():
        """
        test_baseline
        this should be the longest and the normal ashes behavior
        """
        renders = {}
        _ashesLoader = ashes.TemplatePathLoader(utils._chert_dir)
        _ashesEnv = ashes.AshesEnv(loaders=(_ashesLoader, ))
        for (fname, fdata) in utils.ChertDefaults.chert_data.items():
            rendered = _ashesEnv.render(fname, fdata)
            renders[fname] = fdata

    def test_ast():
        _ashesLoader = utils.TemplatePathLoaderExtended()
        _ashesEnv = ashes.AshesEnv(loaders=(_ashesLoader, ))
        renders = {}
        for (fname, fdata) in templateData['ast'].items():
            _ashesEnv.register(
                _ashesLoader.load_precompiled(fname, source_ast=fdata),
                name=fname,
            )
        for (fname, fdata) in utils.ChertDefaults.chert_data.items():
            rendered = _ashesEnv.render(fname, fdata)
            renders[fname] = fdata

    def test_python_string():
        _ashesLoader = utils.TemplatePathLoaderExtended()
        _ashesEnv = ashes.AshesEnv(loaders=(_ashesLoader, ))
        renders = {}
        for (fname, fdata) in templateData['python_string'].items():
            _ashesEnv.register(
                _ashesLoader.load_precompiled(fname,
                                              source_python_string=fdata),
                name=fname,
            )
        for (fname, fdata) in utils.ChertDefaults.chert_data.items():
            rendered = _ashesEnv.render(fname, fdata)
            renders[fname] = fdata

    def test_python_code():
        _ashesLoader = utils.TemplatePathLoaderExtended()
        _ashesEnv = ashes.AshesEnv(loaders=(_ashesLoader, ))
        renders = {}
        for (fname, fdata) in templateData['python_code-marshal'].items():
            fdata = marshal.loads(fdata)
            _ashesEnv.register(
                _ashesLoader.load_precompiled(fname, source_python_code=fdata),
                name=fname,
            )
        for (fname, fdata) in utils.ChertDefaults.chert_data.items():
            rendered = _ashesEnv.render(fname, fdata)
            renders[fname] = fdata

    def test_python_func():
        _ashesLoader = utils.TemplatePathLoaderExtended()
        _ashesEnv = ashes.AshesEnv(loaders=(_ashesLoader, ))
        renders = {}
        for (fname, fdata) in templateData['python_func'].items():
            _ashesEnv.register(
                _ashesLoader.load_precompiled(fname, source_python_func=fdata),
                name=fname,
            )
        for (fname, fdata) in utils.ChertDefaults.chert_data.items():
            rendered = _ashesEnv.render(fname, fdata)
            renders[fname] = fdata

    # test_persisted()
    # test_baseline()
    # test_ast()
    # test_python_string()
    # test_python_code()
    # test_python_func()

    timed = {}
    ranged = range(0, 100)
    for t in (
        ('test_baseline', test_baseline),
        ('test_ast', test_ast),
        ('test_python_string', test_python_string),
        ('test_python_code', test_python_code),
        ('test_python_func', test_python_func),
        ('test_persisted', test_persisted),
    ):
        print(" .%s" % t[0])
        timed[t[0]] = []
        for i in ranged:
            t_start = time.time()
            t[1]()
            t_fin = time.time()
            timed[t[0]].append(t_fin - t_start)
    utils.print_timed(timed)