"Fossies" - the Fresh Open Source Software Archive

Member "cheetah3-3.2.6.post2/Cheetah/LoadTemplate.py" (20 Apr 2021, 1700 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 "LoadTemplate.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 import os
    2 import sys
    3 from Cheetah.ImportHooks import CheetahDirOwner
    4 
    5 
    6 def loadTemplateModule(templatePath, debuglevel=0):
    7     """Load template's module by full or relative path (extension is optional)
    8 
    9     Examples:
   10 
   11         template = loadTemplateModule('views/index')
   12         template = loadTemplateClass('views/index.tmpl')
   13 
   14     Template is loaded from from .py[co], .py or .tmpl -
   15     whatever will be found. Files *.tmpl are compiled to *.py;
   16     *.py are byte-compiled to *.py[co]. Compiled files are cached
   17     in the template directory. Errors on writing are silently ignored.
   18     """
   19     drive, localPath = os.path.splitdrive(templatePath)
   20     dirname, filename = os.path.split(localPath)
   21     filename, ext = os.path.splitext(filename)
   22     if dirname:
   23         # Cleanup: Convert /Templates//views/ -> /Templates/views
   24         dirname_list = dirname.replace(os.sep, '/').split('/')
   25         dirname_list = [d for (i, d) in enumerate(dirname_list)
   26                         if i == 0 or d]  # Preserve root slash
   27         dirname = os.sep.join(dirname_list)
   28     template_dir = CheetahDirOwner(drive + dirname)
   29     if ext:
   30         template_dir.templateFileExtensions = (ext,)
   31     template_dir.debuglevel = debuglevel
   32     mod = template_dir.getmod(filename)
   33     if mod is None:
   34         raise ImportError("Cannot find {}".format(templatePath))
   35     mod.__name__ = filename
   36     sys.modules[filename] = mod
   37     co = mod.__co__
   38     del mod.__co__
   39     exec(co, mod.__dict__)
   40     return mod
   41 
   42 
   43 def loadTemplateClass(templatePath, debuglevel=0):
   44     """Load template's class by full or relative path"""
   45     mod = loadTemplateModule(templatePath, debuglevel=debuglevel)
   46     return getattr(mod, mod.__name__)