def getConfigHtml(self): filepath = os.path.join(self.get_plugin_isntall_path()["path"], "config.ji2") with open(filepath, "r") as f: tpl = f.read() env = Environment(loader=DictLoader({"this": tpl}), extensions=["jinja2.ext.i18n"]) elementTemplate = env.get_template("this") return elementTemplate.render(plugin_instance=self.instance, plugin_identifier=self.identifier)
def generate_pages(destination): """Generate static pages. """ # Check if the pages folder exists. if not os.path.exists("pages"): print("Unable to find pages folder... " + red("abort")) return # Load template engine. env = Environment() # Load all files from the pages folder and from the template # folder, to get the skeleton and the content. env.loader = FileSystemLoader(["pages", "template"]) # Loop through all static pages. for page in os.listdir("pages"): dest = os.path.join(destination, page) print("Generating page {0}...".format(dest)), # Load the template for the current page. template = env.get_template(page) # Generate the HTML output for the static page. html = template.render(dict( blog=cfg["blog"], page=page )) # Write the HTML to the destination file. with open(dest, "w") as handle: handle.write(html) print(green("done"))
class RegisterLogin(object): env = None def __init__(self): # Référence au dossier HTML self.env = Environment(loader=FileSystemLoader('html')) def inscription(self): # Charger et compléter le template HTML return self.env.get_template('creationCompte.html').render() def confirmerSMS(self): # Charger et compléter le template HTML return self.env.get_template('confirmationCompteSMS.html').render() def confirmerEmail(self): # Charger et compléter le template HTML return self.env.get_template('confirmationCompteEmail.html').render() def login(self, user_name=None, password=None): # Charger et compléter le template HTML if user_name and password: # verify login print(cherrypy.url()) # after login cherrypy.session[SESSION_KEY] = cherrypy.request.login = user_name raise cherrypy.HTTPRedirect("/compte") else: return self.env.get_template('login.html').render() def logout(self): cherrypy.session[SESSION_KEY] = None cherrypy.request.login = None raise cherrypy.HTTPRedirect("/")
def test_conf_generated_ok(self): '''This test case ensures configuration file is generated correctly and added to nginx enabled sites.''' self._args.extend([ "--ipaddress", "127.0.0.1", "--vhost-name", "test-app.com", "--uwsgi-port", "12090", "--root-folder", "/test/folder/vhost", "--modules-folder", "/"]) root_folder = os.path.abspath(instantiator.get_class_abslocation(BasicSettings) + "../") tpl_loader = FileSystemLoader(searchpath=root_folder) tpl_env = Environment(loader=tpl_loader) config_data = {"ip_address": "127.0.0.1", "vhost_name": "test-app.com", "http_port": 80, "uwsgi_port": 12090, "root_folder": "/test/folder/vhost", "modules_folder": "/"} expected_config = tpl_env.get_template("/deployment/conf/nginx/fantastico-wsgi").render(config_data) config = self._config_nginx(self._args) self.assertEqual(expected_config, config)
def render_message_list(msg_list): env = Environment() env.loader = FileSystemLoader('.') template = env.get_template('static/msg_list_template.html') env.globals['date_render'] = _human_date_render env.globals['msg_render'] = _human_msg_render return template.render(msg_list=msg_list)
def test_config_generation_ok(self): '''This test case ensures config nginx script works as expected.''' settings_cls = SettingsFacade root_folder = os.path.abspath(instantiator.get_class_abslocation(settings_cls) + "../") tpl_loader = FileSystemLoader(searchpath=root_folder) tpl_env = Environment(loader=tpl_loader) config_data = {"ip_address": "127.0.0.1", "vhost_name": "test-app.com", "http_port": 80, "uwsgi_port": 12090, "root_folder": "/test/folder/vhost", "modules_folder": "/"} expected_config = tpl_env.get_template("/deployment/conf/nginx/fantastico-wsgi").render(config_data) sys.argv = ["config_nginx.py", "--ipaddress", "127.0.0.1", "--vhost-name", "test-app.com", "--uwsgi-port", "12090", "--root-folder", "/test/folder/vhost", "--modules-folder", "/"] from fantastico.deployment import config_nginx generated_config = StringIO() config_nginx.main(generated_config) self.assertEqual(expected_config, generated_config.getvalue())
def run(self): start_time = time() try: server_list = browse_server(**self.params) except URLError: self.network_error_signal.emit() else: time_elapsed = round(time() - start_time, 2) # PyInstaller extracts the files (inc. layout.html) to a temp dir and its path can is stored in sys._MEIPASS if getattr(sys, 'frozen', None): basedir = sys._MEIPASS else: basedir = os.path.dirname(__file__) or '.' template_file = open(basedir + '\\layout.html').read().decode('utf8') template_env = Environment() template_args = dict(servers=enumerate(server_list), bf3=BF3Server, time_elapsed=time_elapsed) output = template_env.from_string(template_file).render(**template_args).encode('utf8') temp_storage_dir = gettempdir() + '\\bf3sb' if not os.access(temp_storage_dir, os.F_OK): os.mkdir(temp_storage_dir) random_file_name = ''.join(random.sample(string.lowercase + string.digits, 20)) + '.html' random_file_path = temp_storage_dir + '\\' + random_file_name f = open(random_file_path, 'w') f.write(output) f.close() webbrowser.open(random_file_path) finally: self.completed.emit()
def renderTemplate(json, dimensions, templatename): fsl = FileSystemLoader(dirname(templatename), 'utf-8') e = Environment() e.loader = fsl templatename = basename(templatename) return e.get_template(templatename).render(json=json, dimensions=dimensions)
def prompt_for_config(context, no_input=False): """ Prompts the user to enter new config, using context as a source for the field names and sample values. :param no_input: Prompt the user at command line for manual configuration? """ cookiecutter_dict = {} env = Environment() for key, raw in iteritems(context['cookiecutter']): if key.startswith('_'): cookiecutter_dict[key] = raw continue raw = raw if is_string(raw) else str(raw) val = env.from_string(raw).render(cookiecutter=cookiecutter_dict) if not no_input: prompt = '{0} (default is "{1}")? '.format(key, val) new_val = read_response(prompt).strip() if new_val != '': val = new_val cookiecutter_dict[key] = val return cookiecutter_dict
def write_error(self, status_code, **kwargs): try: error_env = Environment(loader = FileSystemLoader(self.__project_path + '/public')) temp = error_env.get_template(str(status_code) + '.html') self.write(temp.render()) except TemplateNotFound: RequestHandler.write_error(self, status_code, **kwargs)
def load_template(self, template_name): """ Use jinja2 template engine to load the corresponding template file """ env = Environment(loader=FileSystemLoader(self.get_template_base_dir())) template = env.get_template(template_name) return template
def raises_exception(self, exception): """ Expect an exception. """ class ExpectedExceptionDidNotHappen(Exception): pass error_path = self.path.state.joinpath("error.txt") runpy = self.path.state.joinpath("runmypy.py") if error_path.exists(): error_path.remove() env = Environment() env.loader = DictLoader(load(self.path.key.joinpath("codetemplates.yml").bytes().decode('utf8')).data) runpy.write_text(env.get_template("raises_exception").render( setup=self.preconditions['setup'], code=self.preconditions['code'], variables=self.preconditions.get('variables', None), yaml_snippet=self.preconditions.get("yaml_snippet"), modified_yaml_snippet=self.preconditions.get("modified_yaml_snippet"), exception=exception, error_path=error_path, )) self.python(runpy).run() if not error_path.exists(): raise ExpectedExceptionDidNotHappen() else: assert exception.strip() in error_path.bytes().decode('utf8'), "expected:\n{0}\nshould be:\n{1}".format( exception, error_path.bytes().decode('utf8'), )
def should_be_equal_to(self, rhs): """ Code should be equal to rhs """ class UnexpectedException(Exception): pass error_path = self.path.state.joinpath("error.txt") runpy = self.path.state.joinpath("runmypy.py") if error_path.exists(): error_path.remove() env = Environment() env.loader = DictLoader(load(self.path.key.joinpath("codetemplates.yml").bytes().decode('utf8')).data) runpy.write_text(env.get_template("shouldbeequal").render( setup=self.preconditions['setup'], code=self.preconditions['code'], variables=self.preconditions.get('variables', None), yaml_snippet=self.preconditions.get("yaml_snippet"), modified_yaml_snippet=self.preconditions.get("modified_yaml_snippet"), rhs=rhs, error_path=error_path, )) self.python(runpy).run() if error_path.exists(): raise UnexpectedException(error_path.bytes().decode("utf8"))
def build_creator(self): environment = Environment() environment.filters['article'] = Filters.select_article environment.filters['pluralize'] = Filters.select_pluralize template = environment.from_string(self.creator_template) self.creator = template.render(npc=self.npc)
def generate_index(posts, destination): """This generate the index page with the list of blog posts. """ dest = os.path.join(destination, "index.html") print("Generating blog index..."), # Load template engine. env = Environment() # Load the template files, base and post. env.loader = FileSystemLoader("template") # Load template file. tpl = env.get_template("index.html") # Generate HTML content. first = None # Check if there are any posts at all. if posts: first = posts.pop(0) # Check if there's the "read more" break in the post content. # If there is, only displays up to the break and append a link # to the full post. more = "<!--more-->" if more in first["content"]: first["content"] = first["content"].split(more)[0] first["content"] += '<p><a href="{0}">Read more...</a></p>'.format(first["link"]) html = tpl.render({"page": "index", "first": first, "posts": posts}) # Create file. with open(dest, "w") as handle: handle.write(html) print(green("done"))
def babel_extract(fileobj, keywords, comment_tags, options): extensions = set() for extension in options.get('extensions', '').split(','): extension = extension.strip() if not extension: continue extensions.add(import_string(extension)) if InternationalizationExtension not in extensions: extensions.add(InternationalizationExtension) def getbool(options, key, default = False): return options.get(key, str(default)).lower() in ('1', 'on', 'yes', 'true') silent = getbool(options, 'silent', True) environment = Environment(options.get('block_start_string', BLOCK_START_STRING), options.get('block_end_string', BLOCK_END_STRING), options.get('variable_start_string', VARIABLE_START_STRING), options.get('variable_end_string', VARIABLE_END_STRING), options.get('comment_start_string', COMMENT_START_STRING), options.get('comment_end_string', COMMENT_END_STRING), options.get('line_statement_prefix') or LINE_STATEMENT_PREFIX, options.get('line_comment_prefix') or LINE_COMMENT_PREFIX, getbool(options, 'trim_blocks', TRIM_BLOCKS), NEWLINE_SEQUENCE, frozenset(extensions), cache_size=0, auto_reload=False) if getbool(options, 'newstyle_gettext'): environment.newstyle_gettext = True source = fileobj.read().decode(options.get('encoding', 'utf-8')) try: node = environment.parse(source) tokens = list(environment.lex(environment.preprocess(source))) except TemplateSyntaxError as e: if not silent: raise return finder = _CommentFinder(tokens, comment_tags) for lineno, func, message in extract_from_ast(node, keywords): yield (lineno, func, message, finder.find_comments(lineno))
class CreerAnnonce(object): env = None def __init__(self): # Référence au dossier HTML self.env = Environment(loader=FileSystemLoader('html')) def creer(self): # Charger et compléter le template HTML return self.env.get_template('CreerAnnonce.html').render() def save(self, **kwargs): if 'type' in kwargs and 'category' in kwargs and 'title' in kwargs: db = openDB() cursor = db.cursor() cursor.execute("INSERT INTO annonce VALUES(NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)", (kwargs['type'], kwargs['category'], kwargs['title'], '', '', '', '', '', '', '', '', '', '', '', '','')) # Enregistrer les insertions. db.commit() if cursor.rowcount == 1: return self.env.get_template('CreerAnnonce.html').render(msg = "Annonce enregistrée !") #TODO : bouton aller sur mon compte --> <a href="/compte">Aller sur mon compte</a></p>' else: #TODO : afficher le msg à la fin return self.env.get_template('CreerAnnonce.html').render(msg = "Erreur d'enregistrement !") cursor.close() db.close() else: return self.env.get_template('CreerAnnonce.html').render(msg = "Il manque des paramètres... Réessayez")
def write_build_report(build_report, template_filename, filename): """Write a build report to disk using a template file Positional arguments: build_report - a report generated by the build system template_filename - a file that contains the template for the style of build report filename - the location on disk to write the file to """ build_report_failing = [] build_report_passing = [] for report in build_report: if len(report["failing"]) > 0: build_report_failing.append(report) else: build_report_passing.append(report) env = Environment(extensions=['jinja2.ext.with_']) env.loader = FileSystemLoader('ci_templates') template = env.get_template(template_filename) with open(filename, 'w+') as placeholder: placeholder.write(template.render( failing_builds=build_report_failing, passing_builds=build_report_passing))
class Annonces(object): env = None def __init__(self): # Référence au dossier HTML self.env = Environment(loader=FileSystemLoader('html')) def annonces(self): # Charger et compléter le template HTML return self.env.get_template('afficherAnnonces.html').render() def annonce(self, a_id): db = openDB() cursor = db.cursor() cursor.execute("SELECT * FROM annonce WHERE id='{0}'".format(a_id)) annonce = cursor.fetchone() # prendre une ligne. fetchall() égal à tous les lignes. cursor.execute("SELECT * FROM picture WHERE a_id='{0}'".format(a_id)) picture = cursor.fetchone() cursor.close() db.close() if annonce: # Charger et compléter le template HTML return self.env.get_template('afficherAnnonce.html').render(image = picture[1], type = annonce[1], auteur = annonce[2], catego = annonce[3], faculty = annonce[4], titre = annonce[5], desc = annonce[6], prix = annonce[7], prixdesc = annonce[8], datepublic = annonce[9], etat = annonce[10]) else: return self.env.get_template('afficherAnnonceErreur.html').render(msg="Erreur, cette annonce n'existe pas")
def generate(model): parts = [block for block in model.blocks if block.__class__.__name__ == "PartType"] for part in parts: part_pl = part.namePiece.partname.lower() + mean_gen_config.PLURAL part_dir = os.path.join(mean_gen_config.GEN_DIR, part_pl) if not os.path.exists(part_dir): os.makedirs(part_dir) env = Environment( trim_blocks=True, lstrip_blocks=True, loader=PackageLoader(mean_gen_config.TEMPLATES_DIR, ".") ) template = env.get_template(TEMPLATE_NAME) rendered = template.render( { "ModuleName": part_pl.capitalize(), "DBName": part_pl, "PartNamePlural": part_pl, "PartNameSingular": part.namePiece.partname.lower(), } ) file_name = os.path.join(part_dir, TEMPLATE_NAME) with open(file_name, "w+") as f: f.write(rendered) print(mean_gen_config.GENERATED_MESSAGE + file_name)
def build_timer(app): app.add_javascript("event_timer.js") data = yaml.load(open("events.yaml").read()) # add better text for tabulatted planning now = time.time() outdated = '*' for ev in data: d, h = ev['date'].split('T') epoch_time = calendar.timegm( time.strptime(ev['date'], "%Y-%m-%dT%H:%M")) ev['status'] = 'past' if epoch_time > now: if outdated == '*': # Mark upcoming event as bold ev['status'] = 'current' outdated = '**' else: ev['status'] = 'future' outdated = '' ev['date_str'] = "%s%s, %s UTC%s" % (outdated, d, h, outdated) ev['name_str'] = "%s%s%s" % (outdated, ev['name'], outdated) output_file = os.path.join(PATH_PREFIX, "events.rst") with open(output_file, "w") as out: template_dir = os.path.join(".", "doc", "source", "_exts") loader = FileSystemLoader(template_dir) env = Environment(trim_blocks=True, loader=loader) template = env.get_template("events.jinja") out.write(template.render({'events': data}))
def generate_skeleton(data, destination_dir, data_config, context): TEMPLATE_BASE_DIR = os.path.join('templates', 'skeleton') WB_HUC_TEMPLATE = 'waterbudget_huc.html' SF_HUC_TEMPLATE = 'streamflow_huc.html' SF_GAGE_TEMPLATE = 'streamflow_gage.html' PROJECT_TEMPLATE = 'project.html' DATA_TEMPLATE = 'data.html' skeleton_destination_dir = destination_dir gc.make_sure_path_exists(skeleton_destination_dir) env = Environment(autoescape=True) env.loader = FileSystemLoader(TEMPLATE_BASE_DIR) print 'Creating files in %s' % skeleton_destination_dir generate_themed_skeletons(data['datasets'], DATA_TEMPLATE, context, {}, '#!data-discovery/dataDetail/', env, skeleton_destination_dir) generate_themed_skeletons(data['projects'], PROJECT_TEMPLATE, context, {}, '#!data-discovery/projectDetail/', env, skeleton_destination_dir) generate_themed_skeletons(data['streamflow_gages'], SF_GAGE_TEMPLATE, context, data_config.get('streamflow').get('gage'), '#!streamflow-stats/gage/', env, skeleton_destination_dir) generate_themed_skeletons(data['streamflow_hucs'], SF_HUC_TEMPLATE, context, data_config.get('streamflow').get('huc12'), '#!streamflow-stats/huc/', env, skeleton_destination_dir) generate_themed_skeletons(data['waterbudget_hucs']['huc12'], WB_HUC_TEMPLATE, context, data_config.get('watershed').get('huc12'), '#!waterbudget/huc/', env, skeleton_destination_dir) generate_themed_skeletons(data['waterbudget_hucs']['huc08'], WB_HUC_TEMPLATE, context, data_config.get('watershed').get('huc08'), '#!waterbudget/huc/', env, skeleton_destination_dir)
def test_flusher_message(spawn, joinall): """ Test that we email a simple record appropriately. """ template_dir = os.path.join(os.path.dirname(__file__), '..') pager_window = 5 pager_limit = 10 message_rate = MessageRate(pager_window, pager_limit) spawn_ret_val = object() spawn.return_value = spawn_ret_val env = Environment(loader=FileSystemLoader(template_dir)) message_buffer = MessageBuffer(env.get_template('subject-template.txt'), env.get_template('body-template.txt')) message = UniqueMessage('module', 'funcName', 'filename', 'message', 'pathname', 'lineno', 'exc_text', 'kind') message_buffer.add(message, 'source') flusher(message_buffer, message_rate) spawn.assert_called_once_with(mailer, ANY, ANY, ANY) # We shouldn't have done anything eq_(1, spawn.call_count) eq_(1, joinall.call_count) joinall.assert_called_once_with([spawn_ret_val])
def generate_html(input_dir, output_dir, context=None): """ Renders the HTML templates from `input_dir`, and writes them to `output_dir`. :param input_dir: The Complexity input directory, e.g. `project/`. :paramtype input_dir: directory :param output_dir: The Complexity output directory, e.g. `www/`. :paramtype output_dir: directory :param context: Jinja2 context that holds template variables. See http://jinja.pocoo.org/docs/api/#the-context """ templates_dir = os.path.join(input_dir, 'templates/') if not os.path.exists(templates_dir): raise MissingTemplateDirException( 'Your project is missing a templates/ directory containing your \ HTML templates.' ) context = context or {} env = Environment() env.loader = FileSystemLoader(templates_dir) # Create the output dir if it doesn't already exist make_sure_path_exists(output_dir) for root, dirs, files in os.walk(templates_dir): for f in files: print(f) generate_html_file(f, output_dir, env, context)
def test_flusher_rate_excede(spawn, joinall, pager, mailer): """ Test that we complain extra loudly if we get too many errors. """ template_dir = os.path.join(os.path.dirname(__file__), '..') pager_window = 5 pager_limit = 10 message_rate = MessageRate(pager_window, pager_limit) spawn_ret_val = object() spawn.return_value = spawn_ret_val env = Environment(loader=FileSystemLoader(template_dir)) message_buffer = MessageBuffer(env.get_template('subject-template.txt'), env.get_template('body-template.txt')) for i in range(pager_limit + 1): message_buffer.add(UniqueMessage('module', 'funcName', 'filename', 'message', 'pathname', i, 'exc_text', 'kind'), 'source') flusher(message_buffer, message_rate) eq_([call(pager, pager_limit + 1), call(mailer, ANY, ANY, ANY)], spawn.call_args_list) # We shouldn't have done anything eq_(2, spawn.call_count) eq_(1, joinall.call_count) joinall.assert_called_once_with([spawn_ret_val] * 2)
def write_html(self, output_name, tracks): template_dir = os.path.join(os.path.dirname(__file__), "../templates") env = Environment(loader=FileSystemLoader(template_dir), autoescape=True) for tpl_name in ("cards", "table"): tpl = env.get_template("%s.jinja" % tpl_name) with codecs.open("%s-%s.html" % (output_name, tpl_name), "wb", encoding="UTF-8") as outf: outf.write(tpl.render(tracks=tracks))
def generate(model): env = Environment(trim_blocks=True, lstrip_blocks=True, loader=PackageLoader("templates", TEMPLATE_DIR)) template = env.get_template(TEMPLATE_NAME) for block in model.blocks: if block.__class__.__name__ == 'PartType': itemName = block.namePiece.partname itemName_pl = itemName + mean_gen_config.PLURAL props = block.propertiesPiece.properties properties = [models.Property(prop.name, prop.type, prop.visibility) for prop in props] item = models.Item(itemName) item.properties = properties rendered = template.render({'ControllerName': (itemName + 'Controller'), 'item': itemName.lower(), 'items': itemName_pl.lower(), 'properties': item.properties}) file_path = os.path.join(mean_gen_config.GEN_DIR, itemName_pl.lower(), TEMPLATE_DIR) if not os.path.exists(file_path): os.makedirs(file_path) file_name = os.path.join(file_path, TEMPLATE_NAME) with open(file_name, "w+") as f: f.write(rendered) print(mean_gen_config.GENERATED_MESSAGE + file_name)
def create_config_from_template(template_path, attr): folder, file = path.split(template_path) env = Environment() env.loader = FileSystemLoader(folder) config = env.get_template(file) return config.render(attr)
def generateProject(projectName, mainXamlPath, outputPath): templatePath = "../../resources/template/csproj-template" env = Environment(loader=FileSystemLoader(templatePath)) for root, dirs, files in os.walk(templatePath): # print root.replace(a,"") for file in files: fPath = root[len(templatePath) + 1 :] newPath = (fPath + "/" if fPath else "") + file template = env.get_template(newPath) oPath = os.path.join(outputPath, fPath) try: os.makedirs(oPath) except: pass template.module.projectname = projectName filename = template.module.filename(projectName) oPath = os.path.join(oPath, filename) with codecs.open(oPath, "w", "utf-8") as f: f.write(template.render(projectname=projectName, mainxmlpath=mainXamlPath)) return outputPath
def generate_sitemap(data, destination_dir, context): TEMPLATE_BASE_DIR = os.path.join('templates', 'sitemap') WB_HUC_TEMPLATE = 'waterbudget_huc.xml' SF_HUC_TEMPLATE = 'streamflow_huc.xml' SF_GAGE_TEMPLATE = 'streamflow_gage.xml' PROJECT_TEMPLATE = 'project.xml' DATA_TEMPLATE = 'data.xml' INDEX_TEMPLATE = 'index.xml' HOME_TEMPLATE = 'home.xml' sitemap_destination_dir = destination_dir gc.make_sure_path_exists(sitemap_destination_dir) env = Environment(autoescape=True) env.loader = FileSystemLoader(TEMPLATE_BASE_DIR) print 'Creating sitemap files in %s' % sitemap_destination_dir sitemap_files = [] sitemap_files.extend(create_sitemaps([{}], HOME_TEMPLATE, sitemap_destination_dir, 'sitemap_home', context, env)) sitemap_files.extend(create_sitemaps(data['waterbudget_hucs'], WB_HUC_TEMPLATE, sitemap_destination_dir, 'sitemap_wb_huc', context, env)) sitemap_files.extend(create_sitemaps(data['streamflow_gages'], SF_GAGE_TEMPLATE, sitemap_destination_dir, 'sitemap_sf_gage', context, env)) sitemap_files.extend(create_sitemaps(data['streamflow_hucs'], SF_HUC_TEMPLATE, sitemap_destination_dir, 'sitemap_sf_huc', context, env)) sitemap_files.extend(create_sitemaps(data['projects'], PROJECT_TEMPLATE, sitemap_destination_dir, 'sitemap_project', context, env)) sitemap_files.extend(create_sitemaps(data['datasets'], DATA_TEMPLATE, sitemap_destination_dir, 'sitemap_data', context, env)) template = env.get_template(INDEX_TEMPLATE) index_context = context.copy() index_context['sitemap_files'] = sitemap_files sitemap_file = open(os.path.join(sitemap_destination_dir, 'sitemap.xml'), 'w') sitemap_file.write(template.render(index_context)) sitemap_file.close()
def generate_feed(posts, destination): """This generate the Atom feed with the list of blog posts. """ dest = os.path.join(destination, "feed.xml") print("Generating atom feed..."), # Load template engine. env = Environment() # Load the template files, base and post. env.loader = FileSystemLoader("template") xml = env.get_template("feedtemplate.xml").render(items=posts) # Create file. with open(dest, "w") as handle: handle.write(xml) print(green("done"))
def write_build_report(build_report, template_filename, filename): build_report_failing = [] build_report_passing = [] for report in build_report: if len(report["failing"]) > 0: build_report_failing.append(report) else: build_report_passing.append(report) env = Environment(extensions=['jinja2.ext.with_']) env.loader = FileSystemLoader('ci_templates') template = env.get_template(template_filename) with open(filename, 'w+') as f: f.write( template.render(failing_builds=build_report_failing, passing_builds=build_report_passing))
def generate(model): env = Environment(trim_blocks=True, lstrip_blocks=True, loader=PackageLoader(mean_gen_config.TEMPLATES_DIR, mean_gen_config.USERS_TEMPLATE_DIR)) template = env.get_template(TEMPLATE_NAME) rendered = template.render() users_dir = os.path.join(mean_gen_config.GEN_DIR, mean_gen_config.GEN_DIR_USERS) if not os.path.exists(users_dir): os.makedirs(users_dir) file_name = os.path.join(users_dir, TEMPLATE_NAME) with open(file_name, "w+") as f: f.write(rendered) print(mean_gen_config.GENERATED_MESSAGE + mean_gen_config.DEFAULT_USER_GENERATED_MESSAGE + file_name)
def create_jinja2_env(configuration): env = Environment(line_comment_prefix='@', loader=FileSystemLoader( os.path.join(base_dir, 'templates')), extensions=[DeviceConfigurationExtension]) builtin_context = BuiltinContext() env.globals.update(get_builtins(builtin_context)) env.globals.update(configuration) return env
def __collectStatsAndSendMail(self): stats = self.crawler.stats.get_stats() self.mail.send(to=self.notify_mails, subject='{} PerodicStats Report Mail'.format( self.crawler.settings.get('JIRA_ID')), body=Environment().from_string(config.HTML).render( {'stats': stats}), mimetype='text/html', _callback=self._catch_mail_sent)
def get_or_create_environment(): global env if env: return env jinja_tag = jinjatag.JinjaTag() env = Environment(loader=PrefixLoader(loaders, delimiter=':'), trim_blocks=True, lstrip_blocks=True, autoescape=True, extensions=['jinja2.ext.i18n', jinja_tag]) jinja_tag.init() env.filters.update(filters) env.install_null_translations() env.created_by = to_unicode(b''.join(traceback.format_stack()), strict=False) executing_test = get_executing_test(optional=True) if executing_test: executing_test.addCleanup(reset_template_environment) return env
def createRenderer(self, loader): ''' Construct the template renderer. @param loader: BaseLoader The loader to extract the templates from. ''' assert isinstance(loader, BaseLoader), 'Invalid loader %s' % loader environment = Environment(loader=loader, extensions=['jinja2.ext.do']) def doRender(tpath, path, **data): ''' Render the template into the provided path. ''' try: template = environment.get_template(tpath) except TemplateNotFound: log.info('Template \'%s\' is missing', tpath) return False except: log.exception('Template \'%s\' has a problem', tpath) return False content = template.render(**data) try: folder = path[:path.index('/')] except ValueError: folder = self.pathDocumentation else: folder = os.path.join(self.pathDocumentation, folder.replace('/', os.sep)) if not os.path.exists(folder): os.makedirs(folder) file = os.path.join(self.pathDocumentation, path.replace('/', os.sep)) with open(file, 'w') as dest: dest.write(content) return True environment.globals['render'] = doRender environment.globals['isColl'] = lambda value: isinstance( value, (list, tuple, deque)) environment.globals['isDict'] = lambda value: isinstance(value, dict) environment.globals['upperFirst'] = lambda value: modifyFirst( value, True) environment.globals['transform'] = lambda coll, format: [ format % item for item in coll ] environment.globals['TextTable'] = TextTable return doRender
class VerilogTmplContainer(): tmplEnv = Environment( loader=PackageLoader('hwt', 'serializer/verilog/templates')) moduleHeadTmpl = tmplEnv.get_template('module_head.v') moduleBodyTmpl = tmplEnv.get_template('module_body.v') processTmpl = tmplEnv.get_template('process.v') ifTmpl = tmplEnv.get_template("if.v") componentInstanceTmpl = tmplEnv.get_template("component_instance.v") switchTmpl = tmplEnv.get_template("switch.v")
def render_template(self, template, params, **kwargs): ''' renders a Jinja template into HTML ''' # check if template exists template_path = '%s/%s' % (settings.TEMPLATES[0]['DIRS'][0], template) if not os.path.exists(template_path): print(('No template file present: %s' % template_path)) sys.exit() import jinja2 return jinja2.load_template(template) templateLoader = jinja2.FileSystemLoader(searchpath="./") templateEnv = jinja2.Environment(loader=templateLoader) templ = templateEnv.get_template(template_path) return templ.render(**kwargs) env = Environment(loader=PackageLoader('poultry', 'templates')) template = env.get_template(template) return template.render(params)
def print_issue(list_of_issues, issue_path): file_system_loader = FileSystemLoader(TEMPLATE_PATH) env = Environment(loader=file_system_loader, trim_blocks=True) template = env.get_template("issues.template") context = {'issueList': list_of_issues } output = template.render(context) issue_path = os.path.join(issue_path, "issue.txt") try: with open(issue_path, "wb") as issue_file: issue_file.write(output) print "\nFile containing issues created: ", issue_path except: print "Issues report file is not created" flag_prio = 0 counter = 0 swc_counter = 0 counter_witout = 0 swc_prio_a = [] swc_without = [] for issue in list_of_issues: swc_counter = swc_counter + 1 for msg in issue.mssg: if msg.prio == "PRIO_A": counter = counter + 1 swc_prio_a.append(issue.swc) flag_prio = 1 break if flag_prio != 1: counter_witout = counter_witout + 1 swc_without.append(issue.swc) flag_prio = 0 prio_msg = "\nNumber of SWCs with [PRIO_A]: " print "%32s %2s" % (prio_msg, counter) for prio_a in swc_prio_a: print " ", prio_a out_prio_msg = "\nNumber of SWCs without [PRIO_A]:" print "%32s %2s" % (out_prio_msg, counter_witout) for swc_no_prio_a in swc_without: print " ", swc_no_prio_a total_msg = "\nTotal number od components: " print "%32s %2s" % (total_msg, swc_counter)
def jinja_environment(**options): if settings.DEBUG is True: options['cache_size'] = 0 env = Environment(**options) env.globals.update({ 'static': static, 'url': reverse, 'urlencode': urlencode, }) return env
def test_generate_file_verbose_template_syntax_error(self): env = Environment() env.loader = FileSystemLoader('.') try: generate.generate_file(project_dir=".", infile='tests/files/syntax_error.txt', context={'syntax_error': 'syntax_error'}, env=env) except TemplateSyntaxError as exception: expected = ( 'Missing end of comment tag\n' ' File "./tests/files/syntax_error.txt", line 1\n' ' I eat {{ syntax_error }} {# this comment is not closed}') expected = expected.replace("/", os.sep) self.assertEquals(str(exception), expected) except exception: self.fail('Unexpected exception thrown:', exception) else: self.fail('TemplateSyntaxError not thrown')
def init_jinja2(app, **kw): logging.info('init jinja2...') options = dict(autoescape=kw.get('autoescape', True), block_start_string=kw.get('block_start_string', '{%'), block_end_string=kw.get('block_end_string', '%}'), variable_start_string=kw.get('variable_start_string', '{{'), variable_end_string=kw.get('variable_end_string', '}}'), auto_reload=kw.get('auto_reload', True)) path = kw.get('path', None) if path is None: path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates') logging.info('set jinja2 template path: %s' % path) env = Environment(loader=FileSystemLoader(path), **options) filters = kw.get('filters', None) if filters is not None: for name, f in filters.items(): env.filters[name] = f app['__templating__'] = env
class VhdlTmplContainer(): tmplEnv = Environment( loader=PackageLoader('hwt', 'serializer/vhdl/templates')) architectureTmpl = tmplEnv.get_template('architecture.vhd') entityTmpl = tmplEnv.get_template('entity.vhd') processTmpl = tmplEnv.get_template('process.vhd') componentTmpl = tmplEnv.get_template('component.vhd') componentInstanceTmpl = tmplEnv.get_template('component_instance.vhd') ifTmpl = tmplEnv.get_template('if.vhd') switchTmpl = tmplEnv.get_template('switch.vhd')
def render(path, **kwargs): """Render a template with jinja2 Args: path (str): the path to the template; should be of the form "dir/filename.html" """ env = Environment( loader=FileSystemLoader(template_rootdir), extensions=[formencode_jinja2.formfill, 'jinja2.ext.i18n']) env.install_gettext_translations(i18n) template = env.get_template(path) static_cache_version = config.get("openspending.static_cache_version", "") if static_cache_version != "": static_cache_version = "?" + static_cache_version params = { "script_root": h.script_root(), "script_boot": h.script_tag('prod/boot'), "bootstrap_css": h.static('style/bootstrap.css'), "style_css": h.static('style/style.css'), "number_symbols_group": c.locale.number_symbols.get('group'), "number_symbols_decimal": c.locale.number_symbols.get('decimal'), "site_title": app_globals.site_title, "static": config.get("openspending.static_path", "/static/"), "static_cache_version": static_cache_version, "messages": list(h._flash.pop_messages()), "languages": languages(c.detected_l10n_languages, c.language), "section_active": section_active(c.content_section), "account": c.account is not None, "h": h, "c": c, "g": app_globals, "can": can, "show_rss": hasattr(c, 'show_rss') and c.show_rss or None } params.update(kwargs) form_errors = params.get('form_errors', {}) return postprocess_forms(template.render(params), form_errors)
def TestOneInput(data): fdp = atheris.FuzzedDataProvider(data) template_str = fdp.ConsumeString(sys.maxsize) tmp_path = "/tmp/mytemplates" temp_file = os.path.join(tmp_path, "template.jinja2") if not os.path.isdir(tmp_path): os.mkdir(tmp_path) if os.path.isfile(temp_file): os.remove(temp_file) with open(temp_file, "wb") as fd: fd.write(data) env = Environment(loader=DictLoader({"foo": template_str})) try: env.compile_templates(tmp_path, zip=None) except RecursionError: return return
def config_to_header(config, fname=None): """ Convert the configuration data to the content of a C header file, meant to be included to a C/C++ file. The content is returned as a string. Positional arguments: config - configuration data as (ConfigParam instances, ConfigMacro instances) tuple (as returned by get_config_data()) Keyword arguments: fname - also write the content is to the file called "fname". WARNING: if 'fname' names an existing file, it will be overwritten! """ params, macros = config[0] or {}, config[1] or {} Config._check_required_parameters(params) params_with_values = [ p for p in params.values() if p.value is not None ] ctx = { "cfg_params": sorted([(p.macro_name, str(p.value), p.set_by) for p in params_with_values]), "macros": sorted([(m.macro_name, str(m.macro_value or ""), m.defined_by) for m in macros.values()]), "name_len": max([len(m.macro_name) for m in macros.values()] + [len(m.macro_name) for m in params_with_values] + [0]), "val_len": max([len(str(m.value)) for m in params_with_values] + [len(m.macro_value or "") for m in macros.values()] + [0]), } jinja_loader = FileSystemLoader(dirname(abspath(__file__))) jinja_environment = Environment(loader=jinja_loader, undefined=StrictUndefined) header_data = jinja_environment.get_template("header.tmpl").render(ctx) # If fname is given, write "header_data" to it if fname: with open(fname, "w+") as file_desc: file_desc.write(header_data) return header_data
def generate(model): env = Environment(trim_blocks=True, lstrip_blocks=True, loader=PackageLoader("templates", TEMPLATE_DIR)) #dodajemo filter pod nazivom inputType env.filters["inputType"] = filters.inputType template = env.get_template(TEMPLATE_NAME) for block in model.blocks: if block.__class__.__name__ == 'PartType': itemName = block.namePiece.partname itemName_pl = itemName + mean_gen_config.PLURAL props = block.propertiesPiece.properties properties = [ models.Property(prop.name, prop.type, prop.visibility) for prop in props ] for p in properties: print(p.type) item = models.Item(itemName) item.properties = properties rendered = template.render({ 'ControllerName': (itemName + 'Controller'), 'formName': itemName.lower() + 'Form', 'item': item }) file_path = os.path.join(mean_gen_config.GEN_DIR, itemName_pl.lower(), TEMPLATE_DIR) if not os.path.exists(file_path): os.makedirs(file_path) file_name = os.path.join(file_path, TEMPLATE_NAME) with open(file_name, "w+") as f: f.write(rendered) print(mean_gen_config.GENERATED_MESSAGE + file_name)
def renderTemplate(script_path, time_file_path, dimensions=(24, 80), templatename=DEFAULT_TEMPLATE): with copen(script_path, encoding='utf-8', errors='replace', newline='\r\n') as scriptf: # with open(script_path) as scriptf: with open(time_file_path) as timef: timing = getTiming(timef) json = scriptToJSON(scriptf, timing) fsl = FileSystemLoader(dirname(templatename), 'utf-8') e = Environment() e.loader = fsl templatename = basename(templatename) rendered = e.get_template(templatename).render(json=json, dimensions=dimensions) return rendered
def __init__(self, rootdir): self.outputdir = os.path.join(rootdir, "output") self.analyticsroot = os.path.join(rootdir, "analytics") if not os.path.exists(self.analyticsroot): os.makedirs(self.analyticsroot) self.analyticsfilename = os.path.join(self.analyticsroot, "index.html") self.done = [] self.env = Environment() self.env.loader = FileSystemLoader( os.path.join(os.path.dirname(__file__), "templates"))
def __init__(self, target, inputDir, program_name, build_url_resolver, extra_symbols=None, sources_relative=True): self.inputDir = inputDir self.target = target self.program_name = program_name self.toolchain = TOOLCHAIN_CLASSES[self.get_toolchain()]( TARGET_MAP[target]) self.build_url_resolver = build_url_resolver jinja_loader = FileSystemLoader( os.path.dirname(os.path.abspath(__file__))) self.jinja_environment = Environment(loader=jinja_loader) self.extra_symbols = extra_symbols if extra_symbols else [] self.config_macros = [] self.sources_relative = sources_relative self.config_header = None
def generate(model): env = Environment(trim_blocks=True, lstrip_blocks=True, loader=PackageLoader(mean_gen_config.TEMPLATES_DIR , TEMPLATE_DIR)) template = env.get_template(TEMPLATE_NAME) for block in model.blocks: if block.__class__.__name__ == 'PartType': itemName = block.namePiece.partname itemName_pl = itemName + mean_gen_config.PLURAL rendered = template.render({'ControllerName': itemName + 'Controller','item': itemName.lower(), 'Items': itemName_pl, 'items': itemName_pl.lower()}) file_path = os.path.join(mean_gen_config.GEN_DIR, itemName_pl.lower(), TEMPLATE_DIR) if not os.path.exists(file_path): os.makedirs(file_path) file_name = os.path.join(file_path, itemName_pl.lower() + '.js') with open(file_name, "w+") as f: f.write(rendered) print(mean_gen_config.GENERATED_MESSAGE + file_name)
def generate(model): parts = [block for block in model.blocks if block.__class__.__name__ == "PartType"] for part in parts: part_pl = part.namePiece.partname.lower() + mean_gen_config.PLURAL part_dir = os.path.join(mean_gen_config.GEN_DIR, part_pl) if not os.path.exists(part_dir): os.makedirs(part_dir) env = Environment(trim_blocks=True, lstrip_blocks=True, loader=PackageLoader(mean_gen_config.TEMPLATES_DIR, '.')) template = env.get_template(TEMPLATE_NAME) rendered = template.render({'ModuleName': part_pl.capitalize(), 'DBName': part_pl, 'PartNamePlural': part_pl, 'PartNameSingular': part.namePiece.partname.lower()}) file_name = os.path.join(part_dir, TEMPLATE_NAME) with open(file_name, "w+") as f: f.write(rendered) print(mean_gen_config.GENERATED_MESSAGE + file_name)
def renderTemplate(json, dimensions, templatename=None, outfname=None, contents=None): if contents: template = Template(contents) else: fsl = FileSystemLoader(dirname(templatename), 'utf-8') e = Environment() e.loader = fsl templatename = basename(templatename) template = e.get_template(templatename) rendered = template.render(json=json, dimensions=dimensions) if not outfname: return rendered with closing(outfname): outfname.write(rendered)
def render_from_string(template_string, context): """ Shortcut for using simple strings as templates, e.g. '<p>{{ foo }}</p>' Args: * `template_string` - string - the string to use as a template * `context` - mapping - the template context Return: The rendered template string """ return Environment().from_string(template_string).render(context)
def render_content(base_path: str, data: List[Post], template_folder: str) -> None: env = Environment() env.loader = FileSystemLoader(os.path.join(base_path, template_folder)) index_folder = None index_config = dict() # Create the posts html files tmpl = env.get_template('post.html') for post in data: folder_path, filen = os.path.split(post.path) post_data = post.to_dict() if post.is_index: index_tmpl = env.get_template('index.html') # TODO: Overwrite this hardcoded constant get = min(len(data), 10) post_data['posts'] = [ x.to_dict() for x in sorted([x for x in data if not x.is_index][:get], reverse=True) if not x.is_index ] render = index_tmpl.render(post_data) index_folder = folder_path index_config = post.config else: render = tmpl.render(post_data) if not os.path.exists(folder_path): os.makedirs(folder_path) with open(post.path, 'w') as f: f.write(render) # Create the history of posts history = env.get_template("full_list.html") content = dict() content['posts'] = list( map(lambda x: x.to_dict(), sorted([x for x in data if not x.is_index], reverse=True))) content['config'] = index_config render = history.render(content) if not os.path.exists(os.path.join(index_folder, 'history')): os.makedirs(os.path.join(index_folder, 'history')) with open( os.path.join(os.path.join(index_folder, 'history'), 'index.html'), 'w') as f: f.write(render)
def generate_release_index(): """ Generates the index pages pointing to each release for which we have stats. """ config = ConfigParser.ConfigParser() config.readfp(open(os.path.join(DATADIR, 'index_release.csv'))) releases = {} for option in config.options('logos'): tmp = {'release': option, 'banner': config.get('logos', option)} releases[int(option)] = tmp keys = releases.keys() keys.sort() keys.reverse() cnt = 0 while cnt < len(keys): release = keys[cnt] if int(release) < 10: release = '0%s' % release if not os.path.exists(os.path.join(DATADIR, 'release_%s.csv' % release)): keys.remove(keys[cnt]) cnt = cnt - 1 cnt = cnt + 1 try: env = Environment() env.loader = FileSystemLoader(TEMPLATEDIR) mytemplate = env.get_template('index_release.html') # Fill the template html = mytemplate.render( keys=keys, releases=releases, ) # Write down the page stream = open('output/index_release.html', 'w') stream.write(to_bytes(html)) stream.close() except IOError, err: print 'ERROR: %s' % err
def generate_files(template_dir, context=None): """ Renders the templates and saves them to files. :param input_dir: Project template input directory. :paramtype input_dir: directory """ # Always use utf-8 template_dir = template_dir logging.debug('Generating project from {0}...'.format(template_dir)) context = context or {} env = Environment() env.loader = FileSystemLoader('.') # Render dirname before writing name_tmpl = Template(template_dir) output_dir = name_tmpl.render(**context) if output_dir == template_dir: raise NonTemplatedInputDirException logging.debug("output_dir is {0}".format(output_dir)) make_sure_path_exists(output_dir) for root, dirs, files in os.walk(template_dir): for d in dirs: indir = os.path.join(root, d) outdir = indir.replace(template_dir, output_dir, 1) # Render dirname before writing name_tmpl = Template(outdir) rendered_dirname = name_tmpl.render(**context) make_sure_path_exists(rendered_dirname) for f in files: logging.debug("f is {0}".format(f)) infile = os.path.join(root, f) generate_file(infile, context, env)
def generate_html(talk): html_room_dir = os.path.join(html_dir, talk.room, str(talk.date)) try: os.makedirs(html_room_dir) except OSError as e: if e.errno == errno.EEXIST: pass else: raise start_time = talk.start_time.replace(':', '') # it's no good to have ':' in a filename file_name = '{}_{}.html'.format(start_time, talk.slug) html_path = os.path.join(html_room_dir, file_name) env = Environment(loader=FileSystemLoader('create_slides/templates')) template = env.get_template('slide.html') html = template.render(talk=talk, BASE_DIR=os.getcwd() + '/create_slides') html_file = open(html_path, 'wb') html_file.write(html.encode('utf8')) html_file.close() return html_path
def generate_index(posts, destination): """This generate the index page with the list of blog posts. """ dest = os.path.join(destination, "index.html") print("Generating blog index..."), # Load template engine. env = Environment() # Load the template files, base and post. env.loader = FileSystemLoader("template") # Load template file. tpl = env.get_template("index.html") # Generate HTML content. first = posts.pop(0) html = tpl.render({"page": "index", "first": first, "posts": posts}) # Create file. with open(dest, "w") as handle: handle.write(html) print(green("done"))