def test_get_config_value(): config_path = get_config_path('.testconfigrc') if os.path.exists(config_path): os.remove(config_path) value = get_config_value(config_filename='.testconfigrc', section='opts', option='setting1', default_generator=lambda: 'somevalue', write_default=True) assert value == 'somevalue' value = get_config_value(config_filename='.testconfigrc', section='opts', option='setting1', default_generator=lambda: 'someothervalue', write_default=True) assert value == 'somevalue' value = get_config_value(config_filename='.testconfigrc', section='opts', option='setting2', default_generator=lambda: 'blah', write_default=True) assert value == 'blah' value = get_config_value(config_filename='.testconfigrc', section='opts', option='setting1') assert value == 'somevalue' value = get_config_value(config_filename='.testconfigrc', section='opts', option='setting2') assert value == 'blah' with raises(NoSectionError): _ = get_config_value(config_filename='.testconfigrc', section='schmopts', option='setting3') with raises(NoOptionError): _ = get_config_value(config_filename='.testconfigrc', section='opts', option='setting3') with raises(AssertionError): _ = get_config_value(config_filename='.testconfigXXXrc', section='opts', option='setting3') os.remove(config_path)
def test_get_config_value(): config_path = get_config_path('.testconfigrc') if os.path.exists(config_path): os.remove(config_path) value = get_config_value(config_filename='.testconfigrc', section='opts', option='setting1', default_generator=lambda: 'somevalue', write_default=True) assert value == 'somevalue' value = get_config_value(config_filename='.testconfigrc', section='opts', option='setting1', default_generator=lambda: 'someothervalue', write_default=True) assert value == 'somevalue' value = get_config_value(config_filename='.testconfigrc', section='opts', option='setting2', default_generator=lambda: 'blah', write_default=True) assert value == 'blah' value = get_config_value(config_filename='.testconfigrc', section='opts', option='setting1') assert value == 'somevalue' value = get_config_value(config_filename='.testconfigrc', section='opts', option='setting2') assert value == 'blah' with raises(NoSectionError): _ = get_config_value(config_filename='.testconfigrc', section='schmopts', option='setting3') with raises(NoOptionError): _ = get_config_value(config_filename='.testconfigrc', section='opts', option='setting3') with raises(AssertionError): _ = get_config_value(config_filename='.testconfigXXXrc', section='opts', option='setting3') set_non_persistent_config_value(config_filename='.testconfigrc', section='opts', option='setting2',value="bob") value = get_config_value(config_filename='.testconfigrc', section='opts', option='setting2') assert value == 'bob' value = get_config_value(config_filename='.testconfigrc', section='opts', option='setting2', use_cashed_config=False) assert value == 'blah' value = get_config_value(config_filename='.testconfigrc', section='opts', option='setting2') assert value == 'bob' set_non_persistent_config_value(config_filename='.testconfigrc', section='schmapts', option='setting2', value="bob") with raises(NoOptionError): _ = get_config_value(config_filename='.testconfigrc', section='schmapts', option='setting3') with raises(NoSectionError): _ = get_config_value(config_filename='.testconfigrc', section='schmapts', option='setting2', use_cashed_config=False) value = get_config_value(config_filename='.testconfigrc', section='schmapts', option='setting2') assert value == 'bob' os.remove(config_path)
def get_artemis_config(): """ :return: A ConfigParser object, containing the information in the ~/.artemisrc file. Create a default file if none exists. """ global _CONFIG if _CONFIG is None: config_path = get_config_path('.artemisrc') if not os.path.exists(config_path): with open(config_path, 'w') as f: f.write('[plotting]\nbackend: matplotlib') config = ConfigParser.ConfigParser(defaults={ 'update_period': '1', }) config.read(os.path.join(os.path.expanduser('~'), '.artemisrc')) _CONFIG = config return _CONFIG
def check_or_create_artemis_config(): config_path = get_config_path(_CONFIG_FILE_NAME) if not os.path.exists(config_path): with open(config_path, 'w') as f: f.write(_DEFAULT_ARTEMIS_CONFIG) return _CONFIG_FILE_NAME