Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 129 lines (108 sloc) 3.983 kb
0900226c » rtyler
2009-12-27 Remove references to the `new` module
1 #!/usr/bin/env python
d608a470 » tavis_rudd
2002-04-18 initial import
2
0900226c » rtyler
2009-12-27 Remove references to the `new` module
3 """
4 Provides some import hooks to allow Cheetah's .tmpl files to be imported
d608a470 » tavis_rudd
2002-04-18 initial import
5 directly like Python .py modules.
6
a3fd4307 » tavis_rudd
2006-06-20 made it possible to specify a list of template filename extentions th…
7 To use these:
8 import Cheetah.ImportHooks
9 Cheetah.ImportHooks.install()
0900226c » rtyler
2009-12-27 Remove references to the `new` module
10 """
d608a470 » tavis_rudd
2002-04-18 initial import
11
12 import sys
13 import os.path
16223256 » tavis_rudd
2002-04-27 changed to use Gordon McMillan's ui.py import system
14 import types
d608a470 » tavis_rudd
2002-04-18 initial import
15 import __builtin__
16223256 » tavis_rudd
2002-04-27 changed to use Gordon McMillan's ui.py import system
16 import imp
16612e76 » tavis_rudd
2007-11-14 replace lock with RLock to avoid deadlock
17 from threading import RLock
8a2f66fa » tavis_rudd
2002-08-09 *** empty log message ***
18 import string
01f9cb32 » tavis_rudd
2002-08-09 *** empty log message ***
19 import traceback
0900226c » rtyler
2009-12-27 Remove references to the `new` module
20 import types
21
70c9d5dc » tavis_rudd
2005-12-05 fix name error
22 from Cheetah import ImportManager
451f2b1d » tavis_rudd
2005-01-03 general cleanup
23 from Cheetah.ImportManager import DirOwner
24 from Cheetah.Compiler import Compiler
918f7cfd » tavis_rudd
2005-12-30 moved convertTmplPathToModuleName to shared location
25 from Cheetah.convertTmplPathToModuleName import convertTmplPathToModuleName
d608a470 » tavis_rudd
2002-04-18 initial import
26
27 _installed = False
28
29 ##################################################
8a2f66fa » tavis_rudd
2002-08-09 *** empty log message ***
30 ## HELPER FUNCS
31
1c9ca0d2 » tavis_rudd
2002-08-09 *** empty log message ***
32 _cacheDir = []
33 def setCacheDir(cacheDir):
34 global _cacheDir
f34a3714 » tavis_rudd
2002-08-09 *** empty log message ***
35 _cacheDir.append(cacheDir)
1c9ca0d2 » tavis_rudd
2002-08-09 *** empty log message ***
36
8a2f66fa » tavis_rudd
2002-08-09 *** empty log message ***
37 ##################################################
d608a470 » tavis_rudd
2002-04-18 initial import
38 ## CLASSES
39
16223256 » tavis_rudd
2002-04-27 changed to use Gordon McMillan's ui.py import system
40 class CheetahDirOwner(DirOwner):
16612e76 » tavis_rudd
2007-11-14 replace lock with RLock to avoid deadlock
41 _lock = RLock()
87e50914 » tavis_rudd
2002-08-09 *** empty log message ***
42 _acquireLock = _lock.acquire
e5112aeb » tavis_rudd
2002-08-09 added a lock to the ImportHook
43 _releaseLock = _lock.release
16223256 » tavis_rudd
2002-04-27 changed to use Gordon McMillan's ui.py import system
44
a3fd4307 » tavis_rudd
2006-06-20 made it possible to specify a list of template filename extentions th…
45 templateFileExtensions = ('.tmpl',)
46
47 def getmod(self, name):
766fd9d7 » tavis_rudd
2007-11-16 cosmetic
48 self._acquireLock()
a3fd4307 » tavis_rudd
2006-06-20 made it possible to specify a list of template filename extentions th…
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'
3da8b937 » tavis_rudd
2006-01-03 made the exception handling a bit more robust
65 ' %(name)s, original traceback follows:\n%(exc_txt)s'%locals())
a3fd4307 » tavis_rudd
2006-06-20 made it possible to specify a list of template filename extentions th…
66 ##
67 return None
68
69 finally:
70 self._releaseLock()
3da8b937 » tavis_rudd
2006-01-03 made the exception handling a bit more robust
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:
cef58d09 » tavis_rudd
2006-01-03 finish up last checkin
77 __file__ = os.path.join(_cacheDir[0],
78 convertTmplPathToModuleName(tmplPath)) + '.py'
3da8b937 » tavis_rudd
2006-01-03 made the exception handling a bit more robust
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)
f033618a » tavis_rudd
2002-08-09 *** empty log message ***
84 __file__ = tmplPath
3da8b937 » tavis_rudd
2006-01-03 made the exception handling a bit more robust
85 else:
86 __file__ = tmplPath
87 co = compile(code+'\n', __file__, 'exec')
88
0900226c » rtyler
2009-12-27 Remove references to the `new` module
89 mod = types.ModuleType(name)
3da8b937 » tavis_rudd
2006-01-03 made the exception handling a bit more robust
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
d608a470 » tavis_rudd
2002-04-18 initial import
97
98 ##################################################
99 ## FUNCTIONS
100
a3fd4307 » tavis_rudd
2006-06-20 made it possible to specify a list of template filename extentions th…
101 def install(templateFileExtensions=('.tmpl',)):
16223256 » tavis_rudd
2002-04-27 changed to use Gordon McMillan's ui.py import system
102 """Install the Cheetah Import Hooks"""
a3fd4307 » tavis_rudd
2006-06-20 made it possible to specify a list of template filename extentions th…
103
16223256 » tavis_rudd
2002-04-27 changed to use Gordon McMillan's ui.py import system
104 global _installed
105 if not _installed:
a3fd4307 » tavis_rudd
2006-06-20 made it possible to specify a list of template filename extentions th…
106 CheetahDirOwner.templateFileExtensions = templateFileExtensions
16223256 » tavis_rudd
2002-04-27 changed to use Gordon McMillan's ui.py import system
107 import __builtin__
47fbe57d » rtyler
2009-11-15 Clean up a swath of fixes suggested by 2to3
108 if isinstance(__builtin__.__import__, types.BuiltinFunctionType):
16223256 » tavis_rudd
2002-04-27 changed to use Gordon McMillan's ui.py import system
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"""
d608a470 » tavis_rudd
2002-04-18 initial import
120 global _installed
121 if not _installed:
16223256 » tavis_rudd
2002-04-27 changed to use Gordon McMillan's ui.py import system
122 import __builtin__
47fbe57d » rtyler
2009-11-15 Clean up a swath of fixes suggested by 2to3
123 if isinstance(__builtin__.__import__, types.MethodType):
16223256 » tavis_rudd
2002-04-27 changed to use Gordon McMillan's ui.py import system
124 __builtin__.__import__ = __oldimport__
125 global _manager
126 del _manager
d608a470 » tavis_rudd
2002-04-18 initial import
127
128 if __name__ == '__main__':
129 install()
Something went wrong with that request. Please try again.