2 # $Id: ImportHooks.py,v 1.25 2006/06/20 19:23:27 tavis_rudd Exp $
4 """Provides some import hooks to allow Cheetah's .tmpl files to be imported
5 directly like Python .py modules.
8 import Cheetah.ImportHooks
9 Cheetah.ImportHooks.install()
12 ================================================================================
13 Author: Tavis Rudd <tavis@damnsimple.com>
14 License: This software is released for unlimited distribution under the
15 terms of the MIT license. See the LICENSE file.
16 Version: $Revision: 1.25 $
17 Start Date: 2001/03/30
18 Last Revision Date: $Date: 2006/06/20 19:23:27 $
20 __author__
= "Tavis Rudd <tavis@damnsimple.com>"
21 __revision__
= "$Revision: 1.25 $"[11:-2]
29 from threading
import Lock
32 from Cheetah
import ImportManager
33 from Cheetah
.ImportManager
import DirOwner
34 from Cheetah
.Compiler
import Compiler
35 from Cheetah
.convertTmplPathToModuleName
import convertTmplPathToModuleName
39 ##################################################
43 def setCacheDir(cacheDir
):
45 _cacheDir
.append(cacheDir
)
47 ##################################################
50 class CheetahDirOwner(DirOwner
):
52 _acquireLock
= _lock
.acquire
53 _releaseLock
= _lock
.release
55 templateFileExtensions
= ('.tmpl',)
57 def getmod(self
, name
):
60 mod
= DirOwner
.getmod(self
, name
)
64 for ext
in self
.templateFileExtensions
:
65 tmplPath
= os
.path
.join(self
.path
, name
+ ext
)
66 if os
.path
.exists(tmplPath
):
68 return self
._compile
(name
, tmplPath
)
71 exc_txt
= traceback
.format_exc()
72 exc_txt
=' '+(' \n'.join(exc_txt
.splitlines()))
74 'Error while compiling Cheetah module'
75 ' %(name)s, original traceback follows:\n%(exc_txt)s'%locals())
82 def _compile(self
, name
, tmplPath
):
83 ## @@ consider adding an ImportError raiser here
84 code
= str(Compiler(file=tmplPath
, moduleName
=name
,
87 __file__
= os
.path
.join(_cacheDir
[0],
88 convertTmplPathToModuleName(tmplPath
)) + '.py'
90 open(__file__
, 'w').write(code
)
92 ## @@ TR: need to add some error code here
93 traceback
.print_exc(file=sys
.stderr
)
97 co
= compile(code
+'\n', __file__
, 'exec')
99 mod
= imp
.new_module(name
)
100 mod
.__file
__ = co
.co_filename
102 mod
.__orig
_file
__ = tmplPath
# @@TR: this is used in the WebKit
103 # filemonitoring code
108 ##################################################
111 def install(templateFileExtensions
=('.tmpl',)):
112 """Install the Cheetah Import Hooks"""
116 CheetahDirOwner
.templateFileExtensions
= templateFileExtensions
118 if type(__builtin__
.__import
__) == types
.BuiltinFunctionType
:
120 __oldimport__
= __builtin__
.__import
__
121 ImportManager
._globalOwnerTypes
.insert(0, CheetahDirOwner
)
122 #ImportManager._globalOwnerTypes.append(CheetahDirOwner)
124 _manager
=ImportManager
.ImportManager()
125 _manager
.setThreaded()
129 """Uninstall the Cheetah Import Hooks"""
133 if type(__builtin__
.__import
__) == types
.MethodType
:
134 __builtin__
.__import
__ = __oldimport__
138 if __name__
== '__main__':