def clear_taxonomic_coverage(package_name): # When user selects Clear Taxonomic Coverage to get rid of coverage imported from XML, we delete it and # set the flag that says its OK for ezEML to handle even though the package was imported from XML. eml_node = load_eml(filename=package_name) coverage_node = eml_node.find_single_node_by_path([names.DATASET, names.COVERAGE]) if coverage_node: taxonomic_coverage_nodes = coverage_node.find_all_children(names.TAXONOMICCOVERAGE) for taxonomic_coverage_node in taxonomic_coverage_nodes: coverage_node.remove_child(taxonomic_coverage_node) Node.delete_node_instance(taxonomic_coverage_node.id) clear_taxonomy_imported_from_xml(eml_node, package_name)
def expand(node: Node): references = list() node.find_all_descendants(names.REFERENCES, references) ids = _register_ids(node) for reference in references: if reference.content not in ids: msg = f"ID not found for REFERENCE '{reference}'" raise ValueError(msg) source_node = ids[reference.content] destination_node = reference.parent destination_node.remove_child(reference) Node.delete_node_instance(reference.id) for source_child in source_node.children: source_child_copy = source_child.copy() destination_node.add_child(source_child_copy)
def test_delete_node_no_children(self): eml = Node(names.EML) eml.add_attribute('packageId', 'edi.23.1') eml.add_attribute('system', 'metapype') access = Node(names.ACCESS, parent=eml) access.add_attribute('authSystem', 'pasta') access.add_attribute('order', 'allowFirst') eml.add_child(access) allow = Node(names.ALLOW, parent=access) access.add_child(allow) principal = Node(names.PRINCIPAL, parent=allow) principal.content = 'uid=gaucho,o=EDI,dc=edirepository,dc=org' allow.add_child(principal) permission = Node(names.PERMISSION, parent=allow) permission.content = 'all' allow.add_child(permission) node = Node.get_node_instance(principal.id) self.assertIs(principal, node) Node.delete_node_instance(eml.id, children=False) self.assertIn(principal.id, Node.store)
def test_delete_node_no_children(): eml = Node(names.EML) eml.add_attribute("packageId", "edi.23.1") eml.add_attribute("system", "metapype") access = Node(names.ACCESS, parent=eml) access.add_attribute("authSystem", "pasta") access.add_attribute("order", "allowFirst") eml.add_child(access) allow = Node(names.ALLOW, parent=access) access.add_child(allow) principal = Node(names.PRINCIPAL, parent=allow) principal.content = "uid=gaucho,o=EDI,dc=edirepository,dc=org" allow.add_child(principal) permission = Node(names.PERMISSION, parent=allow) permission.content = "all" allow.add_child(permission) node = Node.get_node_instance(principal.id) assert principal is node Node.delete_node_instance(eml.id, children=False) assert principal.id in Node.store
def prune(n: Node, strict: bool = False) -> list: """ Prune in place all non-valid nodes from the tree Args: n: Node strict: Returns: List of pruned nodes Side-effects: Non-valid nodes are pruned from the tree """ pruned = list() if n.name != "metadata": try: node(n) except UnknownNodeError as ex: logger.debug(f"Pruning: {n.name}") pruned.append((n, str(ex))) if n.parent is not None: n.parent.remove_child(n) Node.delete_node_instance(n.id) return pruned except ChildNotAllowedError as ex: r = rule.get_rule(n.name) children = n.children.copy() for child in children: if not r.is_allowed_child(child.name): logger.debug(f"Pruning: {child.name}") pruned.append((child, str(ex))) n.remove_child(child) Node.delete_node_instance(child.id) except MetapypeRuleError as ex: logger.info(ex) children = n.children.copy() for child in children: pruned += prune(child, strict) if strict and child not in pruned: try: node(child) except MetapypeRuleError as ex: logger.debug(f"Pruning: {child.name}") pruned.append((child, str(ex))) n.remove_child(child) Node.delete_node_instance(child.id) return pruned