def init_jinja2_environ(self):
    if os.environ.get("SERVER_NAME", None) == "localhost" or \
          os.environ.get("SERVER_SOFTWARE", "").startswith("Dev"):
      from jinja2 import (FileSystemLoader, ChoiceLoader, PrefixLoader,)
      template_postfix = ""
    else:
      from kay.utils.jinja2utils.code_loaders import FileSystemCodeLoader as \
          FileSystemLoader
      from kay.utils.jinja2utils.code_loaders import ChoiceCodeLoader as \
          ChoiceLoader
      from kay.utils.jinja2utils.code_loaders import PrefixCodeLoader as \
          PrefixLoader
      template_postfix = "_compiled"
    per_app_loaders = {}
    for app in self.get_installed_apps():
      try:
        mod = import_string(app)
      except (ImportError, AttributeError):
        logging.warning("Failed to import app '%s', skipped." % app)
        continue
      try:
        app_key = getattr(mod, 'template_loader_key')
      except AttributeError:
        app_key = get_app_tailname(app)
      per_app_loaders[app_key] = FileSystemLoader(
        os.path.join(os.path.dirname(mod.__file__),
                     "templates"+template_postfix))
    loader = PrefixLoader(per_app_loaders)
    target_dirs = self.app_settings.TEMPLATE_DIRS+("kay/templates",)
    def replace_dirname(orig):
      if 'templates' in orig:
        return orig.replace('templates', 'templates'+template_postfix)
      else:
        return orig+template_postfix
    import kay
    base_loader = FileSystemLoader(
      [os.path.join(kay.PROJECT_DIR, replace_dirname(d)) for d in target_dirs])
    loader = ChoiceLoader([base_loader, loader])
    env_dict = {}
    env_dict.update(self.app_settings.JINJA2_ENVIRONMENT_KWARGS)
    jinja2_ext = []
    for ext_str in self.app_settings.JINJA2_EXTENSIONS:
      try:
        ext = import_string(ext_str)
      except (ImportError, AttributeError), e:
        logging.warn('Failed to import jinja2 extension %s: "%s", skipped.'
                     % (ext_str, e))
        continue
      jinja2_ext.append(ext)
    env_dict.update(dict(loader = loader, undefined=NullUndefined,
                         extensions=jinja2_ext))
    self._jinja2_env = Environment(**env_dict)
    for key, filter_str in self.app_settings.JINJA2_FILTERS.iteritems():
      try: 
        func = import_string(filter_str)
      except (ImportError, AttributeError):
        logging.warn('Cannot import %s.' % filter_str)
        continue
      if self._jinja2_env.filters.has_key(key):
        logging.warn('Key "%s" has already defined, skipped.' % key)
        continue
      if not callable(func):
        logging.warn('%s is not a callable.' % filter_str)
        continue
      self._jinja2_env.filters[key] = func
Initialize the environment for jinja2.
dataset/ETHPy150Open IanLewis/kay/kay/app.py/KayApp.init_jinja2_environ