def html_output(result, options, *_): try: from jinja2 import Environment, FileSystemLoader, select_autoescape except ImportError: sys.stderr.write( "HTML Output depends on jinja2. `pip install jinja2` first") sys.exit(2) file_list = [] for source_file in result: if source_file: source_file_dict = {"filename": source_file.filename} func_list = [] for source_function in source_file.function_list: if source_function: source_function_dict = _create_dict(source_function) func_list.append(source_function_dict) source_file_dict["functions"] = func_list file_list.append(source_file_dict) cwd = os.path.join(sys.prefix, 'lizard_ext') if not os.path.exists(cwd): cwd = os.path.join(sys.exec_prefix, 'local/lizard_ext') if not os.path.exists(cwd): cwd = os.path.dirname(os.path.abspath(__file__)) env = Environment(loader=FileSystemLoader(cwd), autoescape=select_autoescape(['html'])) output = env.get_template('template.html').render( title='Lizard code complexity report', date=datetime.datetime.now().strftime('%Y-%m-%d %H:%M'), thresholds=options.thresholds, files=file_list) print(output) return 0
def index(self, scheme, url, tab="appdaemon"): params = {} params["tab"] = tab params["appdaemon"] = {} params["appdaemon"]["booted"] = self.AD.booted params["apps"] = {} for obj in self.AD.objects: params["apps"][obj] = {} params["plugins"] = {} for plug in self.AD.plugin_objs: params["plugins"][plug] = \ { "name": self.AD.plugin_objs[plug].name, "type": self.AD.plugin_objs[plug].__class__.__name__, "namespace": self.AD.plugin_objs[plug].namespace, } env = Environment( loader=FileSystemLoader(self.template_dir), autoescape=select_autoescape(['html', 'xml']) ) template = env.get_template("adminindex.jinja2") rendered_template = template.render(params) return (rendered_template)
def loadJinjaTemplates(self, custom_templates): '''Loads default templates from the templates directory, if a custom template is given that will override the default template. Args: custom_templates: a dictionary of names to custom templates which are used to override default templates. Returns: A dictionary of tuples containing template-names to compiled jinja templated. ''' templates = {} env = Environment( loader=PackageLoader('verto', 'html-templates'), autoescape=select_autoescape(['html']) ) for file in listdir(os.path.join(os.path.dirname(__file__), 'html-templates')): html_file = re.search(r'(.*?).html$', file) if html_file: processor_name = html_file.groups()[0] if processor_name in custom_templates: templates[processor_name] = env.from_string(custom_templates[processor_name]) else: templates[processor_name] = env.get_template(file) return templates
def build_templates(filenames, extension, use_backup_system=True): """For each template import the data and build a template. Send the data to the template as a expanded dictionary of non-builtin values. Each generate python file must exist as: 1. a template.py composed of Jinja and Python 2. a data file composed of pure Python 3. and output file composed of pure Python. The output file is the file that the main game code will run. NOTE: the extention variable allows me to run '.py' or '.html' templates. """ # Fix extension so it always includes the period. if extension[0] != '.': extension = "." + extension env = Environment( loader=FileSystemLoader(''), autoescape=select_autoescape(default_for_string=False, default=False), trim_blocks=True, lstrip_blocks=True, ) env.globals['get_names'] = services.naming.get_names env.globals['normalize_attrib_name'] = services.naming.normalize_attrib_name env.globals['normalize_attrib_names'] = services.naming.normalize_attrib_names env.globals['normalize_class_name'] = services.naming.normalize_class_name env.globals['normalize_class_names'] = services.naming.normalize_class_names for name in filenames: temp_name = "../" + name + '.tmp' final_name = "../" + name + extension template_name = name + "_template" + extension data = csv_or_module_data(name) template = env.get_template(template_name) # Output a header. comment = LANGUAGES[extension]['multi_line_comment'] with open(temp_name, 'w') as file: file.write('''{} This file is generated by 'build_code.py'. It has been set to read only so that you don't edit it without using 'build_code.py'. Thought that may change in the future. {}\n '''.format(comment['start'], comment['end'])) # Save the newly built code. with open(temp_name, 'a') as file: template.stream(**data).dump(file) if use_backup_system: # Build a backup of all files if needed. backup_system(temp_name, final_name, extension) else: no_backup(temp_name, final_name)
def not_found(): jinja_env = Environment( loader=PackageLoader('templates'), autoescape=select_autoescape(['html', 'xml']) ) template = jinja_env.get_template('404.html') return template.render(), 404
def __init__(self, buildmap, _config, opts, db): self.db = db self.buildmap = buildmap self.opts = opts self.env = Environment( loader=PackageLoader('buildmap.plugins.stats', 'templates'), autoescape=select_autoescape(['html', 'xml']) )
def jinja2_templates(): global jinja2_env if jinja2_env: return jinja2_env else: jinja2_env = Environment( loader=PackageLoader( 'cuppa', 'test_report/templates' ), autoescape=select_autoescape(['html', 'xml']) ) return jinja2_env
def get_dashboard(self, name, skin, recompile): try: dash = self._conditional_compile(name, skin, recompile) if dash is None: errors = [] head_includes = [] body_includes = [] else: errors = dash["errors"] if "widgets" in dash: widgets = dash["widgets"] else: widgets = {} if "scalable" in dash: scalable = dash["scalable"] else: scalable = True include_path = os.path.join(self.compiled_html_dir, skin, "{}_head.html".format(name.lower())) with open(include_path, "r") as include_file: head_includes = include_file.read() include_path = os.path.join(self.compiled_html_dir, skin, "{}_body.html".format(name.lower())) with open(include_path, "r") as include_file: body_includes = include_file.read() # # return params # params = {"errors": errors, "name": name.lower(), "skin": skin, "widgets": widgets, "head_includes": head_includes, "body_includes": body_includes, "scalable": scalable, "fa4compatibility": self.fa4compatibility } env = Environment( loader=FileSystemLoader(self.template_dir), autoescape=select_autoescape(['html', 'xml']) ) template = env.get_template("dashboard.jinja2") rendered_template = template.render(params) return(rendered_template) except: ha.log(self.logger, "WARNING", '-' * 60) ha.log(self.logger, "WARNING", "Unexpected error during DASH creation") ha.log(self.logger, "WARNING", '-' * 60) ha.log(self.logger, "WARNING", traceback.format_exc()) ha.log(self.logger, "WARNING", '-' * 60) return {"errors": ["An unrecoverable error occured fetching dashboard"]}
def __init__(self, show): ''' Constructor ''' self.show = show self.env = Environment( loader=PackageLoader('rest', 'templates'), autoescape=select_autoescape(['j2, xspf']) ) self.env.filters['tofileurl'] = tofileurl
def get_xml_text(xml_template_path, metadata_object): '''Helper function to perform substitutions on XML template text ''' template_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'templates') jinja_environment = Environment( loader=FileSystemLoader(template_dir or './'), autoescape=select_autoescape(['html', 'xml'] ) ) xml_template = jinja_environment.get_template(xml_template_path, parent=None) value_dict = dict(metadata_object.metadata_dict['Template']) # Copy template values # Convert multiple sets of comma-separated lists to lists of strings to a list of dicts #TODO: Make this slicker value_dict['keywords'] = [] for keyword_list_key in [key for key in value_dict.keys() if re.match('^KEYWORD_\w+_LIST$', key)]: keywords = [keyword.strip() for keyword in value_dict[keyword_list_key].split(',')] keyword_code = value_dict[re.sub('_LIST$', '_CODE', keyword_list_key)] value_dict['keywords'] += [{'value': keyword, 'code': keyword_code } for keyword in keywords ] # Create dict containing distribution info for DOI if required value_dict['distributions'] = [] dataset_doi = metadata_object.get_metadata(['Calculated', 'DOI']) if dataset_doi: try: distribution_dict = {'formatSpecification': 'html', 'distributor_name': metadata_object.get_metadata(['Template', 'ORGANISATION_NAME']), 'distributor_telephone': metadata_object.get_metadata(['Template', 'ORGANISATION_PHONE']), 'distributor_address': metadata_object.get_metadata(['Template', 'ORGANISATION_ADDRESS']), 'distributor_city': metadata_object.get_metadata(['Template', 'ORGANISATION_CITY']), 'distributor_state': metadata_object.get_metadata(['Template', 'ORGANISATION_STATE']), 'distributor_postcode': metadata_object.get_metadata(['Template', 'ORGANISATION_POSTCODE']), 'distributor_country': metadata_object.get_metadata(['Template', 'ORGANISATION_COUNTRY']), 'distributor_email': metadata_object.get_metadata(['Template', 'ORGANISATION_EMAIL']), 'url': dataset_doi, 'protocol': 'WWW:LINK-1.0-http--link', 'name': 'Digital Object Identifier for dataset %s' % metadata_object.get_metadata(['Calculated', 'UUID']), 'description': 'Dataset DOI' } for key, value in distribution_dict.iteritems(): assert value, '%s has no value defined' % key value_dict['distributions'].append(distribution_dict) except Exception as e: print 'WARNING: Unable to create DOI distribution: %s' % e.message return xml_template.render(**value_dict)
def loadJinjaTemplate(self, template): '''Loads a jinja template from the given processor name. Args: template: the processor name to load the template. Returns: A jinja template. ''' env = Environment( loader=PackageLoader('verto', 'html-templates'), autoescape=select_autoescape(['html']) ) jinja_template = env.get_template(template + '.html') return jinja_template
def logon(self): params = {} env = Environment( loader=FileSystemLoader(self.template_dir), autoescape=select_autoescape(['html', 'xml']) ) template = env.get_template("adminlogon.jinja2") rendered_template = template.render(params) return (rendered_template)
def render(self, template, **data): """Renders the template using Jinja2 with given data arguments. """ if(type(template) != str): raise TypeError("String expected") env = Environment( loader=FileSystemLoader(os.getcwd() + '/View'), autoescape=select_autoescape() ) template = env.get_template(template) return self.finish(template.render(data))
def get_generated_files(self, env=None): yield 'all.css', self.generate_css, False if env is None: import jinja2 directory = os.path.join(os.path.dirname(__file__), 'templates') env = jinja2.Environment(loader=jinja2.FileSystemLoader(directory), autoescape=jinja2.select_autoescape()) env.globals['site_url'] = SITE_URL env.globals['get_assets_checksum'] = partial(self.get_assets_checksum, env) for filename in env.loader.list_templates(): yield filename, partial(self.render_template, filename, env), True
def __init__(self, settings, package): # type: (Settings, str) -> None self.settings = settings loader = PackageLoader(package, "templates") self.environment = Environment(loader=loader, autoescape=select_autoescape(["html"])) filters = { "expires_when_str": self.expires_when_str, "long_ago_str": self.long_ago_str, "print_date": self.print_date, } self.environment.filters.update(filters) template_globals = {"ROLES": GROUP_EDGE_ROLES, "TYPES": OBJ_TYPES_IDX} self.environment.globals.update(template_globals)
def get_dashboard_list(self, paramOverwrite=None): if paramOverwrite is None: dash = self._list_dashes() else: dash = paramOverwrite env = Environment( loader=FileSystemLoader(self.template_dir), autoescape=select_autoescape(['html', 'xml']) ) template = env.get_template("dashboard.jinja2") rendered_template = template.render(dash) return (rendered_template)
def __init__(self, loader=None, global_vars=None): """ :param loader: The loader to supply to the environment. :type loader: :py:class:`jinja2.BaseLoader` :param dict global_vars: Additional global variables for the environment. """ self.logger = logging.getLogger('KingPhisher.TemplateEnvironment') autoescape = jinja2.select_autoescape(['html', 'htm', 'xml'], default_for_string=False) extensions = ['jinja2.ext.autoescape', 'jinja2.ext.do'] super(TemplateEnvironmentBase, self).__init__(autoescape=autoescape, extensions=extensions, loader=loader, trim_blocks=True) # misc. string filters self.filters['cardinalize'] = boltons.strutils.cardinalize self.filters['ordinalize'] = boltons.strutils.ordinalize self.filters['pluralize'] = boltons.strutils.pluralize self.filters['singularize'] = boltons.strutils.singularize self.filters['possessive'] = lambda word: word + ('\'' if word.endswith('s') else '\'s') self.filters['encode'] = self._filter_encode self.filters['decode'] = self._filter_decode self.filters['hash'] = self._filter_hash # counter part to https://jinja.readthedocs.io/en/stable/templates.html#tojson self.filters['fromjson'] = self._filter_json # time filters self.filters['strftime'] = self._filter_strftime self.filters['timedelta'] = self._filter_timedelta self.filters['tomorrow'] = lambda dt: dt + datetime.timedelta(days=1) self.filters['next_week'] = lambda dt: dt + datetime.timedelta(weeks=1) self.filters['next_month'] = lambda dt: dt + datetime.timedelta(days=30) self.filters['next_year'] = lambda dt: dt + datetime.timedelta(days=365) self.filters['yesterday'] = lambda dt: dt + datetime.timedelta(days=-1) self.filters['last_week'] = lambda dt: dt + datetime.timedelta(weeks=-1) self.filters['last_month'] = lambda dt: dt + datetime.timedelta(days=-30) self.filters['last_year'] = lambda dt: dt + datetime.timedelta(days=-365) # global variables self.globals['version'] = version.version # global functions self.globals['fetch'] = self._func_fetch self.globals['parse_user_agent'] = ua_parser.parse_user_agent self.globals['password_is_complex'] = utilities.password_is_complex self.globals['random_integer'] = random.randint # additional globals self.globals.update(global_vars or {})
def __init__(self, hs): """ Args: hs (synapse.server.HomeServer): homeserver """ Resource.__init__(self) self.hs = hs self.store = hs.get_datastore() self.registration_handler = hs.get_handlers().registration_handler # this is required by the request_handler wrapper self.clock = hs.get_clock() self._default_consent_version = hs.config.user_consent_version if self._default_consent_version is None: raise ConfigError( "Consent resource is enabled but user_consent section is " "missing in config file.", ) # daemonize changes the cwd to /, so make the path absolute now. consent_template_directory = path.abspath( hs.config.user_consent_template_dir, ) if not path.isdir(consent_template_directory): raise ConfigError( "Could not find template directory '%s'" % ( consent_template_directory, ), ) loader = jinja2.FileSystemLoader(consent_template_directory) self._jinja_env = jinja2.Environment( loader=loader, autoescape=jinja2.select_autoescape(['html', 'htm', 'xml']), ) if hs.config.form_secret is None: raise ConfigError( "Consent resource is enabled but form_secret is not set in " "config file. It should be set to an arbitrary secret string.", ) self._hmac_secret = hs.config.form_secret.encode("utf-8")
def setup(__pkg: str) -> jinja2.Environment: """Configure a new Jinja environment with our filters. Args: __pkg: Package name to use as base for templates searches Returns: Configured Jinja environment """ dirs = [path.join(d, 'templates') for d in xdg_basedir.get_data_dirs(__pkg)] env = jinja2.Environment( autoescape=jinja2.select_autoescape(['html', 'xml']), loader=jinja2.ChoiceLoader([jinja2.FileSystemLoader(s) for s in dirs])) env.loader.loaders.append(jinja2.PackageLoader(__pkg, 'templates')) env.filters.update(FILTERS) return env
def make_html_report(self, template_data, destination_path): """ Expects several filepaths in a *template_data* dicitionary. """ # chrom_plot_paths = sorted(chrom_plot_paths or [], # key=self._plot_file_chrom_index) jinja_env = jinja2.Environment( loader=jinja2.PackageLoader('paip', 'templates'), autoescape=jinja2.select_autoescape(['html']) ) template = jinja_env.get_template('coverage_report.html.jinja') html = template.render(template_data) with open(destination_path, 'w') as f: f.write(html) return destination_path
def validation_report(path, notifications, filename): """ Generate a validation report from a notification object. Parameters ---------- path : string Path to model file. notifications : dict A simple dictionary structure containing a list of errors and warnings. """ env = Environment( loader=PackageLoader('memote.suite', 'templates'), autoescape=select_autoescape(['html', 'xml']) ) template = env.get_template('validation_template.html') model = os.path.basename(path) with open(filename, "w") as file_h: file_h.write(template.render(model=model, notifications=notifications))
def __init__(self): self.requests_session = requests.Session() self.requests_session.headers = { 'User-agent': 'NotEnoughMods:Polling/1.X (+https://github.com/NotEnoughMods/NotEnoughModPolling)' } self.requests_session.max_redirects = 5 self.jinja_env = Environment( loader=FileSystemLoader('commands/NEMP'), autoescape=select_autoescape(['html']) ) self.load_config() self.load_version_blacklist() self.load_mc_blacklist() self.load_mc_mapping() self.buildModDict() self.QueryNEM() self.init_nem_versions() self.buildHTML()
def render(rust_stories): file = os.path.realpath(__file__) dir = os.path.split(file)[0] dir = os.path.split(dir)[0] env = Environment( loader=FileSystemLoader('{}/tools/templates'.format(dir)), autoescape=select_autoescape(['html']) ) for story in rust_stories: story["datetime"] = datetime.datetime.fromtimestamp( story["time"]).strftime("%Y-%m-%d %H:%M:%S") template = env.get_template('hacker_news.html') content = template.render( static_path=STATIC_PATH, stories=rust_stories, ) print(content)
def main(): parser = argparse.ArgumentParser(description="Render a TOML file using " "Jinja2 template.") parser.add_argument('--template', '-t', required=True, help='Jinja2 template to render with') parser.add_argument('input', nargs=1, help='input toml') parser.add_argument('output', nargs=1, help='output file') args = parser.parse_args() with open(args.input[0], 'rb') as f: data = toml.loads(f.read().decode("utf-8"), _dict=OrderedDict) env = Environment(loader=FileSystemLoader('.'), autoescape=select_autoescape(['tmpl'])) env.filters['linkify'] = filter_linkify env.filters['basename'] = filter_basename template = env.get_template(args.template) with open(args.output[0], "wb+") as f: f.write(template.render(data, output=args.output[0]).encode("utf-8"))
def LoadTemplate(template, template_type, replace): import jinja2 from netgrasp import netgrasp ng = netgrasp.netgrasp_instance try: ng.debugger.debug("entering email.LoadTemplate(%s, %s)", (template, template_type, replace)) # @TODO allow template overrides env = jinja2.Environment( loader = jinja2.PackageLoader("netgrasp", "mail_templates"), autoescape = jinja2.select_autoescape(['html']), extensions=['jinja2.ext.i18n'] ) # For now we're just using i18n for pluralization, not translations. env.install_null_translations() templates = {} for extension in ["subject.txt", "html", "txt"]: try: specific_template = "template." + template_type + "." + template + "." + extension templates[extension] = env.get_template(specific_template) ng.debugger.debug("loaded specific %s template: %s", (extension, specific_template)) except jinja2.TemplateNotFound: default_template = "template." + template_type + ".default." + extension templates[extension] = env.get_template(default_template) ng.debugger.debug("loaded default %s template: %s", (extension, default_template)) subject = templates["subject.txt"].render(replace) body_html = templates["html"].render(replace) body_text = templates["txt"].render(replace) return subject, body_html, body_text except: ng.debugger.dump_exception("LoadTemplate() exception")
def _configure_templating(cls, app): cachedir = app.config["PYLOAD_API"].get_cachedir() cache_path = os.path.join(cachedir, "jinja") os.makedirs(cache_path, exist_ok=True) app.create_jinja_environment() # NOTE: enable autoescape for all file extensions (included .js) # maybe this will break .txt rendering, but we don't render this kind of files actually # that does not change 'default_for_string=False' (by default) app.jinja_env.autoescape = jinja2.select_autoescape(default=True) app.jinja_env.bytecode_cache = jinja2.FileSystemBytecodeCache(cache_path) for fn in cls.JINJA_TEMPLATE_FILTERS: app.add_template_filter(fn) for fn in cls.JINJA_TEMPLATE_GLOBALS: app.add_template_global(fn) for fn in cls.JINJA_CONTEXT_PROCESSORS: app.context_processor(fn)
def get_html(node, fullpage=False): """ Return a string containing html markup of the annoted listfile which has been parsed into the parse tree rooted at `node`. """ outfile = io.StringIO() dump_html(node, outfile) content = outfile.getvalue() if not fullpage: return content tpl_kwargs = {"content" : content} try: import jinja2 except ImportError: logging.error( "Cannot import jinja. Please install jinja in your python environment" " to use the fullpage html renderer") return thisdir = os.path.realpath(os.path.dirname(__file__)) tpldir = os.path.join(thisdir, "templates") env = jinja2.Environment( loader=jinja2.FileSystemLoader(tpldir), autoescape=jinja2.select_autoescape(["html"]) ) stylesheet_path = os.path.join(tpldir, "style.css") with io.open(stylesheet_path, "r", encoding="utf-8") as infile: tpl_kwargs["stylesheet"] = infile.read() template = env.get_template("layout.html.tpl") return template.render(**tpl_kwargs)
from __future__ import division import sys import os import re import shutil import filecmp from collections import namedtuple, OrderedDict from itertools import groupby import operator from jinja2 import Environment, PackageLoader, select_autoescape env = Environment( loader=PackageLoader('templates', ''), autoescape=select_autoescape([]), keep_trailing_newline=True, lstrip_blocks=True ) gen_dir = os.path.dirname(os.path.abspath(__file__)) csv_dir = os.path.join(gen_dir, "csv") dest_dir = os.path.abspath(os.path.join(gen_dir, "..", "src", "generated")) tmp_dir = os.path.join(dest_dir, "tmp") cpp_types = { 'Boolean': 'bool', 'Double': 'double', 'UInt8': 'uint8_t', 'UInt16': 'uint16_t', 'UInt32': 'uint32_t',
async def init_state(app): # We have to put "kill" into a dict to prevent getting: # DeprecationWarning: Changing state of started or joined application is deprecated app["data"] = {"kill": False} if "db" not in app: app["db"] = None app["users"] = { "Random-Mover": User(app, bot=True, username="******"), "Fairy-Stockfish": User(app, bot=True, username="******"), "Discord-Relay": User(app, anon=True, username="******"), } app["users"]["Random-Mover"].online = True app["lobbysockets"] = {} app["seeks"] = {} app["games"] = {} app["invites"] = {} app["chat"] = collections.deque([], 100) app["game_channels"] = set() app["invite_channels"] = set() app["highscore"] = {variant: ValueSortedDict(neg) for variant in VARIANTS} app["crosstable"] = {} app["stats"] = {} # counters for games app["g_cnt"] = 0 # last game played app["tv"] = None # fishnet active workers app["workers"] = set() # fishnet works app["works"] = {} # fishnet worker tasks app["fishnet"] = asyncio.PriorityQueue() # fishnet workers monitor app["fishnet_monitor"] = {} app["fishnet_versions"] = {} for key in FISHNET_KEYS: app["fishnet_monitor"][FISHNET_KEYS[key]] = collections.deque([], 50) rm = app["users"]["Random-Mover"] for variant in VARIANTS: variant960 = variant.endswith("960") variant_name = variant[:-3] if variant960 else variant byoyomi = variant == "janggi" or variant.endswith( "shogi") or variant == "shogun" seek = Seek(rm, variant_name, base=5, inc=30 if byoyomi else 3, level=0, chess960=variant960, byoyomi_period=1 if byoyomi else 0) app["seeks"][seek.id] = seek rm.seeks[seek.id] = seek ai = app["users"]["Fairy-Stockfish"] loop = asyncio.get_event_loop() loop.create_task(BOT_task(ai, app)) loop.create_task(BOT_task(rm, app)) # Configure templating. app["jinja"] = {} base = os.path.dirname(__file__) for lang in LANGUAGES: # Generate compiled mo file folder = os.path.join(base, "../lang/", lang, "LC_MESSAGES") poname = os.path.join(folder, "server.po") moname = os.path.join(folder, "server.mo") try: with open(poname, 'rb') as po_file: po_lines = [ line for line in po_file if line[:8] != b"#, fuzzy" ] mo = Msgfmt(po_lines).get() with open(moname, 'wb') as mo_file: mo_file.write(mo) except PoSyntaxError: log.error("PoSyntaxError in %s", poname) # Create translation class try: translation = gettext.translation("server", localedir="lang", languages=[lang]) except FileNotFoundError: log.warning("Missing translations file for lang %s", lang) translation = gettext.NullTranslations() env = jinja2.Environment(enable_async=True, extensions=['jinja2.ext.i18n'], loader=jinja2.FileSystemLoader("templates"), autoescape=jinja2.select_autoescape(["html"])) env.install_gettext_translations(translation, newstyle=True) app["jinja"][lang] = env if app["db"] is None: return # Read users and highscore from db try: cursor = app["db"].user.find() async for doc in cursor: if doc["_id"] not in app["users"]: perfs = doc.get("perfs") if perfs is None: perfs = {variant: DEFAULT_PERF for variant in VARIANTS} app["users"][doc["_id"]] = User( app, username=doc["_id"], title=doc.get("title"), first_name=doc.get("first_name"), last_name=doc.get("last_name"), country=doc.get("country"), bot=doc.get("title") == "BOT", perfs=perfs, enabled=doc.get("enabled", True)) db_collections = await app["db"].list_collection_names() if "highscore" not in db_collections: await generate_highscore(app["db"]) cursor = app["db"].highscore.find() async for doc in cursor: app["highscore"][doc["_id"]] = ValueSortedDict(neg, doc["scores"]) if "crosstable" not in db_collections: await generate_crosstable(app["db"]) cursor = app["db"].crosstable.find() async for doc in cursor: app["crosstable"][doc["_id"]] = doc await app["db"].game.create_index("us") await app["db"].game.create_index("v") await app["db"].game.create_index("y") await app["db"].game.create_index("by") except Exception: print("Maybe mongodb is not running...") raise
from subprocess import call logging.basicConfig(level=logging.DEBUG) ###################################################################### # Setup inputs and outputs # Import all ATL_* and Dockerfile environment variables. We lower-case # these for compatability with Ansible template convention. We also # support CATALINA variables from older versions of the Docker images # for backwards compatability, if the new version is not set. env = {k.lower(): v for k, v in os.environ.items()} # Setup Jinja2 for templating jenv = j2.Environment(loader=j2.FileSystemLoader('/opt/atlassian/etc/'), autoescape=j2.select_autoescape(['xml'])) ###################################################################### # Utils def set_perms(path, user, group, mode): shutil.chown(path, user=user, group=group) os.chmod(path, mode) for dirpath, dirnames, filenames in os.walk(path): shutil.chown(dirpath, user=user, group=group) os.chmod(dirpath, mode) for filename in filenames: shutil.chown(os.path.join(dirpath, filename), user=user, group=group)
DIR = path.abspath(path.join(__dir__, 'sites')) OUTDIR = path.abspath(path.join(__dir__, 'out')) OLDDIR = path.abspath(path.join(OUTDIR, 'sites')) CERTIFIER_DIR = path.abspath(path.join(__dir__, '../certifier')) DNSSECDIR = '/etc/powerdns/dnssec' CERTIFIER_DEFFS_DIR = '/mnt/certifier' dynConfig['_self']['dnssecDir'] = DNSSECDIR dynConfig['_self']['certDir'] = path.abspath( path.join(CERTIFIER_DEFFS_DIR, 'certs')) dynConfig['_self']['keyDir'] = path.abspath( path.join(CERTIFIER_DEFFS_DIR, 'keys')) j2env = Environment(loader=FileSystemLoader(path.join(__dir__, 'templates')), autoescape=select_autoescape([])) RECORD_MAX_LEN = 240 def j2_escape_txt_record(value): escaped = value.replace('\\', '\\\\').replace('"', '\\"') return f'"{escaped}"' def j2_format_txt_record(value): value_len = len(value) if value_len <= RECORD_MAX_LEN: return j2_escape_txt_record(value) result = []
def __init__(self, path): self.target_path = os.path.join(path, 'generated') self.template_path = os.path.join(path, 'templates') self.env = Environment(loader=FileSystemLoader(self.template_path), autoescape=select_autoescape(['html', 'xml']))
self.birthdays = [ Birthday(event) for event in search_birthdays(calendar=calendar, days=days) ] logging.info("Connecting to caldav calendar") client = caldav.DAVClient(calendar_url) principal = client.principal() calendars = principal.calendars() if len(calendars) > 0: calendar = calendars[0] else: logging.error("No calendar found") sys.exit(1) reports = [ Report(calendar, days=7), Report(calendar, days=30) ] env = Environment(loader=FileSystemLoader('templates'), autoescape=select_autoescape(['html', 'xml'])) template = env.get_template('email_notification.html') email_body_html = template.render(reports=reports) message = Message(From=os.environ.get('EMAIL_FROM'), To=[os.environ.get('EMAIL_TO')], charset="utf-8") message.Subject = "Rappel anniversaires !" message.Html = email_body_html message.Body = """This is alternate text.""" sender = Mailer(os.environ.get('SMTP_HOST'), port=os.environ.get('SMTP_PORT'), use_tls=False, use_ssl=True) sender.login(os.environ.get('SMTP_USER'),os.environ.get('SMTP_PASS')) sender.send(message)
from jinja2 import Environment, PackageLoader, select_autoescape env = Environment(loader=PackageLoader('myapp', 'views'), autoescape=select_autoescape(['html', 'xml']))
import sys import numpy as np from jinja2 import Environment, PackageLoader, select_autoescape env = Environment(loader=PackageLoader("shops"), autoescape=select_autoescape()) joseph = "Elaho.JosephsSeedShopDisplayCP" paisley = "Elaho.PaisleysBridalBoutiqueCP" template = env.get_template("patch.json") class Gen: def render(self): variants = "Bismarck, BurntOrange, Plain, Leaves, Purple, Spots, TwoTone".split( ", ") layouts = { "1 Shop": [1], "3 Shops": [1, 7, 8], "5 Shops": [1, 5, 6, 7, 8], "6 shops": [1, 2, 6, 7, 8, 9], "7 Shops": [1, 2, 5, 6, 7, 8, 9], "9 Shops": range(1, 10), } exclude_joseph = [2, 7] exclude_paisley = [5] in_layout = {}
from babel.dates import format_date from jinja2 import Environment, select_autoescape, PackageLoader settings_overrides = {} SLACK_DATE_FORMAT = "date" def slack_date_format(dt): return f"<!date^{int(dt.timestamp())}^{{{SLACK_DATE_FORMAT}}}|{date_format(dt)}>" def date_format(d, fmt="medium"): return format_date(d, locale='fr', format=fmt) env = Environment(loader=PackageLoader("core", "templates"), autoescape=select_autoescape( enabled_extensions=('html', 'htm', 'xml'), default_for_string=False, default=False, )) env.filters["dateformat"] = date_format env.filters["slackdateformat"] = slack_date_format
def _process_content(self, mapname, filelist): """ Converts markdown to html with jinja. """ logging.info('Processing files for map %s' % mapname) logging.debug('File list extracted as %s' % filelist) indexlist = [] template_paths = [ self.mapping[mapname]['templates'], pjc.COMMON_TEMPLATES ] if len(filelist) == 0: logging.info('No files to process for map %s!' % mapname) return [] self.j2env = Environment(loader=FileSystemLoader(template_paths), autoescape=select_autoescape(['xml'])) # Load article.html template_name = 'article.html' logging.info('Processing %s' % template_name) try: article_temp = self.j2env.get_template(template_name) logging.debug('Loaded jinja2 template.') except je.TemplateNotFound: logging.error('Missing template %s!' % template_name) # List of files and attribs to be returned out. for entry in filelist: if not self.md: self.md = markdown.Markdown(output_format="xhtml5", extensions=pjc.MARKDOWN_EXTENSIONS) logging.debug('Initialized Markdown engine') logging.info("Processing content file : %s" % os.path.join(entry['dir'], entry['filename'])) f = open(os.path.join(entry['dir'], entry['filename']), 'r', encoding="utf-8") read_text = f.read() f.close() # logging.debug("Text read as: \n %s" % read_text) md_converted = self.md.reset().convert(read_text) logging.debug('Metadata extracted by Markdown: %s' % self.md.Meta) md_meta = self._build_meta_dict(self.md.Meta) logging.debug('Metadata after pyjello processing: %s' % md_meta) # Rewrite markdown file to include missing and default attributes. if md_meta['rewrite']: logging.info('Adding meta attributes to file %s' % entry['filename']) # Backup vars tstamp = datetime.datetime.now().strftime("%Y%m%d%H24%M%S") bkp_path = os.path.join(pjc.BACKUP_ROOT, self.mapping[mapname]['content']) os.makedirs(bkp_path, exist_ok=True) full_bkp_filepath = os.path.join( bkp_path, entry['filename'] + ".bkp." + tstamp) # Backup the existing md file shutil.copyfile(os.path.join(entry['dir'], entry['filename']), full_bkp_filepath) # Rewrite file. del md_meta['rewrite'] outtext = [] for k in md_meta.keys(): if isinstance(md_meta[k], bool) and md_meta[k]: outtext.append(k + ": ") elif isinstance(md_meta[k], list): outtext.append(k + ": " + " ".join(md_meta[k])) elif isinstance(md_meta[k], str): outtext.append(k + ": " + md_meta[k]) else: logging.info( "Not writing attribute with key %s and value %s as it does not satisfy conditions." % (k, md_meta[k])) outtext.append("\n") md_content = pju.util_md_meta_cleanup(read_text) outtext.extend(md_content) f = open(os.path.join(entry['dir'], entry['filename']), 'w', encoding="ascii") f.write("\n".join(outtext)) f.close() logging.info('Meta attributes added to file %s' % entry['filename']) if md_meta['draft'] == False: logging.debug('Markdown converted for %s' % entry['filename']) logging.debug('Metadata extracted as %s' % md_meta) outfilename = os.path.splitext(entry['filename'])[0] + '.html' fulloutpath = os.path.join(self.mapping[mapname]['output'], outfilename) absfulloutpath = os.path.join(os.getcwd(), fulloutpath) logging.debug('Output will be written to %s' % absfulloutpath) os.makedirs(os.path.dirname(absfulloutpath), exist_ok=True) with open(absfulloutpath, 'w+') as o: o.write( article_temp.render(content=md_converted, meta=md_meta, site_url=pjc.SITE_URL)) logging.info('Output write complete for %s' % outfilename) md_meta['fullhtmlout'] = os.path.splitext( entry['filename'])[0] + '.html' indexlist.append(md_meta) else: logging.info( '%s is marked as draft. Not converting markdown.' % entry['filename']) logging.info('Content processing complete for map %s' % mapname) if len(indexlist) != 0: # Index Rebuild logging.info('Rebuilding index.html for mapname %s' % mapname) # Load article_list.html template_name = 'article_list.html' logging.info('Processing %s' % template_name) try: article_list_temp = self.j2env.get_template(template_name) logging.info('Loaded jinja2 template.') except je.TemplateNotFound: logging.error('Missing template %s!' % template_name) fulloutpath = os.path.join(self.mapping[mapname]['output'], 'index.html') logging.info('Writing output to %s.' % fulloutpath) with open(fulloutpath, 'w+') as o: o.write( article_list_temp.render(content=indexlist, site_url=pjc.SITE_URL)) logging.debug('index.html written for map %s' % mapname) else: logging.info( 'No published articles for mapname %s. Skipping index.html build.' % mapname)
language_name, b64qrcode, get_pub_url, get_insert_card_url, get_add_shipment_url, get_public_download_url, get_public_download_torrent_url, public_download_url_has_torrent, ) logger = logging.getLogger(__name__) locale_dir = pathlib.Path(__file__).parent.joinpath("locale") jinja_env = Environment( loader=FileSystemLoader("templates"), autoescape=select_autoescape(["html", "xml", "txt"]), extensions=[ "jinja2.ext.i18n", "jinja2.ext.autoescape", "jinja2.ext.with_" ], ) jinja_env.filters["id"] = get_id jinja_env.filters["yesno"] = yesno jinja_env.filters["qrcode"] = b64qrcode jinja_env.filters["pub_url"] = get_pub_url jinja_env.filters["insert_card_url"] = get_insert_card_url jinja_env.filters["add_shipment_url"] = get_add_shipment_url jinja_env.filters["public_download_url"] = get_public_download_url jinja_env.filters[ "public_download_torrent_url"] = get_public_download_torrent_url jinja_env.filters[ "public_download_url_has_torrent"] = public_download_url_has_torrent
def main(): parser = argparse.ArgumentParser() parser.add_argument( "-f", "--file", required=True, default="attributes.yaml", help="metadata to generate experiment_chartserviceversion yaml") parser.add_argument( "-g", "--generate", required=True, default="experiment", help="scaffold a new chart or experiment into existing chart") parser.add_argument("-t", "--type", required=False, default="all", help="type of the chaos chart") args = parser.parse_args() entity_metadata_source = args.file entity_type = args.generate chartType = args.type # Load data from YAML file into a dictionary # scalar values to Python the dictionary format # YAML document to a Python object. with open(entity_metadata_source) as f: config = yaml.safe_load(f) # get name and category entity_name = config['name'] category_name = config['category'] env = Environment(loader=FileSystemLoader('./'), trim_blocks=True, lstrip_blocks=True, autoescape=select_autoescape(['yaml'])) # store the litmus root from bootstrap folder litmus_root = os.path.abspath(os.path.join("..", os.pardir)) # initilise directories exp_root_dir = litmus_root + '/experiments/' + '/' + config['category'] create_dir(exp_root_dir) experiment_root_dir = exp_root_dir + '/' + config['name'] create_dir(experiment_root_dir) generate_init(exp_root_dir) # if generate_type is chart, only generate the chart(top)-level CSV & package manifests if entity_type == 'chart': # initilise chart directory chart_dir = experiment_root_dir + '/charts' create_dir(chart_dir) if chartType == "category" or chartType == "all": # generate icon for category generate_icon(chart_dir, litmus_root, category_name, env) # generate category chartserviceversion generate_csv_cat(chart_dir, category_name, config, env) # generate package generate_package(chart_dir, config, category_name, env) if chartType == "experiment" or chartType == "all": # generate icon for category generate_icon(chart_dir, litmus_root, entity_name, env) # generate experiment charts generate_csv(chart_dir, entity_name, config, env) # generate experiment-custom-resource generate_chart(chart_dir, config, env) # generate experiment specific rbac generate_rbac(chart_dir, config, env) # generate experiment specific chaos engine generate_engine(chart_dir, config, env) if chartType != "experiment" and chartType != "category" and chartType != "all": print("Provided --chartType={} flag is invalid".format(chartType)) return print("chart created successfully") # if generate_type is experiment, generate the litmusbook arefacts elif entity_type == 'experiment': # initilise experiment directory experiment_dir = experiment_root_dir + '/experiment' create_dir(experiment_dir) # initialise test directory test_dir = experiment_root_dir + '/test' create_dir(test_dir) # generate __init__.py file in root experiment dir generate_init(experiment_root_dir) # initialise chaosLib, environment and types directory chaoslib_dir = litmus_root + '/chaosLib/litmus/' + config[ 'name'] + '/lib' environment_dir = litmus_root + '/pkg/' + config[ 'category'] + '/environment' types_dir = litmus_root + '/pkg/' + config['category'] + '/types' # create and generate __init__.py file in chaosLib experiment dir create_dir(litmus_root + '/chaosLib/litmus/' + config['name']) generate_init(litmus_root + '/chaosLib/litmus/' + config['name']) # generate experiment.py generate_experiment(experiment_dir, entity_name, config, env) # generate chaosLib generate_chaoslib(chaoslib_dir, entity_name, config, env) # generate environment.py generate_environment(environment_dir, config, env) # generate environment.py generate_types(types_dir, config, env) # generate k8s deployment generate_k8s_deployment(test_dir, config, env) generate_init(litmus_root + '/pkg/' + config['category']) print("experiment created successfully") else: print("Provided --generate={} flag is invalid".format(entity_type))
def load_env(): from jinja2 import Environment, FileSystemLoader, select_autoescape return Environment(loader=FileSystemLoader('templates/'), autoescape=select_autoescape(['html']))
# def send_email(server, person, subject, body): # # http://naelshiab.com/tutorial-send-email-python/ # msg = MIMEMultipart() # msg['From'] = os.environ['GMAIL_EMAIL'] # msg['To'] = person.email # msg['Subject'] = subject # msg.attach(MIMEText(body, 'plain')) # server.sendmail(os.environ['GMAIL_EMAIL'], person.email, msg.as_string()) # render email for bonuses env = jinja2.Environment( loader=jinja2.PackageLoader('a_model', 'templates'), autoescape=jinja2.select_autoescape(['html', 'xml']) ) email_template = env.get_template('bonus_email.txt') # prepare email server for bonuses # http://naelshiab.com/tutorial-send-email-python/ if args.send_emails: print >> sys.stderr, "NOTE: SENDING EMAILS ISNT WORKING YET DUE TO ISSUE" print >> sys.stderr, "WITH GMAIL." # server = smtplib.SMTP('smtp.gmail.com', 587) # server.starttls() # server.login(os.environ['GMAIL_EMAIL'], os.environ['GMAIL_PASSWORD']) # print out the bonuses for each person total_time = sum([person.fraction_of_year for person in people]) total_golden_six_packs = sum([person.n_golden_six_packs for person in people])
def get_template(template_filename): env = Environment( loader=FileSystemLoader(searchpath="app/emailing/templates"), autoescape=select_autoescape(["html", "xml"]), ) return env.get_template(template_filename)
import webbrowser from io import open import unicodecsv as csv from jinja2 import Environment, FileSystemLoader, select_autoescape logging.basicConfig() logger = logging.getLogger(__package__) package_path = os.path.dirname(os.path.abspath(__file__)) templates_dir = os.path.join(package_path, "templates") # Initialize Jinja 2 env env = Environment( loader=FileSystemLoader(templates_dir), autoescape=select_autoescape(["html", "xml", "j2"]) ) template = env.get_template("template.j2") # Regex to match src property in script tags js_src_pattern = re.compile(r'<script.*?src=\"(.*?)\".*?<\/script>', re.IGNORECASE | re.MULTILINE) # Path to JS files inside templates js_files_path = os.path.join(package_path, templates_dir) def convert(input_file_name, **kwargs): """Convert CSV file to HTML table""" delimiter = kwargs["delimiter"] or "," quotechar = kwargs["quotechar"] or "|"
def __init__( self, *, debug=False, title=None, version=None, openapi=None, openapi_route="/schema.yml", static_dir="static", static_route="/static", templates_dir="templates", auto_escape=True, secret_key=DEFAULT_SECRET_KEY, enable_hsts=False, docs_route=None, cors=False, cors_params=DEFAULT_CORS_PARAMS, allowed_hosts=None, ): self.background = BackgroundQueue() self.secret_key = secret_key self.title = title self.version = version self.openapi_version = openapi self.static_dir = Path(os.path.abspath(static_dir)) self.static_route = static_route self.templates_dir = Path(os.path.abspath(templates_dir)) self.built_in_templates_dir = Path( os.path.abspath(os.path.dirname(__file__) + "/templates")) self.routes = {} self.docs_theme = DEFAULT_API_THEME self.docs_route = docs_route self.schemas = {} self.session_cookie = DEFAULT_SESSION_COOKIE self.hsts_enabled = enable_hsts self.cors = cors self.cors_params = cors_params self.debug = debug if not allowed_hosts: # if not debug: # raise RuntimeError( # "You need to specify `allowed_hosts` when debug is set to False" # ) allowed_hosts = ["*"] self.allowed_hosts = allowed_hosts # Make the static/templates directory if they don't exist. for _dir in (self.static_dir, self.templates_dir): os.makedirs(_dir, exist_ok=True) self.whitenoise = WhiteNoise(application=self._default_wsgi_app) self.whitenoise.add_files(str(self.static_dir)) self.whitenoise.add_files((Path(apistar.__file__).parent / "themes" / self.docs_theme / "static").resolve()) self.apps = {} self.mount(self.static_route, self.whitenoise) self.formats = get_formats() # Cached requests session. self._session = None if self.openapi_version: self.add_route(openapi_route, self.schema_response) if self.docs_route: self.add_route(self.docs_route, self.docs_response) self.default_endpoint = None self.app = self.dispatch self.add_middleware(GZipMiddleware) if self.hsts_enabled: self.add_middleware(HTTPSRedirectMiddleware) self.add_middleware(TrustedHostMiddleware, allowed_hosts=self.allowed_hosts) self.lifespan_handler = LifespanMiddleware(self.app) if self.cors: self.add_middleware(CORSMiddleware, **self.cors_params) self.add_middleware(ServerErrorMiddleware, debug=debug) # Jinja enviroment self.jinja_env = jinja2.Environment( loader=jinja2.FileSystemLoader( [str(self.templates_dir), str(self.built_in_templates_dir)], followlinks=True, ), autoescape=jinja2.select_autoescape( ["html", "xml"] if auto_escape else []), ) self.jinja_values_base = {"api": self} # Give reference to self. self.requests = ( self.session() ) #: A Requests session that is connected to the ASGI app.
def netcdf_to_erddap_dataset(deployments_path, datasets_path, netcdf_path, flag_path): tmp_handle, tmp_path = tempfile.mkstemp(prefix='gutils_errdap_', suffix='.xml') try: loader = PackageLoader('gutils', 'templates') jenv = Environment(loader=loader, autoescape=select_autoescape(['html', 'xml'])) # Copy datasets.xml to a tmpfile if it exists if os.path.isfile(datasets_path): shutil.copy(datasets_path, tmp_path) else: # Render the base template to the tmpfile datasets_template_string = jenv.get_template( 'erddap_datasets.xml').render() with open(tmp_path, 'wt') as f: f.write( etree.tostring(etree.fromstring(datasets_template_string), encoding='ISO-8859-1', pretty_print=True, xml_declaration=True).decode('iso-8859-1')) f.write('\n') # Go back until we hit the deployments_path, then we extract the deployment name and mode deployment_directory = Path(netcdf_path).parent mode = deployment_directory.parent.name dep_path = Path(deployments_path) individual_dep_path = None for pp in deployment_directory.parents: if dep_path == pp: break individual_dep_path = pp deployment_name = f'{individual_dep_path.name}_{mode}' with nc4.Dataset(netcdf_path) as ncd: xmlstring = jenv.get_template('erddap_deployment.xml').render( deployment_name=deployment_name, deployment_directory=str(deployment_directory), deployment_variables=ncd.variables, datatype_mapping=datatype_mapping, destination_mapping=destination_mapping) deployment_xml_node = etree.fromstring(xmlstring) # Create xmltree = etree.parse(tmp_path).getroot() find_dataset = etree.XPath( "//erddapDatasets/dataset[@datasetID=$name]") # Find an existing datasetID within the datasets.xml file dnode = find_dataset(xmltree, name=deployment_name) if not dnode: # No datasetID found, create a new one xmltree.append(deployment_xml_node) L.debug("Added Deployment: {}".format(deployment_name)) else: if lxml_elements_equal(dnode[0], deployment_xml_node): L.debug("Not replacing identical deployment XML node") return else: # Now make sure we don't remove any variables since some could be # missing from this file but present in others new_vars = [ d.findtext('sourceName') for d in deployment_xml_node.iter('dataVariable') ] # iterate over the old_vars and figure out which ones # are not in the new_vars for dv in dnode[0].iter('dataVariable'): vname = dv.findtext('sourceName') if vname not in new_vars: # Append the old variable block into the new one L.debug('Carried over variable {}'.format(vname)) deployment_xml_node.append(deepcopy(dv)) # Update the existing datasetID with a new XML block xmltree.replace(dnode[0], deployment_xml_node) L.debug("Replaced Deployment: {}".format(deployment_name)) # Create tempfile for the new modified file new_datasets_handle, new_datasets_path = tempfile.mkstemp( prefix='gutils_erddap_', suffix='.xml') with open(new_datasets_path, 'wt') as f: f.write( etree.tostring(xmltree, encoding='ISO-8859-1', pretty_print=True, xml_declaration=True).decode('iso-8859-1')) f.write('\n') # Replace old datasets.xml os.close(new_datasets_handle) os.chmod(new_datasets_path, 0o664) shutil.move(new_datasets_path, datasets_path) finally: # Write dataset update flag if it doesn't exist if flag_path is not None: flag_tmp_handle, flag_tmp_path = tempfile.mkstemp( prefix='gutils_errdap_', suffix='.flag') final_flagfile = os.path.join(flag_path, deployment_name) if not os.path.isfile(final_flagfile): with open(flag_tmp_path, 'w') as ff: ff.write(datetime.utcnow().isoformat()) os.chmod(flag_tmp_path, 0o666) shutil.move(flag_tmp_path, final_flagfile) os.close(flag_tmp_handle) if os.path.exists(flag_tmp_path): os.remove(flag_tmp_path) os.close(tmp_handle) if os.path.exists(tmp_path): os.remove(tmp_path)
def create_app(test_config=None): ### TUYAUTERIE ### # create and configure the app # c'est la doc qui dit qu'il vaut mieux utiliser la forme split app = Flask(__name__.split('.')[0], instance_relative_config=True) app.config.from_mapping( SECRET_KEY='dev', TOOLBAR=os.environ.get('TOOLBAR'), TITRE='Des Nombres Premiers à chaque mise à jour !', DEBUG_TB_TEMPLATE_EDITOR_ENABLED=True, #EXPLAIN_TEMPLATE_LOADING=True ) if test_config is None: # load the instance config, if it exists, when not testing # contient essentiellement la "SECRET_KEY" app.config.from_pyfile('config.py', silent=True) else: # load the test config if passed in app.config.from_mapping(test_config) # si la variable d'environnement "SECRET_KEY" existe alors c'est la bonne (bonne pratique docker) if os.environ.get('SECRET_KEY'): app.config.from_mapping(SECRET_KEY=os.environ['SECRET_KEY'], ) # autoescape sur .j2 from jinja2 import select_autoescape app.jinja_env.autoescape = select_autoescape(default_for_string=True, disabled_extensions=('txt', ), default=True) # ensure the instance folder exists try: os.makedirs(app.instance_path) except OSError: pass # la toolbar if app.config['TOOLBAR']: from flask_debugtoolbar import DebugToolbarExtension #toolbar = DebugToolbarExtension(app) DebugToolbarExtension(app) if app.debug: # lorsqu'il y a la toolbar, le logger n'envoie plus les debug app.logger.propagate = True @app.route('/robots.txt') @app.route('/humans.txt') @app.route('/favicon.ico') @app.route('/sitemap.xml') def static_from_root(): return send_from_directory(app.static_folder, request.path[1:]) # session côté serveur redis = os.environ.get('REDIS') if os.environ.get('REDIS') else False app.config.update( SESSION_TYPE='redis' if redis else 'filesystem', SESSION_COOKIE_HTTPONLY=True, SESSION_COOKIE_SAMESITE='Lax', #SESSION_COOKIE_SECURE=True, # impose HTTPS SESSION_USE_SIGNER=True, SESSION_FILE_DIR=os.path.join(app.instance_path, 'flask_session'), PERMANENT_SESSION_LIFETIME=2 * 24 * 3600 + 3600 # 49 heures ) if redis: from redis import Redis hp = redis.split(':') h = hp[0] p = 6379 if len(hp) == 1 else int(hp[1]) app.config['SESSION_REDIS'] = Redis(host=h, port=p) Session(app) # Markdown from flaskext.markdown import Markdown Markdown(app) # sécuriser les FORM HTML # from flask_wtf.csrf import CSRFProtect # CSRFProtect(app) ### Mon appli ### from primes import primes app.register_blueprint(primes.bp) app.add_url_rule('/', endpoint='homepage') return app
def __init__(self): templates_path =os.environ['SQL_TEMPLATES_PATH'] self.env = Environment( loader=FileSystemLoader(templates_path), autoescape=select_autoescape(['html', 'xml'])) load_filters(self.env)
def __init__(self, db: dhtc.Database): self.db = db self.env = Environment(loader=PackageLoader("dhtc", "templates"), autoescape=select_autoescape(["html"]))
def get_template(path, template_name): env = Environment(loader=FileSystemLoader(path), autoescape=select_autoescape(['html', 'xml'])) template = env.get_template(template_name) return template
def __init__(self, model, controller, import_name=__name__, title='Results Database', auth_decorator=None, archive_routes=None): super(ViewRoutes, self).__init__('view', import_name, url_prefix=None, auth_decorator=auth_decorator) self._cache = {} self.title = title self.environment = Environment( loader=PackageLoader(package_name='resultsdbpy.view', package_path='templates'), autoescape=select_autoescape(['html', 'xml']), ) # Protecting js and css with auth doesn't make sense self.add_url_rule('/library/<path:path>', 'library', self.library, authed=False, methods=('GET', )) self.add_url_rule('/assets/<path:path>', 'assets', self.assets, authed=False, methods=('GET', )) self.site_menu = SiteMenu(title=self.title) self.commits = CommitView( environment=self.environment, commit_controller=controller.commit_controller, site_menu=self.site_menu, ) self.suites = SuiteView( environment=self.environment, upload_controller=controller.upload_controller, suite_controller=controller.suite_controller, site_menu=self.site_menu, ) self.ci = CIView( environment=self.environment, ci_controller=controller.ci_controller, site_menu=self.site_menu, ) self.archive = ArchiveView( environment=self.environment, archive_controller=controller.archive_controller, site_menu=self.site_menu, ) self.add_url_rule('/', 'main', self.suites.search, methods=('GET', )) self.add_url_rule('/search', 'search', self.suites.search, methods=('GET', )) self.add_url_rule('/documentation', 'documentation', self.documentation, methods=('GET', )) self.add_url_rule('/commit', 'commit', self.commits.commit, methods=('GET', )) self.add_url_rule('/commit/info', 'commit_info', self.commits.info, methods=('GET', )) self.add_url_rule('/commit/previous', 'commit_previous', self.commits.previous, methods=('GET', )) self.add_url_rule('/commit/next', 'commit_next', self.commits.next, methods=('GET', )) self.add_url_rule('/commits', 'commits', self.commits.commits, methods=('GET', )) self.add_url_rule('/suites', 'suites', self.suites.results, methods=('GET', )) self.add_url_rule('/investigate', 'investigate', self.suites.investigate, methods=('GET', )) self.add_url_rule('/urls/queue', 'urls-queue', self.ci.queue, methods=('GET', )) self.add_url_rule('/urls/worker', 'urls-worker', self.ci.worker, methods=('GET', )) self.add_url_rule('/urls/build', 'urls-build', self.ci.build, methods=('GET', )) self.add_url_rule('/archive', 'archive-list', self.archive.extract, methods=('GET', )) self.add_url_rule('/archive/<path:path>', 'archive', self.archive.extract, methods=('GET', )) # Archive routes control which suites get linked to result archives and how those links appear in the UI. # This dictionary will be processed by archiveRouter.js, there are 3 active parameters respected: # enabled: If false, no archive link will be generated for this suite # path: The file in the archive to link to # label: Defines the user-visible name of the archive link # Dictionaries have a defined ancestry, and will use the most specific parameters possible. The ancestry # arguments are: # <suite>: Name of a suite (like layout-tests) # <result>: One of the expected results, like FAIL, TEXT or IMAGE self.archive_routes = archive_routes or dict() self.add_url_rule('/archive-routes', 'archive-routes', cache_for(hours=24 * 7)(lambda: jsonify(self.archive_routes)), methods=('GET', )) self.site_menu.add_endpoint('Main', self.name + '.main', parameters=['branch']) self.site_menu.add_endpoint('Suites', self.name + '.suites', parameters=['branch']) self.site_menu.add_endpoint('Investigate', self.name + '.investigate', parameters=['branch']) self.site_menu.add_endpoint('Documentation', self.name + '.documentation') self.site_menu.add_endpoint('Commits', self.name + '.commits', parameters=['branch']) self.register_error_handler(500, self.response_500)
def get_template(): env = Environment(loader=PackageLoader('updatechangelog', 'templates'), autoescape=select_autoescape(['txt'])) return env.get_template('header.txt')
"build_environment": f"{env_prefix}binary-libtorchvision_ops-android-upload", "context": "org-member", "filters": gen_filter_branch_tree("nightly"), "name": f"{name_prefix}binary_libtorchvision_ops_android_upload", } jobs.append({"binary_android_upload": upload_job}) else: jobs.append({"binary_android_build": build_job}) return indent(indentation, jobs) if __name__ == "__main__": d = os.path.dirname(__file__) env = jinja2.Environment( loader=jinja2.FileSystemLoader(d), lstrip_blocks=True, autoescape=select_autoescape(enabled_extensions=("html", "xml")), keep_trailing_newline=True, ) with open(os.path.join(d, "config.yml"), "w") as f: f.write( env.get_template("config.yml.in").render( build_workflows=build_workflows, unittest_workflows=unittest_workflows, cmake_workflows=cmake_workflows, ios_workflows=ios_workflows, android_workflows=android_workflows, ))
import jinja2 import tornado.web env = jinja2.Environment(loader=jinja2.PackageLoader('intake.cli.server'), autoescape=jinja2.select_autoescape(['html', 'xml'])) def get_browser_handlers(local_catalog): return [ (r"/", BrowserHandler, dict(local_catalog=local_catalog)), ] class BrowserHandler(tornado.web.RequestHandler): def initialize(self, local_catalog): self.local_catalog = local_catalog def get(self): try: template = env.get_template('index.html') sources = [] for _, name, source in self.local_catalog.walk(): description = source.describe() sources.append(dict(name=name, description=description)) self.write(template.render(dict(sources=sources))) except: pass
def render_to_html(self): env = Environment(loader=PackageLoader(__name__, 'templates'), autoescape=select_autoescape(['html', 'xml'])) env.get_template()
def fetch_template(template_file): template_loader = FileSystemLoader(template_folder) env = Environment(loader=template_loader, autoescape=select_autoescape(['html', 'xml'])) template = env.get_template(template_file) return template
# -*- coding: utf-8 -*- import math from datetime import datetime from jinja2 import Environment, PackageLoader, select_autoescape def is_prime(n): if n == 2: return True for i in range(2, int(math.ceil(math.sqrt(n))) + 1): if n % i == 0: return False return True package_loader = PackageLoader('testapp', 'templates') env = Environment(loader=package_loader, autoescape=select_autoescape( enabled_extensions=['html', 'xml'], default_for_string=True)) env.tests['prime'] = is_prime template = env.get_template('tests.html') print(template.render(name='Rahul', number=24))
def translate(self): visitor = PDFTranslator(self.document, self.builder) self.document.walkabout(visitor) lang = self.config.language or 'en' langmod = get_language_available(lang)[2] self.docutils_languages = {lang: langmod} # Generate Contents topic manually if self.use_toc: contents = nodes.topic(classes=['contents']) contents += nodes.title('') contents[0] += nodes.Text(langmod.labels['contents']) contents['ids'] = ['Contents'] pending = nodes.topic() contents.append(pending) pending.details = {} self.document.insert( 0, nodes.raw(text='SetPageCounter 1 arabic', format='pdf')) self.document.insert( 0, nodes.raw(text='OddPageBreak %s' % self.page_template, format='pdf')) self.document.insert(0, contents) self.document.insert( 0, nodes.raw(text='SetPageCounter 1 lowerroman', format='pdf')) contTrans = PDFContents(self.document) contTrans.toc_depth = self.toc_depth contTrans.startnode = pending contTrans.apply() if self.use_coverpage: # Generate cover page # FIXME: duplicate from createpdf, refactor! # Add the Sphinx template paths def add_template_path(path): return os.path.join(self.srcdir, path) jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader([ self.srcdir, os.path.expanduser('~/.rst2pdf'), os.path.join(self.PATH, 'templates') ] + list(map(add_template_path, self.config.templates_path))), autoescape=jinja2.select_autoescape( ['html', 'xml'])) try: template = jinja_env.get_template( self.config.pdf_cover_template) except jinja2.TemplateNotFound: log.error("Can't find cover template %s, using default" % self.custom_cover) template = jinja_env.get_template('sphinxcover.tmpl') # This is what's used in the python docs because # Latex does a manual linebreak. This sucks. authors = self.document.settings.author.split('\\') # Honour the "today" config setting if self.config.today: date = self.config.today else: date = time.strftime(self.config.today_fmt or _('%B %d, %Y')) # Feed data to the template, get restructured text. cover_text = template.render(title=self.document.settings.title or visitor.elements['title'], subtitle='%s %s' % (_('version'), self.config.version), authors=authors, date=date) cover_tree = docutils.core.publish_doctree(cover_text) self.document.insert(0, cover_tree) sio = BytesIO() if self.invariant: createpdf.patch_PDFDate() createpdf.patch_digester() createpdf.RstToPdf( sphinx=True, stylesheets=self.stylesheets, language=self.__language, breaklevel=self.breaklevel, breakside=self.breakside, fit_mode=self.fitmode, font_path=self.fontpath, inline_footnotes=self.inline_footnotes, highlightlang=self.highlightlang, splittables=self.splittables, style_path=self.style_path, repeat_table_rows=self.repeat_table_rows, basedir=self.srcdir, def_dpi=self.default_dpi, real_footnotes=self.real_footnotes, numbered_links=self.use_numbered_links, background_fit_mode=self.fit_background_mode, baseurl=self.baseurl, section_header_depth=self.section_header_depth).createPdf( doctree=self.document, output=sio, compressed=self.compressed) self.output = sio.getvalue()
import os import sys from jinja2 import Environment, FileSystemLoader, select_autoescape env = Environment(loader=FileSystemLoader(searchpath='templates'), autoescape=select_autoescape(['html', 'txt'])) for template_file in os.listdir('templates'): template = env.get_template(template_file) result = template.render() with open('results/%s' % template.filename.split('/')[-1], 'w') as f: f.write(result)
import os import arrow from jinja2 import Environment, FileSystemLoader, select_autoescape from lemur.plugins.utils import get_plugin_option loader = FileSystemLoader(searchpath=os.path.dirname(os.path.realpath(__file__))) env = Environment(loader=loader, # nosec: potentially dangerous types esc. autoescape=select_autoescape(['html', 'xml'])) def human_time(time): return arrow.get(time).format('dddd, MMMM D, YYYY') def interval(options): return get_plugin_option('interval', options) def unit(options): return get_plugin_option('unit', options) env.filters['time'] = human_time env.filters['interval'] = interval env.filters['unit'] = unit
def main(template_names: List[str] = ['index.html', 'rss.xml'], output_dir: str = 'build', render_image: bool = True) -> int: date_data = date.today() if is_same_day(): date_data = date.today() - timedelta(days=1) latest_data = _diff_data(date_data) if latest_data: previous_data = [ _diff_data(date.today() - timedelta(days=x)) for x in range(1, 7) ] previous_data = list( filter(lambda c: c.contagions > 0 and c.tests > 0, previous_data)) trend = previous_data.copy() trend.reverse() if not is_same_day(): trend.append(latest_data) template_path = pathlib.Path().absolute() env = Environment(loader=FileSystemLoader(template_path), autoescape=select_autoescape(['html', 'xml'])) env.filters['datetimeformat'] = datetime_format env.filters['currencyformat'] = format_currency env.filters['percentformat'] = format_percent env.filters['rfcformat'] = datetime_rfc_2822_format for template_name in template_names: template = env.get_template(template_name) output_from_parsed_template = template.render( latest_data=latest_data, previous_data=previous_data, trend=trend) out_path = f"{output_dir}/{template_name}" print(f" Writing: {out_path}") with open(out_path, "w") as fh: fh.write(output_from_parsed_template) print(f"Rendering image ? {render_image}") if render_image: with plt.xkcd(): fig, ax = plt.subplots() labels = list( map( lambda d: datetime_format(d.report_date, format="%d-%m"), previous_data)) chart_data = list(map(lambda d: d.percents, previous_data)) labels.reverse() chart_data.reverse() fig.text( 0.15, 0.8, f'{latest_data.report_date} : {format_percent(latest_data.percents)} %', fontsize=24) ax.plot(labels, chart_data) plt.savefig(f'{output_dir}/chart.png') print("Image chart updated") return 0 else: return -1