00001
00002 class GBSConfig:
00003 """ The class responsible for loading configuration data at program start
00004
00005 It first reads it's internal configuration file and then looks for external ones.
00006 After that it's just a dictionary"""
00007
00008 def __init__(self):
00009 """ Search for the configuration files (internal and external) and assemble a dictionary"""
00010
00011 self.__config = {}
00012
00013 import os
00014
00015
00016 rcFile = os.path.expandvars("$GBS_HOME/python/.gbsrc")
00017 if os.path.isfile(rcFile):
00018 self.__ReadConfigFile(rcFile)
00019 else :
00020 print "ERROR: Unable to locate configuration file " + str(rcFile)
00021 raise ConfigurationMissingError
00022
00023
00024 rcFile = os.path.expanduser("~/.gbsrc")
00025 if os.path.isfile(rcFile):
00026 self.__ReadConfigFile(rcFile)
00027
00028 rcFile = os.path.expandvars("$GBS_CONFIG_PATH")
00029 if os.path.isfile(rcFile):
00030 self.__ReadConfigFile(rcFile)
00031
00032 def GetValue(self,key):
00033 if self.__config.has_key(key): return self.__config[key]
00034 return ""
00035
00036 def __ReadConfigFile(self,rcFile):
00037 """ Read configuration files into dictionary. """
00038
00039
00040 print "GBSConfig: Reading configuration from " + str(rcFile) + " ... ",
00041 f=open(rcFile)
00042
00043 import re
00044 num_line = 0
00045 for line in f:
00046 num_line = num_line + 1
00047
00048 if re.search(r"^\s*$",line): continue
00049 if re.search(r"^\s*#",line): continue
00050
00051 mo = re.search(r"^(\S+)\s+(.+?)\s*$",line)
00052 if mo :
00053 (key,value) = mo.groups()
00054 self.__config[key] = value
00055 else :
00056 print "Bad data on line " + str(num_line) + " of file " + str(rcFile),
00057 print ":" + str(line)
00058 print str(num_line) + " lines"
00059
00060
00061
00062 __config = GBSConfig()
00063
00064 def GetConfig(): return __config
00065
00066 def GetConfigValue(key): return GetConfig().GetValue(key)