new optparser class that support ; and # in option's value
This commit is contained in:
parent
0ab5416432
commit
5e4520bd0c
|
@ -16,44 +16,49 @@
|
||||||
## GNU General Public License for more details.
|
## GNU General Public License for more details.
|
||||||
##
|
##
|
||||||
|
|
||||||
import ConfigParser, logging, os, string
|
import logging, os, string
|
||||||
|
|
||||||
log = logging.getLogger('common.options')
|
log = logging.getLogger('common.options')
|
||||||
|
|
||||||
class OptionsParser(ConfigParser.ConfigParser):
|
class OptionsParser:
|
||||||
def __init__(self, fname):
|
def __init__(self, fname):
|
||||||
ConfigParser.ConfigParser.__init__(self)
|
|
||||||
self.__fname = os.path.expanduser(fname)
|
self.__fname = os.path.expanduser(fname)
|
||||||
self.tab = {}
|
self.tab = {}
|
||||||
# END __init__
|
# END __init__
|
||||||
|
|
||||||
def parseCfgFile(self):
|
def parseCfgFile(self):
|
||||||
try:
|
try:
|
||||||
self.__fd = open(self.__fname)
|
fd = open(self.__fname)
|
||||||
except:
|
except:
|
||||||
print 'error cannot open file %s\n' % (self.__fname);
|
print 'error cannot open file %s\n' % (self.__fname);
|
||||||
return
|
return
|
||||||
|
|
||||||
self.readfp(self.__fd)
|
self.tab = {}
|
||||||
self.__sections = self.sections()
|
section = ''
|
||||||
|
for line in fd.readlines():
|
||||||
for section in self.__sections:
|
if line[0] in "#;":
|
||||||
self.tab[section] = {}
|
continue
|
||||||
for option in self.options(section):
|
if line[0] == '[':
|
||||||
value = self.get(section, option, 1)
|
section = line[1:line.find(']')]
|
||||||
#convert to int options than can be
|
self.tab[section] = {}
|
||||||
if string.find(option, 'password') == -1:
|
continue
|
||||||
try:
|
index = line.find('=')
|
||||||
i = string.atoi(value)
|
if index == -1:
|
||||||
except ValueError:
|
continue
|
||||||
self.tab[section][option] = value
|
option = line[0:index]
|
||||||
else:
|
option = option.strip()
|
||||||
self.tab[section][option] = i
|
value = line[index+1:]
|
||||||
else:
|
value = value.strip()
|
||||||
|
if string.find(option, 'password') == -1:
|
||||||
|
try:
|
||||||
|
i = string.atoi(value)
|
||||||
|
except ValueError:
|
||||||
self.tab[section][option] = value
|
self.tab[section][option] = value
|
||||||
|
else:
|
||||||
# setattr(self, str(section) + '_' + \
|
self.tab[section][option] = i
|
||||||
# str(option), value)
|
else:
|
||||||
|
self.tab[section][option] = value
|
||||||
|
fd.close()
|
||||||
# END parseCfgFile
|
# END parseCfgFile
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
@ -73,19 +78,15 @@ class OptionsParser(ConfigParser.ConfigParser):
|
||||||
# END __getattr__
|
# END __getattr__
|
||||||
|
|
||||||
def writeCfgFile(self):
|
def writeCfgFile(self):
|
||||||
#Remove all sections
|
|
||||||
for s in self.sections():
|
|
||||||
self.remove_section(s)
|
|
||||||
#recreate sections
|
|
||||||
for s in self.tab.keys():
|
|
||||||
self.add_section(s)
|
|
||||||
for o in self.tab[s].keys():
|
|
||||||
self.set(s, o, self.tab[s][o])
|
|
||||||
try:
|
try:
|
||||||
self.write(open(self.__fname, 'w'))
|
fd = open(self.__fname, 'w')
|
||||||
except:
|
except:
|
||||||
log.debug("Can't write config %s" % self.__fname)
|
log.debug("Can't write config %s" % self.__fname)
|
||||||
return 0
|
return 0
|
||||||
|
for s in self.tab.keys():
|
||||||
|
fd.write('[' + s + ']\n\n')
|
||||||
|
for o in self.tab[s].keys():
|
||||||
|
fd.write(o + ' = ' + str(self.tab[s][o]) + '\n')
|
||||||
return 1
|
return 1
|
||||||
# END writeCfgFile
|
# END writeCfgFile
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue