"Fossies" - the Fresh Open Source Software Archive

Member "cheetah3-3.2.6.post2/Cheetah/Tools/turbocheetah/cheetahsupport.py" (20 Apr 2021, 4127 Bytes) of package /linux/www/cheetah3-3.2.6.post2.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. For more information about "cheetahsupport.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 3-3.2.0_vs_3-3.2.1.

    1 "Template support for Cheetah"
    2 
    3 import os
    4 import pkg_resources
    5 import sys
    6 
    7 from Cheetah import Compiler
    8 from Cheetah.compat import new_module
    9 
   10 
   11 def _recompile_template(package, basename, tfile, classname):
   12     tmpl = pkg_resources.resource_string(package, "%s.tmpl" % basename)
   13     c = Compiler.Compiler(source=tmpl, mainClassName='GenTemplate')
   14     code = str(c)
   15     mod = new_module(classname)
   16     ns = dict()
   17     exec(code, ns)
   18     tempclass = ns.get("GenTemplate",
   19                        ns.get('DynamicallyCompiledCheetahTemplate'))
   20     assert tempclass
   21     tempclass.__name__ = basename
   22     setattr(mod, basename, tempclass)
   23     sys.modules[classname] = mod
   24     return mod
   25 
   26 
   27 class TurboCheetah:
   28     extension = "tmpl"
   29 
   30     def __init__(self, extra_vars_func=None, options=None):
   31         if options is None:
   32             options = dict()
   33         self.get_extra_vars = extra_vars_func
   34         self.options = options
   35         self.compiledTemplates = {}
   36         self.search_path = []
   37 
   38     def load_template(self, template=None,
   39                       template_string=None, template_file=None,
   40                       loadingSite=False):
   41         """Searches for a template along the Python path.
   42 
   43         Template files must end in ".tmpl" and be in legitimate packages.
   44         """
   45         given = len(
   46             [_f for _f in (template, template_string, template_file) if _f])
   47         if given > 1:
   48             raise TypeError(
   49                 "You may give only one of template, template_string, and "
   50                 "template_file")
   51         if not given:
   52             raise TypeError(
   53                 "You must give one of template, template_string, or "
   54                 "template_file")
   55         if template:
   56             return self.load_template_module(template)
   57         elif template_string:
   58             return self.load_template_string(template_string)
   59         elif template_file:
   60             return self.load_template_file(template_file)
   61 
   62     def load_template_module(self, classname):
   63 
   64         ct = self.compiledTemplates
   65 
   66         divider = classname.rfind(".")
   67         if divider > -1:
   68             package = classname[0:divider]
   69             basename = classname[divider+1:]  # noqa: E226,E501 missing whitespace around operator
   70         else:
   71             raise ValueError("All templates must be in a package")
   72 
   73         if not self.options.get("cheetah.precompiled", False):
   74             tfile = pkg_resources.resource_filename(package,
   75                                                     "%s.%s" %
   76                                                     (basename,
   77                                                      self.extension))
   78             if classname in ct:
   79                 mtime = os.stat(tfile).st_mtime
   80                 if ct[classname] != mtime:
   81                     ct[classname] = mtime
   82                     del sys.modules[classname]
   83                     mod = _recompile_template(package, basename,
   84                                               tfile, classname)
   85                 else:
   86                     mod = __import__(classname, dict(), dict(), [basename])
   87             else:
   88                 ct[classname] = os.stat(tfile).st_mtime
   89                 mod = _recompile_template(package, basename,
   90                                           tfile, classname)
   91         else:
   92             mod = __import__(classname, dict(), dict(), [basename])
   93         tempclass = getattr(mod, basename)
   94         return tempclass
   95 
   96     def load_template_string(self, content):
   97         raise NotImplementedError
   98 
   99     def load_template_file(self, filename):
  100         raise NotImplementedError
  101 
  102     def render(self, info, format="html", fragment=False, template=None,
  103                template_string=None, template_file=None):
  104         tclass = self.load_template(
  105             template=template, template_string=template_string,
  106             template_file=template_file)
  107         if self.get_extra_vars:
  108             extra = self.get_extra_vars()
  109         else:
  110             extra = {}
  111         tempobj = tclass(searchList=[info, extra])
  112         if fragment:
  113             return tempobj.fragment()
  114         else:
  115             return tempobj.respond()