ImportHooks vs imp
Богун Дмитрий <[email protected]> Mon, 12 Jul 2010 22:54:47 +0300
| Newsgroups | gmane.comp.python.cheetah |
|---|---|
| Message-ID | <[email protected]> |
Hello. I need to make 2 sets of templates. One generic level, and some count of overlays for gnereric templates. for example I have generic template /tmpl/web/main.tmpl and possible overlays for it /tmpl/_users_/vasya/web/main.tmpl and /tmpl/_users_/petya/web/main.tmpl. Now to load templates I use direct calls to Template.compile and give it required open file descriptor. But this is not very comfortable. I want to try ImportHooks but, because of multiple, dynamically changed search path, I don't want to add all path into sys.path. I want to use imp module to load module from required places... After try I see that ImportHooks and imp can't work together. Into attach my example, where I try to use ImportHooks and imp toogether(_gen.tmpl and t0.tmpl locaten into /tmp/tmpl. empty __init__.py too placed into /tmp/tmpl). Is there a way to use ImportHooks and not place all search path into sys.path? PS Sorry for my English. ------------------------------------------------------------------------------ This SF.net email is sponsored by Sprint What will you do first with EVO, the first 4G phone? Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first _______________________________________________ Cheetahtemplate-discuss mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
z.py
(text/x-python, 776 B)
#!/usr/bin/env python
#-*- coding:utf-8 -*-
#import pdb
#import os, sys
#import time
#import logging
#import re
#import errno
#import locale
#import pprint
# mutable inputs
import imp
import Cheetah
import Cheetah.ImportHooks
Cheetah.ImportHooks.install()
#Cheetah.ImportHooks.setCacheDir('/home/vugluskr/tmp/z/cache')
def load_tmpl(name):
fp, pathname, description = imp.find_module(name)
#fp, pathname, description = imp.find_module(name, ['/tmp/tmpl'])
try:
m = imp.load_module(name, fp, pathname, description)
return getattr(m, name)
finally:
# Since we may exit via an exception, close fp explicitly.
if fp:
fp.close()
def main():
t = load_tmpl('t0')
print t()
if __name__ == '__main__':
main()
_gen.tmpl
(text/plain, 75 B)
#def meth0() meth0 aAaaAa #end def byaka byak. $meth0 ## comment zzz...
t0.tmpl
(text/x-objcsrc, 80 B)
#import _gen #extends _gen._gen #def meth0() meth0 - t0 aAaaAa BbbBbb #end def