16 |
# write to the Free Software Foundation, Inc., 59 Temple Place |
# write to the Free Software Foundation, Inc., 59 Temple Place |
17 |
# - Suite 330, Boston, MA 02111-1307, USA. |
# - Suite 330, Boston, MA 02111-1307, USA. |
18 |
# |
# |
19 |
# Copyright 2000-2002 Free Software Foundation |
# Copyright 2000-2003 Free Software Foundation |
20 |
# |
# |
21 |
# FILE: |
# FILE: |
22 |
# GConfig.py |
# GConfig.py |
53 |
__builtin__.__dict__['gConfig'] = self.gConfig |
__builtin__.__dict__['gConfig'] = self.gConfig |
54 |
__builtin__.__dict__['gConfigDict'] = self.gConfigDict |
__builtin__.__dict__['gConfigDict'] = self.gConfigDict |
55 |
|
|
|
|
|
56 |
def registerAlias(self, name, section): |
def registerAlias(self, name, section): |
57 |
alias = GConfigAlias(self.gConfig, section) |
alias = GConfigAlias(self.gConfig, section) |
58 |
import __builtin__ |
import __builtin__ |
59 |
__builtin__.__dict__[name] = alias.gConfig |
__builtin__.__dict__[name] = alias.gConfig |
60 |
|
|
61 |
|
|
62 |
|
# |
63 |
|
# loadApplicationConfig |
64 |
|
# |
65 |
|
# Loads the specified file only once. |
66 |
|
# Subsequent calls setup the defaults for any missing values |
67 |
|
# |
68 |
def loadApplicationConfig(self, configFilename="gnue.conf", homeConfigDir=".gnue", section="DEFAULT", defaults = None): |
def loadApplicationConfig(self, configFilename="gnue.conf", homeConfigDir=".gnue", section="DEFAULT", defaults = None): |
69 |
|
|
70 |
GDebug.printMesg(1,'Reading configuration info from %s section %s' %(configFilename,section)) |
GDebug.printMesg(1,'Reading configuration info from %s section %s' %(configFilename,section)) |
71 |
|
|
72 |
# Create parser if it doesn't exist |
# |
73 |
readFiles = 0 |
# Create parser and populate it if it doesn't exist |
74 |
|
# |
75 |
if not self._loadedConfigs.has_key(configFilename): |
if not self._loadedConfigs.has_key(configFilename): |
76 |
parser = ConfigParser() |
parser = ConfigParser() |
77 |
self._loadedConfigs[configFilename]=parser |
self._loadedConfigs[configFilename]=parser |
|
readFiles = 1 |
|
|
|
|
|
# Load any passed in defaults to the requested section |
|
|
defaultValues = self._buildDefaults(defaults) |
|
|
try: |
|
|
self._loadedConfigs[configFilename].add_section(section) |
|
|
except DuplicateSectionError: |
|
|
pass |
|
|
for key in defaultValues.keys(): |
|
|
self._loadedConfigs[configFilename].set(section,key,defaultValues[key]) |
|
78 |
|
|
79 |
# Load any [common] defaults |
|
80 |
defaultValues = self._buildDefaults(GCConfig.ConfigOptions) |
# Build valid file list |
81 |
try: |
fileLocations = [] |
82 |
self._loadedConfigs[configFilename].add_section('common') |
etc_base = getInstalledBase('%s_etc' % section, 'common_etc') |
83 |
except DuplicateSectionError: |
|
84 |
pass |
# system config file |
85 |
for key in defaultValues.keys(): |
if etc_base: |
86 |
self._loadedConfigs[configFilename].set('common',key,defaultValues[key]) |
fileLocations.append(os.path.join(etc_base,configFilename)) |
87 |
|
|
88 |
|
# user config file |
89 |
|
try: |
90 |
|
fileLocations.append(os.path.join(os.environ['HOME'], homeConfigDir ,configFilename)) |
91 |
|
except KeyError: |
92 |
|
pass |
93 |
|
|
94 |
|
# system fixed config file |
95 |
|
if etc_base: |
96 |
|
fileLocations.append(os.path.join(etc_base,configFilename+'.fixed')) |
97 |
|
|
98 |
|
# |
99 |
|
# Load the values from the files specified |
100 |
|
# |
101 |
|
try: |
102 |
|
parser.read(fileLocations) |
103 |
|
except DuplicateSectionError: |
104 |
|
raise InvalidFormatError, _('The file has duplicate source definitions.') |
105 |
|
except MissingSectionHeaderError: |
106 |
|
raise InvalidFormatError, _('The file has no source definitions.') |
107 |
|
except: |
108 |
|
print _('The file cannot be parsed. %s :: %s') % (sys.exc_type, sys.exc_value) |
109 |
|
raise InvalidFormatError, _('The file cannot be parsed.') |
110 |
|
|
111 |
|
# |
112 |
|
# Common only needs checked once |
113 |
|
# |
114 |
|
# Load any [common] defaults |
115 |
|
self._integrateDefaultDict(configFilename,'common', |
116 |
|
self._buildDefaults(GCConfig.ConfigOptions)) |
117 |
|
|
118 |
|
# |
119 |
|
# Load anything set in the DEFAULT section |
120 |
|
# |
121 |
|
self._integrateDefaultDict(configFilename,section, |
122 |
|
self._loadedConfigs[configFilename].defaults()) |
123 |
|
|
124 |
|
# |
125 |
|
# If any values are still blank after loading from file's |
126 |
|
# specific section and then the default section then load the |
127 |
|
# defaults specified by the application itself. |
128 |
|
# |
129 |
|
self._integrateDefaultDict(configFilename,section,self._buildDefaults(defaults)) |
130 |
|
|
|
# Skip reading the files if the parser for that section already existed |
|
|
if not readFiles: |
|
|
return |
|
|
|
|
|
# Build valid file list |
|
|
fileLocations = [] |
|
|
etc_base = getInstalledBase('%s_etc' % section, 'common_etc') |
|
|
|
|
|
# system config file |
|
|
if etc_base: |
|
|
fileLocations.append(os.path.join(etc_base,configFilename)) |
|
131 |
|
|
132 |
# user config file |
def _integrateDefaultDict(self,filename, section,defaults): |
133 |
try: |
try: |
134 |
fileLocations.append(os.path.join(os.environ['HOME'], homeConfigDir ,configFilename)) |
self._loadedConfigs[filename].add_section(section) |
135 |
except KeyError: |
except DuplicateSectionError: |
136 |
pass |
pass |
137 |
|
for key in defaults.keys(): |
138 |
|
# Only set the value to the default if config file didn't contain |
139 |
|
# custom setting. |
140 |
|
try: |
141 |
|
self._loadedConfigs[filename].get(section,key) |
142 |
|
except NoOptionError: |
143 |
|
self._loadedConfigs[filename].set(section,key,defaults[key]) |
144 |
|
|
|
# system fixed config file |
|
|
if etc_base: |
|
|
fileLocations.append(os.path.join(etc_base,configFilename+'.fixed')) |
|
|
|
|
|
try: |
|
|
parser.read(fileLocations) |
|
|
except DuplicateSectionError: |
|
|
raise InvalidFormatError, _('The file has duplicate source definitions.') |
|
|
except MissingSectionHeaderError: |
|
|
raise InvalidFormatError, _('The file has no source definitions.') |
|
|
except: |
|
|
print _('The file cannot be parsed. %s :: %s') % (sys.exc_type, sys.exc_value) |
|
|
raise InvalidFormatError, _('The file cannot be parsed.') |
|
145 |
|
|
146 |
def gConfig(self, varName, configFilename=None, section=None): |
def gConfig(self, varName, configFilename=None, section=None): |
147 |
if not configFilename: configFilename = self._defaultConfigFilename |
if not configFilename: configFilename = self._defaultConfigFilename |