Skip to content

Commit ecfd688

Browse files
Add experimental file icon support
1 parent e012be7 commit ecfd688

File tree

5 files changed

+19
-7
lines changed

5 files changed

+19
-7
lines changed

index_generator/__main__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def app(args):
6262
raise IndexGeneratorTemplateNotFound(str(e))
6363

6464

65-
def generate_once(theme, root, files, name, if_print, base=os.path.sep, human=False, template=''):
65+
def generate_once(theme, root, files, name, if_print, base=os.path.sep, human=False, template='', iconset='material'):
6666
if not template:
6767
environment = jinja2.Environment(
6868
loader=jinja2.PackageLoader('index_generator', 'templates/' + theme),
@@ -88,7 +88,8 @@ def generate_once(theme, root, files, name, if_print, base=os.path.sep, human=Fa
8888
'size': entry.size,
8989
'modified': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(entry.modified)),
9090
'mime': entry.mime,
91-
'isDir': entry.isDir
91+
'isDir': entry.isDir,
92+
'icon': entry.icon
9293
})
9394
html = template.render(ig={
9495
'root': base + root.lstrip('.*' + os.path.sep),
@@ -107,7 +108,7 @@ def generate_once(theme, root, files, name, if_print, base=os.path.sep, human=Fa
107108
print(html, file=f)
108109

109110

110-
def generate_recursively(theme, path, name, if_print, max_depth=0, base=os.path.sep, human=False, template=''):
111+
def generate_recursively(theme, path, name, if_print, max_depth=0, base=os.path.sep, human=False, template='', iconset='material'):
111112
os.chdir(path)
112113
for root, dirs, files in os.walk('.'):
113114
if max_depth != 0 and root.count(os.sep) >= max_depth:
@@ -124,7 +125,7 @@ def generate_recursively(theme, path, name, if_print, max_depth=0, base=os.path.
124125
print('files: {}'.format(files))
125126
print('-----------------------------------------')
126127

127-
generate_once(theme, root, dirs + files, name, if_print, base=base, human=human, template=template)
128+
generate_once(theme, root, dirs + files, name, if_print, base=base, human=human, template=template, iconset=iconset)
128129

129130

130131
if __name__ == '__main__':
Lines changed: 3 additions & 0 deletions
Loading

index_generator/models/entries.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import mimetypes
3+
import base64
34

45

56
def sizeof_fmt(num, suffix='B'):
@@ -14,7 +15,7 @@ def sizeof_fmt(num, suffix='B'):
1415

1516

1617
class Entry(object):
17-
def __init__(self, file, root, base=os.path.sep, human=False):
18+
def __init__(self, file, root, base=os.path.sep, human=False, iconset='material'):
1819
path = root + os.path.sep + file
1920
self.path = base + path.lstrip('.*' + os.path.sep)
2021
self.name = os.path.basename(path)
@@ -25,3 +26,5 @@ def __init__(self, file, root, base=os.path.sep, human=False):
2526
self.size = os.path.getsize(path)
2627
self.modified = os.path.getmtime(path)
2728
self.isDir = os.path.isdir(path)
29+
with open(os.path.dirname(__file__) + os.path.sep + '..' + os.path.sep + 'icons' + os.path.sep + iconset + os.path.sep + 'default.svg', 'rb') as f:
30+
self.icon = base64.b64encode(f.read()).decode()

index_generator/templates/default/index.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,30 @@
66
{% endblock %}
77
{% block files %}
88
<table>
9-
<col width="600">
9+
<col width="40">
10+
<col width="560">
1011
<col width="180">
1112
<col width="300">
1213
<tr>
14+
<th align="left"></th>
1315
<th align="left">Filename</th>
1416
<th align="left">Size</th>
1517
<th align="left">Last modified</th>
1618
</tr>
1719
<tr>
20+
<td></td>
1821
<td><a href="../">Parent directory/</a></td>
1922
<td>-</td>
2023
<td>-</td>
2124
</tr>
2225
{% for file in ig.files %}
2326
<tr>
2427
{% if file.isDir %}
28+
<td><img src="data:image/svg+xml;base64,{{ file.icon }}" width="30" /></td>
2529
<td><a href="{{ file.path }}">{{ file.name }}/</a></td>
2630
<td>-</td>
2731
{% else %}
32+
<td><img src="data:image/svg+xml;base64,{{ file.icon }}" width="30" /></td>
2833
<td><a href="{{ file.path }}">{{ file.name }}</a></td>
2934
<td>{{ file.size }}</td>
3035
{% endif %}

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
'index_generator.models'
1919
],
2020
package_data={
21-
'index_generator': ['templates/*/*']
21+
'index_generator': ['templates/*/*', 'icons/*/*']
2222
},
2323
include_package_data=True,
2424
url=APP_URL,

0 commit comments

Comments
 (0)