comparison src/tools/config.py @ 1235:80d2ed788b40

core (config): added the Exception default value which raise an exception instead of returning the default in getConfig
author Goffi <goffi@goffi.org>
date Sat, 11 Oct 2014 16:26:43 +0200
parents 9c17bd37e6e5
children 251ae99a6c0e
comparison
equal deleted inserted replaced
1234:9c17bd37e6e5 1235:80d2ed788b40
71 """Get a configuration option 71 """Get a configuration option
72 72
73 @param config (SafeConfigParser): the configuration instance 73 @param config (SafeConfigParser): the configuration instance
74 @param section (str): section of the config file (None or '' for DEFAULT) 74 @param section (str): section of the config file (None or '' for DEFAULT)
75 @param name (str): name of the option 75 @param name (str): name of the option
76 @param default: value to use if not found 76 @param default: value to use if not found, or Exception to raise an exception
77 @return: str, list or dict 77 @return: str, list or dict
78 @raise: NoOptionError if option is not present and default is Exception
79 NoSectionError if section doesn't exists and default is Exception
78 """ 80 """
79 if not section: 81 if not section:
80 section = DEFAULTSECT 82 section = DEFAULTSECT
81 83
82 try: 84 try:
83 value = config.get(section, name) 85 value = config.get(section, name)
84 except (NoOptionError, NoSectionError): 86 except (NoOptionError, NoSectionError) as e:
87 if default is Exception:
88 raise e
85 return default 89 return default
86 90
87 if name.endswith('_path') or name.endswith('_dir'): 91 if name.endswith('_path') or name.endswith('_dir'):
88 value = os.path.expanduser(value) 92 value = os.path.expanduser(value)
89 # thx to Brian (http://stackoverflow.com/questions/186857/splitting-a-semicolon-separated-string-to-a-dictionary-in-python/186873#186873) 93 # thx to Brian (http://stackoverflow.com/questions/186857/splitting-a-semicolon-separated-string-to-a-dictionary-in-python/186873#186873)