source: ndgCommon/trunk/ndg/common/src/models/myconfig.py @ 4793

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/ndgCommon/trunk/ndg/common/src/models/myconfig.py@5136
Revision 4793, 1.6 KB checked in by cbyrom, 11 years ago (diff)

Checking in initial codebase for ndgUtils restructure.

Line 
1'''
2Config class to allow graceful handling of non-existent config data
3@author: ??
4'''
5import ConfigParser, os
6
7
8class myConfig:
9   
10   ''' Handle missing sections and variables in a config file a bit gracefully. Also
11   instantiates a logger if necessary '''
12   
13   def __init__(self,configfile,logName='NDGLOG'):
14       self.config=ConfigParser.ConfigParser()
15       if not os.path.exists(configfile): 
16           raise ValueError("Config file [%s] doesn't exist in [%s]"%(configfile,os.getcwd()))
17       self.config.read(configfile)
18       logfile=self.get('logging','debugLog',None)
19       # move to a wsgi logger ... safer I think in a multithread environment
20       #
21       #self.logfile=None #deprecated
22       self.logger=None
23       self.logfile=logfile
24       #
25       if logfile is not None:
26           #logger=logging.getLogger(logName)
27           #handler=logging.FileHandler(logfile)
28           #formatter=logging.Formatter('%(asctime)s %(levelname)s %(message)s')
29           #handler.setFormatter(formatter)
30           #logger.addHandler(handler)
31           #logger.setLevel(logging.INFO)
32           #self.logger=logger
33           self.logger=None
34   
35   def get(self,section,key,default=None):
36       ''' Return a config file value for key frm section '''
37       try:
38           return self.config.get(section,key)
39       except:
40           return default
41   
42   def log(self,string):
43       ''' Log some debug information '''
44       if self.logger is None: return
45       if string is not None:
46          self.logger.info(string)
47       else:
48          self.logger.info('empty log entry')
49         
50   def getLog(self):
51       return self.logger
Note: See TracBrowser for help on using the repository browser.