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.
24 class PlasmaImporter(object):
27 self
.toplevelcount
= {}
28 sys
.path
.append('<plasma>')
29 sys
.path_hooks
.append(self
.hook
)
30 #print("PlasmaImporter.__init__: sys.path:" + repr(sys.path))
34 #print("plasma_thingy:"+ path)
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
44 self
.toplevelcount
[name
] += 1
46 def unregister_top_level(self
,name
):
47 value
= self
.toplevelcount
[name
]-1
48 self
.toplevelcount
[name
] = value
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
+"."):
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])
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
70 def find_pymod(self
,modulename
):
71 parts
= modulename
.split('.')
75 if toppackage
in self
.toplevel
:
76 path
= self
.toplevel
[toppackage
]
78 # Simple top level Plasma package
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)
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')
101 if os
.path
.isfile(initlocation
):
102 #code = self._get_code(initlocation)
103 code
= open(initlocation
)
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
112 mod
.__path
__ = ["<plasma>"]
114 #print("code is:"+code)
116 exec code
in mod
.__dict
__
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