-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_processor.py
More file actions
31 lines (23 loc) · 931 Bytes
/
file_processor.py
File metadata and controls
31 lines (23 loc) · 931 Bytes
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
import tempfile
import os
class SubmissionDirectory:
def __init__(self, submission_id):
self.tmpdir = tempfile.mkdtemp()
# create a directory with the submission id
self.submission_dir = os.path.join(self.tmpdir, str(submission_id))
os.mkdir(self.submission_dir)
self.files = []
def __enter__(self):
return self.submission_dir
def __exit__(self, exc_type, exc_val, exc_tb):
for filename in self.files:
os.remove(os.path.join(self.submission_dir, filename))
os.rmdir(self.submission_dir)
os.rmdir(self.tmpdir)
def add_file(self, filename, content):
self.files.append(filename)
with open(os.path.join(self.submission_dir, filename), "w") as f:
f.write(content)
def get_file(self, filename):
with open(os.path.join(self.submission_dir, filename), "r") as f:
return f.read()