-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
67 lines (59 loc) · 1.83 KB
/
main.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import re
import random
SPACES = 3
INDENT = SPACES * " "
ROOT_DIRECTORY = os.getcwd()
ID = random.randrange(1000, 9999)
FILE_NAME = "tree" + str(ID) + ".txt"
IGNORE = [ r'^\/?(?:\w+\/)*(\.\w+)' ]
'''
@returns: Boolean.
@params: string item: file name.
@desc: Returns True if file should be ignored based on its name.
'''
def _ignore(item):
for pattern in IGNORE:
if re.match(pattern, item): return True
'''
@returns: Boolean.
@params: string item, string directory
@desc: Returns True if the item is a file name.
'''
def _isfile(item, directory):
return (os.path.isfile(item) if directory == "." else os.path.isfile(os.path.join(directory, item)))
'''
@returns: string.
@params: string directory: key word arg defaults to "."
@desc: Returns string representing current folders tree structure
'''
def _generate_tree(directory = "."):
directory_items = os.listdir(directory)
directory_path = ROOT_DIRECTORY if directory == "." else directory
directory_path_array = directory_path.split("/")
indents = len(directory.split("/"))
Root = directory_path_array[-1]
Tree = Root + "/" + "\n"
for item in directory_items:
if _ignore(item): continue
Tree += indents * INDENT
if _isfile(item, directory):
Tree += item + "\n"
else:
Tree += _generate_tree(directory = directory + "/" + item)
return Tree
def create_tree_file(filename = None):
try:
global FILE_NAME
if filename is None: filename = FILE_NAME
IGNORE.append(filename)
file = open(filename, "w")
tree = _generate_tree()
file.write(tree)
return True
except Exception as e:
print(e)
finally:
file.close()
if __name__ == "__main__":
create_tree_file()