def test_find_parent(self):
        root = struct.Node(label=['root'], children=[
            struct.Node(label=['root', '1'], children=[
                struct.Node(label=['root', '1', 'a']),
                struct.Node(label=['root', '1', 'b'])]),
            struct.Node(label=['root', '2'])])

        self.assertEqual(None, struct.find_parent(root, 'root'))
        # Doesn't find the _expected_ parent
        self.assertEqual(None, struct.find_parent(root, 'root-1-c'))
        self.assertEqual(root, struct.find_parent(root, 'root-1'))
        self.assertEqual(root, struct.find_parent(root, root.children[1]))
        self.assertEqual(root.children[0],
                         struct.find_parent(root, 'root-1-b'))
    def move_to_subpart(self, label, subpart_label):
        """ Move an existing node to another subpart. If the new subpart
        doesn't exist, create it. """
        if len(label.split('-')) != 2:
            logger.error(
                "Trying to move a non-section into a subpart: %s -> %s",
                label, subpart_label)
            return

        destination = find(self.tree, '-'.join(subpart_label))

        if destination is None:
            destination = self.create_new_subpart(subpart_label)

        subpart_with_node = find_parent(self.tree, label)

        if destination and subpart_with_node:
            node = find(subpart_with_node, label)
            other_children = [c for c in subpart_with_node.children
                              if c.label_id() != label]
            subpart_with_node.children = other_children
            destination.children = self.add_child(destination.children, node)

            if not subpart_with_node.children:
                self.delete('-'.join(subpart_with_node.label))
 def get_parent(self, node):
     """ Get the parent of a node. Returns None if parent not found. """
     parent = find_parent(self.tree, node)
     if not parent:  # e.g. because the node doesn't exist in the tree yet
         parent_label_id = get_parent_label(node)
         parent = find(self.tree, parent_label_id)
     return parent
 def get_parent(self, node):
     """ Get the parent of a node. Returns None if parent not found. """
     parent = find_parent(self.tree, node)
     if not parent:  # e.g. because the node doesn't exist in the tree yet
         parent_label_id = get_parent_label(node)
         parent = find(self.tree, parent_label_id)
     if not parent:
         logger.error("Could not find parent of %s. Misparsed amendment?",
                      node.label_id())
     return parent