-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathfile_list.py
More file actions
96 lines (83 loc) · 2.83 KB
/
file_list.py
File metadata and controls
96 lines (83 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Copyright (c) 2022 The Regents of the University of Michigan
# All rights reserved.
# This software is licensed under the BSD 3-Clause License.
import mimetypes
import os
from flask import render_template
from signac_dashboard.module import Module
class FileList(Module):
"""Lists files in the job directory with download links.
:param context: Supports :code:`'JobContext'`.
:type context: str
:param prefix_jobid: Whether filenames should be prefixed with the job id
when being downloaded (default: :code:`True`).
:type prefix_jobid: bool
"""
_supported_contexts = {"JobContext"}
def __init__(
self,
name="File List",
context="JobContext",
template="cards/file_list.html",
prefix_jobid=True,
**kwargs,
):
super().__init__(
name=name,
context=context,
template=template,
**kwargs,
)
self.prefix_jobid = prefix_jobid
def _get_icon(self, filename):
_, ext = os.path.splitext(filename)
ext = ext.lstrip(".").lower()
icon_map = {
"pdf": "fa-file-pdf",
"zip": "fa-file-archive",
"tar": "fa-file-archive",
"gz": "fa-file-archive",
"7z": "fa-file-archive",
}
if ext in icon_map:
return icon_map[ext]
mtype, _ = mimetypes.guess_type(filename)
if mtype:
if mtype.startswith("image/"):
return "fa-file-image"
if mtype.startswith("audio/"):
return "fa-file-audio"
if mtype.startswith("video/"):
return "fa-file-video"
if "word" in mtype:
return "fa-file-word"
if "excel" in mtype or "spreadsheet" in mtype or "csv" in mtype:
return "fa-file-excel"
if "powerpoint" in mtype or "presentation" in mtype:
return "fa-file-powerpoint"
if "x-" in mtype or "json" in mtype:
return "fa-file-code"
if mtype.startswith("text/"):
return "fa-file-alt"
return "fa-file"
def download_name(self, job, filename):
if self.prefix_jobid:
return f"{str(job)}_{filename}"
else:
return filename
def get_cards(self, job):
files = sorted(
(
{
"name": filename,
"jobid": job._id,
"download": self.download_name(job, filename),
"icon": self._get_icon(filename),
}
for filename in os.listdir(job.path)
),
key=lambda filedata: filedata["name"],
)
return [
{"name": self.name, "content": render_template(self.template, files=files)}
]