Initial commit
[ta/distributed-state-server.git] / plugins / dssfile.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 #
15
16 from dss.api import dss_plugin
17 from dss.api import dss_error
18
19 import ConfigParser
20 import logging
21 import os
22
23 class dssfile(dss_plugin.DSSPlugin):
24     """
25     Read the ini file. The structure is as follows
26     [default]
27     directory = <directory>
28     """
29     def __init__(self, config_file):
30         super(dssfile, self).__init__(config_file)
31         self.directory = None
32         try:
33             config = ConfigParser.ConfigParser()
34             config.read([config_file])
35             self.directory = config.get('default', 'directory')
36         except Exception as exp:
37             raise dss_error.Error('Failed to parse configuration, got error %s' % exp)
38
39         if not os.path.isdir(self.directory):
40             raise dss_error.Error('%s is not a valid directory name' % self.directory)
41
42     def read_file(self, domain):
43         filename = self.directory + '/' + domain
44         if not os.path.isfile(filename):
45             raise dss_error.Error('Key not found %s' % domain)
46
47         try:
48             with open(filename) as f:
49                 return f.read().splitlines()
50         except Exception as exp:
51             raise dss_error.Error('Failed with error %s' % exp)
52
53     def write_file(self, domain, lines):
54         filename = self.directory + '/' + domain
55         try:
56             with open(filename, 'w') as f:
57                 for line in lines:
58                     f.write(line+'\n')
59         except Exception as exp:
60             raise dss_error.Error('Failed with error %s' % exp)
61
62     def get(self, domain, name):
63         lines = self.read_file(domain)
64         try:
65             for line in lines:
66                 index=line.find('=')
67                 part0 = line[:index]
68                 part1 = line[index+1:]
69                 if name == part0:
70                     return part1
71         except Exception as exp:
72             raise dss_error.Error('Failed with error %s' % exp)
73
74         raise dss_error.Error('Key not found %s/%s' % (domain, name))
75
76     def get_domain(self, domain):
77         lines = self.read_file(domain)
78         ret = {}
79         for line in lines:
80             index=line.find('=')
81             part0 = line[:index]
82             part1 = line[index+1:]
83             ret[part0] = part1
84         return ret
85
86     def get_domains(self):
87         ret = []
88         try:
89             entries = os.listdir(self.directory)
90             for entry in entries:
91                 filename = self.directory + '/' + entry
92                 if os.path.isfile(filename):
93                     ret.append(entry)
94             if not ret:
95                 raise dss_error.Error('No domains found')
96
97             return ret
98         except Exception as exp:
99             raise dss_error.Error('Failed with error %s' % exp)
100
101     def set(self, domain, name, value):
102         filename = self.directory + '/' + domain
103         lines = []
104         if os.path.isfile(filename):
105             lines = self.read_file(domain)
106         try:
107             found = False
108             for index, line in enumerate(lines):
109                 i=line.find('=')
110                 part0 = line[:i]
111                 part1 = line[i+1:]
112                 if part0 == name:
113                     found = True
114                     lines[index] = name+'='+value
115                     break
116             if not found:
117                 lines.append(name+'='+value)
118             self.write_file(domain, lines)
119         except Exception as exp:
120             raise dss_error.Error('Failed with error %s' % exp)
121
122     def delete(self, domain, name):
123         lines = self.read_file(domain)
124         found_index = -1
125         try:
126             for index, line in enumerate(lines):
127                 i=line.find('=')
128                 part0 = line[:i]
129                 part1 = line[i+1:]
130                 if part0 == name:
131                     found_index = index
132                     break
133             if found_index != -1:
134                 del lines[found_index]
135                 self.write_file(domain, lines)
136         except Exception as exp:
137             raise dss_error.Error('Failed with error %s' % exp)
138
139         if found_index == -1:
140             raise dss_error.Error('%s/%s not found' % (domain, name))
141
142     def delete_domain(self, domain):
143         filename = self.directory + '/' + domain
144         if os.path.isfile(filename):
145             os.remove(filename)
146         else:
147             raise dss_error.Error('Invalid domain %s' % domain)