8 from StringIO import StringIO
9 from util.xml import Xml
10 from optparse import OptionParser
16 DEFAULT_CONFIG_FN = '/opt/planetstack/plstackapi_config'
19 return v.lower() in ("true", "false")
22 return v.lower() in ("true", "1")
26 def __init__(self, config_file=None):
27 if (config_file==None):
28 config_file = self.get_config_fn()
31 self.config_path = os.path.dirname(config_file)
32 self.config = ConfigParser.ConfigParser()
33 self.filename = config_file
34 if not os.path.isfile(self.filename):
35 self.create(self.filename)
36 self.load(self.filename)
38 def get_config_fn(self):
39 parser = OptionParser(usage="%s [options]" % sys.argv[0],
40 description="The planetstack observer")
42 parser.add_option("-C", "--config-file", dest="config_fn",
43 help="name of observer config file", metavar="FILENAME", default=DEFAULT_CONFIG_FN)
45 (options, args) = parser.parse_args(sys.argv[1:])
47 return options.config_fn
51 DO NOT EDIT. This file was automatically generated at
55 """ % (time.asctime(), os.linesep.join(self._files))
57 # Get rid of the surrounding newlines
58 return header.strip().split(os.linesep)
60 def create(self, filename):
61 if not os.path.exists(os.path.dirname(filename)):
62 os.makedirs(os.path.dirname(filename))
63 configfile = open(filename, 'w')
64 configfile.write(default_config)
68 def load(self, filename):
71 self.config.read(filename)
72 except ConfigParser.MissingSectionHeaderError:
73 if filename.endswith('.xml'):
74 self.load_xml(filename)
76 self.load_shell(filename)
77 self._files.append(filename)
80 def load_xml(self, filename):
82 categories = xml.xpath('//configuration/variables/category')
83 for category in categories:
84 section_name = category.get('id')
85 if not self.config.has_section(section_name):
86 self.config.add_section(section_name)
87 options = category.xpath('./variablelist/variable')
88 for option in options:
89 option_name = option.get('id')
90 value = option.xpath('./value')[0].text
93 self.config.set(section_name, option_name, value)
95 def load_shell(self, filename):
96 f = open(filename, 'r')
99 if line.startswith('#'):
101 parts = line.strip().split("=")
105 value = parts[1].replace('"', '').replace("'","")
106 section, var = self.locate_varname(option, strict=False)
108 self.set(section, var, value)
113 def locate_varname(self, varname, strict=True):
114 varname = varname.lower()
115 sections = self.config.sections()
118 for section in sections:
119 if varname.startswith(section.lower()) and len(section) > len(section_name):
120 section_name = section.lower()
121 var_name = varname.replace(section_name, "")[1:]
122 if strict and not self.config.has_option(section_name, var_name):
123 raise ConfigParser.NoOptionError(var_name, section_name)
124 return (section_name, var_name)
126 def set_attributes(self):
127 sections = self.config.sections()
128 for section in sections:
129 for item in self.config.items(section):
130 name = "%s_%s" % (section, item[0])
133 value = str2bool(value)
134 elif value.isdigit():
136 setattr(self, name, value)
137 setattr(self, name.upper(), value)
140 def verify(self, config1, config2, validate_method):
143 def validate_type(self, var_type, value):
147 def is_xml(config_file):
155 def is_ini(config_file):
157 c = ConfigParser.ConfigParser()
160 except ConfigParser.MissingSectionHeaderError:
164 def dump(self, sections = []):
165 sys.stdout.write(output_python())
167 def output_python(self, encoding = "utf-8"):
168 buf = codecs.lookup(encoding)[3](StringIO())
169 buf.writelines(["# " + line + os.linesep for line in self._header()])
171 for section in self.sections():
172 buf.write("[%s]%s" % (section, os.linesep))
173 for (name,value) in self.items(section):
174 buf.write("%s=%s%s" % (name,value,os.linesep))
175 buf.write(os.linesep)
176 return buf.getvalue()
178 def output_shell(self, show_comments = True, encoding = "utf-8"):
180 Return variables as a shell script.
183 buf = codecs.lookup(encoding)[3](StringIO())
184 buf.writelines(["# " + line + os.linesep for line in self._header()])
186 for section in self.sections():
187 for (name,value) in self.items(section):
188 # bash does not have the concept of NULL
190 option = "%s_%s" % (section.upper(), name.upper())
192 value = str(str2bool(value))
193 elif not value.isdigit():
194 value = '"%s"' % value
195 buf.write(option + "=" + value + os.linesep)
196 return buf.getvalue()
198 def output_php(self, encoding = "utf-8"):
200 Return variables as a PHP script.
203 buf = codecs.lookup(encoding)[3](StringIO())
204 buf.write("<?php" + os.linesep)
205 buf.writelines(["// " + line + os.linesep for line in self._header()])
207 for section in self.sections():
208 for (name,value) in self.items(section):
209 option = "%s_%s" % (section, name)
210 buf.write(os.linesep)
211 buf.write("// " + option + os.linesep)
214 buf.write("define('%s', %s);" % (option, value) + os.linesep)
216 buf.write("?>" + os.linesep)
218 return buf.getvalue()
220 def output_xml(self, encoding = "utf-8"):
223 def output_variables(self, encoding="utf-8"):
225 Return list of all variable names.
228 buf = codecs.lookup(encoding)[3](StringIO())
229 for section in self.sections():
230 for (name,value) in self.items(section):
231 option = "%s_%s" % (section,name)
232 buf.write(option + os.linesep)
234 return buf.getvalue()
237 def write(self, filename=None):
239 filename = self.filename
240 configfile = open(filename, 'w')
241 self.config.write(configfile)
243 def save(self, filename=None):
246 def __getattr__(self, attr):
247 return getattr(self.config, attr)
249 if __name__ == '__main__':
251 if len(sys.argv) > 1:
252 filename = sys.argv[1]
253 config = Config(filename)