00001
00002
00003
00004
00005
00006
00007
00008 import sys
00009 import weakref
00010 import gobject
00011
00012 def load(path, name, datadir):
00013 '''Add PATH to module load path and import all classes matching NAME . Call
00014 the init_extension function with DATADIR if the imported modules
00015 have one. Extension classes are expected to register themselves
00016 as GObjects.'''
00017
00018 origpath = sys.path
00019
00020 if path:
00021 sys.path = [path] + sys.path
00022
00023 try:
00024 if name.endswith('.*'):
00025 pkgname = name[:-2]
00026 pkg = _import(pkgname)
00027
00028 names = ['%s.%s' % (pkgname, i) for i in pkg.__all__]
00029 else:
00030 names = [name]
00031
00032 for name in names:
00033 try:
00034 mod = _import(name)
00035
00036 if 'init_extension' in dir(mod):
00037 mod.init_extension(datadir)
00038
00039 except Exception, e:
00040 if path:
00041 pretty = '%s:%s' % (path, name)
00042 else:
00043 pretty = name
00044
00045 print >>sys.stderr, 'Failed to load module %s (%s)' % (pretty, e)
00046
00047 finally:
00048 sys.path = origpath
00049
00050 def _import(name):
00051 list = name.split('.')
00052 return __import__(name, globals(), locals(), list[:-1])
00053
00054 refs_by_class = {}
00055
00056 def get_children(parent):
00057 '''Get a list of classes that are derived from PARENT (must be a GObject).'''
00058
00059 return [i.pytype for i in gobject.type_children(parent)]
00060
00061 def get_singleton_children(parent):
00062 return [get_singleton(i) for i in get_children(parent)]
00063
00064 def get_singleton(cls):
00065 if '__instance' not in dir(cls):
00066 cls.__instance = cls()
00067 return cls.__instance
00068
00069 def get_class(name):
00070 '''Get a class by its NAME (must be a GObject).'''
00071
00072 gname = name.replace('.', '+')
00073 gtype = gobject.type_from_name(gname)
00074 return gtype.pytype
00075
00076 class Extension (gobject.GObject):
00077 '''Base for extension tag classes.'''
00078
00079 def __init__(self):
00080 gobject.GObject.__init__(self)
00081
00082 def destroy(self):
00083 '''Emits the "destroy" signal.'''
00084 self.emit('destroy')
00085
00086 gobject.type_register(Extension)
00087 gobject.signal_new('destroy', Extension, 0, gobject.TYPE_NONE, [])