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

Revision 380, 2.3 kB (checked in by asterix, 4 years ago)

new optparser class that support ; and # in option's value

  • 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 logging, os, string
20
21log = logging.getLogger('common.options')
22
23class OptionsParser:
24        def __init__(self, fname):
25                self.__fname = os.path.expanduser(fname)
26                self.tab = {}
27        # END __init__
28
29        def parseCfgFile(self):
30                try:
31                        fd = open(self.__fname)
32                except:
33                        print 'error cannot open file %s\n' % (self.__fname);
34                        return
35   
36                self.tab = {}
37                section = ''
38                for line in fd.readlines():
39                        if line[0] in "#;":
40                                continue
41                        if line[0] == '[':
42                                section = line[1:line.find(']')]
43                                self.tab[section] = {}
44                                continue
45                        index = line.find('=')
46                        if index == -1:
47                                continue
48                        option = line[0:index]
49                        option = option.strip()
50                        value = line[index+1:]
51                        value = value.strip()
52                        if string.find(option, 'password') == -1:
53                                try:
54                                        i = string.atoi(value)
55                                except ValueError:
56                                        self.tab[section][option] = value
57                                else:
58                                        self.tab[section][option] = i
59                        else:
60                                self.tab[section][option] = value
61                fd.close()
62        # END parseCfgFile
63
64        def __str__(self):
65                return "OptionsParser"
66        # END __str__
67
68        def __getattr__(self, attr):
69                if attr.startswith('__') and attr in self.__dict__.keys():
70                        return self.__dict__[attr]
71                elif self.tab.has_key(attr):
72                        return self.tab[attr]
73                else:
74#                       for key in self.__dict__.keys():
75#                               if key == attr:
76#                                       return self.__dict__[attr]
77                        return None
78        # END __getattr__
79
80        def writeCfgFile(self):
81                try:
82                        fd = open(self.__fname, 'w')
83                except:
84                        log.debug("Can't write config %s" % self.__fname)
85                        return 0
86                for s in self.tab.keys():
87                        fd.write('[' + s + ']\n\n')
88                        for o in self.tab[s].keys():
89                                fd.write(o + ' = ' + str(self.tab[s][o]) + '\n')
90                return 1
91        # END writeCfgFile
92
93        def stop(self):
94                return self.writeCfgFile()
95        # END stop
96# END OptionsParser
Note: See TracBrowser for help on using the browser.