2003-10-22 20:45:13 +02:00
|
|
|
## common/optparser.py
|
|
|
|
##
|
|
|
|
## Gajim Team:
|
2005-01-07 22:52:38 +01:00
|
|
|
## - Yann Le Boulanger <asterix@lagaule.org>
|
2004-06-18 11:25:15 +02:00
|
|
|
## - Vincent Hanquez <tab@snarc.org>
|
2003-10-22 20:45:13 +02:00
|
|
|
##
|
2005-01-07 22:52:38 +01:00
|
|
|
## Copyright (C) 2003-2005 Gajim Team
|
2003-10-22 20:45:13 +02:00
|
|
|
##
|
|
|
|
## 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.
|
|
|
|
##
|
|
|
|
|
2005-01-22 21:26:43 +01:00
|
|
|
import logging, os, string
|
2003-10-22 20:45:13 +02:00
|
|
|
|
|
|
|
log = logging.getLogger('common.options')
|
|
|
|
|
2005-01-22 21:26:43 +01:00
|
|
|
class OptionsParser:
|
2003-10-22 20:45:13 +02:00
|
|
|
def __init__(self, fname):
|
2003-10-23 20:29:15 +02:00
|
|
|
self.__fname = os.path.expanduser(fname)
|
2004-01-24 21:32:51 +01:00
|
|
|
self.tab = {}
|
2003-10-22 20:45:13 +02:00
|
|
|
# END __init__
|
|
|
|
|
|
|
|
def parseCfgFile(self):
|
|
|
|
try:
|
2005-01-22 21:26:43 +01:00
|
|
|
fd = open(self.__fname)
|
2003-10-22 20:45:13 +02:00
|
|
|
except:
|
|
|
|
print 'error cannot open file %s\n' % (self.__fname);
|
|
|
|
return
|
|
|
|
|
2005-01-22 21:26:43 +01:00
|
|
|
self.tab = {}
|
|
|
|
section = ''
|
|
|
|
for line in fd.readlines():
|
|
|
|
if line[0] in "#;":
|
|
|
|
continue
|
|
|
|
if line[0] == '[':
|
|
|
|
section = line[1:line.find(']')]
|
|
|
|
self.tab[section] = {}
|
|
|
|
continue
|
|
|
|
index = line.find('=')
|
|
|
|
if index == -1:
|
|
|
|
continue
|
|
|
|
option = line[0:index]
|
|
|
|
option = option.strip()
|
|
|
|
value = line[index+1:]
|
|
|
|
value = value.strip()
|
|
|
|
if string.find(option, 'password') == -1:
|
|
|
|
try:
|
|
|
|
i = string.atoi(value)
|
|
|
|
except ValueError:
|
2005-01-06 13:45:57 +01:00
|
|
|
self.tab[section][option] = value
|
2005-01-22 21:26:43 +01:00
|
|
|
else:
|
|
|
|
self.tab[section][option] = i
|
|
|
|
else:
|
|
|
|
self.tab[section][option] = value
|
|
|
|
fd.close()
|
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]
|
2004-01-24 21:32:51 +01:00
|
|
|
elif self.tab.has_key(attr):
|
|
|
|
return self.tab[attr]
|
2003-10-22 20:45:13 +02:00
|
|
|
else:
|
2004-01-24 21:32:51 +01:00
|
|
|
# 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):
|
|
|
|
try:
|
2005-01-22 21:26:43 +01:00
|
|
|
fd = open(self.__fname, 'w')
|
2003-10-22 20:45:13 +02:00
|
|
|
except:
|
|
|
|
log.debug("Can't write config %s" % self.__fname)
|
|
|
|
return 0
|
2005-01-22 21:26:43 +01:00
|
|
|
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')
|
2003-10-22 20:45:13 +02:00
|
|
|
return 1
|
|
|
|
# END writeCfgFile
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
return self.writeCfgFile()
|
|
|
|
# END stop
|
|
|
|
# END OptionsParser
|