From 23c9c26d270ff766133e7aeebffc99a35633ef41 Mon Sep 17 00:00:00 2001 From: Luke Macken Date: Tue, 15 Jan 2008 17:42:05 -0500 Subject: Simplify our modules by auto-detecting them and registering their handlers - Auto-detect and load all FuncModules. This obsoletes the need to have our modules define a register_rpc method. - Use introspection in our FuncModule to auto-register all method handlers that do not being with an underscore. This obsoletes the need to hardcode methods in our modules. - Remove all __init__ methods from our modules, along with register_rpc - Modify the func-create-module script to reflect these changes. Note that doing 'from modules import func_module' is no longer supported in our modules, do to some interesting path issues with our auto-detection code. Supported methods are now: 'import func_module' or 'from func.minion.modules import func_module' --- func/minion/modules/func_module.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'func/minion/modules/func_module.py') diff --git a/func/minion/modules/func_module.py b/func/minion/modules/func_module.py index 5965e24..c911b91 100644 --- a/func/minion/modules/func_module.py +++ b/func/minion/modules/func_module.py @@ -10,6 +10,7 @@ ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ## +import inspect from func import logger from func.config import read_config @@ -34,7 +35,7 @@ class FuncModule(object): "module_api_version" : self.__module_api_version, "module_description" : self.__module_description, "list_methods" : self.__list_methods - } + } def __init_log(self): log = logger.Logger() @@ -45,8 +46,11 @@ class FuncModule(object): # can get clobbbered by subclass versions for meth in self.__base_methods: handlers["%s.%s" % (module_name, meth)] = self.__base_methods[meth] - for meth in self.methods: - handlers["%s.%s" % (module_name,meth)] = self.methods[meth] + + # register all methods that don't start with an underscore + for attr in dir(self): + if inspect.ismethod(getattr(self, attr)) and attr[0] != '_': + handlers["%s.%s" % (module_name, attr)] = getattr(self, attr) def __list_methods(self): return self.methods.keys() + self.__base_methods.keys() @@ -59,7 +63,3 @@ class FuncModule(object): def __module_description(self): return self.description - - -methods = FuncModule() -register_rpc = methods.register_rpc -- cgit From a433b0073efcbeb2028dceed5105cc40f5936ddf Mon Sep 17 00:00:00 2001 From: Luke Macken Date: Tue, 15 Jan 2008 18:12:35 -0500 Subject: Don't expose FuncModule.register_rpc --- func/minion/modules/func_module.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'func/minion/modules/func_module.py') diff --git a/func/minion/modules/func_module.py b/func/minion/modules/func_module.py index c911b91..99758f9 100644 --- a/func/minion/modules/func_module.py +++ b/func/minion/modules/func_module.py @@ -49,7 +49,8 @@ class FuncModule(object): # register all methods that don't start with an underscore for attr in dir(self): - if inspect.ismethod(getattr(self, attr)) and attr[0] != '_': + if inspect.ismethod(getattr(self, attr)) and attr[0] != '_' and \ + attr != 'register_rpc': handlers["%s.%s" % (module_name, attr)] = getattr(self, attr) def __list_methods(self): -- cgit From 79d75b06a1bdae8c5c42026de606ed1787be6030 Mon Sep 17 00:00:00 2001 From: Luke Macken Date: Tue, 15 Jan 2008 19:37:40 -0500 Subject: Fix our FuncModule list_methods --- func/minion/modules/func_module.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'func/minion/modules/func_module.py') diff --git a/func/minion/modules/func_module.py b/func/minion/modules/func_module.py index 99758f9..7d476dc 100644 --- a/func/minion/modules/func_module.py +++ b/func/minion/modules/func_module.py @@ -47,14 +47,24 @@ class FuncModule(object): for meth in self.__base_methods: handlers["%s.%s" % (module_name, meth)] = self.__base_methods[meth] - # register all methods that don't start with an underscore + # register our module's handlers + for name, handler in self.__list_handlers().items(): + handlers["%s.%s" % (module_name, name)] = handler + + def __list_handlers(self): + """ Return a dict of { handler_name, method, ... }. + All methods that do not being with an underscore will be exposed. + We also make sure to not expose our register_rpc method. + """ + handlers = {} for attr in dir(self): if inspect.ismethod(getattr(self, attr)) and attr[0] != '_' and \ attr != 'register_rpc': - handlers["%s.%s" % (module_name, attr)] = getattr(self, attr) + handlers[attr] = getattr(self, attr) + return handlers def __list_methods(self): - return self.methods.keys() + self.__base_methods.keys() + return self.__list_handlers().keys() + self.__base_methods.keys() def __module_version(self): return self.version -- cgit