Mercurial > libervia-backend
comparison src/tools/config.py @ 1234:9c17bd37e6e5
core: better management of default value in getConfig
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 07 Oct 2014 17:12:41 +0200 |
parents | 7ee9d9db67b9 |
children | 80d2ed788b40 |
comparison
equal
deleted
inserted
replaced
1233:0b87d029f0a3 | 1234:9c17bd37e6e5 |
---|---|
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 (str): eventually default to this value, if not None | 76 @param default: value to use if not found |
77 @return: str, list or dict | 77 @return: str, list or dict |
78 """ | 78 """ |
79 if not section: | 79 if not section: |
80 section = DEFAULTSECT | 80 section = DEFAULTSECT |
81 | 81 |
82 try: | 82 try: |
83 value = config.get(section, name) | 83 value = config.get(section, name) |
84 except (NoOptionError, NoSectionError) as e: | 84 except (NoOptionError, NoSectionError): |
85 if default is None: | 85 return default |
86 raise e | |
87 value = default | |
88 | 86 |
89 if name.endswith('_path') or name.endswith('_dir'): | 87 if name.endswith('_path') or name.endswith('_dir'): |
90 value = os.path.expanduser(value) | 88 value = os.path.expanduser(value) |
91 # thx to Brian (http://stackoverflow.com/questions/186857/splitting-a-semicolon-separated-string-to-a-dictionary-in-python/186873#186873) | 89 # thx to Brian (http://stackoverflow.com/questions/186857/splitting-a-semicolon-separated-string-to-a-dictionary-in-python/186873#186873) |
92 elif name.endswith('_list'): | 90 elif name.endswith('_list'): |