5
5
import os
6
6
import jinja2
7
7
import argparse
8
- import os .path as path
9
8
from datetime import datetime
10
9
11
10
from index_generator .models .entries import Entry
12
11
from index_generator .models .exceptions import IndexGeneratorException
13
12
from . import *
14
13
15
- indexIgnore = [ 'index.html' , 'templates' ]
14
+ indexIgnore = ( 'index.html' 'images' 'favicon.ico' )
16
15
17
16
18
17
def main ():
19
- global template
20
- global arguments
21
18
parser = argparse .ArgumentParser ()
22
19
parser .add_argument ('--version' , '-V' , action = 'store_true' , default = False ,
23
20
help = 'Print version infomation and quit.' )
@@ -27,7 +24,8 @@ def main():
27
24
parser .add_argument ('--name' , '-n' , type = str , default = 'index.html' ,
28
25
help = 'Default output filename.' )
29
26
parser .add_argument ('--print' , '-P' , action = 'store_true' , default = False , help = 'Whether to print to stdout.' )
30
- parser .add_argument ('path' , type = str , default = '.' , help = 'Path' )
27
+ parser .add_argument ('path' , type = str , default = '' , help = 'Path' )
28
+ parser .add_argument ('--depth' ,'-d' , type = int , default = 0 , help = 'Set cutoff depth.' )
31
29
arguments = parser .parse_args (sys .argv [1 :])
32
30
app (arguments )
33
31
@@ -37,22 +35,26 @@ def app(args):
37
35
print (APP_NAME + ' ' + APP_VERSION + ' ' + APP_URL )
38
36
sys .exit (0 )
39
37
if args .no_recursive :
40
- generate_once (args .template , args .path , args .name , args .print )
38
+ generate_once (args .template , args .path , os . listdir ( args . path ), args .name , args .print )
41
39
else :
42
- raise IndexGeneratorException ( IndexGeneratorException . NOT_IMPLEMENTED )
40
+ generate_recursively ( args . template , args . path , args . name , args . print , args . depth )
43
41
44
42
45
- def generate_once (template_dir , path = '.' , name = 'index.html' , if_print = False ):
43
+ def generate_once (template_dir , root , files , name , if_print ):
46
44
environment = jinja2 .Environment (
47
45
loader = jinja2 .PackageLoader ('index_generator' , template_dir ),
48
46
autoescape = jinja2 .select_autoescape (['html' , 'htm' ])
49
47
)
50
48
template = environment .get_template (name )
51
- entries = list (map (lambda f : Entry (f ), os .listdir (path )))
52
- entries .sort (key = lambda x : x .isDir , reverse = True )
53
- files = []
49
+
50
+ entries = list (map (lambda f : Entry (f ,root ), files ))
51
+ #entries.sort(key=lambda x: x.isDir, reverse=True)
52
+
53
+ filelist = []
54
54
for entry in entries :
55
- files .append ({
55
+ if entry .name in indexIgnore :
56
+ continue
57
+ filelist .append ({
56
58
'path' : entry .path ,
57
59
'name' : entry .name ,
58
60
'size' : entry .size ,
@@ -61,52 +63,33 @@ def generate_once(template_dir, path='.', name='index.html', if_print=False):
61
63
'isDir' : entry .isDir
62
64
})
63
65
html = template .render (ig = {
64
- 'currentPath ' : '/' ,
65
- 'files' : files
66
+ 'root ' : '/' + root . lstrip ( '.*/' ) ,
67
+ 'files' : filelist
66
68
})
69
+
67
70
if if_print :
68
71
print (html )
69
72
else :
70
- raise IndexGeneratorException ( IndexGeneratorException . NOT_IMPLEMENTED )
71
-
73
+ with open ( root + os . path . sep + name , 'w' ) as f :
74
+ print ( html , file = f )
72
75
73
- def generate (currentDir = '' ):
74
- filelist = []
75
- dirlist = []
76
- for file in os .listdir ():
77
- if file in indexIgnore :
76
+ def generate_recursively (template_dir , path , name , if_print , max_depth = 0 ):
77
+ for root , dirs , files in os .walk (path ):
78
+ if max_depth != 0 and root .count (os .sep ) >= max_depth :
79
+ dirs .clear ()
78
80
continue
79
- if path .isdir (file ):
80
- dirlist .append ({
81
- 'name' : file ,
82
- 'modified' : datetime .fromtimestamp (path .getmtime (file )).strftime ('%Y-%m-%d %H:%M' )
83
- })
84
- else :
85
- filelist .append ({
86
- 'name' : file ,
87
- 'modified' : datetime .fromtimestamp (path .getmtime (file )).strftime ('%Y-%m-%d %H:%M' ),
88
- 'size' : path .getsize (file )
89
- })
90
-
91
- index = template .render (ig = {
92
- 'currentPath' : currentDir ,
93
- 'dirs' : dirlist ,
94
- 'files' : filelist
95
- })
96
- if arguments .print :
97
- print (index )
98
-
99
- with open ('index.html' , 'w' ) as f :
100
- print (index , file = f )
81
+
82
+ dirs .sort ()
83
+ files .sort ()
101
84
102
- if not arguments . no_recursive and dirlist :
103
- for file in dirlist :
104
- if arguments . print :
105
- print ('------------------------------------------------' )
106
- os . chdir ( file [ 'name' ] )
107
- generate ( currentDir + '/' + file [ 'name' ] )
108
-
109
- os . chdir ( '..' )
85
+ if if_print :
86
+ print ( '-----------------------------------------' )
87
+ print ( 'Path: ' + root )
88
+ print ('dirs: {}' . format ( dirs ) )
89
+ print ( 'files: {}' . format ( files ) )
90
+ print ( '-----------------------------------------' )
91
+
92
+ generate_once ( template_dir , root , dirs + files , name , if_print )
110
93
111
94
112
95
if __name__ == '__main__' :
0 commit comments