/
svgscantron.py
649 lines (532 loc) · 20 KB
/
svgscantron.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#!/usr/bin/python
# svgscantron.py: a module for writing properly formatted scantron answer sheets
# using output from a LaTeX compile (or any other CR-separated integer data source)
#
# The formatting only supports one particular scantron form at this time.
#
#
# Version 0.2
# October 2015
# Jesse Hamner
# j h a m n e r at gee mail period com
#
#
import os
import sys
import re
import string
import argparse
import subprocess
import datetime
import getpass
import io
def readInSheetParameters(**params):
filename = params['parameterfile']
if (params['reallyverbose']):
print "reading in parameters from %s" % filename
print "parameters thus far are: " % params
fileobject = open(filename, 'r')
fi = fileobject.read()
fs=fi.rstrip().split('\n')
for i in fs:
# print str(i)
if (re.search("^#", i) ) :
continue
if (re.search("^\s*$", i) ) :
continue
i=re.sub('\s*=\s*','\t', i)
# print(str(i))
[j,k]=i.split('\t')
j=j.rstrip()
k=k.rstrip()
# print(str(j) + ':' + str(k) )
params[j]=k
return (params)
def printRowPartTwo(*args, **params):
qu=params['qu']
num=params['num']
params['thisx']=0
params['thisy']=0
answers=args[1]
columnxincrement=params['columnxincrement']
print(str(answers))
yincrement=float(params['yincrement'])
thisx=float(params['x1'])
columnxincrement=float(params['columnxincrement'])
for col in range(0,int(params['colcount']) ):
thisy=float(params['y1'])
for y in range(0,int(params['rowcount'])):
num=num+1
try:
answers[qu]
except:
answers.append(-9)
drawFive( )
thisy=thisy+yincrement
qu=qu+1
thisx=thisx+columnxincrement
filehandle.write("<!-- question counter is " + str(qu) + " -->\n")
params['qu']=qu
params['num']=num
return params
# array is a list of [qu, num]
def printRowByColumn(x1, y1, colcount, rowcount,yincrement, columnxincrement, circdist, radius, hexcolor, answers, array, filehandle, alph, onlyprintanswers, verbose, reallyverbose):
qu=array[0]
num=array[1]
yincrement=float(yincrement)
thisx=float(x1)
columnxincrement = float(columnxincrement)
for col in range(0,colcount):
thisy=float(y1)
for y in range(0,rowcount):
num=num+1
try:
answers[qu]
except:
answers.append(-9)
# x, y, xincrement, radius, color, correct, number, verbose
drawFive(thisx,thisy, circdist, radius, hexcolor, answers[qu],num,filehandle, alph, onlyprintanswers,verbose, reallyverbose)
thisy=thisy + yincrement
qu=qu+1
thisx=thisx+columnxincrement
filehandle.write("<!-- question counter is " + str(qu) + " -->\n")
array=[qu,num]
return array
def checkArguments(arguments):
print (str(arguments))
try:
onlyprintanswers=int(arguments['onlyprintanswers'])
if (onlyprintanswers==0):
print("onlyprintanswers equals " + str(onlyprintanswers) + \
"; the script will print the whole sheet." )
elif (onlyprintanswers==1):
print("onlyprintanswers equals " + str(onlyprintanswers) + \
"; the script will print only the answer circles." )
else:
print("The variable indicating whether the script should print " + \
"only filled circles has an error.")
sys.exit()
except:
print("Something went wrong with parsing the program arguments, at " + \
"least the 'onlyprintanswers' argument.")
sys.exit()
try:
verbose=int(arguments['verbose'])
# print('verbose equals ' + str(verbose))
if (verbose==1):
print "verbose is set to TRUE."
elif (verbose==0):
print "verbose is set to FALSE."
else:
print "We shouldn't be here! verbose should have an answer."
sys.exit()
except:
print "verbose is undef and caused an exception; that's a problem."
sys.exit()
try:
reallyverbose=int(arguments['reallyverbose'])
if (reallyverbose==0):
print("reallyverbose is set to FALSE.")
elif (reallyverbose==1):
print("reallyverbose is set to TRUE.")
else:
print("The variable indicating 'really verbose' status is not " + \
"behaving correctly. You should fix it.")
sys.exit()
except:
reallyverbose=0 # ill behaved but reallyverbose isn't critical right now.
return 1
def checkOutputFile(arguments, verbose):
try:
outputfilename = str(arguments['outputfilename'])
outputfilename = re.sub('\.svg', '', outputfilename)
except:
print("\nSomething went horribly wrong if you're seeing " + \
"this error message.\n")
sys.exit()
return outputfilename
def writeSVGHeader(**params): # width="279.5mm", height="216.2mm", verbose):
# write the file header for SVG:
try:
height= params['pageheight']
except:
height=216.2 # assumes landscape, not portrait
try:
width= params['pagewidth']
except:
width=279.5
header='''<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" width="%smm" height="%smm" >
<g id="main">''' % (width,height)
try:
params['filehandle'].write(header + '\n' )
return 1
except:
print "WARNING COULD NOT WRITE TO OUTPUT FILE!\n\n"
sys.exit()
return 0
# make css:
def addCSS(**params): #rgbcolor="80,80,180", verbose):
try:
rgbcolor=params['rgbcolor']
except:
rgbcolor="80,80,180"
try:
if (params['verbose']):
print "rgbcolor set to %s" % rgbcolor
except:
print "something went wrong with assigning rgbcolor."
sys.exit()
cssreal= '''
<defs>
<style type="text/css">
<![CDATA[
circle.rightanswer {
stroke:rgb(%s);
stroke-width:0.1mm;
fill:black;
}
circle.blank {
stroke:rgb(%s);
stroke-width:0.1mm;
fill:none;
}
text.sizenineend {
font-family:Arial;
font-size:9px;
text-anchor:end;
fill:rgb(%s);
}
text.sizetenend {
font-family:Arial;
font-size:10px;
text-anchor:end;
fill:rgb(%s);
}
text.sizeninemiddle {
font-family:Arial;
font-size:9px;
text-anchor:middle;
fill:rgb(%s);
}
line.strut {
stroke:black;
stroke-width:1.5mm
}
line.fatstrut {
stroke:black;
stroke-width:3mm
}
text.regular {
font-size:10px;
font-family:Arial;
text-anchor:start;
fill:black;
}
]]>
</style>
</defs>
''' % (rgbcolor, rgbcolor, rgbcolor, rgbcolor, rgbcolor)
params['filehandle'].write(cssreal + '\n')
return 1
def convertWithInkscape(inkscapeinstalled,inkscapepath,outputfilename,verbose):
'''If the script has found Inkscape, convert the svg file to png.
Otherwise, return 0. '''
#pathtome=os.path.realpath(__file__)
pathtome=os.getcwd() # NOTE this file should be in the same directory with everything else!
outputsvg=str(outputfilename + '.svg')
outputpng=str(outputfilename + '.png')
if verbose:
print("Path to this script is: " + str(pathtome))
if inkscapeinstalled:
appstring=str('"' + inkscapepath + '" ' )
string=str(' -b ffffff -y 255 -z -d 300 --export-png="' + pathtome + "/" + str(outputpng) + '" ' + \
'"' + pathtome + '/' + str(outputsvg) + '" ')
submit=str(appstring + string)
if verbose:
print(appstring)
print('-----------------')
print(string)
print('=================')
print(submit)
subprocess.call(submit, shell=True)
else:
return 0
return 1
def parseOptions():
if (sys.argv[0] == ""):
print("WARNING: you need to provide a filename for the output file.")
sys.exit()
username=getpass.getuser()
today=datetime.date.today()
defaultdate=str("Sheet created by " + username + ", " + str(today) )
# print(defaultdate)
parser=argparse.ArgumentParser(description='''makesvg.py; a python script
that converts a series of numeric multiple-choice test answers into a
properly mapped set of Scantron answers that should print accurately
enough to automate production of the test key.\n The script options
require that the user add a filename from the command line using the
"-f" option, and the user may add zero or more options, including
verbose output, really verbose output, and a choice to print only
the dots or the whole scantron form.
Example:
python makesvg.py -f outputsvg.svg -v \n''')
parser.add_argument("-f", "--file", action='store', \
dest="outputfilename", default="NONAME",
help="provide a filename for output SVG and PNG files", metavar="FILE")
parser.add_argument("-v", "--verbose", \
action='store_true', dest="verbose",
help="provide more verbose output to STDOUT", default=0 )
parser.add_argument("-o", "--only-scantron", "--only-dots", "--dots-only", \
action='store_true', dest="onlyprintanswers", default=0,
help="print only the black circles needed for your answer key")
parser.add_argument("-vv", "--reallyverbose", \
action='store_true', dest="reallyverbose",
help="provide even MORE verbose output to STDOUT", default=0 )
parser.add_argument("-a", "--answerlist", metavar="FILE", \
action='store', dest="answerlist", default="answerlist.txt",\
help='''provide an alternate source for the file of carriage-return
separated integer scantron answers. The default value is 'answerlist.txt' ''')
parser.add_argument("-l" ,"--label", metavar="TEXT", \
action='store', dest="sheetlabel", default=defaultdate , \
help='''A user-specified label for the top left of the form, usually
for identification of the test, class, semester, etc.''')
parser.add_argument("-i", "--input-file", metavar="FILENAME", \
action='store', dest="inputformat", default="old4521.txt", \
help='''A user-specifed parameters file to format the location and spacing
of the dots within the output SVG. Defaults to old4521.txt, which should be consulted
regarding the format of the input file. Other options include "pdp4521.txt".''')
arguments = parser.parse_args()
args = vars(arguments)
# print("Looks like the input filename argument is: " + str(args['outputfilename']))
return (args)
def __drawStub(x,y,height,width,color,filehandle,onlyprintanswers,verbose):
'''Draw a small rectangle/line (strut) aligned with x,y specified'''
if(onlyprintanswers):
return 1
if (color=="black"):
if (width==1.5):
classname='class="strut"'
if (width==3):
classname='class="fatstrut"'
else:
classname='style="stroke:' + color + ';stroke-width:' + \
str(width) + 'mm"'
y2 = y + height
mark = str('<line x1="' + str(x) + 'mm" y1="' + str(y) + 'mm" x2="' + \
str(x) + 'mm" y2="' + str(y2) + 'mm" ' + classname + '/>\n')
filehandle.write(str(mark))
return 1
def drawFatMark(x,y,color,filehandle, onlyprintanswers,verbose):
'''This strut is fatter than others.'''
if(color==""):
color="black"
width=3
height=4
__drawStub(x,y,height,width,color,filehandle, onlyprintanswers, verbose)
return 1
def drawMark(x,y,color, filehandle, onlyprintanswers, verbose):
'''Draw a standard strut alignment mark, usually at the bottom of the page '''
if(color==""):
color="black"
height=4
width=1.5
__drawStub(x,y,height,width,color,filehandle,onlyprintanswers,verbose)
return 1
def drawCircle(x,y,radius,color,correct,filehandle, verbose):
'''Draw a circle, here one that is the Scantron answer bubble size.'''
circletext1='<circle class="'
circletext2='r="' + str(radius) + 'mm" '
if correct:
style="rightanswer"
else:
style="blank"
filehandle.write(circletext1 + style + '" cy="' + str(y) + \
'mm" cx="' + str(x) + 'mm" ' + circletext2 + '/>\n')
return 1
def drawFive(x,y,xincrement, radius, color,correct,number,filehandle, alph, onlyprintanswers, verbose, reallyverbose):
'''Loop to print five empty bubbles with one filled in, plus numbers
and letters above the bubbles. '''
x=float(x)
y=float(y)
xincrement=float(xincrement)
radius=float(radius)
circletext1='<circle cy='
circletext2='r="1.6mm" stroke="' + color + '" stroke-width="0.1mm"'
drawNum(x,y,number,color, -3, 10, "end", filehandle, onlyprintanswers,verbose, reallyverbose)
for i in range(0,5):
if (i == (correct -1)):
drawCircle(x,y,radius,color,1,filehandle,verbose)
elif (onlyprintanswers):
x=x+xincrement
continue
else:
drawCircle(x,y,radius,color,0, filehandle, verbose)
drawNum(x,y,(i+1), color, 0.7, 9, "end" , filehandle, onlyprintanswers, verbose, reallyverbose)
drawNum(x, (y-3.5), alph[i], color, 0.9, 9, "end", filehandle, onlyprintanswers, verbose, reallyverbose )
x = x + xincrement
return 1
def drawNum(x,y,number,color, align, fontsize, anchor, filehandle, onlyprintanswers,verbose, reallyverbose):
align=float(align)
if(onlyprintanswers):
return 1
# Inkscape doesn't read the 'dominant-baseline="central" ' parameter for text.
# This creates a problem because I can't do raw y-offsets without knowing
# the size of the font. FIXME
if (anchor==""):
anchor="end"
x=float(x) + float(align) # -3 for numbers to the side; 0 for within-the-circle numbers
if (anchor=="end"):
if (fontsize==9):
classname='class="sizenineend" '
elif (fontsize==10):
classname='class="sizetenend" '
else:
pass
if reallyverbose:
print 'Anchor is "end" and fontsize is "' + str(fontsize) + '"'
elif(anchor=="middle"):
if (fontsize==9):
classname='class="sizeninemiddle" '
if reallyverbose:
print 'Anchor is "middle" and fontsize is "' + str(fontsize) + '"'
else:
classname=str('fill="' + color + \
'" font-family="Arial" font-size="' + str(fontsize) + \
'" text-anchor="end"')
text = str('<text ' + classname + ' x="' + str(x) + 'mm" y="' + \
str(y) + 'mm"><tspan dy="3">' + str(number) + '</tspan></text>\n')
filehandle.write(str(text))
return 1
def getAnswers(filename,verbose):
if(os.path.isfile(filename)):
print("Found file " + filename + " -- continuing.")
else:
print("Unable to find input file " + filename + " -- check to see it is in the same directory as this program, or create it." )
sys.exit()
rt = open(filename, 'r')
rs = rt.read()
rs=re.sub('\n$','',rs)
answerstrings = rs.split('\n')
# if verbose:
# print(str(answerstrings))
answerints = map( int, answerstrings )
return answerints
def printBlueLines(**params ):
verbose=params['verbose']
filehandle=params['filehandle']
onlyprintanswers=params['onlyprintanswers']
params['hexcolor'] = re.sub('"', '', params['hexcolor'])
hexcolor=params['hexcolor']
if(params['onlyprintanswers'] == 1):
if (verbose ==1):
print "Since 'onlyprintanswers' is 1, there's no ' + \
'need to print anything from this function."
return 1
else:
# print the parbox in the upper right:
filehandle.write('<rect x="130.0mm" y="12mm" width="144mm" height="12.5mm" rx="0.1mm" ry="0.1mm"\nstroke="#5050b4" stroke-width="0.2mm" fill="none" />\n')
# print the heavy vertical divider:
filehandle.write(str('<line x1="127.5mm" y1="12mm" x2="127.5mm" y2="207mm" style="stroke:rgb(80,80,180);stroke-width:3.5mm" />\n'))
# print the thin horizontal divider:
filehandle.write(str('<line x1="129.2mm" y1="118mm" x2="275mm" y2="118mm" style="stroke:rgb(80,80,180); stroke-width=0.1mm"/>\n'))
text1params = {'text1': "GENERAL PURPOSE - PYTHON - ANSWER SHEET",\
'x':140 , \
'y':17.5, \
'font':"Arial", \
'fontsize':18, \
'hexcolor':hexcolor, \
'onlyprintanswers':onlyprintanswers,\
'verbose':verbose, \
'filehandle':filehandle \
}
# if verbose:
# print("\n*\ttext1params is a: " + str(type(text1params)))
# print("text1params is : %s " % (text1params) )
writeText(**text1params)
text1params={'text1': "SEE IMPORTANT MARKING INSTRUCTIONS ON SIDE 2", \
'x':153 , \
'y':22.5, \
'font':"Arial", \
'fontsize':13, \
'filehandle':filehandle, \
'onlyprintanswers':params['onlyprintanswers'],\
'hexcolor':hexcolor, \
'verbose':params['verbose'], \
'filehandle':params['filehandle'] \
}
writeText(**text1params )
return 1
def get_selected_values(d, *args):
return [d[arg] for arg in args]
def writeStyledText(xpos,ypos,classname,text,filehandle,verbose):
'''write arbitrary text with a class format, at arbitrary coordinates. '''
# typically classname would be "regular" but of course there can be exceptions
line=str('<text x="' + str(xpos) + 'mm" y="' + str(ypos) + 'mm" class="' + \
str(classname) + '">' + \
str(text) + "</text>\n")
filehandle.write(line)
return 1
def writeText(**tp):
if (tp['onlyprintanswers'] == 1) :
return 1
if (tp['verbose'] == 1 ):
print ("\n*\ttextparams (tp) is: %s" % (tp) )
filehandle=tp['filehandle']
line = str('<text x="%(x)smm" y="%(y)smm" ' \
'fill="%(hexcolor)s" font-family="%(font)s" font-size="%(fontsize)s" ' \
'text-anchor="start" dominant-baseline="auto" >%(text1)s</text>\n' % tp )
filehandle.write(line)
return 1
class TextWriter(object):
TEXT_FORMAT = '<text x="%(x)smm" y="%(y)smm" fill="%(color)s" ' \
'font-family="%(font)s" font-size="%(fontsize)s" ' \
'text-anchor="start" dominant-baseline="auto">' \
'%(text)s</text>\n'
def __init__(self, filehandle, onlyprintanswers=False):
self.f = filehandle
self.onlyprintanswers = onlyprintanswers
def __del__(self):
self.f.close()
def writeText(self, text, **kwargs):
if self.onlyprintanswers:
return
kwargs['text'] = text
kwargs.setdefault('x', '0')
kwargs.setdefault('y', '0')
kwargs.setdefault('color', 'black')
kwargs.setdefault('font', 'serif')
kwargs.setdefault('fontsize', 12)
self.f.write(self.TEXT_FORMAT % kwargs)
# writer = TextWriter(filehandle=open('test.xml', 'w'))
# writer.writeText('hello world', x=100, y=100, color='blue')
def which(program):
# Thanks to Jay (http://stackoverflow.com/users/20840/jay) for this.
# http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
def checkForInkscape(inkscapedict, params):
verbose=params['verbose']
reallyverbose=params['reallyverbose']
for key, value in inkscapedict.iteritems():
if reallyverbose:
print(key + '\t' + str(value[0] ))
if (which(value[0])):
inkscape=1
if verbose:
print(str(value[1]))
return str(value[0])
return False
# EOF