add more spacing
[personal-kdebase.git] / workspace / plasma / scriptengines / python / plasma_importer.py
bloba1229efdddaef6dfc5119bbb26f9a6fdef0032f4
2 # Copyright 2008 Simon Edwards <simon@simonzone.com>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU Library General Public License as
6 # published by the Free Software Foundation; either version 2, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details
14 # You should have received a copy of the GNU Library General Public
15 # License along with this program; if not, write to the
16 # Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 import sys
21 import os
22 import imp
24 class PlasmaImporter(object):
25 def __init__(self):
26 self.toplevel = {}
27 self.toplevelcount = {}
28 sys.path.append('<plasma>')
29 sys.path_hooks.append(self.hook)
30 #print("PlasmaImporter.__init__: sys.path:" + repr(sys.path))
32 def hook(self,path):
33 if path=='<plasma>':
34 #print("plasma_thingy:"+ path)
35 return self
36 else:
37 raise ImportError()
39 def register_top_level(self,name,path):
40 if name not in self.toplevel:
41 self.toplevelcount[name] = 1
42 self.toplevel[name] = path
43 else:
44 self.toplevelcount[name] += 1
46 def unregister_top_level(self,name):
47 value = self.toplevelcount[name]-1
48 self.toplevelcount[name] = value
49 if value==0:
50 del self.toplevelcount[name]
51 del self.toplevel[name]
52 for key in list(sys.modules.keys()):
53 if key==name or key.startswith(name+"."):
54 del sys.modules[key]
56 def find_module(self,fullname, path=None):
57 #print('find_module(%s,%s)' % (fullname,path) )
59 location = self.find_pymod(fullname)
60 if location is not None:
61 #print("Found in " + location[0])
62 return self
63 else:
64 return None
66 # Find the given module in the plasma directory.
67 # Result a tuple on success otherwise None. The tuple contains the location
68 # of the module/package in disk and a boolean indicating if it is a package
69 # or module
70 def find_pymod(self,modulename):
71 parts = modulename.split('.')
72 toppackage = parts[0]
73 rest = parts[1:]
75 if toppackage in self.toplevel:
76 path = self.toplevel[toppackage]
77 if len(rest)==0:
78 # Simple top level Plasma package
79 return (path,True)
80 else:
81 restpath = apply(os.path.join,rest)
82 fullpath = os.path.join(path,'contents','code',restpath)
83 if os.path.isdir(fullpath):
84 return (fullpath,True)
85 elif os.path.exists(fullpath+'.py'):
86 return (fullpath+'.py',False)
87 else:
88 return None
90 def _get_code(self,location):
91 return open(location).read()
93 def load_module(self, fullname):
94 #print('load_module fullname: '+fullname)
96 location,ispkg = self.find_pymod(fullname)
97 #print("Location:"+location)
98 if ispkg: # Package dir.
99 initlocation = os.path.join(location,'__init__.py')
100 code = None
101 if os.path.isfile(initlocation):
102 #code = self._get_code(initlocation)
103 code = open(initlocation)
104 else:
105 #code = self._get_code(location)
106 code = open(location)
108 mod = sys.modules.setdefault(fullname, imp.new_module(fullname))
109 mod.__file__ = location #"<%s>" % self.__class__.__name__
110 mod.__loader__ = self
111 if ispkg:
112 mod.__path__ = ["<plasma>"]
113 if code is not None:
114 #print("code is:"+code)
115 try:
116 exec code in mod.__dict__
117 finally:
118 code.close()
119 return mod
121 #plasma_importer = PlasmaImporter()
122 #plasma_importer.register_top_level('plasma_pyclock','/home/sbe/devel/python_plasma_2/test/plasma-pyclock')
123 #from plasma_pyclock import main