2
2
# *-- coding: utf-8 --*
3
3
import sys
4
4
5
+ import os
6
+ import os .path as path
7
+ from datetime import datetime
8
+
5
9
import jinja2
6
10
import argparse
7
11
12
+ indexIgnore = ['index.html' , 'templates' ]
8
13
9
14
def main ():
15
+ global template
16
+ global arguments
10
17
parser = argparse .ArgumentParser ()
11
18
parser .add_argument ('--version' , '-V' , action = 'store_true' , default = False ,
12
19
help = 'Print version infomation and quit.' )
@@ -17,25 +24,52 @@ def main():
17
24
help = 'Default output filename.' )
18
25
parser .add_argument ('--print' , '-p' , action = 'store_true' , default = False , help = 'Whether to print to stdout.' )
19
26
arguments = parser .parse_args (sys .argv [1 :])
20
-
21
-
22
- def generate (templateDir ):
23
27
environment = jinja2 .Environment (
24
- loader = jinja2 .PackageLoader ('index_generator' , templateDir ),
28
+ loader = jinja2 .PackageLoader ('index_generator' , arguments . template ),
25
29
autoescape = jinja2 .select_autoescape (['html' , 'htm' ])
26
30
)
27
31
template = environment .get_template ('index.html' )
28
- print (template .render (ig = {
29
- 'currentPath' : '/example' ,
30
- 'files' : [
31
- {
32
- 'name' : 'test.txt' ,
33
- 'modified' : '-' ,
34
- 'size' : '12'
35
- }
36
- ]
37
- }))
32
+
33
+ generate ()
34
+
35
+
36
+ def generate (currentDir = '' ):
37
+ filelist = []
38
+ dirlist = []
39
+ for file in os .listdir ():
40
+ if file in indexIgnore :
41
+ continue
42
+ if path .isdir (file ):
43
+ dirlist .append ({
44
+ 'name' : file ,
45
+ 'modified' : datetime .fromtimestamp (path .getmtime (file )).strftime ('%Y-%m-%d %H:%M' )
46
+ })
47
+ else :
48
+ filelist .append ({
49
+ 'name' : file ,
50
+ 'modified' : datetime .fromtimestamp (path .getmtime (file )).strftime ('%Y-%m-%d %H:%M' ),
51
+ 'size' : path .getsize (file )
52
+ })
53
+
54
+ index = template .render (ig = {
55
+ 'currentPath' : currentDir ,
56
+ 'dirs' : dirlist ,
57
+ 'files' : filelist
58
+ })
59
+ if arguments .print :
60
+ print (index )
61
+
62
+ with open ('index.html' ,'w' ) as f :
63
+ print (index , file = f )
38
64
65
+ if not arguments .no_recursive and dirlist :
66
+ for file in dirlist :
67
+ if arguments .print :
68
+ print ('------------------------------------------------' )
69
+ os .chdir (file ['name' ])
70
+ generate (currentDir + '/' + file ['name' ])
71
+
72
+ os .chdir ('..' )
39
73
40
74
if __name__ == '__main__' :
41
- main ()
75
+ main ()
0 commit comments