Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
assert config_tree.get_bool("bool-string-false") is False
config_tree.put("bool-string-yes", "yes")
assert config_tree.get_bool("bool-string-yes") is True
config_tree.put("bool-string-no", "no")
assert config_tree.get_bool("bool-string-no") is False
config_tree.put("bool-string-on", "on")
assert config_tree.get_bool("bool-string-on") is True
config_tree.put("bool-string-off", "off")
assert config_tree.get_bool("bool-string-off") is False
config_tree.put("invalid-bool-string", "invalid")
with pytest.raises(ConfigException):
config_tree.get_bool("invalid-bool-string")
def test_plain_ordered_dict(self):
config = ConfigFactory.parse_string(
"""
e : ${a} {
}
""",
resolve=False
)
with pytest.raises(ConfigException):
config.as_plain_ordered_dict()
www.example-ö.com {
us {
name = "second domain"
}
}
"""
config = ConfigFactory.parse_string(input_string)
assert config.get_string(u'www.sample.com.us.name') == 'first domain'
assert config.get_string(u'www.example-ö.com.us.name') == 'second domain'
with pytest.raises(ConfigWrongTypeException):
config.put(u'www.example-ö', 'append_failure', append=True)
with pytest.raises(ConfigMissingException):
config.get_string(u'missing_unicode_key_ö')
with pytest.raises(ConfigException):
config.get_bool(u'www.example-ö.com.us.name')
with pytest.raises(ConfigException):
config.get_list(u'www.example-ö.com.us.name')
with pytest.raises(ConfigException):
config.get_config(u'www.example-ö.com.us.name')
with pytest.raises(ConfigWrongTypeException):
config.get_string(u'www.example-ö.com.us.name.missing')
www.example-ö.com {
us {
name = "second domain"
}
}
"""
config = ConfigFactory.parse_string(input_string)
assert config.get_string(u'www.sample.com.us.name') == 'first domain'
assert config.get_string(u'www.example-ö.com.us.name') == 'second domain'
with pytest.raises(ConfigWrongTypeException):
config.put(u'www.example-ö', 'append_failure', append=True)
with pytest.raises(ConfigMissingException):
config.get_string(u'missing_unicode_key_ö')
with pytest.raises(ConfigException):
config.get_bool(u'www.example-ö.com.us.name')
with pytest.raises(ConfigException):
config.get_list(u'www.example-ö.com.us.name')
with pytest.raises(ConfigException):
config.get_config(u'www.example-ö.com.us.name')
with pytest.raises(ConfigWrongTypeException):
config.get_string(u'www.example-ö.com.us.name.missing')
"""
# String conversions as per API-recommendations:
# https://github.com/typesafehub/config/blob/master/HOCON.md#automatic-type-conversions
bool_conversions = {
None: None,
'true': True, 'yes': True, 'on': True,
'false': False, 'no': False, 'off': False
}
string_value = self.get_string(key, default)
if string_value is not None:
string_value = string_value.lower()
try:
return bool_conversions[string_value]
except KeyError:
raise ConfigException(
u"{key} does not translate to a Boolean value".format(key=key))
:type key: basestring
:param default: default value if key not found
:type default: list
:return: list value
:type return: list
"""
value = self.get(key, default)
if isinstance(value, list):
return value
elif isinstance(value, ConfigTree):
lst = []
for k, v in sorted(value.items(), key=lambda kv: kv[0]):
if re.match('^[1-9][0-9]*$|0', k):
lst.append(v)
else:
raise ConfigException(u"{key} does not translate to a list".format(key=key))
return lst
elif value is None:
return None
else:
raise ConfigException(
u"{key} has type '{type}' rather than 'list'".format(key=key, type=type(value).__name__))
def plain_value(v):
if isinstance(v, list):
return [plain_value(e) for e in v]
elif isinstance(v, ConfigTree):
return v.as_plain_ordered_dict()
else:
if isinstance(v, ConfigValues):
raise ConfigException("The config tree contains unresolved elements")
return v
"""Return tree config representation of value found at key
:param key: key to use (dot separated). E.g., a.b.c
:type key: basestring
:param default: default value if key not found
:type default: config
:return: config value
:type return: ConfigTree
"""
value = self.get(key, default)
if isinstance(value, dict):
return value
elif value is None:
return None
else:
raise ConfigException(
u"{key} has type '{type}' rather than 'config'".format(key=key, type=type(value).__name__))
class ConfigException(Exception):
def __init__(self, message, ex=None):
super(ConfigException, self).__init__(message)
self._exception = ex
class ConfigMissingException(ConfigException, KeyError):
pass
class ConfigSubstitutionException(ConfigException):
pass
class ConfigWrongTypeException(ConfigException):
pass
class ConfigException(Exception):
def __init__(self, message, ex=None):
super(ConfigException, self).__init__(message)
self._exception = ex
class ConfigMissingException(ConfigException, KeyError):
pass
class ConfigSubstitutionException(ConfigException):
pass
class ConfigWrongTypeException(ConfigException):
pass