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