"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "Cheetah/LoadTemplate.py" between
cheetah3-3.2.0.tar.gz and cheetah3-3.2.1.tar.gz

About: Cheetah3 is a template engine and code generation tool for e.g. for Web development or Java, SQL, LaTeX, form email ... (written in Python).

LoadTemplate.py  (cheetah3-3.2.0):LoadTemplate.py  (cheetah3-3.2.1)
import imp
import os import os
import sys import sys
from Cheetah.ImportHooks import CheetahDirOwner from Cheetah.ImportHooks import CheetahDirOwner
def _loadTemplate(templatePath, debuglevel=0): def loadTemplateModule(templatePath, debuglevel=0):
"""Load template by full or relative path (including extension) """Load template's module by full or relative path (extension is optional)
Examples:
Example: template = loadTemplate('views/index.tmpl') template = loadTemplateModule('views/index')
template = loadTemplateClass('views/index.tmpl')
Template is loaded from from .py[co], .py or .tmpl - Template is loaded from from .py[co], .py or .tmpl -
whatever will be found. Files *.tmpl are compiled to *.py; whatever will be found. Files *.tmpl are compiled to *.py;
*.py are byte-compiled to *.py[co]. Compiled files are cached *.py are byte-compiled to *.py[co]. Compiled files are cached
in the template directory. Errors on writing are silently ignored. in the template directory. Errors on writing are silently ignored.
""" """
drive, localPath = os.path.splitdrive(templatePath) drive, localPath = os.path.splitdrive(templatePath)
dirname, filename = os.path.split(localPath) dirname, filename = os.path.split(localPath)
filename, ext = os.path.splitext(filename) filename, ext = os.path.splitext(filename)
if dirname: if dirname:
# Cleanup: Convert /Templates//views/ -> /Templates/views # Cleanup: Convert /Templates//views/ -> /Templates/views
dirname_list = dirname.replace(os.sep, '/').split('/') dirname_list = dirname.replace(os.sep, '/').split('/')
dirname_list = [d for (i, d) in enumerate(dirname_list) dirname_list = [d for (i, d) in enumerate(dirname_list)
if i == 0 or d] # Preserve root slash if i == 0 or d] # Preserve root slash
dirname = os.sep.join(dirname_list) dirname = os.sep.join(dirname_list)
# Add all "modules" to sys.modules
components = []
for d in dirname_list:
components.append(d)
_mod_name = '.'.join(components)
_mod = imp.new_module(_mod_name)
_d = os.path.abspath(os.sep.join(components))
_mod.__file__ = _d
_mod.__path__ = [_d]
sys.modules[_mod_name] = _mod
template_dir = CheetahDirOwner(drive + dirname) template_dir = CheetahDirOwner(drive + dirname)
if ext: if ext:
template_dir.templateFileExtensions = (ext,) template_dir.templateFileExtensions = (ext,)
template_dir.debuglevel = debuglevel template_dir.debuglevel = debuglevel
mod = template_dir.getmod(filename) mod = template_dir.getmod(filename)
fqname = os.path.join(dirname, filename).replace(os.sep, '.') if mod is None:
mod.__name__ = fqname raise ImportError("Cannot find {}".format(templatePath))
sys.modules[fqname] = mod mod.__name__ = filename
sys.modules[filename] = mod
co = mod.__co__ co = mod.__co__
del mod.__co__ del mod.__co__
exec(co, mod.__dict__) exec(co, mod.__dict__)
return mod, filename return mod
def loadTemplateModule(templatePath, debuglevel=0):
return _loadTemplate(templatePath, debuglevel=debuglevel)[0]
def loadTemplateClass(templatePath, debuglevel=0): def loadTemplateClass(templatePath, debuglevel=0):
mod, filename = _loadTemplate(templatePath, debuglevel=debuglevel) """Load template's class by full or relative path"""
return getattr(mod, filename) mod = loadTemplateModule(templatePath, debuglevel=debuglevel)
return getattr(mod, mod.__name__)
 End of changes. 7 change blocks. 
21 lines changed or deleted 11 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)