1
2
3 """
4 A simple class to read configurations
5
6 copyright (c) 2005 the icecube collaboration
7
8 @version: $Revision: $
9 @date: $Date: $
10 @author: Juan Carlos Diaz Velez <juancarlos@icecube.wisc.edu>
11 @todo: This class should just be replaced by the builtin ConfigParser.py
12
13 """
14
15 import sys,os,re
16 import logging
17
18 logger = logging.getLogger('Config')
19
22
23 self.cfgparams = {}
24 self.cfg = cfg
25
27 try:
28 configfile = open(self.cfg);
29 for s in configfile:
30 cols = re.split(r'\s*=\s*', s)
31 self.SetValue(cols[0].strip(), cols[1].strip())
32 configfile.close()
33 except Exception,e:
34 logger.debug(e)
35 logger.warning("Could not read configuration file '%s'!" % self.cfg )
36 logger.warning("File should contain name value pairs separated by a '=' sign." )
37 raise Exception,e
38
39
41 if param.upper() in self.cfgparams.keys():
42 return self.cfgparams[param.upper()]
43
45 self.cfgparams[param.upper()] = value
46
48 output = open(self.cfg,'w');
49 for c in self.cfgparams.keys():
50 output.write("%s = %s\n" % (c,self.GetValue(c)));
51 output.close();
52
55
58
60 value = None
61 while not value:
62 sys.stdout.write('Please enter value for "%s": ' % param)
63 value = sys.stdin.readline().strip()
64
65 self.SetValue(param,value)
66 if write:
67 self.Write()
68 return value
69