Skip to content

Commit 09d4a5d

Browse files
committed
basic functions achieved
1 parent 6ce7b5e commit 09d4a5d

File tree

3 files changed

+64
-18
lines changed

3 files changed

+64
-18
lines changed

index_generator/__main__.py

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22
# *-- coding: utf-8 --*
33
import sys
44

5+
import os
6+
import os.path as path
7+
from datetime import datetime
8+
59
import jinja2
610
import argparse
711

12+
indexIgnore=['index.html', 'templates']
813

914
def main():
15+
global template
16+
global arguments
1017
parser = argparse.ArgumentParser()
1118
parser.add_argument('--version', '-V', action='store_true', default=False,
1219
help='Print version infomation and quit.')
@@ -17,25 +24,52 @@ def main():
1724
help='Default output filename.')
1825
parser.add_argument('--print', '-p', action='store_true', default=False, help='Whether to print to stdout.')
1926
arguments = parser.parse_args(sys.argv[1:])
20-
21-
22-
def generate(templateDir):
2327
environment = jinja2.Environment(
24-
loader=jinja2.PackageLoader('index_generator', templateDir),
28+
loader=jinja2.PackageLoader('index_generator', arguments.template),
2529
autoescape=jinja2.select_autoescape(['html', 'htm'])
2630
)
2731
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)
3864

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('..')
3973

4074
if __name__ == '__main__':
41-
main()
75+
main()

index_generator/templates/default/index.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,21 @@
1414
<th align="left">Last modified</th>
1515
<th align="left">Size</th>
1616
</tr>
17+
<tr>
18+
<td><a href="../">Parent directory/</a></td>
19+
<td>-</td>
20+
<td>-</td>
21+
</tr>
22+
{% for dir in ig.dirs %}
23+
<tr>
24+
<td><a href="{{ ig.currentPath }}/{{ dir.name }}">{{ dir.name }}/</a></td>
25+
<td>{{ dir.modified }}</td>
26+
<td>-</td>
27+
</tr>
28+
{% endfor %}
1729
{% for file in ig.files %}
1830
<tr>
19-
<td><a href="#">{{ file.name }}</a></td>
31+
<td><a href="{{ ig.currentPath }}/{{ file.name }}">{{ file.name }}</a></td>
2032
<td>{{ file.modified }}</td>
2133
<td>{{ file.size }}</td>
2234
</tr>

index_generator/templates/default/layout.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
5-
<title>{% block title %}Files: {{ ig.currentPath }}{% endblock %}</title>
5+
<title>{% block title %}Files: {{ ig.currentPath }}/{% endblock %}</title>
66
{% block extra %}{% endblock %}
77
</head>
88
<body>
99
{% block head %}
10-
<h1>Index of {{ ig.currentPath }}</h1>
10+
<h1>Index of {{ ig.currentPath }}/</h1>
1111
<hr>
1212
{% block files %}
1313
{% endblock %}

0 commit comments

Comments
 (0)