multiple inheritance of a dynamic list of classes?

devicerandom at gmail.com devicerandom at gmail.com
Mon Feb 12 10:48:33 EST 2007


Hi,

I am currently using the Cmd module for a mixed cli+gui application. I
am starting to refactor my code and it would be highly desirable if
many commands could be built as simple plugins.

My idea was:
- Load a list of plugin names (i.e. from the config file, or from the
plugins directory)
- Import all plugins found dynamically:
and this is easy, since I can do, for example:

PLUGIN_NAMES=['foo', 'bar']
PLUGIN_MODULES = map(__import__, PLUGIN_NAMES)
PLUGINS = [item.Commands for item in PLUGIN_MODULES]

Now, what I have to do is to define my command line class. This is
usually done by subclassing cmd.Cmd:

class MyCli(cmd.Cmd):
    ....

Now I want to add the commands defined in foo.Commands and
bar.Commands. foo.Commands contains the functions corresponding to the
new commands this way:
#foo.py
class Commands

     def do_this(self,args):
          ...
     def do_that(self,args):
          ...

I've seen I can do it by explicitely import them and using multiple
inheritance:

class MyCli(cmd.Cmd , foo.Commands, bar.Commands)
      ....

so that do_this and do_that are now methods of a Cmd command line.

Now:
- how can I instead have MyCli inherit from a dynamic list of modules?
- is there a better way than using multiple inheritance to plug-in
dynamically commands in a Cmd command line?

I hope it's all clear.

Thanks,
Massimo




More information about the Python-list mailing list