hallo nochmal...
ich möchte auch dynamisch Module laden.
Dazu verwende ich eine Klasse die die ganze Sache automatisiert lädt.
Problem: Ich bekomme zwar keine Fehlermeldung, aber die Klassen werden nicht geladen.
So sieht die ganze Sache aus:
Code: Alles auswählen
class modules:
""" class for importing the modules and get there outputs """
def __init__(self, mods=[], txts='', bars=''):
""" initialize all the things in modules """
self.mods = str(readcfg('modules')).strip().split(sep)
debug('Module: %s' % str(self.mods), 3, 'modules.__init__')
self.mods = modules.modfunc(self)
self.txts = modules.txtfunc(self)
self.bars = modules.barfunc(self)
def modfunc(self):
x = []
for mod in self.mods:
try:
exec "import %s" % mod
x.append(mod)
except:
raise
print "Can't load %s cause above error(s)." % mod
rmods=[]
for i in globals():
if i in self.mods:
rmods.append(i)
debug("globals() sollte enthalten: %s" % str(self.mods), 3, 'modules.mods')
debug("...und enthält davon: %s" % str(rmods), 3, 'modules.mods')
return self.mods
def txtfunc(self):
x = []
for mod in self.mods:
try:
#exec "x.append(%s.main()[0])" % mod
modobj = __import__(mod)
x.append(modobj.main()[0])
except:
raise
# print "Can't add text from %s cause above error(s)." % mod
return x
def barfunc(self):
x = []
for mod in self.mods:
try:
#exec "x.append(%s.main()[0])" % mod
modobj = __import__(mod)
x.append(modobj.main()[1])
except:
raise
# print "Can't add meter from %s cause above error(s)." % mod
return x
ausgabe:
Code: Alles auswählen
Debug from modules.__init__: Module: ['battery', 'time']
Debug from modules.mods: globals() sollte enthalten: ['battery', 'time']
Debug from modules.mods: ...und enthält davon: []
Traceback (most recent call last):
File "./szs.py", line 228, in ?
init('all')
File "./szs.py", line 187, in init
modules()
File "./szs.py", line 24, in __init__
self.txts = modules.txts(self)
File "./szs.py", line 55, in txts
exec "x.append(%s.main()[0])" % mod
File "<string>", line 1, in ?
NameError: name 'battery' is not defined