test_config.py revision dbdbd0e1807583a9aacf63ef983392c100716058
1""" 2Wrapper around ConfigParser to manage testcases configuration. 3""" 4 5__author__ = 'rsalveti@linux.vnet.ibm.com (Ricardo Salveti de Araujo)' 6 7from ConfigParser import ConfigParser 8from os import path 9 10__all__ = ['config_loader'] 11 12class config_loader: 13 """Base class of the configuration parser""" 14 def __init__(self, filename="test.conf"): 15 self.filename = filename 16 if not path.isfile(self.filename): 17 raise IOError, "File '%s' not found" % (self.filename) 18 self.parser = ConfigParser() 19 self.parser.read(self.filename) 20 21 22 def get(self, section, name, default=None): 23 """Get the value of a option. 24 25 Section of the config file and the option name. 26 You can pass a default value if the option doesn't exist. 27 """ 28 if not self.parser.has_option(section, name): 29 return default 30 return self.parser.get(section, name) 31 32 33 def set(self, section, option, value): 34 """Set an option. 35 36 This change is not persistent unless saved with 'save()'. 37 """ 38 if not self.parser.has_section(section): 39 self.parser.add_section(section) 40 return self.parser.set(section, name, value) 41 42 43 def remove(self, section, name): 44 """Remove an option.""" 45 if self.parser.has_section(section): 46 self.parser.remove_option(section, name) 47 48 49 def save(self): 50 """Save the configuration file with all modifications""" 51 if not self.filename: 52 return 53 fileobj = file(self.filename, 'w') 54 try: 55 self.parser.write(fileobj) 56 finally: 57 fileobj.close() 58