Python jinja2.PrefixLoader() Examples

The following are 7 code examples of jinja2.PrefixLoader(). 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: template.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def get_jinja_env():
  """Returns jinja2.Environment object that knows how to render templates."""
  # TODO(maruel): Add lstrip_blocks=True when jinja2 2.7 becomes available in
  # the GAE SDK.
  env = jinja2.Environment(
      loader=jinja2.PrefixLoader({
        prefix: jinja2.FileSystemLoader(path)
        for prefix, path in _TEMPLATE_PATHS.items()
      }),
      autoescape=True,
      extensions=['jinja2.ext.autoescape'],
      trim_blocks=True,
      undefined=jinja2.StrictUndefined)
  env.filters.update(_GLOBAL_FILTERS)
  env.globals.update(_GLOBAL_ENV)
  return env 
Example #2
Source File: template.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def get_jinja_env():
  """Returns jinja2.Environment object that knows how to render templates."""
  # TODO(maruel): Add lstrip_blocks=True when jinja2 2.7 becomes available in
  # the GAE SDK.
  env = jinja2.Environment(
      loader=jinja2.PrefixLoader({
        prefix: jinja2.FileSystemLoader(path)
        for prefix, path in _TEMPLATE_PATHS.items()
      }),
      autoescape=True,
      extensions=['jinja2.ext.autoescape'],
      trim_blocks=True,
      undefined=jinja2.StrictUndefined)
  env.filters.update(_GLOBAL_FILTERS)
  env.globals.update(_GLOBAL_ENV)
  return env 
Example #3
Source File: template.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def get_jinja_env():
  """Returns jinja2.Environment object that knows how to render templates."""
  # TODO(maruel): Add lstrip_blocks=True when jinja2 2.7 becomes available in
  # the GAE SDK.
  env = jinja2.Environment(
      loader=jinja2.PrefixLoader({
        prefix: jinja2.FileSystemLoader(path)
        for prefix, path in _TEMPLATE_PATHS.items()
      }),
      autoescape=True,
      extensions=['jinja2.ext.autoescape'],
      trim_blocks=True,
      undefined=jinja2.StrictUndefined)
  env.filters.update(_GLOBAL_FILTERS)
  env.globals.update(_GLOBAL_ENV)
  return env 
Example #4
Source File: template.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def get_jinja_env():
  """Returns jinja2.Environment object that knows how to render templates."""
  # TODO(maruel): Add lstrip_blocks=True when jinja2 2.7 becomes available in
  # the GAE SDK.
  env = jinja2.Environment(
      loader=jinja2.PrefixLoader({
        prefix: jinja2.FileSystemLoader(path)
        for prefix, path in _TEMPLATE_PATHS.items()
      }),
      autoescape=True,
      extensions=['jinja2.ext.autoescape'],
      trim_blocks=True,
      undefined=jinja2.StrictUndefined)
  env.filters.update(_GLOBAL_FILTERS)
  env.globals.update(_GLOBAL_ENV)
  return env 
Example #5
Source File: template.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def get_jinja_env():
  """Returns jinja2.Environment object that knows how to render templates."""
  # TODO(maruel): Add lstrip_blocks=True when jinja2 2.7 becomes available in
  # the GAE SDK.
  env = jinja2.Environment(
      loader=jinja2.PrefixLoader({
        prefix: jinja2.FileSystemLoader(path)
        for prefix, path in _TEMPLATE_PATHS.items()
      }),
      autoescape=True,
      extensions=['jinja2.ext.autoescape'],
      trim_blocks=True,
      undefined=jinja2.StrictUndefined)
  env.filters.update(_GLOBAL_FILTERS)
  env.globals.update(_GLOBAL_ENV)
  return env 
Example #6
Source File: __init__.py    From geonotebook with Apache License 2.0 5 votes vote down vote up
def get_notebook_jinja2_loader(nbapp):
    """Return the appropriate jinja2 template loader for the notebook app.

    This is confusing but necessary to meet the following criteria:
    - Templates in geonotebook/templates will override those in core notebook
      templates
    - Core notebook templates can still be referred to/extended by referring
      to them as core@template.html

    The ChoiceLoader tries each of the loaders in turn until one of them
    provides the right template.
    The PrefixLoader allows us to refer to core templates using the core@
    prefix, this is necessary
    because we want to extend templates of the same name while referring to
    templates in a different loader (core).
    The PackageLoader lets us put our templates ahead in priority of the
    notebooks templates.
    The core_loader is last which falls back to the original loader the
    notebook uses.

    This implementation is weird, but should be compatible/composable with
    other notebook extensions and fairly future proof.

    :param nbapp: NotebookApp instance
    :returns: A jinja2 loader designed to work with our notebook templates
    :rtype: jinja2.ChoiceLoader
    """
    return ChoiceLoader([
        PrefixLoader(
            {'core': nbapp.web_app.settings['jinja2_env'].loader},
            delimiter='@'
        ),
        PackageLoader('geonotebook'),
        nbapp.web_app.settings['jinja2_env'].loader]) 
Example #7
Source File: _tasks.py    From vulyk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def init_plugins(app) -> Dict[str, AbstractTaskType]:
    """
    Extracts modules (task types) from global configuration.

    :param app: Current Flask application instance
    :type app: flask.Flask

    :return: Dictionary with instantiated TaskType objects
    :rtype: dict[str, AbstractTaskType]
    """
    task_types = {}
    loaders = {}
    enabled_tasks = app.config.get('ENABLED_TASKS', {})
    files_to_watch = []

    app.logger.info('Loading plugins: %s', list(enabled_tasks.keys()))

    for plugin, task in enabled_tasks.items():
        app.logger.debug('Started loading plugin <%s>.', plugin)

        task_settings = import_string(
            '{plugin_name}.settings'.format(plugin_name=plugin)
        )
        plugin_type = import_string(
            '{plugin_name}'.format(plugin_name=plugin))
        settings = plugin_type.configure(task_settings)

        task_type = import_string(
            '{plugin_name}.models.task_types.{task}'.format(
                plugin_name=plugin, task=task)
        )

        loaders[task_type.type_name] = jinja2.PackageLoader(plugin)
        task_types[task_type.type_name] = task_type(settings=settings)

        default_static_path = plugin_type.__path__[0]
        # if Flask-Collect is enabled - get files from collected dir
        if 'COLLECT_STATIC_ROOT' in app.config:
            # all plugin static goes stored in a dir may have prefixed name
            # to prevent any collision e.g. plugin named 'images'
            prefix = app.config.get('COLLECT_PLUGIN_DIR_PREFIX', '')
            static_path = os.path.join(app.static_folder,
                                       '{}{}'.format(prefix, plugin))
        # else - use standard static folder
        else:
            static_path = default_static_path

        files_to_watch.extend(_init_plugin_assets(
            app=app,
            task_type=task_type,
            static_path=static_path))

        app.logger.debug('Finished loading plugin <%s>.', plugin)

    app.jinja_loader = jinja2.ChoiceLoader([
        app.jinja_loader,
        jinja2.PrefixLoader(loaders)])

    app._plugin_files_to_watch = files_to_watch

    return task_types