Changeset 380 for trunk/common
- Timestamp:
- 01/22/05 21:26:43 (4 years ago)
- Files:
-
- 1 modified
-
trunk/common/optparser.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/common/optparser.py
r359 r380 17 17 ## 18 18 19 import ConfigParser,logging, os, string19 import logging, os, string 20 20 21 21 log = logging.getLogger('common.options') 22 22 23 class OptionsParser (ConfigParser.ConfigParser):23 class OptionsParser: 24 24 def __init__(self, fname): 25 ConfigParser.ConfigParser.__init__(self)26 25 self.__fname = os.path.expanduser(fname) 27 26 self.tab = {} … … 30 29 def parseCfgFile(self): 31 30 try: 32 self.__fd = open(self.__fname)31 fd = open(self.__fname) 33 32 except: 34 33 print 'error cannot open file %s\n' % (self.__fname); 35 34 return 36 35 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 52 57 else: 53 self.tab[section][option] = value54 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() 57 62 # END parseCfgFile 58 63 … … 74 79 75 80 def writeCfgFile(self): 76 #Remove all sections77 for s in self.sections():78 self.remove_section(s)79 #recreate sections80 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 81 try: 85 self.write(open(self.__fname, 'w'))82 fd = open(self.__fname, 'w') 86 83 except: 87 84 log.debug("Can't write config %s" % self.__fname) 88 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') 89 90 return 1 90 91 # END writeCfgFile
