8 from StringIO import StringIO
9 from util.xml import Xml
15 DEFAULT_CONFIG_FN = '/opt/planetstack/plstackapi_config'
18 return v.lower() in ("true", "false")
21 return v.lower() in ("true", "1")
25 def __init__(self, config_file=None):
26 if (config_file==None):
27 config_file = self.get_config_fn()
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)
37 def get_config_fn(self):
38 # Look for "-C <something>" to get the
39 # name of the config file. Using a real OptionParser here is
40 # problematic as it will throw 'no such option' errors for options
41 # that it does not understand.
49 return DEFAULT_CONFIG_FN
53 DO NOT EDIT. This file was automatically generated at
57 """ % (time.asctime(), os.linesep.join(self._files))
59 # Get rid of the surrounding newlines
60 return header.strip().split(os.linesep)
62 def create(self, filename):
63 if not os.path.exists(os.path.dirname(filename)):
64 os.makedirs(os.path.dirname(filename))
65 configfile = open(filename, 'w')
66 configfile.write(default_config)
70 def load(self, filename):
73 self.config.read(filename)
74 except ConfigParser.MissingSectionHeaderError:
75 if filename.endswith('.xml'):
76 self.load_xml(filename)
78 self.load_shell(filename)
79 self._files.append(filename)
82 def load_xml(self, filename):
84 categories = xml.xpath('//configuration/variables/category')
85 for category in categories:
86 section_name = category.get('id')
87 if not self.config.has_section(section_name):
88 self.config.add_section(section_name)
89 options = category.xpath('./variablelist/variable')
90 for option in options:
91 option_name = option.get('id')
92 value = option.xpath('./value')[0].text
95 self.config.set(section_name, option_name, value)
97 def load_shell(self, filename):
98 f = open(filename, 'r')
101 if line.startswith('#'):
103 parts = line.strip().split("=")
107 value = parts[1].replace('"', '').replace("'","")
108 section, var = self.locate_varname(option, strict=False)
110 self.set(section, var, value)
115 def locate_varname(self, varname, strict=True):
116 varname = varname.lower()
117 sections = self.config.sections()
120 for section in sections:
121 if varname.startswith(section.lower()) and len(section) > len(section_name):
122 section_name = section.lower()
123 var_name = varname.replace(section_name, "")[1:]
124 if strict and not self.config.has_option(section_name, var_name):
125 raise ConfigParser.NoOptionError(var_name, section_name)
126 return (section_name, var_name)
128 def set_attributes(self):
129 sections = self.config.sections()
130 for section in sections:
131 for item in self.config.items(section):
132 name = "%s_%s" % (section, item[0])
135 value = str2bool(value)
136 elif value.isdigit():
138 setattr(self, name, value)
139 setattr(self, name.upper(), value)
142 def verify(self, config1, config2, validate_method):
145 def validate_type(self, var_type, value):
149 def is_xml(config_file):
157 def is_ini(config_file):
159 c = ConfigParser.ConfigParser()
162 except ConfigParser.MissingSectionHeaderError:
166 def dump(self, sections = []):
167 sys.stdout.write(output_python())
169 def output_python(self, encoding = "utf-8"):
170 buf = codecs.lookup(encoding)[3](StringIO())
171 buf.writelines(["# " + line + os.linesep for line in self._header()])
173 for section in self.sections():
174 buf.write("[%s]%s" % (section, os.linesep))
175 for (name,value) in self.items(section):
176 buf.write("%s=%s%s" % (name,value,os.linesep))
177 buf.write(os.linesep)
178 return buf.getvalue()
180 def output_shell(self, show_comments = True, encoding = "utf-8"):
182 Return variables as a shell script.
185 buf = codecs.lookup(encoding)[3](StringIO())
186 buf.writelines(["# " + line + os.linesep for line in self._header()])
188 for section in self.sections():
189 for (name,value) in self.items(section):
190 # bash does not have the concept of NULL
192 option = "%s_%s" % (section.upper(), name.upper())
194 value = str(str2bool(value))
195 elif not value.isdigit():
196 value = '"%s"' % value
197 buf.write(option + "=" + value + os.linesep)
198 return buf.getvalue()
200 def output_php(self, encoding = "utf-8"):
202 Return variables as a PHP script.
205 buf = codecs.lookup(encoding)[3](StringIO())
206 buf.write("<?php" + os.linesep)
207 buf.writelines(["// " + line + os.linesep for line in self._header()])
209 for section in self.sections():
210 for (name,value) in self.items(section):
211 option = "%s_%s" % (section, name)
212 buf.write(os.linesep)
213 buf.write("// " + option + os.linesep)
216 buf.write("define('%s', %s);" % (option, value) + os.linesep)
218 buf.write("?>" + os.linesep)
220 return buf.getvalue()
222 def output_xml(self, encoding = "utf-8"):
225 def output_variables(self, encoding="utf-8"):
227 Return list of all variable names.
230 buf = codecs.lookup(encoding)[3](StringIO())
231 for section in self.sections():
232 for (name,value) in self.items(section):
233 option = "%s_%s" % (section,name)
234 buf.write(option + os.linesep)
236 return buf.getvalue()
239 def write(self, filename=None):
241 filename = self.filename
242 configfile = open(filename, 'w')
243 self.config.write(configfile)
245 def save(self, filename=None):
248 def __getattr__(self, attr):
249 return getattr(self.config, attr)
251 if __name__ == '__main__':
253 if len(sys.argv) > 1:
254 filename = sys.argv[1]
255 config = Config(filename)