-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigHandler.py
More file actions
174 lines (150 loc) · 6.33 KB
/
ConfigHandler.py
File metadata and controls
174 lines (150 loc) · 6.33 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from pathlib import Path
from textwrap import dedent
from canvasapi import course
from slugify import slugify
import tomlkit
from CanvasHelper import canvas
from CanvasHelper import get_course_url, get_assignment_url
from settings import PROGRAM_DIR
CONFIG_DIR = PROGRAM_DIR / "config_files"
if not CONFIG_DIR.exists():
CONFIG_DIR.mkdir(parents=True, exist_ok=True)
class ConfigHandler():
"""
Parses configuration files which will dictate how student
submissions are tested and graded.
`autograder.toml` provides a mapping of Canvas course IDs to their
corresponding configuration files.
Automatically generates templates for files.
"""
def __init__(self):
self.config_file_path = self.get_config_file_path()
print(self.config_file_path)
if not self.config_file_path.exists():
print("Generating autograder.toml")
self.generate_autograder_config()
self.assignment_mappings = {}
def get_config_file_path(self) -> Path:
""" Return absolute path of `autograder.toml`. """
path = CONFIG_DIR / "autograder.toml"
return path
def get_config_file(self):
""" Get the config file as a TOMLDocument """
with open(self.config_file_path, "r") as f:
doc = tomlkit.load(f)
return doc
def get_course_config_file(self, course_id: int):
path = self.get_course_config_path(course_id)
with open(CONFIG_DIR / path, "r") as f:
doc = tomlkit.load(f)
return doc
def generate_autograder_config(self):
"""
Generate a config file for the entire Autograder program.
"""
doc = tomlkit.document()
doc.add(tomlkit.comment("Autograder uses this to map from Canvas course IDs to course config files."))
doc.add(tomlkit.nl())
doc.add("course_configs", tomlkit.table())
with open(self.config_file_path, "w") as f:
s = tomlkit.dumps(doc)
f.write(s)
# tomlkit.dump(doc, f)
def add_course_to_autograder_config(self, course_id: int):
course = canvas.get_course(course_id)
doc = self.get_config_file()
fname = f"{slugify(course.name)}-{course.id}.toml"
doc["course_configs"].add(str(course.id), fname)
with open(self.config_file_path, "w") as f:
tomlkit.dump(doc, f)
def get_course_config_path(self, course_id: int) -> Path:
"""
Retrieve corresponding course configuration file using course ID.
"""
doc = self.get_config_file()
# if str(course.id) not in doc["course_configs"]:
# self.add_course_to_autograder_config(course)
# doc = self.get_config_file()
fname = doc["course_configs"][str(course_id)]
return CONFIG_DIR / fname
def check_active(self, course_id, assign_id):
if course_id not in self.assignment_mappings:
self.parse_assignment_configs(course_id)
if assign_id not in self.assignment_mappings[course_id]:
return False
doc = self.get_assignment_config(course_id, assign_id)
# What should happen if "active" key isn't included?
return "active" in doc and doc["active"]
def generate_course_config(self, course_id: int):
"""
Generate configuration file for a given course. If the file already exists, update with any new assignments.
"""
course = canvas.get_course(course_id)
# check if course file already listed in autograder.toml
doc = type(self.get_config_file())
if course_id not in doc['course_configs']:
self.add_course_to_autograder_config(course_id)
# Retrieve the path of the configuration file from autograder.toml
config_path = self.get_course_config_path(course_id)
# If the file doesn't exist, generate it using a template.
if not config_path.exists():
s = f"""
# AUTOGRADING CONFIGURATION FOR {course.name}
[meta]
course_name = "{course.name}"
course_id = {course.id}
url = "{get_course_url(course.id)}"
[course]
lua_module = ""
unit_test_dir = ""
"""
s = dedent(s)
doc = tomlkit.parse(s)
for a in course.get_assignments():
assignment_name = slugify(a.name)
aot= tomlkit.aot()
aot.append(tomlkit.item({"type": "UnzipDirectory"}))
doc.add(assignment_name, {
"id": a.id,
"url": get_assignment_url(course.id, a.id),
"active": False,
"input": "",
"modules": aot
})
with open(config_path, "w") as f:
tomlkit.dump(doc, f)
return True
else:
return False
def update_meta(self):
""" If the id of an assignment or course is different, update the metadata. """
pass
def get_course_settings(self, course_id: int):
doc = self.get_course_config_file(course_id)
return dict(doc)["course"]
def parse_assignment_configs(self, course_id: int):
""" Get hashmap of id mappings to their corresponding assignment configurations. """
doc = self.get_course_config_file(course_id)
assignments = dict(doc)
del assignments["meta"]
del assignments["course"]
mappings = {v['id']: v for v in dict(assignments).values() if 'id' in v}
self.assignment_mappings.update({course_id: mappings})
def get_assignment_config(self, course_id: int, assignment_id: int):
"""
Read associated course file and return appropriate assignment config
"""
# doc = self.get_course_config_file(course_id)
# return dict(doc)[str(assign_id)]
if course_id not in self.assignment_mappings:
self.parse_assignment_configs(course_id)
return self.assignment_mappings[course_id][assignment_id]
ch = ConfigHandler()
def get_config_handler() -> ConfigHandler:
return ch
if __name__ == "__main__":
course = canvas.get_course(43491)
ch = ConfigHandler()
ch.generate_course_config(course)
print(ch.get_assignment_config(course, 155997))
print(ch.get_course_settings(course))