-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcodeview.py
More file actions
73 lines (52 loc) · 1.65 KB
/
codeview.py
File metadata and controls
73 lines (52 loc) · 1.65 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
import io
import logging
from zipfile import ZipFile
from interface.backend.minio_api import MissingFile, download_buffer
from django.template.defaulttags import register
log = logging.getLogger(__name__)
def extract_file(request, submission, filename):
buff = io.BytesIO()
try:
download_buffer(f"{submission.pk}.zip", buff)
except MissingFile:
return io.StringIO("The archive is missing!")
submission_archive = ZipFile(buff)
try:
file = submission_archive.read(filename)
except KeyError:
return io.StringIO("The file is missing!")
return io.StringIO(str(file, encoding="ascii"))
def tree_view(request, submission):
buff = io.BytesIO()
try:
submission.download(buff)
except MissingFile:
return {}
submission_archive = ZipFile(buff)
return make_dict(submission_archive)
def make_dict(submission_archive):
tree = {}
for path in submission_archive.infolist():
node = tree
split_path = path.filename.split("/")
for index, level in enumerate(split_path):
if path.is_dir():
obj = {}
else:
if index != len(split_path) - 1:
obj = {}
else:
obj = {"$path": path.filename}
node = node.setdefault(level, obj)
return tree
@register.filter
def get_item(dictionary, key):
return dictionary.get(key)
def table_maker(file):
return file.read().splitlines()
@register.filter
def with_path(things, path):
return things.filter(path=path)
@register.filter
def with_line(things, line):
return things.filter(line=line)