1 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
22 from dss.api import dss_error
23 from dss.server import dss_config
25 class DSSPluginLoader(object):
26 def __init__(self, config):
28 plugin_file = config.get_plugin()
29 location = os.path.dirname(plugin_file)
30 pluginname = os.path.basename(plugin_file).replace(".py", "")
31 sys.path.append(location)
33 self._load_plugin(pluginname, config.get_plugin_config())
34 except Exception as exp:
35 raise dss_error.Error(str(exp))
37 def _load_plugin(self, pluginname, configfile):
38 logging.info('Loading plugin %s' % pluginname)
39 fp, pathname, description = imp.find_module(pluginname)
41 pluginmodule = imp.load_module(pluginname, fp, pathname, description)
42 class_name = getattr(pluginmodule, pluginname)
43 self.plugin = class_name(configfile)
44 except Exception as exp:
45 logging.error('Failed to load plugin %s, got exp %s' % (pluginname, str(exp)))
51 def set(self, domain, name, value):
52 logging.info("Setting %s/%s to %s" % (domain, name, value))
53 self.plugin.set(domain, name, value)
55 def get(self, domain, name):
56 logging.info("Getting attribute %s/%s" % (domain, name))
57 value = self.plugin.get(domain, name)
58 logging.info("Value of %s/%s is %s" % (domain, name, value))
61 def get_domain(self, domain):
62 logging.info("Getting the attributes of domain %s" % domain)
63 attrs = self.plugin.get_domain(domain)
66 def get_domains(self):
67 logging.info("Getting the domains")
68 domains = self.plugin.get_domains()
71 def delete(self, domain, name):
72 logging.info("Deleting %s/%s" % (domain, name))
73 self.plugin.delete(domain, name)
75 def delete_domain(self, domain):
76 logging.info("Delete domain %s" % domain)
77 self.plugin.delete_domain(domain)
79 if __name__ == '__main__':
80 pl = DSSPluginLoader("configfile")
81 pl.set("domain1", "name1", "value1")