"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "Cheetah/ImportHooks.py" between
cheetah3-3.1.0.tar.gz and cheetah3-3.2.0.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).

ImportHooks.py  (cheetah3-3.1.0):ImportHooks.py  (cheetah3-3.2.0)
""" """
Provides some import hooks to allow Cheetah's .tmpl files to be imported Provides some import hooks to allow Cheetah's .tmpl files to be imported
directly like Python .py modules. directly like Python .py modules.
To use these: To use these:
import Cheetah.ImportHooks import Cheetah.ImportHooks
Cheetah.ImportHooks.install() Cheetah.ImportHooks.install()
""" """
try:
from importlib import invalidate_caches
except ImportError:
invalidate_caches = None
import sys import sys
import os.path import os.path
import py_compile
import types import types
try: try:
import builtins as builtin import builtins as builtin
except ImportError: # PY2 except ImportError: # PY2
import __builtin__ as builtin import __builtin__ as builtin
from threading import RLock from threading import RLock
import traceback import traceback
from Cheetah import ImportManager from Cheetah import ImportManager
from Cheetah.ImportManager import DirOwner from Cheetah.ImportManager import DirOwner
skipping to change at line 45 skipping to change at line 50
################################################## ##################################################
# CLASSES # CLASSES
class CheetahDirOwner(DirOwner): class CheetahDirOwner(DirOwner):
_lock = RLock() _lock = RLock()
_acquireLock = _lock.acquire _acquireLock = _lock.acquire
_releaseLock = _lock.release _releaseLock = _lock.release
templateFileExtensions = ('.tmpl',) templateFileExtensions = ('.tmpl',)
debuglevel = 0
def getmod(self, name): def getmod(self, name):
self._acquireLock() self._acquireLock()
try: try:
mod = DirOwner.getmod(self, name) mod = DirOwner.getmod(self, name)
if mod: if mod:
return mod return mod
for ext in self.templateFileExtensions: for ext in self.templateFileExtensions:
tmplPath = os.path.join(self.path, name + ext) tmplPath = os.path.join(self.path, name + ext)
skipping to change at line 72 skipping to change at line 78
raise ImportError( raise ImportError(
'Error while compiling Cheetah module ' 'Error while compiling Cheetah module '
'%(name)s, original traceback follows:\n' '%(name)s, original traceback follows:\n'
'%(exc_txt)s' % locals()) '%(exc_txt)s' % locals())
return None return None
finally: finally:
self._releaseLock() self._releaseLock()
def _compile(self, name, tmplPath): def _compile(self, name, tmplPath):
if invalidate_caches:
invalidate_caches()
# @@ consider adding an ImportError raiser here # @@ consider adding an ImportError raiser here
code = str(Compiler(file=tmplPath, moduleName=name, code = str(Compiler(file=tmplPath, moduleName=name,
mainClassName=name)) mainClassName=name))
if _cacheDir: if _cacheDir:
__file__ = os.path.join(_cacheDir[0], __file__ = os.path.join(
convertTmplPathToModuleName(tmplPath)) \ _cacheDir[0], convertTmplPathToModuleName(tmplPath)) + '.py'
+ '.py'
try:
open(__file__, 'w').write(code)
except OSError:
# @@ TR: need to add some error code here
traceback.print_exc(file=sys.stderr)
__file__ = tmplPath
else: else:
__file__ = os.path.splitext(tmplPath)[0] + '.py'
try:
with open(__file__, 'w') as _py_file:
_py_file.write(code)
except (IOError, OSError):
# @@ TR: need to add some error code here
if self.debuglevel > 0:
traceback.print_exc(file=sys.stderr)
__file__ = tmplPath __file__ = tmplPath
co = compile(code+'\n', __file__, 'exec') else:
try:
py_compile.compile(__file__)
except IOError:
pass
co = compile(code + '\n', __file__, 'exec')
mod = types.ModuleType(name) mod = types.ModuleType(name)
mod.__file__ = co.co_filename mod.__file__ = co.co_filename
if _cacheDir: if _cacheDir:
# @@TR: this is used in the WebKit filemonitoring code # @@TR: this is used in the WebKit filemonitoring code
mod.__orig_file__ = tmplPath mod.__orig_file__ = tmplPath
mod.__co__ = co mod.__co__ = co
return mod return mod
################################################## ##################################################
 End of changes. 7 change blocks. 
10 lines changed or deleted 25 lines changed or added

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