3 # pylint: disable=c0111, c0103, r0904, r1705, r0201
9 from io import StringIO
10 from sfa.util.xml import XML
19 return val.lower() in ("true", "false")
23 return v.lower() in ("true", "1")
28 def __init__(self, config_file='/etc/sfa/sfa_config'):
30 self.config_path = os.path.dirname(config_file)
31 self.config = configparser.ConfigParser()
32 self.filename = config_file
33 if not os.path.isfile(self.filename):
34 self.create(self.filename)
35 self.load(self.filename)
39 DO NOT EDIT. This file was automatically generated at
43 """ % (time.asctime(), os.linesep.join(self._files))
45 # Get rid of the surrounding newlines
46 return header.strip().split(os.linesep)
48 def create(self, filename):
49 if not os.path.exists(os.path.dirname(filename)):
50 os.makedirs(os.path.dirname(filename))
51 configfile = open(filename, 'w')
52 configfile.write(default_config)
55 def load(self, filename):
58 self.config.read(filename)
59 except configparser.MissingSectionHeaderError:
60 if filename.endswith('.xml'):
61 self.load_xml(filename)
63 self.load_shell(filename)
64 self._files.append(filename)
67 def load_xml(self, filename):
69 categories = xml.xpath('//configuration/variables/category')
70 for category in categories:
71 section_name = category.get('id')
72 if not self.config.has_section(section_name):
73 self.config.add_section(section_name)
74 options = category.xpath('./variablelist/variable')
75 for option in options:
76 option_name = option.get('id')
77 value = option.xpath('./value')[0].text
80 self.config.set(section_name, option_name, value)
82 def load_shell(self, filename):
83 f = open(filename, 'r')
86 if line.startswith('#'):
88 parts = line.strip().split("=")
92 value = parts[1].replace('"', '').replace("'", "")
93 section, var = self.locate_varname(option, strict=False)
95 self.set(section, var, value)
100 def locate_varname(self, varname, strict=True):
101 varname = varname.lower()
102 sections = self.config.sections()
105 for section in sections:
106 if varname.startswith(section.lower()) and len(section) > len(section_name):
107 section_name = section.lower()
108 var_name = varname.replace(section_name, "")[1:]
109 if strict and not self.config.has_option(section_name, var_name):
110 raise configparser.NoOptionError(var_name, section_name)
111 return (section_name, var_name)
113 def set_attributes(self):
114 sections = self.config.sections()
115 for section in sections:
116 for item in self.config.items(section):
117 name = "%s_%s" % (section, item[0])
120 value = str2bool(value)
121 elif value.isdigit():
123 setattr(self, name, value)
124 setattr(self, name.upper(), value)
128 Return all variables.
132 variables = { 'category_id': (category, variablelist) }
134 category = { 'id': "category_identifier",
135 'name': "Category name",
136 'description': "Category description" }
138 variablelist = { 'variable_id': variable }
140 variable = { 'id': "variable_identifier",
141 'type': "variable_type",
142 'value': "variable_value",
143 'name': "Variable name",
144 'description': "Variable description" }
148 for section in self.config.sections():
152 'description': section,
155 for item in self.config.items(section):
157 name = "%s_%s" % (section, var_name)
161 elif value.isdigit():
172 variable_list[name] = variable
173 variables[section] = (category, variable_list)
176 def verify(self, _config1, _config2, _validate_method):
179 def validate_type(self, _var_type, _value):
183 def is_xml(config_file):
185 _x = Xml(config_file)
191 def is_ini(config_file):
193 c = configparser.ConfigParser()
196 except configparser.MissingSectionHeaderError:
199 def dump(self, sections=None):
202 sys.stdout.write(self.output_python())
204 def output_python(self):
206 buf.writelines(["# " + line + os.linesep for line in self._header()])
208 for section in self.sections():
209 buf.write("[%s]%s" % (section, os.linesep))
210 for (name, value) in self.items(section):
211 buf.write("%s=%s%s" % (name, value, os.linesep))
212 buf.write(os.linesep)
213 return buf.getvalue()
215 def output_shell(self):
217 Return variables as a shell script.
221 buf.writelines(["# " + line + os.linesep for line in self._header()])
223 for section in self.sections():
224 for (name, value) in self.items(section):
225 # bash does not have the concept of NULL
227 option = "%s_%s" % (section.upper(), name.upper())
229 value = str(str2bool(value))
230 elif not value.isdigit():
231 value = '"%s"' % value
232 buf.write(option + "=" + value + os.linesep)
233 return buf.getvalue()
235 def output_php(selfi):
237 Return variables as a PHP script.
241 buf.write("<?php" + os.linesep)
242 buf.writelines(["// " + line + os.linesep for line in self._header()])
244 for section in self.sections():
245 for (name, value) in self.items(section):
246 option = "%s_%s" % (section, name)
247 buf.write(os.linesep)
248 buf.write("// " + option + os.linesep)
251 buf.write("define('%s', %s);" % (option, value) + os.linesep)
253 buf.write("?>" + os.linesep)
255 return buf.getvalue()
257 def output_xml(self):
260 def output_variables(self):
262 Return list of all variable names.
266 for section in self.sections():
267 for (name, _value) in self.items(section):
268 option = "%s_%s" % (section, name)
269 buf.write(option + os.linesep)
271 return buf.getvalue()
273 def write(self, filename=None):
275 filename = self.filename
276 configfile = open(filename, 'w')
277 self.config.write(configfile)
279 def save(self, filename=None):
282 def get_trustedroots_dir(self):
283 return self.config_path + os.sep + 'trusted_roots'
285 def get_openflow_aggrMgr_info(self):
286 aggr_mgr_ip = 'localhost'
287 if hasattr(self, 'openflow_aggregate_manager_ip'):
288 aggr_mgr_ip = self.OPENFLOW_AGGREGATE_MANAGER_IP
291 if hasattr(self, 'openflow_aggregate_manager_port'):
292 aggr_mgr_port = self.OPENFLOW_AGGREGATE_MANAGER_PORT
294 return (aggr_mgr_ip, aggr_mgr_port)
296 def get_interface_hrn(self):
297 if hasattr(self, 'sfa_interface_hrn'):
298 return self.SFA_INTERFACE_HRN
302 def __getattr__(self, attr):
303 return getattr(self.config, attr)
305 if __name__ == '__main__':
308 if len(sys.argv) > 1:
309 filename = sys.argv[1]
310 config = Config(filename)