I am building a very basic platform in the form of a Python 2.7 module. This module has a read-eval-print loop where entered user commands are mapped to function calls. Since I am trying to make it easy to build plugin modules for my platform, the function calls will be from my Main module to an arbitrary plugin module. I'd like a plugin builder to be able to specify the command that he wants to trigger his function, so I've been looking for a Pythonic way to remotely enter a mapping in the command->function dict in the Main module from the plugin module.
I've looked at several things:
Method name parsing: the Main module would import the plugin module and scan it for method names that match a certain format. For example, it might add the download_file_command(file) method to its dict as "download file" -> download_file_command. However, getting a concise, easy-to-type command name (say, "dl") requires that the function's name also be short, which isn't good for code readability. It also requires the plugin developer to conform to a precise naming format.
Cross-module decorators: decorators would let the plugin developer name his function whatever he wants and simply add something like @Main.register("dl"), but they would necessarily require that I both modify another module's namespace and keep global state in the Main module. I understand this is very bad.
Same-module decorators: using the same logic as above, I could add a decorator that adds the function's name to some command name->function mapping local to the plugin module and retrieve the mapping to the Main module with an API call. This requires that certain methods always be present or inherited though, and - if my understanding of decorators is correct - the function will only register itself the first time it is run and will unnecessarily re-register itself every subsequent time thereafter.
Thus, what I really need is a Pythonic way to annotate a function with the command name that should trigger it, and that way can't be the function's name. I need to be able to extract the command name->function mapping when I import the module, and any less work on the plugin developer's side is a big plus.
Thanks for the help, and my apologies if there are any flaws in my Python understanding; I'm relatively new to the language.
import pluginand do@plugin.register('my_method_name')\ndef somefunc_meeting_an_api_spec():...- Jon Clementscmd.Cmdin conjunction withreadlinetab expansion support negate the need to worry about short names. - sotapme