Fix E713 with stricter pep8 error checker
[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):
30         self._providers_tree = None
31
32     def _load_class(self, tree, class_type, file_name):
33         cherrypy.log.error('Check module %s for class %s' % (file_name,
34                                                              class_type))
35         name, ext = os.path.splitext(os.path.split(file_name)[-1])
36         try:
37             if ext.lower() == '.py':
38                 mod = imp.load_source(name, file_name)
39             #elif ext.lower() == '.pyc':
40             #    mod = imp.load_compiled(name, file_name)
41             else:
42                 return
43         except Exception, e:  # pylint: disable=broad-except
44             cherrypy.log.error('Failed to load "%s" module: [%s]' % (name, e))
45             return
46
47         if hasattr(mod, class_type):
48             instance = getattr(mod, class_type)()
49             public_name = getattr(instance, 'name', name)
50             tree[public_name] = instance
51             cherrypy.log.error('Added module %s as %s' % (name, public_name))
52
53     def _load_classes(self, tree, path, class_type):
54         files = None
55         try:
56             files = os.listdir(path)
57         except Exception, e:  # pylint: disable=broad-except
58             cherrypy.log.error('No modules in %s: [%s]' % (path, e))
59             return
60
61         for name in files:
62             filename = os.path.join(path, name)
63             self._load_class(tree, class_type, filename)
64
65     def get_plugins(self, path, class_type):
66         plugins = dict()
67         self._load_classes(plugins, path, class_type)
68         return plugins
69
70
71 class PluginLoader(object):
72
73     def __init__(self, baseobj, facility, plugin_type):
74         (whitelist, config) = Store().get_plugins_config(facility)
75         if cherrypy.config.get('debug', False):
76             cherrypy.log('[%s] %s: %s' % (facility, whitelist, config))
77         if whitelist is None:
78             whitelist = []
79         if config is None:
80             config = dict()
81
82         p = Plugins()
83         (pathname, dummy) = os.path.split(inspect.getfile(baseobj))
84         self._plugins = {
85             'config': config,
86             'available': p.get_plugins(pathname, plugin_type),
87             'whitelist': whitelist,
88             'enabled': []
89         }
90
91     def get_plugin_data(self):
92         return self._plugins
93
94
95 class PluginInstaller(object):
96     def __init__(self, baseobj):
97         (pathname, dummy) = os.path.split(inspect.getfile(baseobj))
98         self._pathname = pathname
99
100     def get_plugins(self):
101         p = Plugins()
102         return p.get_plugins(self._pathname, 'Installer')
103
104
105 class PluginObject(object):
106
107     def __init__(self):
108         self.name = None
109         self._config = None
110         self._options = None
111         self._data = Store()
112
113     def get_config_desc(self):
114         """ The configuration description is a dictionary that provides
115             A description of the supported configuration options, as well
116             as the default configuration option values.
117             The key is the option name, the value is an array of 3 elements:
118              - description
119              - option type
120              - default value
121         """
122         return self._options
123
124     def set_config(self, config):
125         self._config = config
126
127     def get_config_value(self, name):
128         value = None
129         if self._config:
130             value = self._config.get(name, None)
131         if not value:
132             if self._options:
133                 opt = self._options.get(name, None)
134                 if opt:
135                     value = opt[2]
136
137         if cherrypy.config.get('debug', False):
138             cherrypy.log('[%s] %s: %s' % (self.name, name, value))
139
140         return value
141
142     def set_config_value(self, option, value):
143         if not self._config:
144             self._config = dict()
145         self._config[option] = value
146
147     def get_plugin_config(self, facility):
148         return self._data.get_plugin_config(facility, self.name)
149
150     def save_plugin_config(self, facility):
151         self._data.save_plugin_config(facility, self.name, self._config)
152
153     def get_data(self, idval=None, name=None, value=None):
154         return self._data.get_data(self.name, idval=idval, name=name,
155                                    value=value)
156
157     def save_data(self, data):
158         self._data.save_data(self.name, data)
159
160     def new_datum(self, datum):
161         self._data.new_datum(self.name, datum)
162
163     def del_datum(self, idval):
164         self._data.del_datum(self.name, idval)
165
166     def wipe_config_values(self, facility):
167         self._data.wipe_plugin_config(facility, self.name)
168
169     def wipe_data(self):
170         self._data.wipe_data(self.name)