本文整理汇总了Python中pyhocon.config_tree.ConfigTree类的典型用法代码示例。如果您正苦于以下问题:Python ConfigTree类的具体用法?Python ConfigTree怎么用?Python ConfigTree使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ConfigTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_config_logging
def test_config_logging(self):
import logging.config
config_tree = ConfigTree()
config_tree.put('version', 1)
config_tree.put('root.level', logging.INFO)
assert dict(config_tree)['version'] == 1
logging.config.dictConfig(config_tree)
示例2: _fixup_self_references
def _fixup_self_references(cls, config, accept_unresolved=False):
if isinstance(config, ConfigTree) and config.root:
for key in config: # Traverse history of element
history = config.history[key]
previous_item = history[0]
for current_item in history[1:]:
for substitution in cls._find_substitutions(current_item):
prop_path = ConfigTree.parse_key(substitution.variable)
if len(prop_path) > 1 and config.get(substitution.variable, None) is not None:
continue # If value is present in latest version, don't do anything
if prop_path[0] == key:
if isinstance(previous_item, ConfigValues) and not accept_unresolved: # We hit a dead end, we cannot evaluate
raise ConfigSubstitutionException(
"Property {variable} cannot be substituted. Check for cycles.".format(
variable=substitution.variable
)
)
else:
value = previous_item if len(prop_path) == 1 else previous_item.get(".".join(prop_path[1:]))
_, _, current_item = cls._do_substitute(substitution, value)
previous_item = current_item
if len(history) == 1: # special case, when self optional referencing without existing
for substitution in cls._find_substitutions(previous_item):
prop_path = ConfigTree.parse_key(substitution.variable)
if len(prop_path) > 1 and config.get(substitution.variable, None) is not None:
continue # If value is present in latest version, don't do anything
if prop_path[0] == key and substitution.optional:
cls._do_substitute(substitution, None)
示例3: test_contains_with_quoted_keys
def test_contains_with_quoted_keys(self):
config_tree = ConfigTree()
config_tree.put('a.b."c.d"', 5)
assert 'a' in config_tree
assert 'a.b' in config_tree
assert 'a.c' not in config_tree
assert 'a.b."c.d"' in config_tree
assert 'a.b.c.d' not in config_tree
示例4: test_contains
def test_contains(self):
config_tree = ConfigTree()
config_tree.put('a.b', 5)
config_tree.put('a.c', None)
assert 'a' in config_tree
assert 'a.b' in config_tree
assert 'a.c' in config_tree
assert 'a.b.c' not in config_tree
示例5: create_tree
def create_tree(value):
if isinstance(value, dict):
res = ConfigTree(root=root)
for key, child_value in value.items():
res.put(key, create_tree(child_value))
return res
if isinstance(value, list):
return [create_tree(v) for v in value]
else:
return value
示例6: test_config_tree_special_characters
def test_config_tree_special_characters(self):
special_characters = '$}[]:=+#`^[email protected]*&.'
for char in special_characters:
config_tree = ConfigTree()
escaped_key = "\"test{char}key\"".format(char=char)
key = "a.b.{escaped_key}".format(escaped_key=escaped_key)
config_tree.put(key, "value")
hocon_tree = HOCONConverter.to_hocon(config_tree)
assert escaped_key in hocon_tree
parsed_tree = ConfigFactory.parse_string(hocon_tree)
assert parsed_tree.get(key) == "value"
示例7: test_config_list
def test_config_list(self):
config_tree = ConfigTree()
config_tree.put("a.b.c", [4, 5])
assert config_tree.get("a.b.c") == [4, 5]
config_tree.put("a.b.c", [6, 7])
assert config_tree.get("a.b.c") == [6, 7]
config_tree.put("a.b.c", [8, 9], True)
assert config_tree.get("a.b.c") == [6, 7, 8, 9]
示例8: test_config_tree_quoted_string
def test_config_tree_quoted_string(self):
config_tree = ConfigTree()
config_tree.put("a.b.c", "value")
assert config_tree.get("a.b.c") == "value"
with pytest.raises(ConfigMissingException):
assert config_tree.get("a.b.d")
with pytest.raises(ConfigMissingException):
config_tree.get("a.d.e")
with pytest.raises(ConfigWrongTypeException):
config_tree.get("a.b.c.e")
示例9: test_durations
def test_durations(self):
one_sec = ("1s", "1 s", "1seconds", "1 seconds", " 1s ", " 1 s ",
"1second",
"1000", "1000ms", "1000 ms", "1000 milliseconds", " 1000 milliseconds ",
"1000millisecond",
"1000000us", "1000000 us", "1000000 microseconds", "1000000microsecond",
"1000000000ns", "1000000000 ns", "1000000000 nanoseconds", "1000000000nanosecond",
"0.01666666666666666666666m", "0.01666666666666666666666 minutes", "0.01666666666666666666666 minute",
"0.00027777777777777777777h", "0.00027777777777777777777 hours", "0.00027777777777777777777hour",
"1.1574074074074073e-05d", "1.1574074074074073e-05 days", "1.1574074074074073e-05day")
config_tree = ConfigTree()
nanos_in_one_sec = 1000**3
millis_in_one_sec = 1000
for repr in one_sec:
config_tree.put("one_sec", repr)
assert config_tree.get_nanoseconds() == nanos_in_one_sec
assert config_tree.get_milliseconds() == millis_in_one_sec
示例10: test_numerically_index_objects_to_arrays
def test_numerically_index_objects_to_arrays(self):
config_tree = ConfigTree()
config_tree.put("list.2", "b")
config_tree.put("list.0", "a")
assert config_tree.get_list("list") == ["a", "b"]
config_tree.put("invalid-list.a", "c")
config_tree.put("invalid-list.b", "d")
with pytest.raises(ConfigException):
config_tree.get_list("invalid-list")
示例11: postParse
def postParse(self, instring, loc, token_list):
"""Create ConfigTree from tokens
:param instring:
:param loc:
:param token_list:
:return:
"""
config_tree = ConfigTree()
for element in token_list:
expanded_tokens = element.tokens if isinstance(element, ConfigInclude) else [element]
for tokens in expanded_tokens:
# key, value1 (optional), ...
key = tokens[0].strip()
values = tokens[1:]
# empty string
if len(values) == 0:
config_tree.put(key, "")
else:
value = values[0]
if isinstance(value, list):
config_tree.put(key, value, False)
else:
# Merge dict
if isinstance(value, ConfigValues):
conf_value = value
value.parent = config_tree
value.key = key
else:
conf_value = value
config_tree.put(key, conf_value)
return config_tree
示例12: test_getter_type_conversion_bool_to_string
def test_getter_type_conversion_bool_to_string(self):
config_tree = ConfigTree()
config_tree.put("bool-true", True)
assert config_tree.get_string("bool-true") == "true"
config_tree.put("bool-false", False)
assert config_tree.get_string("bool-false") == "false"
示例13: test_getter_type_conversion_number_to_string
def test_getter_type_conversion_number_to_string(self):
config_tree = ConfigTree()
config_tree.put("int", 5)
assert config_tree.get_string("int") == "5"
config_tree.put("float", 2.345)
assert config_tree.get_string("float") == "2.345"
示例14: test_plain_ordered_dict
def test_plain_ordered_dict(self):
config_tree = ConfigTree()
config_tree.put('"a.b"', 5)
config_tree.put('a."b.c"', [ConfigTree(), 2])
config_tree.get('a."b.c"')[0].put('"c.d"', 1)
d = OrderedDict()
d['a.b'] = 5
d['a'] = OrderedDict()
d['a']['b.c'] = [OrderedDict(), 2]
d['a']['b.c'][0]['c.d'] = 1
assert config_tree.as_plain_ordered_dict() == d
示例15: postParse
def postParse(self, instring, loc, token_list):
"""Create ConfigTree from tokens
:param instring:
:param loc:
:param token_list:
:return:
"""
config_tree = ConfigTree()
for element in token_list:
# from include then merge items
expanded_tokens = element.items() if isinstance(element, ConfigTree) else [element]
for tokens in expanded_tokens:
# key, value1, value2, ...
key = tokens[0].strip()
values = tokens[1:]
# empty string
if len(values) == 0:
config_tree.put(key, '')
else:
if isinstance(values[0], list):
# Merge arrays
config_tree.put(key, values[0], False)
for value in values[1:]:
config_tree.put(key, value, True)
else:
# Merge dict
for value in values:
if isinstance(value, ConfigList):
conf_value = list(value)
elif isinstance(value, ConfigValues):
conf_value = value
value.parent = config_tree
value.key = key
else:
conf_value = value
config_tree.put(key, conf_value)
return config_tree