Package slum :: Module includes
[hide private]
[frames] | no frames]

Source Code for Module slum.includes

 1  # 
 2  # includes.py - adds .hslum extension to python import mechanism, allowing 
 3  #                               templates to just use import to import .hslum includes 
 4  # 
 5  #    Copyright (C) 2008 - Roberto Hradec 
 6  # 
 7  # --------------------------------------------------------------------------- 
 8  #        This file is part of SLUM. 
 9  # 
10  #    SLUM is free software: you can redistribute it and/or modify 
11  #    it under the terms of the GNU General Public License as published by 
12  #    the Free Software Foundation, either version 3 of the License, or 
13  #    (at your option) any later version. 
14  # 
15  #    SLUM is distributed in the hope that it will be useful, 
16  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
17  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
18  #    GNU General Public License for more details. 
19  # 
20  #    You should have received a copy of the GNU General Public License 
21  #    along with SLUM.  If not, see <http://www.gnu.org/licenses/>. 
22  # --------------------------------------------------------------------------- 
23   
24  import sys,  marshal 
25  VER = sys.hexversion 
26   
27  debug = 0 
28   
29 -class DBmodule(type(sys)):
30 - def __repr__(self):
31 return "<DBmodule '%s' originally from '%s'>" % (self.__name__, self.__file__)
32 33 34
35 -class hslumImporter(object):
36
37 - def __init__(self, item, *args, **kw):
38 print 'init',item, args, kw 39 ''' 40 if item != "*db*": 41 raise ImportError 42 if debug: 43 print "dbimporter: item:", id(self), item, "args:", args, "keywords:", kw 44 print "Accepted", item 45 '''
46
47 - def find_module(self, fullname, path=None):
48 print 'find_module',fullname, path 49 ''' 50 if debug: 51 print "%x: find_module: %s from %s" % (id(self), fullname, path) 52 if fullname not in impdict: 53 if debug: 54 print "Bailed on", fullname 55 return None 56 else: 57 if debug: 58 print "found", fullname, "in db" 59 return self 60 '''
61
62 - def load_module(self, modname):
63 print 'load_module' 64 if debug: 65 print "%x: load_module: %s" % (id(self), modname) 66 67 # return it if already loaded 68 if modname in sys.modules: 69 return sys.modules[modname] 70 71 # load it 72 if False: 73 raise ImportError, "DB module %s not found in modules" 74 75 code, package, path = ("","","") 76 code = marshal.loads(code) 77 module = DBmodule(modname) 78 sys.modules[modname] = module 79 module.__name__ = modname 80 module.__file__ = path # was "db:%s" % modname 81 module.__loader__ = self 82 if package: 83 module.__path__ = ["*db*"] 84 exec code in module.__dict__ 85 if debug: 86 print modname, "loaded:", module, "pkg:", package 87 88 return module
89 90
91 -def install():
92 sys.path_hooks.append(hslumImporter) 93 sys.path_importer_cache.clear() # probably not necessary 94 sys.path.insert(0, "*db*") # probably not needed with a metea-path hook?
95