/
cover.go
281 lines (219 loc) · 6.53 KB
/
cover.go
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package ingester
import (
"fmt"
"path/filepath"
"reflect"
"runtime"
"sort"
"strings"
"github.com/rs/zerolog"
"github.com/timdrysdale/gradex-cli/pagedata"
"github.com/timdrysdale/gradex-cli/parsesvg"
"github.com/timdrysdale/gradex-cli/util"
"github.com/timdrysdale/pool"
"vbom.ml/util/sortorder"
)
type CoverPageTask struct {
Command CoverPageCommand
Path string
}
func (g *Ingester) CoverPage(cp CoverPageCommand, logger *zerolog.Logger) error {
//find pages in processed fir
// for each page, mangle the name to get the coverpage name
// sum up all the questions
// make sure only questions in questions conf are included in the cover page
files, err := g.GetFileList(cp.FromPath)
if err != nil {
logger.Error().
Str("dir", cp.FromPath).
Str("error", err.Error()).
Msg("Error getting files")
return err
}
cpTasks := []CoverPageTask{}
for _, path := range files {
if !IsPDF(path) {
continue
}
cpTasks = append(cpTasks, CoverPageTask{
Path: path,
Command: cp,
})
}
//>>>>>>>>>>>>>>>>>>>>> TASKS READY
N := len(cpTasks)
tasks := []*pool.Task{}
for i := 0; i < N; i++ {
cpt := cpTasks[i]
newtask := pool.NewTask(func() error {
err := DoOneCoverPage(cpt, logger)
if err == nil {
setDone(cpt.Path, logger)
logger.Debug().Str("file", cpt.Path).Msg("set done file at source")
logger.Info().
Str("file", cpt.Path).
Msg(fmt.Sprintf("Finished processing %s", cpt.Path))
return nil
} else {
logger.Error().
Str("file", cpt.Path).
Str("error", err.Error()).
Msg(fmt.Sprintf("Error processing %s", cpt.Path))
return err
}
})
tasks = append(tasks, newtask)
}
p := pool.NewPool(tasks, runtime.GOMAXPROCS(-1))
logger.Info().
Int("speed-up", runtime.GOMAXPROCS(-1)).
Msg(fmt.Sprintf("Using parallel processing to get x%d speed-up\n", runtime.GOMAXPROCS(-1)))
p.Run()
var numErrors int
for _, task := range p.Tasks {
if task.Err != nil {
logger.Error().
Str("error", task.Err.Error()).
Msg(fmt.Sprintf("Processing problem %v", task.Err))
numErrors++
}
}
// report how we did
if numErrors > 0 {
logger.Error().
Int("error-count", numErrors).
Int("script-count", N).
Msg(fmt.Sprintf("Processing finished with coverpage tasks returning <%d> errors from <%d> scripts\n", numErrors, N))
} else {
logger.Info().
Int("error-count", numErrors).
Int("script-count", N).
Msg(fmt.Sprintf("Processing finished <%d> scripts without any errors\n", N))
}
return nil
}
func DoOneCoverPage(ct CoverPageTask, logger *zerolog.Logger) error {
pdMap := make(map[int]pagedata.PageData)
path := ct.Path
cp := ct.Command
pdMap, err := pagedata.UnMarshalAllFromFile(path)
if err != nil {
logger.Error().
Str("file", path).
Str("error", err.Error()).
Msg("Error getting pagedata")
}
pageDetails := selectPageDetailsWithMarks(pdMap)
QMap := getQMap(pageDetails)
originalQMap := QMap
for _, k := range cp.Questions {
k = strings.TrimSpace(strings.ToUpper(k))
if _, ok := QMap[k]; !ok {
QMap[k] = "-"
}
}
// put missing marks back into weird named questions that
// are missing marks because the mark is in the name section
MarkSubMap, err := getMarkSubMap(cp.ConfigPath, pageDetails[0].Item.Who)
if err == nil {
fmt.Printf("Applying Mark substitutions for %s\n", pageDetails[0].Item.Who)
QMap = applyMarkSubMap(QMap, MarkSubMap)
logger.Info().
Str("file", path).
Str("path", cp.ConfigPath).
Str("who", pageDetails[0].Item.Who).
Msg("Found mark-substitutions-<who>.csv")
} else {
logger.Error().
Str("file", path).
Str("error", err.Error()).
Str("path", cp.ConfigPath).
Str("who", pageDetails[0].Item.Who).
Msg("No mark-substitutions-<who>.csv available")
}
// figure out which "proper" questions to add these marks to
QSubMap, err := getQSubMap(cp.ConfigPath)
if err == nil {
fmt.Printf("Applying Question substitutions for %s\n", pageDetails[0].Item.Who)
QMap = applyQSubMap(QMap, QSubMap)
logger.Info().
Str("file", path).
Str("path", cp.ConfigPath).
Msg("Found question-substitutions.csv")
} else {
logger.Error().
Str("file", path).
Str("error", err.Error()).
Str("path", cp.ConfigPath).
Msg("No question-substitutions.csv available")
}
if !reflect.DeepEqual(originalQMap, QMap) {
fmt.Printf("Question substitutions made for %s\nWas:\n", pageDetails[0].Item.Who)
util.PrettyPrintStruct(originalQMap)
fmt.Println("Now:")
util.PrettyPrintStruct(QMap)
logger.Info().
Str("original", fmt.Sprintf("%v", originalQMap)).
Str("updated", fmt.Sprintf("%v", QMap)).
Str("path", cp.ConfigPath).
Str("who", pageDetails[0].Item.Who).
Msg("Qmap update")
}
pageNumber := 0 //starts at zero
Prefills := parsesvg.DocPrefills{}
Prefills[pageNumber] = make(map[string]string)
Prefills[pageNumber]["page-number"] = "add"
var thisPageData pagedata.PageData
// get first page data
for _, pdm := range pdMap {
thisPageData = pdm
break
}
fields := []pagedata.Field{}
for k, v := range QMap {
fields = append(fields, pagedata.Field{
Key: "pf-q-" + k,
Value: v,
})
}
thisPageData.Current.Data = fields
Prefills[pageNumber]["author"] = thisPageData.Current.Item.Who
Prefills[pageNumber]["date"] = thisPageData.Current.Item.When
Prefills[pageNumber]["title"] = shortenAssignment(thisPageData.Current.Item.What)
Prefills[pageNumber]["for"] = thisPageData.Current.Process.For
var qkeys []string
for k := range QMap {
qkeys = append(qkeys, k)
}
sort.Sort(sortorder.Natural(qkeys))
for idx, qk := range qkeys {
question := fmt.Sprintf("question-%02d", idx)
mark := fmt.Sprintf("mark-awarded-%02d", idx)
Prefills[pageNumber][question] = qk
Prefills[pageNumber][mark] = QMap[qk]
}
pageFilename := filepath.Join(cp.ToPath, strings.TrimSuffix(filepath.Base(path), filepath.Ext(path))+"-cover.pdf")
current := thisPageData.Current
current.Original.Number = 0 //fix cover pagenumber in reports
current.Own.Number = 0
thisPageData.Current = current
contents := parsesvg.SpreadContents{
SvgLayoutPath: cp.TemplatePath,
SpreadName: cp.SpreadName,
PageNumber: pageNumber,
PdfOutputPath: pageFilename,
PageData: thisPageData,
TemplatePathsRelative: true,
Prefills: Prefills,
}
err = parsesvg.RenderSpreadExtra(contents)
if err != nil {
msg := fmt.Sprintf("Error rendering spread for cover page for (%s) because %v\n", path, err)
logger.Error().
Str("file", path).
Str("error", err.Error()).
Msg(msg)
fmt.Println(msg)
}
return err
}