| 1 |
""" |
| 2 |
Provides some import hooks to allow Cheetah's .tmpl files to be imported |
| 3 |
directly like Python .py modules. |
| 4 |
|
| 5 |
To use these: |
| 6 |
import Cheetah.ImportHooks |
| 7 |
Cheetah.ImportHooks.install() |
| 8 |
""" |
| 9 |
|
| 10 |
try: |
| 11 |
from importlib import invalidate_caches |
| 12 |
except ImportError: |
| 13 |
invalidate_caches = None |
| 14 |
import sys |
| 15 |
import os.path |
| 16 |
import py_compile |
| 17 |
import types |
| 18 |
try: |
| 19 |
import builtins as builtin |
| 20 |
except ImportError: |
| 21 |
import __builtin__ as builtin |
| 22 |
from threading import RLock |
| 23 |
import traceback |
| 24 |
|
| 25 |
from . import ImportManager |
| 26 |
from .ImportManager import DirOwner |
| 27 |
from .Compiler import Compiler |
| 28 |
from .compat import ModuleNotFoundError |
| 29 |
from .convertTmplPathToModuleName import convertTmplPathToModuleName |
| 30 |
|
| 31 |
_installed = False |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
_cacheDir = [] |
| 37 |
|
| 38 |
|
| 39 |
def setCacheDir(cacheDir): |
| 40 |
global _cacheDir |
| 41 |
_cacheDir.append(cacheDir) |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
class CheetahDirOwner(DirOwner): |
| 48 |
_lock = RLock() |
| 49 |
_acquireLock = _lock.acquire |
| 50 |
_releaseLock = _lock.release |
| 51 |
|
| 52 |
templateFileExtensions = ('.tmpl',) |
| 53 |
debuglevel = 0 |
| 54 |
|
| 55 |
def getmod(self, name): |
| 56 |
self._acquireLock() |
| 57 |
try: |
| 58 |
mod = DirOwner.getmod(self, name) |
| 59 |
if mod: |
| 60 |
return mod |
| 61 |
|
| 62 |
for ext in self.templateFileExtensions: |
| 63 |
tmplPath = os.path.join(self.path, name + ext) |
| 64 |
if os.path.exists(tmplPath): |
| 65 |
try: |
| 66 |
return self._compile(name, tmplPath) |
| 67 |
except Exception: |
| 68 |
|
| 69 |
exc_txt = traceback.format_exc() |
| 70 |
exc_txt = ' ' + (' \n'.join(exc_txt.splitlines())) |
| 71 |
raise ModuleNotFoundError( |
| 72 |
'Error while compiling Cheetah module ' |
| 73 |
'%(name)s, original traceback follows:\n' |
| 74 |
'%(exc_txt)s' % locals()) |
| 75 |
return None |
| 76 |
|
| 77 |
finally: |
| 78 |
self._releaseLock() |
| 79 |
|
| 80 |
def _compile(self, name, tmplPath): |
| 81 |
if invalidate_caches: |
| 82 |
invalidate_caches() |
| 83 |
|
| 84 |
|
| 85 |
code = str(Compiler(file=tmplPath, moduleName=name, |
| 86 |
mainClassName=name)) |
| 87 |
if _cacheDir: |
| 88 |
__file__ = os.path.join( |
| 89 |
_cacheDir[0], convertTmplPathToModuleName(tmplPath)) + '.py' |
| 90 |
else: |
| 91 |
__file__ = os.path.splitext(tmplPath)[0] + '.py' |
| 92 |
try: |
| 93 |
with open(__file__, 'w') as _py_file: |
| 94 |
_py_file.write(code) |
| 95 |
except (IOError, OSError): |
| 96 |
|
| 97 |
if self.debuglevel > 0: |
| 98 |
traceback.print_exc(file=sys.stderr) |
| 99 |
__file__ = tmplPath |
| 100 |
else: |
| 101 |
try: |
| 102 |
py_compile.compile(__file__) |
| 103 |
except IOError: |
| 104 |
pass |
| 105 |
co = compile(code + '\n', __file__, 'exec') |
| 106 |
|
| 107 |
mod = types.ModuleType(name) |
| 108 |
mod.__file__ = co.co_filename |
| 109 |
if _cacheDir: |
| 110 |
|
| 111 |
mod.__orig_file__ = tmplPath |
| 112 |
mod.__co__ = co |
| 113 |
return mod |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
def install(templateFileExtensions=('.tmpl',)): |
| 120 |
"""Install the Cheetah Import Hooks""" |
| 121 |
|
| 122 |
global _installed |
| 123 |
if not _installed: |
| 124 |
CheetahDirOwner.templateFileExtensions = templateFileExtensions |
| 125 |
if isinstance(builtin.__import__, |
| 126 |
(types.BuiltinFunctionType, types.FunctionType)): |
| 127 |
global __oldimport__ |
| 128 |
ImportManager.__oldimport__ = __oldimport__ = builtin.__import__ |
| 129 |
ImportManager._globalOwnerTypes.insert(0, CheetahDirOwner) |
| 130 |
global _manager |
| 131 |
_manager = ImportManager.ImportManager() |
| 132 |
_manager.setThreaded() |
| 133 |
_manager.install() |
| 134 |
_installed = True |
| 135 |
|
| 136 |
|
| 137 |
def uninstall(): |
| 138 |
"""Uninstall the Cheetah Import Hooks""" |
| 139 |
global _installed |
| 140 |
if _installed: |
| 141 |
if isinstance(builtin.__import__, types.MethodType): |
| 142 |
builtin.__import__ = __oldimport__ |
| 143 |
if ImportManager._globalOwnerTypes[0] is CheetahDirOwner: |
| 144 |
del ImportManager._globalOwnerTypes[0] |
| 145 |
del ImportManager.__oldimport__ |
| 146 |
global _manager |
| 147 |
del _manager |
| 148 |
_installed = False |