4 # Copyright 2007 Neil Shi <zeegeek@gmail.com>
6 # Configuration File Parser
7 # This file contains the configuration parser for pymailheaders
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 from ConfigParser
import *
20 from sys
import stderr
25 """This class parses and saves the configuration file for pymailheaders.
27 @attention: Put all boolean variables names in __bool_vals so that we
28 can return them in the right type later.
30 @note: Private member variables:
39 __section
= 'settings'
41 __defaults
= {'auth': False,
49 'background': 'black',
50 'foreground': 'green',
51 'foreground new': 'yellow',
62 __bool_vals
= ('auth',
72 __int_vals
= ('interval',
79 # stores configuration filename
82 def __init__(self
, filename
):
85 @type filename: string
86 @param filename: full configuration file name
89 self
.__config
= SafeConfigParser()
90 self
.__config
_file
= filename
93 # if the file does not exist, create it and write
94 # default values to it.
95 if not os
.path
.isfile(self
.__config
_file
):
96 for k
, v
in self
.__defaults
.iteritems():
100 # check if we have the correct permissions
101 fd
= open(self
.__config
_file
, 'rw')
104 self
.__config
.read(self
.__config
_file
)
106 # Insert default values
107 # I have to do this because ConfigParser will insert a
108 # section called DEFAULT if I use the defaults method.
109 for k
, v
in self
.__defaults
.iteritems():
110 if not self
.__has
(k
): self
.set(k
, v
)
111 except (IOError, ParsingError
, MissingSectionHeaderError
), strerr
:
112 raise Error('config (__init__)', str(strerr
))
119 @note: make sure that changes are being written to the file.
124 def __has(self
, opt
):
125 """Determine if an option exists in the config file
128 @param opt: option name
130 @return: True if it has the option, False otherwise.
133 return self
.__config
.has_option(self
.__section
, opt
)
135 def set(self
, opt
, val
):
136 """Set option's value to value
139 @param opt: option name
140 @type val: string or bool or int
141 @param val: option's value
145 # convert from boolean values
146 if opt
in self
.__bool
_vals
:
147 if type(val
) != bool:
151 self
.__config
.set(self
.__section
, opt
, 'yes')
153 self
.__config
.set(self
.__section
, opt
, 'no')
154 # convert from integers
155 elif opt
in self
.__int
_vals
:
159 self
.__config
.set(self
.__section
, opt
, str(val
))
160 elif type(val
) == bool or type(val
) == int:
163 self
.__config
.set(self
.__section
, opt
, val
)
164 except NoSectionError
, strerr
:
165 print >> stderr
, 'config (set):', strerr
166 print >> stderr
, 'config (set): creating...'
169 self
.__config
.add_section(self
.__section
)
170 # try to add the option
173 raise Error('config (set)', 'invalid value type')
178 """Get option's value
180 If the option has a boolean value, convert it into boolean type.
181 If it's a integer value, convert it to integer type.
184 @param opt: option name
185 @rtype: string or bool or int
186 @return: option's value
190 # convert to boolean values
191 if opt
in self
.__bool
_vals
:
192 return self
.__config
.get(self
.__section
, opt
).lower() == 'yes'
193 # convert to integers
194 elif opt
in self
.__int
_vals
:
195 return int(self
.__config
.get(self
.__section
, opt
))
197 return self
.__config
.get(self
.__section
, opt
)
198 except (NoSectionError
, NoOptionError
), strerr
:
199 raise Error('config (get)', str(strerr
))
202 """Get all options' values in the right type
205 @return: options' values
211 opts
= self
.__config
.options(self
.__section
)
214 optvals
[k
] = self
.get(k
)
221 """Write configurations to file
225 # make sure that all options will be written into the
227 for k
, v
in self
.__defaults
.iteritems():
228 if not self
.__has
(k
): self
.set(k
, v
)
230 fd
= open(self
.__config
_file
, 'w')
232 self
.__config
.write(fd
)
236 # Set file permission bits to 0600
237 os
.chmod(self
.__config
_file
, 0600)
239 except IOError, strerr
:
240 raise Error('config (write)', str(strerr
))