hy all, I have the following "wrong" dispatcher:
def _load_methods(self):
import os, sys, glob
sys.path.insert(0, 'modules\commands')
for c in glob.glob('modules\commands\Command*.py'):
if os.path.isdir(c):
continue
c = os.path.splitext(c)[0]
parts = c.split(os.path.sep )
module, name = '.'.join( parts ), parts[-1:]
module = __import__( module, globals(), locals(), name )
_cmdClass = __import__(module).Command
for method_name in list_public_methods(_cmdClass):
self._methods[method_name] = getattr(_cmdClass(), method_name)
sys.path.pop(0)
It produces the following error:
ImportError: No module named commands.CommandAntitheft
where Command*.py is placed into modules\commands\ folder
can someone help me?
One Possible solution (It works!!!) is:
def _load_methods(self):
import os, sys, glob, imp
for file in glob.glob('modules/commands/Command*.py'):
if os.path.isdir(file):
continue
module = os.path.splitext(file)[0].rsplit(os.sep, 1)[1]
fd, filename, desc = imp.find_module(module,
['./modules/commands'])
try:
_cmdClass = imp.load_module( module, fd, filename, desc).Command
finally:
fd.close()
for method_name in list_public_methods(_cmdClass):
self._methods[method_name] = getattr(_cmdClass(), method_name)
It still remains all risks suggested by bobince (tanks :-) )but now I'm able to load commands at "runtime"