Add Service Provider class
[cascardo/ipsilon.git] / ipsilon / util / plugin.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2013  Simo Sorce <simo@redhat.com>
4 #
5 # see file 'COPYING' for use and warranty information
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 import os
21 import imp
22 import cherrypy
23 import inspect
24 from ipsilon.util.data import Store
25
26
27 class Plugins(object):
28
29     def __init__(self, path=None):
30         if path is None:
31             self._path = os.getcwd()
32         else:
33             self._path = path
34         self._providers_tree = None
35
36     def _load_class(self, tree, class_type, file_name):
37         cherrypy.log.error('Check module %s for class %s' % (file_name,
38                                                              class_type))
39         name, ext = os.path.splitext(os.path.split(file_name)[-1])
40         try:
41             if ext.lower() == '.py':
42                 mod = imp.load_source(name, file_name)
43             #elif ext.lower() == '.pyc':
44             #    mod = imp.load_compiled(name, file_name)
45             else:
46                 return
47         except Exception, e:  # pylint: disable=broad-except
48             cherrypy.log.error('Failed to load "%s" module: [%s]' % (name, e))
49             return
50
51         if hasattr(mod, class_type):
52             instance = getattr(mod, class_type)()
53             public_name = getattr(instance, 'name', name)
54             tree[public_name] = instance
55             cherrypy.log.error('Added module %s as %s' % (name, public_name))
56
57     def _load_classes(self, tree, path, class_type):
58         files = None
59         try:
60             files = os.listdir(path)
61         except Exception, e:  # pylint: disable=broad-except
62             cherrypy.log.error('No modules in %s: [%s]' % (path, e))
63             return
64
65         for name in files:
66             filename = os.path.join(path, name)
67             self._load_class(tree, class_type, filename)
68
69     def get_plugins(self, path, class_type):
70         plugins = dict()
71         self._load_classes(plugins, path, class_type)
72         return plugins
73
74
75 class PluginLoader(object):
76
77     def __init__(self, baseobj, facility, plugin_type):
78         (whitelist, config) = Store().get_plugins_config(facility)
79         if cherrypy.config.get('debug', False):
80             cherrypy.log('[%s] %s: %s' % (facility, whitelist, config))
81         if whitelist is None:
82             whitelist = []
83         if config is None:
84             config = dict()
85
86         p = Plugins(path=cherrypy.config['base.dir'])
87         (pathname, dummy) = os.path.split(inspect.getfile(baseobj))
88         self._plugins = {
89             'config': config,
90             'available': p.get_plugins(pathname, plugin_type),
91             'whitelist': whitelist,
92             'enabled': []
93         }
94
95     def get_plugin_data(self):
96         return self._plugins
97
98
99 class PluginObject(object):
100
101     def __init__(self):
102         self.name = None
103         self._config = None
104         self._options = None
105         self._data = Store()
106
107     def get_config_desc(self):
108         """ The configuration description is a dictionary that provides
109             A description of the supported configuration options, as well
110             as the default configuration option values.
111             The key is the option name, the value is an array of 3 elements:
112              - description
113              - option type
114              - default value
115         """
116         return self._options
117
118     def set_config(self, config):
119         self._config = config
120
121     def get_config_value(self, name):
122         value = None
123         if self._config:
124             value = self._config.get(name, None)
125         if not value:
126             if self._options:
127                 opt = self._options.get(name, None)
128                 if opt:
129                     value = opt[2]
130
131         if cherrypy.config.get('debug', False):
132             cherrypy.log('[%s] %s: %s' % (self.name, name, value))
133
134         return value
135
136     def set_config_value(self, option, value):
137         if not self._config:
138             self._config = dict()
139         self._config[option] = value
140
141     def get_data(self, idval=None, name=None, value=None):
142         return self._data.get_data(self.name, idval=idval, name=name,
143                                    value=value)
144
145     def save_data(self, data):
146         self._data.save_data(self.name, data)