Changeset 380 for trunk/common

Show
Ignore:
Timestamp:
01/22/05 21:26:43 (4 years ago)
Author:
asterix
Message:

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

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/common/optparser.py

    r359 r380  
    1717## 
    1818 
    19 import ConfigParser, logging, os, string 
     19import logging, os, string 
    2020 
    2121log = logging.getLogger('common.options') 
    2222 
    23 class OptionsParser(ConfigParser.ConfigParser): 
     23class OptionsParser: 
    2424        def __init__(self, fname): 
    25                 ConfigParser.ConfigParser.__init__(self) 
    2625                self.__fname = os.path.expanduser(fname) 
    2726                self.tab = {} 
     
    3029        def parseCfgFile(self): 
    3130                try: 
    32                         self.__fd = open(self.__fname) 
     31                        fd = open(self.__fname) 
    3332                except: 
    3433                        print 'error cannot open file %s\n' % (self.__fname); 
    3534                        return 
    3635     
    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 
     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 
    5257                                else: 
    53                                         self.tab[section][option] = value 
    54  
    55 #                               setattr(self, str(section) + '_' + \ 
    56 #                                       str(option), value) 
     58                                        self.tab[section][option] = i 
     59                        else: 
     60                                self.tab[section][option] = value 
     61                fd.close() 
    5762        # END parseCfgFile 
    5863 
     
    7479 
    7580        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]) 
    8481                try: 
    85                         self.write(open(self.__fname, 'w')) 
     82                        fd = open(self.__fname, 'w') 
    8683                except: 
    8784                        log.debug("Can't write config %s" % self.__fname) 
    8885                        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') 
    8990                return 1 
    9091        # END writeCfgFile