8 from StringIO import StringIO
9 from sfa.util.xml import XML
17 def __init__(self, config_file='/etc/sfa/sfa_config'):
19 self.config_path = os.path.dirname(config_file)
20 self.config = ConfigParser.ConfigParser()
21 self.filename = config_file
22 if not os.path.isfile(self.filename):
23 self.create(self.filename)
24 self.load(self.filename)
29 DO NOT EDIT. This file was automatically generated at
33 """ % (time.asctime(), os.linesep.join(self._files))
35 # Get rid of the surrounding newlines
36 return header.strip().split(os.linesep)
38 def create(self, filename):
39 if not os.path.exists(os.path.dirname(filename)):
40 os.makedirs(os.path.dirname(filename))
41 configfile = open(filename, 'w')
42 configfile.write(default_config)
46 def load(self, filename):
49 self.config.read(filename)
51 if filename.endswith('.xml'):
52 self.load_xml(filename)
54 self.load_shell(filename)
55 self._files.append(filename)
58 def load_xml(self, filename):
60 categories = xml.xpath('//configuration/variables/category')
61 for category in categories:
62 section_name = category.get('id')
63 if not self.config.has_section(section_name):
64 self.config.add_section(section_name)
65 options = category.xpath('./variablelist/variable')
66 for option in options:
67 option_name = option.get('id')
68 value = option.xpath('./value')[0].text
71 self.config.set(section_name, option_name, value)
73 def load_shell(self, filename):
74 f = open(filename, 'r')
77 if line.startswith('#'):
79 parts = line.strip().split("=")
83 value = parts[1].replace('"', '').replace("'","")
84 section, var = self.locate_varname(option, strict=False)
86 self.set(section, var, value)
91 def locate_varname(self, varname, strict=True):
92 varname = varname.lower()
93 sections = self.config.sections()
96 for section in sections:
97 if varname.startswith(section.lower()) and len(section) > len(section_name):
98 section_name = section.lower()
99 var_name = varname.replace(section_name, "")[1:]
100 if strict and not self.config.has_option(section_name, var_name):
101 raise ConfigParser.NoOptionError(var_name, section_name)
102 return (section_name, var_name)
104 def set_attributes(self):
105 sections = self.config.sections()
106 for section in sections:
107 for item in self.config.items(section):
108 name = "%s_%s" % (section, item[0])
110 setattr(self, name, value)
111 setattr(self, name.upper(), value)
114 def verify(self, config1, config2, validate_method):
117 def validate_type(self, var_type, value):
120 def dump(self, sections = []):
121 sys.stdout.write(output_python())
123 def output_python(self, encoding = "utf-8"):
124 buf = codecs.lookup(encoding)[3](StringIO())
125 buf.writelines(["# " + line + os.linesep for line in self._header()])
127 for section in self.sections():
128 buf.write("[%s]%s" % (section, os.linesep))
129 for (name,value) in self.items(section):
130 buf.write("%s=%s%s" % (name,value,os.linesep))
131 buf.write(os.linesep)
132 return buf.getvalue()
134 def output_shell(self, show_comments = True, encoding = "utf-8"):
136 Return variables as a shell script.
139 buf = codecs.lookup(encoding)[3](StringIO())
140 buf.writelines(["# " + line + os.linesep for line in self._header()])
142 for section in self.sections():
143 for (name,value) in self.items(section):
144 # bash does not have the concept of NULL
146 option = "%s_%s" % (section.upper(), name.upper())
147 if not value.isdigit() and not bool(value):
148 value = "'%s'" % value
149 buf.write(option + "=" + value + os.linesep)
150 return buf.getvalue()
152 def output_php(selfi, encoding = "utf-8"):
154 Return variables as a PHP script.
157 buf = codecs.lookup(encoding)[3](StringIO())
158 buf.write("<?php" + os.linesep)
159 buf.writelines(["// " + line + os.linesep for line in self._header()])
161 for section in self.sections():
162 for (name,value) in self.items(section):
163 option = "%s_%s" % (section, name)
164 buf.write(os.linesep)
165 buf.write("// " + option + os.linesep)
168 buf.write("define('%s', %s);" % (option, value) + os.linesep)
170 buf.write("?>" + os.linesep)
172 return buf.getvalue()
174 def output_xml(self, encoding = "utf-8"):
177 def output_variables(self):
179 Return list of all variable names.
182 buf = codecs.lookup(encoding)[3](StringIO())
183 for section in self.sections():
184 for (name,value) in self.items(section):
185 option = "%s_%s" % (section,name)
186 buf.write(option + os.linesep)
188 return buf.getvalue()
191 def write(self, filename=None):
193 filename = self.filename
194 configfile = open(filename, 'w')
195 self.config.write(configfile)
197 def save(self, filename=None):
201 def get_trustedroots_dir(self):
202 return self.config_path + os.sep + 'trusted_roots'
204 def get_openflow_aggrMgr_info(self):
205 aggr_mgr_ip = 'localhost'
206 if (hasattr(self,'openflow_aggregate_manager_ip')):
207 aggr_mgr_ip = self.OPENFLOW_AGGREGATE_MANAGER_IP
210 if (hasattr(self,'openflow_aggregate_manager_port')):
211 aggr_mgr_port = self.OPENFLOW_AGGREGATE_MANAGER_PORT
213 return (aggr_mgr_ip,aggr_mgr_port)
215 def get_interface_hrn(self):
216 if (hasattr(self,'sfa_interface_hrn')):
217 return self.SFA_INTERFACE_HRN
221 def __getattr__(self, attr):
222 return getattr(self.config, attr)
224 if __name__ == '__main__':
226 if len(sys.argv) > 1:
227 filename = sys.argv[1]
228 config = Config(filename)