def _validate(self, config_path, node): # We are going to mangle the path, so copy it first path = config_path[:] # There are two tricky cases: tag nodes and leaf nodes, # which need special validation of the immediate child. if node.is_leaf() or node.is_tag(): next_item = None if path: next_item = path.pop(0) else: raise PathValidationError( "Configuration path [%s] is incomplete" % vpu.path_to_string(config_path)) (result, errors) = self._validate_leaf_or_tag_node(self.types, node, next_item) if not result: msg = "" if node.is_leaf(): msg = "Value validation failed" else: msg = "Node name validation failed" raise PathValidationError(msg, additional_messages=errors) else: if node.is_leaf(): if not path: return True else: # There are extra items after a leaf node raise PathValidationError( "Configuration path [%s] has extra items" % vpu.path_to_string(config_path)) else: if path: next_item = path.pop(0) next_node = node.find_child(next_item) self._validate(path, next_node) else: return True else: # It's a normal node, just recurse to it, if we have where to # recurse if path: next_node = node.find_child(path.pop(0)) self._validate(path, next_node) else: return True
def validate(self, config_path, config_level=None): """Validates a config path against :param config_path: A list representing config path :type config_path: list :param config_level: The level for the config path. """ path = config_path[:] if config_level is not None: path = config_level + path try: self._validate(path, self.tree) except vyconf.tree.ChildNotFoundError: raise PathValidationError( "Configuration path [%s] is not valid" % vpu.path_to_string(config_path))
def set(self, config_path, abspath=False): _config_path = self._make_path(config_path, abspath) self._validator.validate(_config_path) path, value = self._validator.split_path(_config_path) if value: node = None if not self.exists(path, abspath=True): node = self._proposed_config.insert_child(path) else: node = self._proposed_config.get_child(path) if self._validator.check_node(path, lambda x: x.is_multi()): if node.has_value(value): raise SessionError("Node \"{0}\" already has value \"{1}\"".format( vpu.path_to_string(path), value)) else: node.add_value(value) else: node.set_value(value) else: self._proposed_config.insert_child(path)
def test_path_to_string_separator(self): path_str = vpu.path_to_string(['foo', 'bar'], separator="/") self.assertEqual(path_str, "foo/bar")
def test_path_to_string_default(self): # Default separator is space path_str = vpu.path_to_string(['foo', 'bar']) self.assertEqual(path_str, "foo bar")