"Fossies" - the Fresh Open Source Software Archive

Member "roundup-2.0.0/roundup/cgi/engine_jinja2.py" (29 Feb 2020, 3488 Bytes) of package /linux/www/roundup-2.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "engine_jinja2.py": 1.6.1_vs_2.0.0.

    1 """
    2 Experimental Jinja2 support for Roundup. It will become less
    3 experimental when it is completely clear what information is
    4 passed to template, and when the info is limited to the sane
    5 minimal set (to avoid Roundup state changes from template).
    6 
    7 [ ] fallback mechanizm to use multiple templating engines in
    8     parallel and aid in incremental translation from one
    9     engine to another
   10 
   11 [ ] define a place for templates
   12     probably
   13       TRACKER_HOME/templates/jinja2
   14     with
   15       TRACKER_HOME/templates/INFO.txt
   16         describing how the dir was created, for example
   17           "This is a copy of 'classic' template from ..."
   18         also template fallback mechanizm for multi-engine
   19           configuration
   20     [ ] backward compatibility - if no engine is explicitly
   21           specified, use TRACKER_HOME/html directory
   22     [ ] copy TEMPLATES-INFO.txt to INFO.txt
   23       [ ] implement VERSION file in environment for auto
   24           upgrade
   25 
   26 [ ] precompile() is a stub
   27 
   28 [ ] add {{ debug() }} dumper to inspect available variables
   29     https://github.com/mitsuhiko/jinja2/issues/174
   30 """
   31 
   32 from __future__ import print_function
   33 import jinja2
   34 import gettext
   35 import mimetypes
   36 import sys
   37 
   38 # http://jinja.pocoo.org/docs/api/#loaders
   39 
   40 from roundup.cgi.templating import context, LoaderBase, TemplateBase
   41 from roundup.anypy.strings import s2u
   42 
   43 
   44 class Jinja2Loader(LoaderBase):
   45     def __init__(self, dir):
   46         self._env = jinja2.Environment(
   47             loader=jinja2.FileSystemLoader(dir),
   48             extensions=['jinja2.ext.i18n'],
   49             autoescape=True
   50         )
   51 
   52         # Adding a custom filter that can transform roundup's vars to unicode
   53         # This is necessary because jinja2 can only deal with unicode objects
   54         # and roundup uses utf-8 for the internal representation.
   55         # The automatic conversion will assume 'ascii' and fail sometime.
   56         # Analysed with roundup 1.5.0 and jinja 2.7.1. See issue2550811.
   57         self._env.filters["u"] = s2u
   58 
   59     def _find(self, tplname):
   60         for extension in ('', '.html', '.xml'):
   61             try:
   62                 filename = tplname + extension
   63                 return self._env.get_template(filename)
   64             except jinja2.TemplateNotFound:
   65                 continue
   66 
   67         return None
   68 
   69     def check(self, tplname):
   70         return bool(self._find(tplname))
   71 
   72     def load(self, tplname):
   73         tpl = self._find(tplname)
   74         pt = Jinja2ProxyPageTemplate(tpl)
   75         pt.content_type = mimetypes.guess_type(tpl.filename)[0] or 'text/html'
   76         return pt
   77 
   78     def precompile(self):
   79         pass
   80 
   81 
   82 class Jinja2ProxyPageTemplate(TemplateBase):
   83     def __init__(self, template):
   84         self._tpl = template
   85 
   86     def render(self, client, classname, request, **options):
   87         # [ ] limit the information passed to the minimal necessary set
   88         c = context(client, self, classname, request)
   89 
   90         c.update({'options': options,
   91                   'gettext': lambda s: s2u(client.gettext(s)),
   92                   'ngettext': lambda s, p, n: s2u(client.ngettext(s, p, n))})
   93         s = self._tpl.render(c)
   94         return s if sys.version_info[0] > 2 else \
   95             s.encode(client.STORAGE_CHARSET, )
   96 
   97     def __getitem__(self, name):
   98         # [ ] figure out what are these for
   99         raise NotImplementedError
  100         # return self._pt[name]
  101 
  102     def __getattr__(self, name):
  103         # [ ] figure out what are these for
  104         raise NotImplementedError
  105         # return getattr(self._pt, name)