-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment_parser.go
More file actions
104 lines (93 loc) · 2.65 KB
/
assignment_parser.go
File metadata and controls
104 lines (93 loc) · 2.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
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
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"time"
)
const (
defaultGrading = "Pass/fail"
defaultAuto = "Automatic"
defaultManual = "TA Approval"
noApproval = "No Approval"
defaultIndividual = "Individually"
defaultGroup = "Group"
)
// AssignmentInfo contains fields present in the assignment.json files which accompany each lab.
type AssignmentInfo struct {
Order int
Name string
ScriptFile string
Deadline string // time.Time
ShortDeadline string // only date
Year string // derived from deadline
AutoApprove bool
IsGroupLab bool
ScoreLimit int
Reviewers int
Title string
HoursMin int
HoursMax int
Effort string // replaces the HoursMin-HoursMax
// defaults to $COURSE $NAME
Subject string
// defaults to defaultGrading
Grading string
// SubmissionType is interpreted based on the value of IsGroupLab ("Individually" or "Group")
SubmissionType string
ApproveType string
CourseOrg string // this field is populated after parsing by the main script
}
func parseAssignment(filename string) (*AssignmentInfo, error) {
jsonFile, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("failed to read json file: %w", err)
}
res := &AssignmentInfo{}
err = json.Unmarshal(jsonFile, res)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal json: %w", err)
}
if res.Subject == "" {
res.Subject = fmt.Sprintf("%s %s", strings.ToUpper(course()), name())
}
if res.ApproveType == "" {
if res.AutoApprove {
res.ApproveType = defaultAuto
} else {
res.ApproveType = defaultManual
}
}
switch res.Grading {
case "":
res.Grading = defaultGrading
case "No grading":
res.ApproveType = noApproval
}
if res.IsGroupLab {
res.SubmissionType = defaultGroup
} else {
res.SubmissionType = defaultIndividual
}
if res.Effort == "" {
if res.HoursMin > 0 && res.HoursMax > 0 {
res.Effort = fmt.Sprintf("%d-%d hours", res.HoursMin, res.HoursMax)
}
}
// copied from qf.TimeLayout
const layout = "2006-01-02T15:04:05"
deadline, err := time.Parse(layout, res.Deadline)
if err == nil {
res.Deadline = fmt.Sprintf("%s %d, %d %02d:%02d",
deadline.Month(), deadline.Day(), deadline.Year(), deadline.Hour(), deadline.Minute())
res.ShortDeadline = fmt.Sprintf("%s %d", deadline.Month(), deadline.Day())
res.Year = fmt.Sprintf("%d", deadline.Year())
if res.Year != year() {
fmt.Printf("Warning(%s): deadline (%s) does not match environment variable $YEAR=%s\n", res.Name, res.Deadline, year())
}
} else {
fmt.Printf("Warning(%s): failed to parse deadline: %s\n", res.Name, err)
}
return res, nil
}