Example #1
0
def tree(root_dir, line_spacing=1, show_hidden=False):
    if not os.path.exists(root_dir) or not os.path.isdir(root_dir):
        msg = 'Error opening,  "{0}" does not exist or is not a directory.'
        return msg.format(root_dir)
    from compage import nodeutil
    nodes = []
    first_iteration = True
    for root, dirs, files in os.walk(root_dir):
        if not show_hidden:
            dirs[:] = filter(lambda x: not x.startswith('.'), dirs)
            files[:] = filter(lambda x: not x.startswith('.'), files)

        root_name = root or root_dir
        parent = os.path.basename(os.path.abspath(root_name))

        if first_iteration:
            parent_node = nodeutil.Node(parent, None)
            first_iteration = False
            nodes.append(parent_node)
        else:
            parent_node = filter(lambda n: n.name == parent, nodes)[-1]

        children = sorted(dirs + files)
        for child in children:
            child_node = nodeutil.Node(child, parent=parent_node)
            nodes.append(child_node)
    return nodeutil.Tree(nodes).render(line_spacing=line_spacing)
Example #2
0
 def test_eq(self):
     other_nodes = []
     for node in self.tree.nodes:
         other_nodes.append(nodeutil.Node(
             name=node.name, parent=node.parent, uid=node.uid))
     other_tree = nodeutil.Tree(other_nodes)
     self.assertEqual(self.tree, other_tree)
Example #3
0
    def test_unique_node_validation(self):
        chars = 'abcdefghijkl'
        nodes = [nodeutil.Node(name=char, uid='uid') for char in chars]
        with self.assertRaises(exception.TreeCreationError) as e:
            nodeutil.Tree(nodes)

        err_msg = 'Some of the nodes have same uids, unable to create tree'
        self.assertEqual(e.exception.message, err_msg)
Example #4
0
    def test_validate_parent(self):
        parent = 'not a valid parent'
        with self.assertRaises(exception.NodeCreationError) as e:
            nodeutil.Node('node', parent)

        err_msg = ("parent '{0}' should be of"
                   " type {1} or None").format(parent, nodeutil.Node)
        self.assertEqual(e.exception.message, err_msg)
Example #5
0
 def setUp(self):
     self.parent_node = nodeutil.Node('parent', None)
     self.parent_uid = self.parent_node.uid
     self.node_uid = uuid.uuid4().hex
     self.node = nodeutil.Node('node', self.parent_node)
     self.node_uid = self.node.uid
Example #6
0
 def test_neq(self):
     other_node = nodeutil.Node('node', self.parent_node, uid='foo')
     self.assertNotEqual(self.node, other_node)
Example #7
0
 def test_eq(self):
     other_node = nodeutil.Node(
         'node', parent=self.parent_node, uid=self.node_uid)
     self.assertEqual(self.node, other_node)
Example #8
0
 def test_uid(self):
     node = nodeutil.Node(name='node', parent=None, uid='foo')
     self.assertEqual(node.uid, 'foo')