Package iceprod :: Package core :: Module configuration
[hide private]
[frames] | no frames]

Source Code for Module iceprod.core.configuration

 1  #! /usr/bin/env python 
 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   
20 -class Config:
21 - def __init__(self,cfg=None):
22 23 self.cfgparams = {} 24 self.cfg = cfg
25
26 - def Read(self):
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) # Print actuall exception message for debugging 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
40 - def GetValue(self,param):
41 if param.upper() in self.cfgparams.keys(): 42 return self.cfgparams[param.upper()]
43
44 - def SetValue(self,param,value):
45 self.cfgparams[param.upper()] = value
46
47 - def Write(self):
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
53 - def GetPath(self):
54 return self.cfg
55
56 - def SetPath(self,cfg):
57 self.cfg = cfg
58
59 - def PromptForValue(self,param,write=1):
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