7d476dcb1fa2888a0c00512aff15c31fcc72d1a6
[certmaster.git] / certmaster / minion / modules / func_module.py
1 ##
2 ## Copyright 2007, Red Hat, Inc
3 ## see AUTHORS
4 ##
5 ## This software may be freely redistributed under the terms of the GNU
6 ## general public license.
7 ##
8 ## You should have received a copy of the GNU General Public License
9 ## along with this program; if not, write to the Free Software
10 ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
11 ##
12
13 import inspect
14
15 from func import logger
16 from func.config import read_config
17 from func.commonconfig import FuncdConfig
18
19
20 class FuncModule(object):
21
22 # the version is meant to
23 version = "0.0.0"
24 api_version = "0.0.0"
25 description = "No Description provided"
26
27 def __init__(self):
28
29 config_file = '/etc/func/minion.conf'
30 self.config = read_config(config_file, FuncdConfig)
31 self.__init_log()
32 self.__base_methods = {
33 # __'s so we don't clobber useful names
34 "module_version" : self.__module_version,
35 "module_api_version" : self.__module_api_version,
36 "module_description" : self.__module_description,
37 "list_methods" : self.__list_methods
38 }
39
40 def __init_log(self):
41 log = logger.Logger()
42 self.logger = log.logger
43
44 def register_rpc(self, handlers, module_name):
45 # add the internal methods, note that this means they
46 # can get clobbbered by subclass versions
47 for meth in self.__base_methods:
48 handlers["%s.%s" % (module_name, meth)] = self.__base_methods[meth]
49
50 # register our module's handlers
51 for name, handler in self.__list_handlers().items():
52 handlers["%s.%s" % (module_name, name)] = handler
53
54 def __list_handlers(self):
55 """ Return a dict of { handler_name, method, ... }.
56 All methods that do not being with an underscore will be exposed.
57 We also make sure to not expose our register_rpc method.
58 """
59 handlers = {}
60 for attr in dir(self):
61 if inspect.ismethod(getattr(self, attr)) and attr[0] != '_' and \
62 attr != 'register_rpc':
63 handlers[attr] = getattr(self, attr)
64 return handlers
65
66 def __list_methods(self):
67 return self.__list_handlers().keys() + self.__base_methods.keys()
68
69 def __module_version(self):
70 return self.version
71
72 def __module_api_version(self):
73 return self.api_version
74
75 def __module_description(self):
76 return self.description