def _generate_envs_tofrom(self): """ this generates ashes environments for the to/from tests """ ashesEnvSrc = ashes.AshesEnv(paths=[ utils._chert_dir, ]) ashesEnvDest = ashes.AshesEnv() self._ChertData.register_alt_templates(ashesEnvDest) return (ashesEnvSrc, ashesEnvDest)
def test_loaded_template_conversion(self): """ there had been an earlier uncaught edgecase, in which a loaded template did not generate the right data """ ashesEnv = ashes.AshesEnv(paths=self._SimpleFruitData.directory) for _fruit in self._SimpleFruitData.template_source.keys(): _template = ashesEnv.load(_fruit) _as_ast = _template.to_ast() self.assertEquals( _as_ast, self._SimpleFruitData.compiled_template_data[_fruit]['ast']) _as_python_string = _template.to_python_string() self.assertEquals( _as_python_string, self._SimpleFruitData.compiled_template_data[_fruit] ['python_string']) _as_python_code = _template.to_python_code() self.assertEquals( _as_python_code, self._SimpleFruitData.compiled_template_data[_fruit] ['python_code']) # this will never equate, but it must run! _as_python_func = _template.to_python_func()
def test_AshesEnv_path(self): """this tests an ashesEnv initialized with a path""" ashesEnv = ashes.AshesEnv(paths=self._SimpleFruitData.directory) for _fruit in self._SimpleFruitData.template_source.keys(): _rendered = ashesEnv.render(_fruit, {}) self.assertEquals(_rendered, self._SimpleFruitData.renders_expected[_fruit])
def bench_render_repeat(): """ This bench is designed as a baseline for performance comparisons when adjusting the code. A single template loader is re-used for all iterations """ print("running benchmarks: bench_render_repeat...") if utils.ChertDefaults is None: utils.ChertDefaults = utils._ChertDefaults() _ashesLoader = ashes.TemplatePathLoader(utils._chert_dir) _ashesEnv = ashes.AshesEnv(loaders=(_ashesLoader, )) def test_baseline_chert(): """ test_baseline_chert this just runs though all the chert templates using a default `TemplatePathLoader` """ renders = {} for (fname, fdata) in utils.ChertDefaults.chert_data.items(): rendered = _ashesEnv.render(fname, fdata) renders[fname] = fdata timed = {} ranged = range(0, 1000) timed["baseline_chert"] = [] for i in ranged: t_start = time.time() test_baseline_chert() t_fin = time.time() timed["baseline_chert"].append(t_fin - t_start) utils.print_timed(timed)
def test_TemplatePathLoader(self): """this tests an ashesEnv initialized with a `loaders=ashes.TemplatePathLoader()`""" ashesLoader = ashes.TemplatePathLoader( root_path=self._SimpleFruitData.directory) ashesEnv = ashes.AshesEnv(loaders=(ashesLoader, )) for _fruit in self._SimpleFruitData.template_source.keys(): _rendered = ashesEnv.render(_fruit, {}) self.assertEquals(_rendered, self._SimpleFruitData.renders_expected[_fruit])
def main(content_name, output_name): with codecs.open(content_name, 'r') as file: contents = file.read() from_yaml = yaml.load_all(contents) page_dict = parse_page_contents(from_yaml) ashes_env = ashes.AshesEnv(['./templates'], keep_whitespace=True) rendered = ashes_env.render('index.dust', page_dict) with codecs.open(output_name, 'w', 'utf-8') as file: file.write(rendered) print 'successfully generated %s' % output_name
def test_baseline_chert(): """ test_baseline_chert this just runs though all the chert templates using a default `TemplatePathLoader` """ 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_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_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
def save_rendered(outfile_name, template_name, context): global ASHES_ENV # retain laziness if not ASHES_ENV: ASHES_ENV = ashes.AshesEnv([TEMPLATE_PATH], keep_whitespace=True) rendered = ASHES_ENV.render(template_name, context) try: out_file = codecs.open(outfile_name, 'w', 'utf-8') except IOError: mkdir_p(dirname(outfile_name)) out_file = codecs.open(outfile_name, 'w', 'utf-8') with out_file: out_file.write(rendered) print 'successfully generated %s' % outfile_name
def generate_index(pypier_path): packages = [] pkgs_path = pypier_path + '/packages' for pkg_name in os.listdir(pkgs_path): cur_pkg_path = os.path.join(pkgs_path, pkg_name) if not os.path.isdir(cur_pkg_path): continue cur_pkg = {'name': pkg_name} pkg_info_path = os.path.join(cur_pkg_path, 'pkg_info.json') pkg_info = json.load(open(pkg_info_path)) if pkg_info['name'] != pkg_name: print 'warning: package name/info mismatch for %r: %r' % ( pkg_name, pkg_info_path) cur_pkg['info'] = pkg_info versions = [] for release_fn in os.listdir(cur_pkg_path): if release_fn.split('-')[0] != pkg_name: continue # splitext doesn't work because .tar.gz # always the second item, even with wheels version = _strip_pkg_ext(release_fn).split('-')[1] versions.append({ 'path': os.path.join(pkg_name, release_fn), 'version': version }) # TODO: do better than alphabetical sort cur_pkg['versions'] = sorted(versions, key=lambda x: x['version'], reverse=True) packages.append(cur_pkg) ae = ashes.AshesEnv() ae.register_source('pkg_idx', INDEX_TMPL) ae.register_source('readme', README_TMPL) ctx = { 'packages': sorted(packages, key=lambda x: x['name']), 'gen_date': datetime.datetime.utcnow().isoformat() } index = ae.render('pkg_idx', ctx) readme = ae.render('readme', ctx) return index, readme
def test_cachable_templates(self): """ can we cache the templates in all these ways? this just ensures the renderer can be built """ render_fails = {} for _source_text in ( 'all', 'ast', 'python_string', 'python_code', ): # build a new cacheable templates... ashesLoader = utils.TemplatesLoader() ashesEnv = ashes.AshesEnv(loaders=(ashesLoader, )) ashesLoader.load_from_cacheable( self._ChertData.cacheable_templates_typed[_source_text]) for (fname, fdata) in self._ChertData.chert_data.items(): rendered = ashesEnv.render(fname, fdata) if rendered != self._ChertData.renders_expected[fname]: if _source_text not in render_fails: render_fails[_source_text] = {} render_fails[_source_text][fname] = rendered self.assertEqual(len(render_fails.keys()), 0)
def setUp(self): self.env = ashes.AshesEnv(defaults={'baz': 3}) self.env.register_source('basic_defaults_tmpl', '{foo} {bar} {baz} {blep}.')
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)
def __init__(self): # Globals Setup # load all the chert templates _chert_files_all = os.listdir(_chert_dir) # we only want the .html templates _chert_files_html = [i for i in _chert_files_all if i[-5:] == '.html'] # we only want the .html templates _chert_files_html_json = [ i for i in _chert_files_all if i[-10:] == '.html.json' ] # load the json data # and load html files that don't have json chert_data = {} chert_files_html_alt = {} for f in _chert_files_html: f_json = f + '.json' if f_json in _chert_files_html_json: _fpath = os.path.join(_chert_dir, f_json) json_data = codecs.open(_fpath, 'r', 'utf-8').read() chert_data[f] = json.loads(json_data) else: _fpath = os.path.join(_chert_dir, f) chert_files_html_alt[f] = codecs.open(_fpath, 'r', 'utf-8').read() self.chert_data = chert_data self.chert_files_html_alt = chert_files_html_alt # okay, let's generate the expected data... renders_expected = {} ashesEnv = ashes.AshesEnv(paths=[ _chert_dir, ], ) for (fname, fdata) in chert_data.items(): rendered = ashesEnv.render(fname, fdata) renders_expected[fname] = rendered self.renders_expected = renders_expected # and generate some cacheable templates ashesPreloader = TemplatesLoader(directory=_chert_dir) cacheable_templates = ashesPreloader.generate_all_cacheable() self.cacheable_templates = cacheable_templates # make an isolated version for testing... cacheable_templates_typed = { 'all': {}, 'ast': {}, 'python_string': {}, 'python_code': {}, } for (k, payload) in cacheable_templates.items(): cacheable_templates_typed['all'][k] = payload cacheable_templates_typed['ast'][k] = { 'ast': payload['ast'], } cacheable_templates_typed['python_string'][k] = { 'python_string': payload['python_string'], } cacheable_templates_typed['python_code'][k] = { 'python_code': payload['python_code'], } self.cacheable_templates_typed = cacheable_templates_typed