support command line option to specify config file name
[plstackapi.git] / planetstack / planetstack / config.py
1 #!/usr/bin/python
2 import sys
3 import os
4 import time
5 import ConfigParser
6 import tempfile
7 import codecs
8 from StringIO import StringIO
9 from util.xml import Xml
10 from optparse import OptionParser
11
12 default_config = \
13 """
14 """
15
16 DEFAULT_CONFIG_FN = '/opt/planetstack/plstackapi_config'
17
18 def isbool(v):
19         return v.lower() in ("true", "false")
20
21 def str2bool(v):
22         return v.lower() in ("true", "1")
23
24 class Config:
25
26         def __init__(self, config_file=None):
27                 if (config_file==None):
28                     config_file = self.get_config_fn()
29
30                 self._files = []
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)
37
38         def get_config_fn(self):
39             parser = OptionParser(usage="%s [options]" % sys.argv[0],
40                     description="The planetstack observer")
41
42             parser.add_option("-C", "--config-file", dest="config_fn",
43                     help="name of observer config file", metavar="FILENAME", default=DEFAULT_CONFIG_FN)
44
45             (options, args) = parser.parse_args(sys.argv[1:])
46
47             return options.config_fn
48
49         def _header(self):
50                 header = """
51 DO NOT EDIT. This file was automatically generated at
52 %s from:
53
54 %s
55 """ % (time.asctime(), os.linesep.join(self._files))
56
57                 # Get rid of the surrounding newlines
58                 return header.strip().split(os.linesep)
59
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)
65                 configfile.close()
66
67
68         def load(self, filename):
69                 if filename:
70                         try:
71                                 self.config.read(filename)
72                         except ConfigParser.MissingSectionHeaderError:
73                                 if filename.endswith('.xml'):
74                                         self.load_xml(filename)
75                                 else:
76                                         self.load_shell(filename)
77                         self._files.append(filename)
78                         self.set_attributes()
79
80         def load_xml(self, filename):
81                 xml = XML(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
91                                 if not value:
92                                         value = ""
93                                 self.config.set(section_name, option_name, value)
94
95         def load_shell(self, filename):
96                 f = open(filename, 'r')
97                 for line in f:
98                         try:
99                                 if line.startswith('#'):
100                                         continue
101                                 parts = line.strip().split("=")
102                                 if len(parts) < 2:
103                                         continue
104                                 option = parts[0]
105                                 value = parts[1].replace('"', '').replace("'","")
106                                 section, var = self.locate_varname(option, strict=False)
107                                 if section and var:
108                                         self.set(section, var, value)
109                         except:
110                                 pass
111                 f.close()
112
113         def locate_varname(self, varname, strict=True):
114                 varname = varname.lower()
115                 sections = self.config.sections()
116                 section_name = ""
117                 var_name = ""
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)
125
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])
131                                 value = item[1]
132                                 if isbool(value):
133                                         value = str2bool(value)
134                                 elif value.isdigit():
135                                         value = int(value)
136                                 setattr(self, name, value)
137                                 setattr(self, name.upper(), value)
138
139
140         def verify(self, config1, config2, validate_method):
141                 return True
142
143         def validate_type(self, var_type, value):
144                 return True
145
146         @staticmethod
147         def is_xml(config_file):
148                 try:
149                         x = Xml(config_file)
150                         return True
151                 except:
152                         return False
153
154         @staticmethod
155         def is_ini(config_file):
156                 try:
157                         c = ConfigParser.ConfigParser()
158                         c.read(config_file)
159                         return True
160                 except ConfigParser.MissingSectionHeaderError:
161                         return False
162
163
164         def dump(self, sections = []):
165                 sys.stdout.write(output_python())
166
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()])
170
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()
177
178         def output_shell(self, show_comments = True, encoding = "utf-8"):
179                 """
180                 Return variables as a shell script.
181                 """
182
183                 buf = codecs.lookup(encoding)[3](StringIO())
184                 buf.writelines(["# " + line + os.linesep for line in self._header()])
185
186                 for section in self.sections():
187                         for (name,value) in self.items(section):
188                                 # bash does not have the concept of NULL
189                                 if value:
190                                         option = "%s_%s" % (section.upper(), name.upper())
191                                         if isbool(value):
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()
197
198         def output_php(self, encoding = "utf-8"):
199                 """
200                 Return variables as a PHP script.
201                 """
202
203                 buf = codecs.lookup(encoding)[3](StringIO())
204                 buf.write("<?php" + os.linesep)
205                 buf.writelines(["// " + line + os.linesep for line in self._header()])
206
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)
212                                 if value is None:
213                                         value = 'NULL'
214                                 buf.write("define('%s', %s);" % (option, value) + os.linesep)
215
216                 buf.write("?>" + os.linesep)
217
218                 return buf.getvalue()
219
220         def output_xml(self, encoding = "utf-8"):
221                 pass
222
223         def output_variables(self, encoding="utf-8"):
224                 """
225                 Return list of all variable names.
226                 """
227
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)
233
234                 return buf.getvalue()
235                 pass
236
237         def write(self, filename=None):
238                 if not filename:
239                         filename = self.filename
240                 configfile = open(filename, 'w')
241                 self.config.write(configfile)
242
243         def save(self, filename=None):
244                 self.write(filename)
245
246         def __getattr__(self, attr):
247                 return getattr(self.config, attr)
248
249 if __name__ == '__main__':
250         filename = None
251         if len(sys.argv) > 1:
252                 filename = sys.argv[1]
253                 config = Config(filename)
254         else:
255                 config = Config()
256         config.dump()