-
Notifications
You must be signed in to change notification settings - Fork 34
/
gen.py
39 lines (30 loc) · 946 Bytes
/
gen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os
class Tree:
def __init__(self, node="", *children):
self.node = node
self.width = len(node)
if children:
self.children = children
else:
self.children = []
def __str__(self):
return "%s" % (self.node)
def __repr__(self):
return "%s" % (self.node)
def __getitem__(self, key):
if isinstance(key, int) or isinstance(key, slice):
return self.children[key]
if isinstance(key, str):
for child in self.children:
if child.node == key:
return child
def __iter__(self):
return self.children.__iter__()
def __len__(self):
return len(self.children)
def gentree(path):
root = Tree(os.path.basename(path))
if os.path.isdir(path):
for f in os.listdir(path):
root.children.append(gentree(os.path.join(path, f)))
return root