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