1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
#!/usr/bin/python
#
# Copyright (C) 2013 Simo Sorce <simo@redhat.com>
#
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import imp
import cherrypy
import inspect
from ipsilon.util.data import AdminStore
from ipsilon.util.log import Log
class Plugins(object):
def __init__(self):
self._providers_tree = None
def _load_class(self, tree, class_type, file_name):
cherrypy.log.error('Check module %s for class %s' % (file_name,
class_type))
name, ext = os.path.splitext(os.path.split(file_name)[-1])
try:
if ext.lower() == '.py':
mod = imp.load_source(name, file_name)
# elif ext.lower() == '.pyc':
# mod = imp.load_compiled(name, file_name)
else:
return
except Exception, e: # pylint: disable=broad-except
cherrypy.log.error('Failed to load "%s" module: [%s]' % (name, e))
return
if hasattr(mod, class_type):
instance = getattr(mod, class_type)()
public_name = getattr(instance, 'name', name)
tree[public_name] = instance
cherrypy.log.error('Added module %s as %s' % (name, public_name))
def _load_classes(self, tree, path, class_type):
files = None
try:
files = os.listdir(path)
except Exception, e: # pylint: disable=broad-except
cherrypy.log.error('No modules in %s: [%s]' % (path, e))
return
for name in files:
filename = os.path.join(path, name)
self._load_class(tree, class_type, filename)
def get_plugins(self, path, class_type):
plugins = dict()
self._load_classes(plugins, path, class_type)
return plugins
class PluginLoader(object):
def __init__(self, baseobj, facility, plugin_type):
config = AdminStore().load_options(facility)
cherrypy.log('LOAD: %s\n' % repr(config))
whitelist = []
if 'global' in config:
sec = config['global']
if 'order' in sec:
whitelist = sec['order'].split(',')
if cherrypy.config.get('debug', False):
cherrypy.log('[%s] %s: %s' % (facility, whitelist, config))
if config is None:
config = dict()
p = Plugins()
(pathname, dummy) = os.path.split(inspect.getfile(baseobj))
self._plugins = {
'config': config,
'available': p.get_plugins(pathname, plugin_type),
'whitelist': whitelist,
'enabled': []
}
def get_plugin_data(self):
return self._plugins
class PluginInstaller(object):
def __init__(self, baseobj):
(pathname, dummy) = os.path.split(inspect.getfile(baseobj))
self._pathname = pathname
def get_plugins(self):
p = Plugins()
return p.get_plugins(self._pathname, 'Installer')
class PluginObject(Log):
def __init__(self):
self.name = None
self._config = None
self._options = None
self._data = AdminStore()
def get_config_desc(self, name=None):
""" The configuration description is a dictionary that provides
A description of the supported configuration options, as well
as the default configuration option values.
The key is the option name, the value is an array of 3 elements:
- description
- option type
- default value
"""
if name is None:
return self._options
opt = self._options.get(name, None)
if opt is None:
return ''
return opt[0]
def _value_to_list(self, name):
if name not in self._config:
return
value = self._config[name]
if type(value) is list:
return
vlist = [x.strip() for x in value.split(',')]
self._config[name] = vlist
def set_config(self, config):
self._config = config
if self._config is None:
return
if self._options:
for name, opt in self._options.iteritems():
if opt[1] == 'list':
self._value_to_list(name)
def get_config_value(self, name):
value = None
if self._config:
value = self._config.get(name, None)
if not value:
if self._options:
opt = self._options.get(name, None)
if opt:
value = opt[2]
if cherrypy.config.get('debug', False):
cherrypy.log('[%s] %s: %s' % (self.name, name, value))
return value
def set_config_value(self, option, value):
if not self._config:
self._config = dict()
self._config[option] = value
if self._options and option in self._options:
if self._options[option][1] == 'list':
self._value_to_list(option)
def get_plugin_config(self, facility):
return self._data.load_options(facility, self.name)
def refresh_plugin_config(self, facility):
config = self.get_plugin_config(facility)
self.set_config(config)
def save_plugin_config(self, facility, config=None):
if config is None:
config = self._config
config = config.copy()
for key, value in config.items():
if type(value) is list:
config[key] = ','.join(value)
self._data.save_options(facility, self.name, config)
def get_data(self, idval=None, name=None, value=None):
return self._data.get_data(self.name, idval=idval, name=name,
value=value)
def save_data(self, data):
self._data.save_data(self.name, data)
def new_datum(self, datum):
self._data.new_datum(self.name, datum)
def del_datum(self, idval):
self._data.del_datum(self.name, idval)
def wipe_config_values(self, facility):
self._data.delete_options(facility, self.name, None)
def wipe_data(self):
self._data.wipe_data(self.name)
|