-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert_to_pdf.py
115 lines (96 loc) · 3.64 KB
/
convert_to_pdf.py
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
import shlex
import subprocess
import os
import schedule_grading
import grader
base_start = r"""\documentclass[9pt, onecolumn]{extarticle}
%\usepackage[top=1.0in, left=1.0in, right=1.0in, bottom=1.0in]{geometry}
\usepackage{longtable}
\usepackage{hyperref}
\title{$ass_title}
\date{}
\begin{document}
\maketitle{}
$ass_desc
\begin{center}
\begin{longtable}{| l | l | l | l |}
\hline
\textbf{Student Last} & \textbf{Student First} & \textbf{Grader Name} & \textbf{Grader Email} \\
\hline
\endfirsthead
\multicolumn{4}{c}%
{\tablename\ \thetable\ -- \textit{Continued from previous page}} \\
\hline
\textbf{Student Last} & \textbf{Student First} & \textbf{Grader Name} & \textbf{Grader Email} \\
\hline
\endhead
\hline \multicolumn{4}{r}{\textit{Continued on next page}} \\
\endfoot
\hline
\endlastfoot""" + "\n"
base_end = r"""\end{longtable}
\end{center}
\end{document}""" + "\n"
def make_pdf(assignment, graders, output_filename=None, pdf_title=None):
start_filename = assignment.id + '.csv'
tex_filename = assignment.id + '.tex'
if not output_filename:
output_filename = '$ass_id'
output_filename = output_filename.replace('$ass_name',
assignment.long_name).replace('$ass_id', assignment.id)
if not pdf_title:
pdf_title = '$ass_name'
pdf_title = pdf_title.replace('$ass_name', assignment.long_name)\
.replace('$ass_id', assignment.id)
with open(start_filename, 'r') as csv_file:
lines = csv_file.readlines()
lines = sorted(map(lambda x: x.strip().split(';'), lines), key=lambda l:l[3] + ', ' + l[2])
print(lines)
with open(tex_filename, 'w') as tex_file:
start = base_start.replace('$ass_name', assignment.long_name)
start = base_start.replace('$ass_title', pdf_title)
start = start.replace('$ass_desc', assignment.desc)
tex_file.write(start)
for line in lines:
grader_name = line[0], line[1]
grader = None
for g in graders:
if grader_name == g.name:
grader = g
if not grader:
schedule_grading.error('Could not match grader {}.'.format(grader_name))
else:
tex_file.write('\t{0} & {1} & {2} {3} & \\href{{mailto:{4}}}{{{4}}}\\\\\\hline\n'\
.format(line[3], line[2], grader.name[0], grader.name[1], grader.email))
tex_file.write(base_end)
#running this twice because LaTeX is dumb like that
for i in range(2):
proc=subprocess.Popen(shlex.split(
'pdflatex -interactive=nonstopmode -jobname="{}" {}'
.format(output_filename, tex_filename)))
proc.communicate()
proc=subprocess.Popen(shlex.split(
'rm "{0}.tex" "{1}.out" "{1}.log" "{1}.aux"'.format(assignment.id,
output_filename)))
proc.communicate()
def main():
graders = schedule_grading.load_graders('graders.csv')
assignments = schedule_grading.load_assignments('assignment_list.txt')
try:
with open('output_pdf.txt', 'r') as output_pdf:
output_filename = output_pdf.read().strip()
except FileNotFoundError:
schedule_grading.warning('Could not open output_pdf.txt.')
output_filename = None
try:
with open('pdf_title.txt', 'r') as pdf_title:
pdf_title = pdf_title.read().strip()
except FileNotFoundError:
schedule_grading.warning('Could not open pdf_title.txt.')
pdf_title = None
for assignment in assignments:
filename = assignment.id + '.csv'
if os.path.isfile(filename):
make_pdf(assignment, graders, output_filename, pdf_title)
if __name__ == '__main__':
main()