开发者

Auto register a class when it's defined (but without importing it anywhere)

I would like to register a class (not an instance) when it's created... but without importing it.

Basically, I want to do what's described here:

How to auto register a class when it's defined

... but without having to import the registered class anywhere.

Here's the issue: At a certain point of my application, I need to run certain system commands (let's stick with reboot, halt... for the example). I have created a "base" (abstract-mmnnno-not-really looking) class "Command" and a "CommandManager" which will receive an string with the command to run, create the appropriate instance for said command, queue it and dispatch the commands when needed (or whenever it can). All the instances of the "Command" class will overwrite an "execute" method that actually executes the command. If I want to "reboot", I would extend the "Command" class on a "RebootCommand" class, and overwrite that "execute" making the call to the external application "shutdown -r" or "reboot".

The final goal is being able to do something like:

CommandManager.execute("reboot")

And the system will reboot.

In order to do so, the "reboot" string has to be registered in a dictionary inside the CommandManager. That dictionary will have "reboot" (string) as the key and the class RebootCommand as the value. The CommandManager will receive the string "reboot", will go to it's "commandDictionary", create an instance of a RebootCommand class and call its execute() method (thus, rebooting the system).

Something like:

#------------- CommandManager.py -------------
@classmethod
def execute(cls, parameter):
    if parameter in cls.validCommands:
        tmpCommand = cls.validCommands[parameter]()
        tmpCommand.execute()
#---------------------------------------------

In order to do the registering, the RebootCommand class-object has to be created, and in order to do that, I have to import it somewhere. But I don't want to. I don't need to. I just need the RebootCommand class object created, so it will execute the __new__ method for the Command MetaClass that is where the registering takes place (as detailed in the answer to the other question above).

Is there any way to do it? I've tried importing the actual commands (the classes inheriting from Command) it in the __init__.py, but then, if I c开发者_如何学编程reate a new command, I have to add it to the __init__.py and I would like to avoid that (so another programmer can create a new command without worrying about forgetting adding the class to the __init__.py)

Here's my directory structure:

commands/
    __init__.py
    CommandManager.py
    Command.py
    RebootCommand.py

And here, the contents of Command and RebootCommand that make the registering:

#--------------- Command.py -------------------
def register(newCommand):
    if newCommand and newCommand._command:
        import CommandManager
        CommandManager.CommandManager.registerCommand(newCommand._command, newCommand)


# Fancy registering! https://stackoverflow.com/questions/5189232/how-to-auto-register-a-class-when-its-defined
class CommandMetaClass(type):
    def __new__(cls, clsname, bases, attrs):
        newCommand = super(cls, CommandMetaClass).__new__(cls, clsname, bases, attrs)
        register(newCommand)  # here is your register function
        return newCommand

class Command(object):
    __metaclass__ = CommandMetaClass
    _command = None
#-----------------------------------------------------

and...

#--------------- RebootCommand.py -------------------
class RebootCommand(Command.Command):
    _command = "reboot"
#---------------------------------------------------

The register process is properly executed if I open __init__.py and I write:

import RebootCommand

(at this point the RebootCommand class object is created,

CommandMetaClass.__new__

function is run, "reboot" is registered as the RebootCommand class, and everyone is happy)

but I'd like to avoid having to do that (touching __init__.py), if possible

I have tried importing with the * wildcard without luck.

To sum up, I'd like that another programmer that wants to implement a new Command, just has to create a new class inheriting from Command but without having to add it to __init__.py

I know it's nothing that can't be fixed with a comment in the Command.py file, saying something like "any class inheriting from this have to be added in __init__.py" but I'm curious to know if I can do it in a more "elegant" way.


I feel that your approach is a bit complicated to implement the functionality you want. My suggestion is to you use a strategy like this:

  1. Name your modules so that the command classes are defined in files named after the commands they implement. For example, the RebootCommand class goes into the file commands/reboot.py.
  2. Every command module defines a top-level variable command_cls that contains a reference to the class implementing the command. commands/reboot.py would therefore contain code like this:

    class RebootCommand:
      def execute(self):
        # [...]
    
    command_cls = RebootCommand
    
  3. To execute a command, you use the __import__ function to dynamically load your module. Your CommandManager.py would logically live in a file outside the commands directory (which is reserved for command modules) and would be implemented like the following:

    class CommandManager:
      @classmethod
      def execute(cls, command, *args, **kw):
        # import the command module
        commands_mod = __import__("commands", globals(), locals(), [command])
        mod = getattr(commands_mod, command)
        return mod.command_cls(*args, **kw).execute()
    

EDIT: Seems I confused the usage of the imp module. Updated the code to use __import__.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜