Python jinja2 模块,BaseLoader() 实例源码

我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用jinja2.BaseLoader()

项目:python-confidant-client    作者:lyft    | 项目源码 | 文件源码
def jinja_format(data, template_file):
    class GlobalFileLoader(jinja2.BaseLoader):
        def get_source(self, environment, template):
            if not os.path.exists(template):
                raise jinja2.TemplateNotFound(template)
            with open(template) as f:
                source = f.read().decode('utf-8')
            return source, template, lambda: False

    combined_credentials = combined_credential_pair_format(data)
    env = jinja2.Environment(
        loader=GlobalFileLoader(),
        keep_trailing_newline=True
    )
    template = env.get_template(template_file)
    return template.render(secrets=combined_credentials['credentials'])
项目:manubot    作者:greenelab    | 项目源码 | 文件源码
def template_with_jinja2(text, variables):
    """
    Template using jinja2 with the variables dictionary unpacked as keyword
    arguments.
    """
    jinja_environment = jinja2.Environment(
        loader=jinja2.BaseLoader(),
        undefined=jinja2.make_logging_undefined(logging.getLogger()),
        comment_start_string='{##',
        comment_end_string='##}',
    )
    template = jinja_environment.from_string(text)
    return template.render(**variables)
项目:lago    作者:lago-project    | 项目源码 | 文件源码
def init_str(images):
    init_template = textwrap.dedent(
        """
    domains:
      {% for vm_name, template in images.viewitems() %}
      {{ vm_name }}:
        memory: 1024
        nics:
          - net: net-02
          - net: net-01
        disks:
          - template_name: {{ template }}
            type: template
            name: root
            dev: sda
            format: qcow2
        metadata:
            {{ vm_name }}: {{ vm_name }}
        artifacts:
          - /should/not/exist
          - /root/custom
          - /var/log
          - /etc/hosts
          - /etc/resolv.conf
          - /etc/sysconfig
          - /etc/NetworkManager
          - /root/virt-sysprep-firstboot.log
          - /root/extract-{{ vm_name }}-dead
          - /root/extract-{{ vm_name }}-normal
        groups: group{{ loop.index % 2 }}
      {% endfor %}

    nets:
      net-01:
        type: nat
        dhcp:
          start: 100
          end: 254
        management: true
        dns_domain_name: lago.local

      net-02:
        type: nat
        gw: 192.168.210.4
        dhcp:
          start: 100
          end: 254
    """
    )
    template = Environment(loader=BaseLoader()).from_string(init_template)
    return template.render(images=images)