-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.py
120 lines (106 loc) · 3.19 KB
/
tree.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
#! /usr/bin/env python3
# tree.py
#
# author: Bugaevsky Igor
#
#
from os import listdir, sep#, walk
from os.path import abspath, basename, isdir
from sys import argv, platform
# Имя файла со стилем
css_style = "_styles.css"
# та же папка для иконок
icons_dir = "."
if platform == "win32":
codepage = "CP-1251"
else:
codepage = "UTF-8"
site_template = {
"head": """<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\
\"http://www.w3.org/TR/html4/strict.dtd\">
<html lang=\"ru-RU\"><head><meta http-equiv=\"Content-Type\"\
content=\"text/html;charset=""" + codepage + """\"><!--[if gte IE 9 ]>\
<link rel=\"stylesheet\" type=\"text/css\" href=\"_styles.css\"\
media=\"screen\"><![endif]-->
<!--[if !IE]>--><link rel=\"stylesheet\" type=\"text/css\"\
href=\"""" + css_style + """\" media=\"screen\"><!--<![endif]-->\
</head>""",
"title_begin": "<body><div id=\"titled\"><p>",
"title_end": "</p></div>\n",
"tree_begin": "<ol class=\"tree\">\n",
"file": ["<li class=\"file\">", "</li>\n"],
"dir_begin": "<li><label for=\"folder",
"dir_in_1": "\">",
"dir_in_2": "</label> <input type=\"checkbox\" checked id=\"folder",
"dir_in_3": "\" /><ol>\n",
"dir_end": "</ol></li>\n",
"end": "</ol></body></html>"
}
num = 0
data = ""
def link(full_path):
return("<a href=\"" + full_path + "\">" + basename(full_path) + "</a>")
def tree(dir):
global num
global data
global dwalk
path = dir + sep
dirs = []
files = []
#data = ""
for fl in listdir(dir):
if isdir(path + fl):
dirs.append(path + fl)
else:
files.append(path + fl)
data += site_template["dir_begin"] + str(num) +\
site_template["dir_in_1"] +\
link(dir) + site_template["dir_in_2"] + str(num) +\
site_template["dir_in_3"]
num += 1
dirs.sort()
files.sort()
if len(dirs) > 0:
for sing in dirs:
tree(sing)
if len(files) > 0:
for sing in files:
data += site_template["file"][0] + link(sing) + site_template["file"][1]
data += site_template["dir_end"]
def start(dir):
global data
data += site_template["head"]
data += site_template["title_begin"] + dir + site_template["title_end"]
data += site_template["tree_begin"]
#dwalk = walk(dir)
tree(dir)
data += site_template["end"]
with open("index.html", "w") as site_file:
site_file.write(data)
def usage():
return '''Usage: %s [-f] <PATH>
Print tree structure of path specified.
Options:
-f Print files as well as directories
PATH Path to process''' % basename(argv[0])
def main():
if len(argv) == 1:
print(usage())
elif len(argv) == 2:
# print just directories
path = argv[1]
if isdir(path):
start(abspath(path))
else:
print('ERROR: \'' + path + '\' is not a directory')
elif len(argv) == 3 and argv[1] == '-f':
# print directories and files
path = argv[2]
if isdir(path):
start(abspath(path))
else:
print('ERROR: \'' + path + '\' is not a directory')
else:
print(usage())
if __name__ == '__main__':
main()