Initial commit
[ta/config-manager.git] / cmframework / src / cmframework / utils / cmstatehandler.py
1 # Copyright 2019 Nokia
2
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 from __future__ import print_function
15 import logging
16 from cmframework.apis import cmerror
17
18
19 class CMStateHandler(object):
20     def __init__(self, plugin_path, **kw):
21         try:
22             logging.debug('Loading backend plugin from %s', plugin_path)
23             # Separate class path and module name
24             parts = plugin_path.rsplit('.', 1)
25             self.plugin_path = parts[0]
26             class_name = parts[1]
27             logging.debug('Importing %s from %s', class_name, self.plugin_path)
28             module = __import__(self.plugin_path, fromlist=[self.plugin_path])
29             classobj = getattr(module, class_name)
30             logging.debug('Constructing state handler with args %r', kw)
31             self.plugin = classobj(**kw)
32         except ImportError as exp1:
33             raise cmerror.CMError(str(exp1))
34         except Exception as exp2:
35             raise cmerror.CMError(str(exp2))
36
37     def get(self, domain, name):
38         logging.debug('get called for %s %s', domain, name)
39         return self.plugin.get(domain, name)
40
41     def get_domain(self, domain):
42         logging.debug('get_domain called for %s', domain)
43         return self.plugin.get_domain(domain)
44
45     def set(self, domain, name, value):
46         logging.debug('set called for setting %s %s=%s', domain, name, value)
47         return self.plugin.set(domain, name, value)
48
49     def get_domains(self):
50         logging.debug('get_domains called')
51         return self.plugin.get_domains()
52
53     def delete(self, domain, name):
54         logging.debug('delete called for %s %s', domain, name)
55         return self.plugin.delete(domain, name)
56
57     def delete_domain(self, domain):
58         logging.debug('delete_domain called for %s', domain)
59         return self.plugin.delete_domain(domain)