root/branches/gajim_0.4/common/optparser.py

Revision 359, 2.4 kB (checked in by asterix, 4 years ago)

update my email adress
update copyright
add missing headers

  • Property svn:keywords set to LastChangedDate LastChangedRevision LastChangedBy HeadURL Id
Line 
1##      common/optparser.py
2##
3## Gajim Team:
4##      - Yann Le Boulanger <asterix@lagaule.org>
5##      - Vincent Hanquez <tab@snarc.org>
6##
7##      Copyright (C) 2003-2005 Gajim Team
8##
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
11## by the Free Software Foundation; version 2 only.
12##
13## This program is distributed in the hope that it will be useful,
14## but WITHOUT ANY WARRANTY; without even the implied warranty of
15## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16## GNU General Public License for more details.
17##
18
19import ConfigParser, logging, os, string
20
21log = logging.getLogger('common.options')
22
23class OptionsParser(ConfigParser.ConfigParser):
24        def __init__(self, fname):
25                ConfigParser.ConfigParser.__init__(self)
26                self.__fname = os.path.expanduser(fname)
27                self.tab = {}
28        # END __init__
29
30        def parseCfgFile(self):
31                try:
32                        self.__fd = open(self.__fname)
33                except:
34                        print 'error cannot open file %s\n' % (self.__fname);
35                        return
36   
37                self.readfp(self.__fd)
38                self.__sections = self.sections()
39
40                for section in self.__sections:
41                        self.tab[section] = {}
42                        for option in self.options(section):
43                                value = self.get(section, option, 1)
44                                #convert to int options than can be
45                                if string.find(option, 'password') == -1:
46                                        try:
47                                                i = string.atoi(value)
48                                        except ValueError:
49                                                self.tab[section][option] = value
50                                        else:
51                                                self.tab[section][option] = i
52                                else:
53                                        self.tab[section][option] = value
54
55#                               setattr(self, str(section) + '_' + \
56#                                       str(option), value)
57        # END parseCfgFile
58
59        def __str__(self):
60                return "OptionsParser"
61        # END __str__
62
63        def __getattr__(self, attr):
64                if attr.startswith('__') and attr in self.__dict__.keys():
65                        return self.__dict__[attr]
66                elif self.tab.has_key(attr):
67                        return self.tab[attr]
68                else:
69#                       for key in self.__dict__.keys():
70#                               if key == attr:
71#                                       return self.__dict__[attr]
72                        return None
73        # END __getattr__
74
75        def writeCfgFile(self):
76                #Remove all sections
77                for s in self.sections():
78                        self.remove_section(s)
79                #recreate sections
80                for s in self.tab.keys():
81                        self.add_section(s)
82                        for o in self.tab[s].keys():
83                                self.set(s, o, self.tab[s][o])
84                try:
85                        self.write(open(self.__fname, 'w'))
86                except:
87                        log.debug("Can't write config %s" % self.__fname)
88                        return 0
89                return 1
90        # END writeCfgFile
91
92        def stop(self):
93                return self.writeCfgFile()
94        # END stop
95# END OptionsParser
Note: See TracBrowser for help on using the browser.