def insert_node(self, xpath, icon=""): """ Method inserts node by xpath. Used for DB select simulation. """ parent_xpath = get_parent_xpath(xpath) parent = self.get_node(parent_xpath) if parent is not None and self._is_node_deleted(parent): logger.info("The '{}' element is marked as deleted. So ignoring request.".format(xpath)) return for node in self.all_nodes: if hasattr(node, "key") and is_grandsire_xpath(xpath, node.key) and self._is_node_deleted(node): logger.info("The '{}' element is marked as deleted. So ignoring request.".format(node.key)) return element = create_element(get_tag_name(xpath)) # element.xpath = xpath element.key = xpath element.icon = icon if parent is None: # Has no parent: unlinked one yet or root self.roots.append(element) else: element.parentNode = parent parent.append(element) element.key = "{}{}".format(xpath, self._get_node_num(element)) self._link_children(element) self._validate_roots()
def add_node(self, xpath): """ Method adds a new node which is specified by xpath. It can be used as validation one for creating element only if exists. """ if self.get_node(xpath) is not None: logger.info("'{}' is already exists, no need to add it.".format(xpath)) return parent_xpath = get_parent_xpath(xpath) parent_node = self.get_node(parent_xpath) if parent_node is None: if is_root(xpath): logger.debug("'{}' is the root so adding into the root list.".format(xpath)) root = create_element(get_tag_name(xpath)) root.key = xpath self.add_new_root(root) return root else: # If the parent haven't created yet. logger.error("There is no node for specified xpath: '{}'. Creating it.".format(parent_xpath)) parent_node = self.add_node(parent_xpath) logger.info("Node for specified xpath: '{}' was created it.".format(parent_xpath)) if parent_node is not None: element = create_element(get_tag_name(xpath)) element.key = xpath element.parentNode = parent_node parent_node.append(element) self.add_new_node(element) return element else: print("s") return