Python jinja2.BaseLoader() Examples
The following are 13
code examples of jinja2.BaseLoader().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
jinja2
, or try the search function
.
Example #1
Source File: utils.py From cloud-inquisitor with Apache License 2.0 | 6 votes |
def get_template(template): """Return a Jinja2 template by filename Args: template (str): Name of the template to return Returns: A Jinja2 Template object """ from cloud_inquisitor.database import db tmpl = db.Template.find_one(template_name=template) if not tmpl: raise InquisitorError('No such template found: {}'.format(template)) tmplenv = Environment(loader=BaseLoader, autoescape=True) tmplenv.filters['json_loads'] = json.loads tmplenv.filters['slack_quote_join'] = lambda data: ', '.join('`{}`'.format(x) for x in data) return tmplenv.from_string(tmpl.template)
Example #2
Source File: templating.py From quart with MIT License | 5 votes |
def _loaders(self) -> Generator[BaseLoader, None, None]: loader = self.app.jinja_loader if loader is not None: yield loader for blueprint in self.app.iter_blueprints(): loader = blueprint.jinja_loader if loader is not None: yield loader
Example #3
Source File: create_pipeline_manual.py From foremast with Apache License 2.0 | 5 votes |
def get_rendered_json(self, json_string, pipeline_vars=None): """Takes a string of a manual template and renders it as a Jinja2 template, returning the result Args: json_string (str): pipeline in jinja/json format pipeline_vars (dict): key/value pairs of variables the pipline expects Returns: str: pipeline json after Jinja is rendered""" try: if TEMPLATES_PATH: loader = jinja2.FileSystemLoader(TEMPLATES_PATH) else: loader = jinja2.BaseLoader() jinja_template = jinja2.Environment(loader=loader).from_string(json_string) # Get any pipeline args defined in pipeline.json, default to empty dict if none defined pipeline_args = dict() except jinja2.TemplateNotFound: # Log paths searched for debugging, then re-raise message = 'Jinja2 TemplateNotFound exception in paths {paths}'.format(paths=loader.searchpath) self.log.error(message) raise # Expose permitted functions and variables to the template pipeline_args.update(get_jinja_functions()) pipeline_args.update(get_jinja_variables(self)) # If any args set in the pipeline file add them to the pipeline_args.variables if pipeline_vars is not None: pipeline_args["template_variables"] = pipeline_vars # Render the template return jinja_template.render(pipeline_args)
Example #4
Source File: models.py From senpy with Apache License 2.0 | 5 votes |
def serialize(self, format='json-ld', with_mime=False, template=None, prefix=None, fields=None, **kwargs): js = self.jsonld(prefix=prefix, **kwargs) if template is not None: rtemplate = Environment(loader=BaseLoader).from_string(template) content = rtemplate.render(**self) mimetype = 'text' elif fields is not None: # Emulate field selection by constructing a template content = json.dumps(jmespath.search(fields, js)) mimetype = 'text' elif format == 'json-ld': content = json.dumps(js, indent=2, sort_keys=True) mimetype = "application/json" elif format in ['turtle', 'ntriples']: content = json.dumps(js, indent=2, sort_keys=True) logger.debug(js) context = [self._context, {'prefix': prefix, '@base': prefix}] g = Graph().parse( data=content, format='json-ld', prefix=prefix, context=context) logger.debug( 'Parsing with prefix: {}'.format(kwargs.get('prefix'))) content = g.serialize(format=format, prefix=prefix).decode('utf-8') mimetype = 'text/{}'.format(format) else: raise Error('Unknown outformat: {}'.format(format)) if with_mime: return content, mimetype else: return content
Example #5
Source File: cli.py From gordo with GNU Affero General Public License v3.0 | 5 votes |
def expand_model(model_config: str, model_parameters: dict): """ Expands the jinja template which is the model using the variables in `model_parameters` Parameters ---------- model_config: str Jinja template which when expanded becomes a valid model config json. model_parameters: Parameters for the model config. Raises ------ ValueError If an undefined variable is used in the model_config. Returns ------- str The model config with variables expanded """ try: model_template = jinja2.Environment( loader=jinja2.BaseLoader(), undefined=jinja2.StrictUndefined ).from_string(model_config) model_config = model_template.render(**model_parameters) except jinja2.exceptions.UndefinedError as e: raise ValueError("Model parameter missing value!") from e logger.info(f"Expanded model config: {model_config}") return yaml.safe_load(model_config)
Example #6
Source File: spawner.py From kubespawner with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _render_options_form(self, profile_list): self._profile_list = self._init_profile_list(profile_list) profile_form_template = Environment(loader=BaseLoader).from_string(self.profile_form_template) return profile_form_template.render(profile_list=self._profile_list)
Example #7
Source File: e2e_tests.py From pipdeptree with MIT License | 5 votes |
def final_command(s): tmpl = Environment(loader=BaseLoader).from_string(s) return tmpl.render(pipdeptree=pipdeptree_path)
Example #8
Source File: html_renderer.py From antismash with GNU Affero General Public License v3.0 | 5 votes |
def __init__(self, template_dir: Optional[str] = None) -> None: self.template = None # type: Optional[_jinja2.Template] if not template_dir: loader = _jinja2.BaseLoader() else: loader = _jinja2.FileSystemLoader(template_dir) self.env = _jinja2.Environment(loader=loader, autoescape=True, undefined=_jinja2.StrictUndefined)
Example #9
Source File: notifiers.py From dockupdater with MIT License | 5 votes |
def __init__(self, container_or_service): if isinstance(container_or_service, Service): title = f'dockupdater has updated services!' else: title = f'dockupdater has updated containers!' template = Environment(loader=BaseLoader).from_string(container_or_service.config.template) body = template.render(object=container_or_service) super().__init__(title, body)
Example #10
Source File: _exchange.py From NURBS-Python with MIT License | 4 votes |
def process_template(file_src): """ Process Jinja2 template input :param file_src: file contents :type file_src: str """ def tmpl_sqrt(x): """ Square-root of 'x' """ return math.sqrt(x) def tmpl_cubert(x): """ Cube-root of 'x' """ return x ** (1.0 / 3.0) if x >= 0 else -(-x) ** (1.0 / 3.0) def tmpl_pow(x, y): """ 'x' to the power 'y' """ return math.pow(x, y) # Check if it is possible to import 'jinja2' try: import jinja2 except ImportError: raise GeomdlException("Please install 'jinja2' package to use templated input: pip install jinja2") # Replace jinja2 template tags for compatibility fsrc = file_src.replace("{%", "<%").replace("%}", "%>").replace("{{", "<{").replace("}}", "}>") # Generate Jinja2 environment env = jinja2.Environment( loader=jinja2.BaseLoader(), trim_blocks=True, block_start_string='<%', block_end_string='%>', variable_start_string='<{', variable_end_string='}>' ).from_string(fsrc) # Load custom functions into the Jinja2 environment template_funcs = dict( knot_vector=utilities.generate_knot_vector, sqrt=tmpl_sqrt, cubert=tmpl_cubert, pow=tmpl_pow, ) for k, v in template_funcs.items(): env.globals[k] = v # Process Jinja2 template functions & variables inside the input file return env.render()
Example #11
Source File: templates.py From king-phisher with BSD 3-Clause "New" or "Revised" License | 4 votes |
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 {})
Example #12
Source File: loader.py From rally with Apache License 2.0 | 4 votes |
def render_template(template_source, template_vars=None, template_internal_vars=None, loader=None): macros = [ """ {% macro collect(parts) -%} {% set comma = joiner() %} {% for part in glob(parts) %} {{ comma() }} {% include part %} {% endfor %} {%- endmacro %} """, """ {% macro exists_set_param(setting_name, value, default_value=None, comma=True) -%} {% if value is defined or default_value is not none %} {% if comma %} , {% endif %} {% if default_value is not none %} "{{ setting_name }}": {{ value | default(default_value) | tojson }} {% else %} "{{ setting_name }}": {{ value | tojson }} {% endif %} {% endif %} {%- endmacro %} """ ] # place helpers dict loader first to prevent users from overriding our macros. env = jinja2.Environment( loader=jinja2.ChoiceLoader([ jinja2.DictLoader({"rally.helpers": "".join(macros)}), jinja2.BaseLoader(), loader ]) ) if template_vars: for k, v in template_vars.items(): env.globals[k] = v # ensure that user variables never override our internal variables if template_internal_vars: for macro_type in template_internal_vars: for env_global_key, env_global_value in template_internal_vars[macro_type].items(): getattr(env, macro_type)[env_global_key] = env_global_value template = env.from_string(template_source) return template.render()
Example #13
Source File: hive_email_operators.py From pandora-plugin with Apache License 2.0 | 4 votes |
def execute(self, context): ti = context['ti'] host, dagid, taskid, exectime = ti.hostname.split('.')[0], ti.dag_id, ti.task_id, ti.execution_date.isoformat() hook = HiveCliHook( hive_cli_conn_id=self.hive_cli_conn_id, mapred_queue=self.mapred_queue, mapred_job_name='Airflow HiveEmailOperator task for {}.{}.{}.{}'.format(host, dagid, taskid, exectime) ) hook.hive_cli_params = '-S' # suppress hive junk output output = hook.run_cli(hql=self.hql, schema=self.schema, hive_conf={'hive.cli.print.header': 'true'}) output_rows = [line for line in output.split('\n') if line] col_names = output_rows[0].split('\t') output_rows = output_rows[1:] if len(output_rows) > self.cutoff: msg = 'The query returned > {} rows.. Adding tsv as an attachment.'.format(self.cutoff) logging.warn(msg) f = tempfile.NamedTemporaryFile(delete=False) f.write(output) f.close() self.files = [f.name] self.html_content = '{}<br>Dag id: {}<br>Task id: {}<br>Execution Time: {}'.format(msg, dagid, taskid, exectime) else: context.update({ 'hql': self.hql, 'rows': output_rows, 'col_names': col_names }) if not self.html_content: check_path = os.path.join(os.path.dirname(__file__), '..', 'templates', 'hive_email_default.html') else: dag_path = conf.get('core', 'dags_folder') check_path = os.path.join(dag_path, os.path.dirname(context['dag'].filepath), self.html_content) if os.path.exists(check_path): path, filename = os.path.split(os.path.abspath(check_path)) template = Environment(loader=FileSystemLoader(path)).get_template(filename) logging.info("Using templated file located at: {path}".format(path=check_path)) else: template = Environment(loader=BaseLoader()).from_string(self.html_content) self.html_content = template.render(**context) super(HiveEmailOperator, self).execute(context) # delete the temp file after successfully attached to email if len(output_rows) > self.cutoff: os.unlink(f.name)