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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 from dss.api import dss_plugin
17 from dss.api import dss_error
23 class dssfile(dss_plugin.DSSPlugin):
25 Read the ini file. The structure is as follows
27 directory = <directory>
29 def __init__(self, config_file):
30 super(dssfile, self).__init__(config_file)
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)
39 if not os.path.isdir(self.directory):
40 raise dss_error.Error('%s is not a valid directory name' % self.directory)
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)
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)
53 def write_file(self, domain, lines):
54 filename = self.directory + '/' + domain
56 with open(filename, 'w') as f:
59 except Exception as exp:
60 raise dss_error.Error('Failed with error %s' % exp)
62 def get(self, domain, name):
63 lines = self.read_file(domain)
68 part1 = line[index+1:]
71 except Exception as exp:
72 raise dss_error.Error('Failed with error %s' % exp)
74 raise dss_error.Error('Key not found %s/%s' % (domain, name))
76 def get_domain(self, domain):
77 lines = self.read_file(domain)
82 part1 = line[index+1:]
86 def get_domains(self):
89 entries = os.listdir(self.directory)
91 filename = self.directory + '/' + entry
92 if os.path.isfile(filename):
95 raise dss_error.Error('No domains found')
98 except Exception as exp:
99 raise dss_error.Error('Failed with error %s' % exp)
101 def set(self, domain, name, value):
102 filename = self.directory + '/' + domain
104 if os.path.isfile(filename):
105 lines = self.read_file(domain)
108 for index, line in enumerate(lines):
114 lines[index] = name+'='+value
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)
122 def delete(self, domain, name):
123 lines = self.read_file(domain)
126 for index, line in enumerate(lines):
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)
139 if found_index == -1:
140 raise dss_error.Error('%s/%s not found' % (domain, name))
142 def delete_domain(self, domain):
143 filename = self.directory + '/' + domain
144 if os.path.isfile(filename):
147 raise dss_error.Error('Invalid domain %s' % domain)