Skip to content

Commit fdae0f2

Browse files
implement no recursive print power
1 parent 6ce7b5e commit fdae0f2

File tree

6 files changed

+62
-16
lines changed

6 files changed

+62
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea
22
*.iml
33
__pycache__
4+
/*.html

index_generator/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
APP_NAME = 'Index Generator'
2+
APP_VERSION = '0.0.1'
3+
APP_URL = 'https://github.com/BruceZhang1993/index-generator'

index_generator/__main__.py

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
#!/usr/bin/env python3
22
# *-- coding: utf-8 --*
33
import sys
4+
import time
45

56
import jinja2
67
import argparse
8+
import os
9+
10+
from index_generator.models.entries import Entry
11+
from index_generator.models.exceptions import IndexGeneratorException
12+
from . import *
713

814

915
def main():
@@ -15,27 +21,50 @@ def main():
1521
parser.add_argument('--no-recursive', action='store_true', default=False, help='Do not generate recursively.')
1622
parser.add_argument('--name', '-n', type=str, default='index.html',
1723
help='Default output filename.')
18-
parser.add_argument('--print', '-p', action='store_true', default=False, help='Whether to print to stdout.')
24+
parser.add_argument('--print', '-P', action='store_true', default=False, help='Whether to print to stdout.')
25+
parser.add_argument('path', type=str, default='.', help='Path')
1926
arguments = parser.parse_args(sys.argv[1:])
27+
app(arguments)
28+
29+
30+
def app(args):
31+
if args.version:
32+
print(APP_NAME + ' ' + APP_VERSION + ' ' + APP_URL)
33+
sys.exit(0)
34+
if args.no_recursive:
35+
generate_once(args.template, args.path, args.name, args.print)
36+
else:
37+
raise IndexGeneratorException(IndexGeneratorException.NOT_IMPLEMENTED)
2038

2139

22-
def generate(templateDir):
40+
def generate_once(template_dir, path='.', name='index.html', if_print=False):
2341
environment = jinja2.Environment(
24-
loader=jinja2.PackageLoader('index_generator', templateDir),
42+
loader=jinja2.PackageLoader('index_generator', template_dir),
2543
autoescape=jinja2.select_autoescape(['html', 'htm'])
2644
)
27-
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-
}))
45+
template = environment.get_template(name)
46+
entries = list(map(lambda f: Entry(f), os.listdir(path)))
47+
files = []
48+
for entry in entries:
49+
files.append({
50+
'path': entry.path,
51+
'name': entry.name,
52+
'size': entry.size,
53+
'modified': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(entry.modified)),
54+
'mime': entry.mime
55+
})
56+
html = template.render(ig={
57+
'currentPath': '/',
58+
'files': files
59+
})
60+
if if_print:
61+
print(html)
62+
else:
63+
raise IndexGeneratorException(IndexGeneratorException.NOT_IMPLEMENTED)
3864

3965

4066
if __name__ == '__main__':
41-
main()
67+
try:
68+
main()
69+
except Exception as e:
70+
print(e)

index_generator/models/entries.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import os
2+
import mimetypes
3+
4+
5+
class Entry(object):
6+
def __init__(self, path, root='/'):
7+
self.name = os.path.basename(path)
8+
self.mime = mimetypes.guess_type(path)[0]
9+
self.path = root + path
10+
self.size = os.path.getsize(path)
11+
self.modified = os.path.getmtime(path)

index_generator/models/exceptions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class IndexGeneratorException(BaseException):
2+
NOT_IMPLEMENTED = 'Power has not been implemented.'

index_generator/templates/default/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</tr>
1717
{% for file in ig.files %}
1818
<tr>
19-
<td><a href="#">{{ file.name }}</a></td>
19+
<td><a href="{{ file.path }}">{{ file.name }}</a></td>
2020
<td>{{ file.modified }}</td>
2121
<td>{{ file.size }}</td>
2222
</tr>

0 commit comments

Comments
 (0)