1
# -*- coding: utf-8 -*-
3
# Copyright 2008 Simon Edwards <simon@simonzone.com>
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU Library General Public License as
7
# published by the Free Software Foundation; either version 2, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details
15
# You should have received a copy of the GNU Library General Public
16
# License along with this program; if not, write to the
17
# Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25
class PlasmaImporter(object):
28
self.toplevelcount = {}
29
self.marker = '<plasma>' + str(id(self))
30
sys.path.append(self.marker)
31
sys.path_hooks.append(self.hook)
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
location = self.find_pymod(fullname)
58
if location is not None:
63
# Find the given module in the plasma directory.
64
# Result a tuple on success otherwise None. The tuple contains the location
65
# of the module/package in disk and a boolean indicating if it is a package
67
def find_pymod(self,modulename):
68
parts = modulename.split('.')
72
if toppackage in self.toplevel:
73
path = self.toplevel[toppackage]
75
# Simple top level Plasma package
78
restpath = apply(os.path.join,rest)
79
fullpath = os.path.join(path,'contents','code',restpath)
80
if os.path.isdir(fullpath):
81
return (fullpath,True)
82
elif os.path.exists(fullpath+'.py'):
83
return (fullpath+'.py',False)
87
def _get_code(self,location):
88
return open(location).read()
90
def load_module(self, fullname):
91
location,ispkg = self.find_pymod(fullname)
92
if ispkg: # Package dir.
93
initlocation = os.path.join(location,'__init__.py')
95
if os.path.isfile(initlocation):
96
code = open(initlocation)
100
mod = sys.modules.setdefault(fullname, imp.new_module(fullname))
101
mod.__file__ = location #"<%s>" % self.__class__.__name__
102
mod.__loader__ = self
104
mod.__path__ = [self.marker]
107
exec code in mod.__dict__
112
#plasma_importer = PlasmaImporter()
113
#plasma_importer.register_top_level('plasma_pyclock','/home/sbe/devel/python_plasma_2/test/plasma-pyclock')
114
#from plasma_pyclock import main