-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
187 lines (140 loc) · 5.41 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# -------------------------------- IMPORT MODULES --------------------------------
from docopt import *
from colr import color
import os
from PIL import Image, ImageDraw, ImageFont
from datetime import datetime as dt
# -------------------------------- COLORS --------------------------------
colors = ['#f891a0', '#c1dee7', '#cb7f07', '#6389df', '#c7bab1', '#728239', '#a6b401', '#4a9396', '#fd742d']
# -------------------------------- FUNCTIONS --------------------------------
def generateTree(path):
global colors
i = 0
text = ""
for root, dirs, files in os.walk(path):
root = root.replace(path, "")
# count the seperator -> it tells the level
level = root.count(os.sep)
if level == 0:
text += color(path + "\n", fore='firebrick')
for file in files:
text += color(" "*level + "|--", 'firebrick')
text += color(file + "\n", colors[i])
i += 1
else:
text += color("|" + "--"*level + root + "\n", fore='firebrick', style='bright')
for file in files:
if i == len(colors): i = 0
text += color("|" + " "*level + "|--", 'firebrick')
text += color(file + "\n", colors[i])
i += 1
text += color("|\n", fore='firebrick')
return text
def searchFile(dir_path, filename):
global colors
i = 0
text = ""
for root, dirs, files in os.walk(dir_path):
for file in files:
if filename in file:
if i == len(colors):
i = 0
text += color("\n" + os.path.join(root,file), fore=colors[i])
i += 1
return text
def getWidth(text):
max_ch = 0
no_of_char = 0
for ch in text:
if ch == "\n":
if no_of_char > max_ch:
max_ch = no_of_char
no_of_char = 0
else:
no_of_char += 1
return max_ch*8
def getHeight(text):
lines = 0
for ch in text:
if ch == "\n":
lines += 1
return lines*23
def saveImageForTree(dir_path):
global colors
i = 0
line = 5
width = getWidth(generateTree(dir_path))
height = getHeight(generateTree(dir_path))
#print(f"{width} -> {height}")
image = Image.new('RGB', (width, height), color='white')
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('bahnschrift.ttf', 18)
for root, dirs, files in os.walk(dir_path):
root = root.replace(dir_path, "")
# count the seperator -> it tells the level
level = root.count(os.sep)
if level == 0:
draw.text((5,line), dir_path + "\n", fill='firebrick', font=font)
line += 22
for file in files:
draw.text((5,line), " "*level + "|--" + file + "\n", fill=colors[i], font=font)
i += 1
line += 22
else:
draw.text((5,line), "|" + "--"*level + root + "\n", fill='firebrick', font=font)
line += 22
for file in files:
if i == len(colors): i = 0
draw.text((5,line), "|" + " "*level + "|--" + file + "\n", fill=colors[i], font=font)
i += 1
line += 22
draw.text((5,line), "|\n", fill='white', font=font)
line += 22
filename = "DTG_CMD_" + str(dt.now().year) + str(dt.now().month) + str(dt.now().day) + "_" + str(dt.now().hour) + str(dt.now().minute) + str(dt.now().second) + str(dt.now().microsecond) + '.png'
image.save(filename)
print(color("Image saved successfully as " + filename + ".", fore='green'))
image.show(command='display')
def saveImageForSearch(dir_path, filename):
global colors
i = 0
line = 5
width = getWidth(searchFile(dir_path, filename))
height = getHeight(searchFile(dir_path, filename))
#print(f"{width} -> {height}")
image = Image.new('RGB', (width, height), color='white')
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('bahnschrift.ttf', 18)
for root, dirs, files in os.walk(dir_path):
for file in files:
if filename in file:
if i == len(colors):
i = 0
draw.text((5,line), os.path.join(root,file), fill=colors[i], font=font)
i += 1
line += 22
filename = "DTG_CMD_" + str(dt.now().year) + str(dt.now().month) + str(dt.now().day) + "_" + str(dt.now().hour) + str(dt.now().minute) + str(dt.now().second) + str(dt.now().microsecond) + '.png'
image.save(filename)
print(color("Image saved successfully as " + filename + ".", fore='green'))
image.show(command='display')
# -------------------------------- DOCOPT USAGE --------------------------------
usage = '''
Directory Tree Generator
Usage:
main.py tree <directory_path>
main.py search <directory_path> <filename>
main.py save <directory_path>
main.py save <directory_path> <filename>
'''
# -------------------------------- MAIN --------------------------------
args = docopt(usage)
if args['tree']:
output = generateTree(args['<directory_path>'])
print(output)
if args['search']:
output = searchFile(args['<directory_path>'], args['<filename>'])
print(output)
if args['save']:
if(args['<filename>']):
saveImageForSearch(args['<directory_path>'], args['<filename>'])
else:
saveImageForTree(args['<directory_path>'])