gajim-plural/common/optparser.py

96 lines
2.4 KiB
Python
Raw Normal View History

2003-10-22 20:45:13 +02:00
## common/optparser.py
##
## Gajim Team:
## - Yann Le Boulanger <asterix@crans.org>
2004-06-18 11:25:15 +02:00
## - Vincent Hanquez <tab@snarc.org>
2003-10-22 20:45:13 +02:00
##
## Copyright (C) 2003 Gajim Team
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 2 only.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
import ConfigParser, logging, os, string
2003-10-22 20:45:13 +02:00
log = logging.getLogger('common.options')
2003-12-11 23:26:29 +01:00
class OptionsParser(ConfigParser.ConfigParser):
2003-10-22 20:45:13 +02:00
def __init__(self, fname):
2003-12-11 23:26:29 +01:00
ConfigParser.ConfigParser.__init__(self)
self.__fname = os.path.expanduser(fname)
self.tab = {}
2003-10-22 20:45:13 +02:00
# END __init__
def parseCfgFile(self):
try:
self.__fd = open(self.__fname)
except:
print 'error cannot open file %s\n' % (self.__fname);
return
2003-12-11 23:26:29 +01:00
self.readfp(self.__fd)
self.__sections = self.sections()
2003-10-22 20:45:13 +02:00
for section in self.__sections:
self.tab[section] = {}
2003-12-11 23:26:29 +01:00
for option in self.options(section):
value = self.get(section, option, 1)
#convert to int options than can be
2005-01-06 13:45:57 +01:00
if string.find(option, 'password') == -1:
try:
i = string.atoi(value)
except ValueError:
self.tab[section][option] = value
else:
self.tab[section][option] = i
else:
2005-01-06 13:45:57 +01:00
self.tab[section][option] = value
# setattr(self, str(section) + '_' + \
# str(option), value)
2003-10-22 20:45:13 +02:00
# END parseCfgFile
def __str__(self):
return "OptionsParser"
# END __str__
def __getattr__(self, attr):
if attr.startswith('__') and attr in self.__dict__.keys():
return self.__dict__[attr]
elif self.tab.has_key(attr):
return self.tab[attr]
2003-10-22 20:45:13 +02:00
else:
# for key in self.__dict__.keys():
# if key == attr:
# return self.__dict__[attr]
2003-10-22 20:45:13 +02:00
return None
# END __getattr__
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])
2003-10-22 20:45:13 +02:00
try:
2003-12-11 23:26:29 +01:00
self.write(open(self.__fname, 'w'))
2003-10-22 20:45:13 +02:00
except:
log.debug("Can't write config %s" % self.__fname)
return 0
return 1
# END writeCfgFile
def stop(self):
return self.writeCfgFile()
# END stop
# END OptionsParser