batch.py 4.22 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/env python

"""watermarks, encrypts and prepares PDF files for upload to Moodle assignment

Attention: Contents in output folder will be overwritten in the following!
"""

__author__ = "Amrita Deb (deb@itc.rwth-aachen.de), " +\
    "Christian Rohlfing (rohlfing@ient.rwth-aachen.de)"


Christian Rohlfing's avatar
Christian Rohlfing committed
12
import os
13
import sys
Christian Rohlfing's avatar
Christian Rohlfing committed
14
import time
15
16
import argparse

17
18
import utils.moodle as moodle

Amrita's avatar
Amrita committed
19
import preparepdf
20
import supplements
Christian Rohlfing's avatar
Christian Rohlfing committed
21
22
23
24
import watermark
import encrypt
import preparemoodle

25

26
27
28
29
30
def _make_parser():
    csv_parser = moodle.get_moodle_csv_parser()

    parser = argparse.ArgumentParser(
        parents=[csv_parser],
Christian Rohlfing's avatar
bugfix    
Christian Rohlfing committed
31
        description=__doc__, prog='batch.py',
32
33
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)

34
    parser.add_argument(
35
        "infolder", help="Input folder with PDFs")
36
    parser.add_argument(
37
        "csv", help="Moodle grading CSV sheet")
38
39
    parser.add_argument(
        "outfolder",
40
41
        help="output folder with passwords.csv and Moodle feedbacks ZIP")

42
    parser.add_argument(
43
44
        "--supp", action='store_true',
        help="Flag for watermarking supplements")
45
    parser.add_argument(
46
47
        "--suppinfolder", default="./supplements",
        help="Input folder with supplements such as sample solutions")
48
    parser.add_argument(
49
50
        "--zip", default="0", help="ZIP file with submissions to be " +
        "extracted to input folder")
51
    parser.add_argument(
52
53
        "-e", "--cores", default="1",
        help="Number of cores for parallel processing")
54
    parser.add_argument(
55
56
57
        "-p", "--password", default="",
        help="sets global password. Default='' such that each PDF gets " +
             "custom password")
58
    parser.add_argument(
59
60
        "-d", "--dpi", default="150",
        help="Dots-per-inch parameter for PDF to image conversion")
61
    parser.add_argument(
62
63
64
65
66
67
        "-t", "--tmp", default="./tmp", help="Temporary folder")

    return parser


# Create argument parser with default values
Christian Rohlfing's avatar
Christian Rohlfing committed
68
_parser = _make_parser()
69

70
71
72

def main(args):
    # Argument handling
Christian Rohlfing's avatar
Christian Rohlfing committed
73
    args = _parser.parse_args(args)
Christian Rohlfing's avatar
Christian Rohlfing committed
74
    infolder = args.infolder
75
76
77
78
79
    csv = args.csv
    outfolder = args.outfolder
    cores = args.cores
    dpi = args.dpi
    tmp = args.tmp
Christian Rohlfing's avatar
Christian Rohlfing committed
80
    password = args.password
81
82
    watermark_supp = args.supp
    supinfolder = args.suppinfolder
Amrita's avatar
Amrita committed
83
    inzip = args.zip
84
85
86
    csv_enc = args.csvenc
    csv_delim = args.csvdelim
    csv_quote = args.csvquote
87

Christian Rohlfing's avatar
Christian Rohlfing committed
88
    starttime = time.time()
89

Amrita's avatar
Amrita committed
90
    # Unzip submissions if provided zip archive
91
    if inzip != "0":
Amrita's avatar
Amrita committed
92
93
        if not os.path.exists(infolder):
            os.makedirs(infolder)
94
95
96
97
        preparepdf.main([
            inzip, csv, infolder,
            '--csvenc', csv_enc, '--csvdelim', csv_delim,
            '--csvquote', csv_quote])
98

99
    # Watermarking
Christian Rohlfing's avatar
Christian Rohlfing committed
100
    watermark_outfolder = os.path.join(tmp, 'pdfs_watermarked')
101
102
    if not os.path.exists(watermark_outfolder):
        os.makedirs(watermark_outfolder)
103
104
105
106
107

    watermark.main([
        infolder, watermark_outfolder,
        '--cores', cores, '--dpi', dpi])

108
    if watermark_supp:
Amrita Deb's avatar
Amrita Deb committed
109
110
111
        supoutfolder = os.path.join(tmp, 'supplements_out')
        if not os.path.exists(supoutfolder):
            os.makedirs(supoutfolder)
112
113
114
115
116
117
118

        supplements.main([
            supinfolder, csv, supoutfolder,
            '--csvenc', csv_enc, '--csvdelim', csv_delim,
            '--csvquote', csv_quote
            ])

Christian Rohlfing's avatar
Christian Rohlfing committed
119
120
        watermark.main([supoutfolder, watermark_outfolder,
                        '--cores', cores, '--dpi', dpi])
Christian Rohlfing's avatar
Christian Rohlfing committed
121

122
    # Encryption
Christian Rohlfing's avatar
Christian Rohlfing committed
123
124
125
126
127
    enc_out = os.path.join(tmp, 'pdfs_encrypted')
    if not os.path.exists(enc_out):
        os.makedirs(enc_out)

    passwordcsv = os.path.join(outfolder, 'passwords.csv')
Christian Rohlfing's avatar
Christian Rohlfing committed
128
    encrypt.main([watermark_outfolder, enc_out,
Christian Rohlfing's avatar
Christian Rohlfing committed
129
130
131
132
133
134
135
                  '--passwordout', passwordcsv, '--password', password])

    # ZIP Archive preparation process
    moodle_out = os.path.join(outfolder, 'moodle_feedbacks.zip')
    moodle_tmp = os.path.join(tmp, 'tmp')
    if not os.path.exists(moodle_tmp):
        os.makedirs(moodle_tmp)
136
137
138
139
140

    preparemoodle.main([
        enc_out, csv, moodle_out,
        '--tmp', moodle_tmp, '--csvenc', csv_enc, '--csvdelim', csv_delim,
        '--csvquote', csv_quote])
141
142
143

    endtime = time.time()
    print(f'\nTotal time taken: {endtime-starttime:.2f}s\n')
144
145
146
147


if __name__ == '__main__':
    main(sys.argv[1:])