Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 130 lines (108 sloc) 3.983 kB
0900226 @rtyler Remove references to the `new` module
rtyler authored
1 #!/usr/bin/env python
d608a47 initial import
tavis_rudd authored
2
0900226 @rtyler Remove references to the `new` module
rtyler authored
3 """
4 Provides some import hooks to allow Cheetah's .tmpl files to be imported
d608a47 initial import
tavis_rudd authored
5 directly like Python .py modules.
6
a3fd430 made it possible to specify a list of template filename extentions th…
tavis_rudd authored
7 To use these:
8 import Cheetah.ImportHooks
9 Cheetah.ImportHooks.install()
0900226 @rtyler Remove references to the `new` module
rtyler authored
10 """
d608a47 initial import
tavis_rudd authored
11
12 import sys
13 import os.path
1622325 changed to use Gordon McMillan's ui.py import system
tavis_rudd authored
14 import types
d608a47 initial import
tavis_rudd authored
15 import __builtin__
1622325 changed to use Gordon McMillan's ui.py import system
tavis_rudd authored
16 import imp
16612e7 replace lock with RLock to avoid deadlock
tavis_rudd authored
17 from threading import RLock
8a2f66f *** empty log message ***
tavis_rudd authored
18 import string
01f9cb3 *** empty log message ***
tavis_rudd authored
19 import traceback
0900226 @rtyler Remove references to the `new` module
rtyler authored
20 import types
21
70c9d5d fix name error
tavis_rudd authored
22 from Cheetah import ImportManager
451f2b1 general cleanup
tavis_rudd authored
23 from Cheetah.ImportManager import DirOwner
24 from Cheetah.Compiler import Compiler
918f7cf moved convertTmplPathToModuleName to shared location
tavis_rudd authored
25 from Cheetah.convertTmplPathToModuleName import convertTmplPathToModuleName
d608a47 initial import
tavis_rudd authored
26
27 _installed = False
28
29 ##################################################
8a2f66f *** empty log message ***
tavis_rudd authored
30 ## HELPER FUNCS
31
1c9ca0d *** empty log message ***
tavis_rudd authored
32 _cacheDir = []
33 def setCacheDir(cacheDir):
34 global _cacheDir
f34a371 *** empty log message ***
tavis_rudd authored
35 _cacheDir.append(cacheDir)
1c9ca0d *** empty log message ***
tavis_rudd authored
36
8a2f66f *** empty log message ***
tavis_rudd authored
37 ##################################################
d608a47 initial import
tavis_rudd authored
38 ## CLASSES
39
1622325 changed to use Gordon McMillan's ui.py import system
tavis_rudd authored
40 class CheetahDirOwner(DirOwner):
16612e7 replace lock with RLock to avoid deadlock
tavis_rudd authored
41 _lock = RLock()
87e5091 *** empty log message ***
tavis_rudd authored
42 _acquireLock = _lock.acquire
e5112ae added a lock to the ImportHook
tavis_rudd authored
43 _releaseLock = _lock.release
1622325 changed to use Gordon McMillan's ui.py import system
tavis_rudd authored
44
a3fd430 made it possible to specify a list of template filename extentions th…
tavis_rudd authored
45 templateFileExtensions = ('.tmpl',)
46
47 def getmod(self, name):
766fd9d cosmetic
tavis_rudd authored
48 self._acquireLock()
a3fd430 made it possible to specify a list of template filename extentions th…
tavis_rudd authored
49 try:
50 mod = DirOwner.getmod(self, name)
51 if mod:
52 return mod
53
54 for ext in self.templateFileExtensions:
55 tmplPath = os.path.join(self.path, name + ext)
56 if os.path.exists(tmplPath):
57 try:
58 return self._compile(name, tmplPath)
59 except:
60 # @@TR: log the error
61 exc_txt = traceback.format_exc()
62 exc_txt =' '+(' \n'.join(exc_txt.splitlines()))
63 raise ImportError(
64 'Error while compiling Cheetah module'
3da8b93 made the exception handling a bit more robust
tavis_rudd authored
65 ' %(name)s, original traceback follows:\n%(exc_txt)s'%locals())
a3fd430 made it possible to specify a list of template filename extentions th…
tavis_rudd authored
66 ##
67 return None
68
69 finally:
70 self._releaseLock()
3da8b93 made the exception handling a bit more robust
tavis_rudd authored
71
72 def _compile(self, name, tmplPath):
73 ## @@ consider adding an ImportError raiser here
74 code = str(Compiler(file=tmplPath, moduleName=name,
75 mainClassName=name))
76 if _cacheDir:
cef58d0 finish up last checkin
tavis_rudd authored
77 __file__ = os.path.join(_cacheDir[0],
78 convertTmplPathToModuleName(tmplPath)) + '.py'
3da8b93 made the exception handling a bit more robust
tavis_rudd authored
79 try:
80 open(__file__, 'w').write(code)
81 except OSError:
82 ## @@ TR: need to add some error code here
83 traceback.print_exc(file=sys.stderr)
f033618 *** empty log message ***
tavis_rudd authored
84 __file__ = tmplPath
3da8b93 made the exception handling a bit more robust
tavis_rudd authored
85 else:
86 __file__ = tmplPath
87 co = compile(code+'\n', __file__, 'exec')
88
0900226 @rtyler Remove references to the `new` module
rtyler authored
89 mod = types.ModuleType(name)
3da8b93 made the exception handling a bit more robust
tavis_rudd authored
90 mod.__file__ = co.co_filename
91 if _cacheDir:
92 mod.__orig_file__ = tmplPath # @@TR: this is used in the WebKit
93 # filemonitoring code
94 mod.__co__ = co
95 return mod
96
d608a47 initial import
tavis_rudd authored
97
98 ##################################################
99 ## FUNCTIONS
100
a3fd430 made it possible to specify a list of template filename extentions th…
tavis_rudd authored
101 def install(templateFileExtensions=('.tmpl',)):
1622325 changed to use Gordon McMillan's ui.py import system
tavis_rudd authored
102 """Install the Cheetah Import Hooks"""
a3fd430 made it possible to specify a list of template filename extentions th…
tavis_rudd authored
103
1622325 changed to use Gordon McMillan's ui.py import system
tavis_rudd authored
104 global _installed
105 if not _installed:
a3fd430 made it possible to specify a list of template filename extentions th…
tavis_rudd authored
106 CheetahDirOwner.templateFileExtensions = templateFileExtensions
1622325 changed to use Gordon McMillan's ui.py import system
tavis_rudd authored
107 import __builtin__
47fbe57 @rtyler Clean up a swath of fixes suggested by 2to3
rtyler authored
108 if isinstance(__builtin__.__import__, types.BuiltinFunctionType):
1622325 changed to use Gordon McMillan's ui.py import system
tavis_rudd authored
109 global __oldimport__
110 __oldimport__ = __builtin__.__import__
111 ImportManager._globalOwnerTypes.insert(0, CheetahDirOwner)
112 #ImportManager._globalOwnerTypes.append(CheetahDirOwner)
113 global _manager
114 _manager=ImportManager.ImportManager()
115 _manager.setThreaded()
116 _manager.install()
117
118 def uninstall():
119 """Uninstall the Cheetah Import Hooks"""
d608a47 initial import
tavis_rudd authored
120 global _installed
121 if not _installed:
1622325 changed to use Gordon McMillan's ui.py import system
tavis_rudd authored
122 import __builtin__
47fbe57 @rtyler Clean up a swath of fixes suggested by 2to3
rtyler authored
123 if isinstance(__builtin__.__import__, types.MethodType):
1622325 changed to use Gordon McMillan's ui.py import system
tavis_rudd authored
124 __builtin__.__import__ = __oldimport__
125 global _manager
126 del _manager
d608a47 initial import
tavis_rudd authored
127
128 if __name__ == '__main__':
129 install()
Something went wrong with that request. Please try again.