How to use the pyhocon.config_tree.ConfigTree function in pyhocon

To help you get started, we’ve selected a few pyhocon examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github chimpler / pyhocon / tests / test_config_tree.py View on Github external
def test_keyerror_raised(self):
        config_tree = ConfigTree()
        config_tree.put("a", {'b': 5})

        with pytest.raises(KeyError):
            assert config_tree['c']
github chimpler / pyhocon / tests / test_config_tree.py View on Github external
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"
github chimpler / pyhocon / tests / test_config_tree.py View on Github external
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
github chimpler / pyhocon / tests / test_config_tree.py View on Github external
assert config_tree.pop("string", "default") == "string"
        assert config_tree.pop("string-new", "default") == "default"
        assert config_tree == ConfigTree()

        with pytest.raises(ConfigMissingException):
            assert config_tree.pop("string-new")

        config_tree.put("list", [1, 2, 3])
        assert config_tree.pop("list", [4]) == [1, 2, 3]
        assert config_tree.pop("list-new", [4]) == [4]
        assert config_tree == ConfigTree()

        config_tree.put("config", {'a': 5})
        assert config_tree.pop("config", {'b': 1}) == {'a': 5}
        assert config_tree.pop("config-new", {'b': 1}) == {'b': 1}
        assert config_tree == ConfigTree()

        config_tree = ConfigTree()
        config_tree.put('key', 'value')
        assert config_tree.pop('key', 'value') == 'value'
        assert 'key' not in config_tree

        config_tree = ConfigTree()
        config_tree.put('a.b.c.one', 1)
        config_tree.put('a.b.c.two', 2)
        config_tree.put('"f.k".g.three', 3)

        exp = OrderedDict()
        exp['a'] = OrderedDict()
        exp['a']['b'] = OrderedDict()
        exp['a']['b']['c'] = OrderedDict()
        exp['a']['b']['c']['one'] = 1
github chimpler / pyhocon / tests / test_config_tree.py View on Github external
config_tree.put("list", [1, 2, 3])
        assert config_tree.pop("list", [4]) == [1, 2, 3]
        assert config_tree.pop("list-new", [4]) == [4]
        assert config_tree == ConfigTree()

        config_tree.put("config", {'a': 5})
        assert config_tree.pop("config", {'b': 1}) == {'a': 5}
        assert config_tree.pop("config-new", {'b': 1}) == {'b': 1}
        assert config_tree == ConfigTree()

        config_tree = ConfigTree()
        config_tree.put('key', 'value')
        assert config_tree.pop('key', 'value') == 'value'
        assert 'key' not in config_tree

        config_tree = ConfigTree()
        config_tree.put('a.b.c.one', 1)
        config_tree.put('a.b.c.two', 2)
        config_tree.put('"f.k".g.three', 3)

        exp = OrderedDict()
        exp['a'] = OrderedDict()
        exp['a']['b'] = OrderedDict()
        exp['a']['b']['c'] = OrderedDict()
        exp['a']['b']['c']['one'] = 1
        exp['a']['b']['c']['two'] = 2

        exp['f.k'] = OrderedDict()
        exp['f.k']['g'] = OrderedDict()
        exp['f.k']['g']['three'] = 3

        assert config_tree.pop('a.b.c').as_plain_ordered_dict() == exp['a']['b']['c']
github chimpler / pyhocon / pyhocon / config_parser.py View on Github external
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:
                    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:
                            value = os.environ.get(key)
                            if value is not None:
                                cls._do_substitute(substitution, value)
                                continue
                            if substitution.optional:  # special case, when self optional referencing without existing
                                cls._do_substitute(substitution, None)
github chimpler / pyhocon / pyhocon / config_tree.py View on Github external
prev_value=l_value,
                            type=l_value.__class__.__name__)
                    )
            else:
                # if there was an override keep overide value
                if isinstance(value, ConfigValues):
                    value.parent = self
                    value.key = key_elt
                    value.overriden_value = self.get(key_elt, None)
                self._push_history(key_elt, value)
                self[key_elt] = value
        else:
            next_config_tree = super(ConfigTree, self).get(key_elt)
            if not isinstance(next_config_tree, ConfigTree):
                # create a new dictionary or overwrite a previous value
                next_config_tree = ConfigTree()
                self._push_history(key_elt, next_config_tree)
                self[key_elt] = next_config_tree
            next_config_tree._put(key_path[1:], value, append)
github chimpler / pyhocon / pyhocon / config_tree.py View on Github external
def _put(self, key_path, value, append=False):
        key_elt = key_path[0]
        if len(key_path) == 1:
            # if value to set does not exist, override
            # if they are both configs then merge
            # if not then override
            if key_elt in self and isinstance(self[key_elt], ConfigTree) and isinstance(value, ConfigTree):
                if self.root:
                    new_value = ConfigTree.merge_configs(ConfigTree(), self[key_elt], copy_trees=True)
                    new_value = ConfigTree.merge_configs(new_value, value, copy_trees=True)
                    self._push_history(key_elt, new_value)
                    self[key_elt] = new_value
                else:
                    ConfigTree.merge_configs(self[key_elt], value)
            elif append:
                # If we have t=1
                # and we try to put t.a=5 then t is replaced by {a: 5}
                l_value = self.get(key_elt, None)
                if isinstance(l_value, ConfigValues):
                    l_value.tokens.append(value)
                    l_value.recompute()
                elif isinstance(l_value, ConfigTree) and isinstance(value, ConfigValues):
                    value.overriden_value = l_value
                    value.tokens.insert(0, l_value)
                    value.recompute()