当前位置: 首页>>代码示例>>Python>>正文


Python jinja2.BaseLoader方法代码示例

本文整理汇总了Python中jinja2.BaseLoader方法的典型用法代码示例。如果您正苦于以下问题:Python jinja2.BaseLoader方法的具体用法?Python jinja2.BaseLoader怎么用?Python jinja2.BaseLoader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在jinja2的用法示例。


在下文中一共展示了jinja2.BaseLoader方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get_template

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import BaseLoader [as 别名]
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) 
开发者ID:RiotGames,项目名称:cloud-inquisitor,代码行数:22,代码来源:utils.py

示例2: _loaders

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import BaseLoader [as 别名]
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 
开发者ID:pgjones,项目名称:quart,代码行数:11,代码来源:templating.py

示例3: get_rendered_json

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import BaseLoader [as 别名]
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) 
开发者ID:foremast,项目名称:foremast,代码行数:37,代码来源:create_pipeline_manual.py

示例4: serialize

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import BaseLoader [as 别名]
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 
开发者ID:gsi-upm,项目名称:senpy,代码行数:36,代码来源:models.py

示例5: expand_model

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import BaseLoader [as 别名]
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) 
开发者ID:equinor,项目名称:gordo,代码行数:34,代码来源:cli.py

示例6: _render_options_form

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import BaseLoader [as 别名]
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) 
开发者ID:jupyterhub,项目名称:kubespawner,代码行数:6,代码来源:spawner.py

示例7: final_command

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import BaseLoader [as 别名]
def final_command(s):
    tmpl = Environment(loader=BaseLoader).from_string(s)
    return tmpl.render(pipdeptree=pipdeptree_path) 
开发者ID:naiquevin,项目名称:pipdeptree,代码行数:5,代码来源:e2e_tests.py

示例8: __init__

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import BaseLoader [as 别名]
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) 
开发者ID:antismash,项目名称:antismash,代码行数:10,代码来源:html_renderer.py

示例9: __init__

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import BaseLoader [as 别名]
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) 
开发者ID:dockupdater,项目名称:dockupdater,代码行数:11,代码来源:notifiers.py

示例10: process_template

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import BaseLoader [as 别名]
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() 
开发者ID:orbingol,项目名称:NURBS-Python,代码行数:49,代码来源:_exchange.py

示例11: __init__

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import BaseLoader [as 别名]
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 {}) 
开发者ID:rsmusllp,项目名称:king-phisher,代码行数:48,代码来源:templates.py

示例12: render_template

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import BaseLoader [as 别名]
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() 
开发者ID:elastic,项目名称:rally,代码行数:47,代码来源:loader.py

示例13: execute

# 需要导入模块: import jinja2 [as 别名]
# 或者: from jinja2 import BaseLoader [as 别名]
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) 
开发者ID:airflow-plugins,项目名称:pandora-plugin,代码行数:53,代码来源:hive_email_operators.py


注:本文中的jinja2.BaseLoader方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。