batch.py 4.25 KB
Newer Older
Christian Rohlfing's avatar
Christian Rohlfing committed
1
import os
2
import sys
Christian Rohlfing's avatar
Christian Rohlfing committed
3
import time
4
5
import argparse

6
import supplements
Christian Rohlfing's avatar
Christian Rohlfing committed
7
8
9
10
import watermark
import encrypt
import preparemoodle

11
12
13

def main(args):
    # Argument handling
14
    parser = argparse.ArgumentParser(description='''
Christian Rohlfing's avatar
Christian Rohlfing committed
15
      Watermark and encrypts exams and prepares everything for moodle upload.
16
17
18
19
20
Attention: contents of folder 'out' will be deleted in the beginning!

Options:
    -h, --help      show this help text
    -i, --in        input folder with PDFs. Default: ./pdfs
21
22
23
24
25
26
    -c, --csv       Moodle grading CSV file, needed to construct folder names
                    for moodle
    -o, --out       output folder containing passwords.csv and
                    moodle_feedbacks.zip. Default: ./out
    -p, --password  sets global password. Default: empty, such that each PDF
                    gets custom password
27
    -e, --cores     number of cores for watermarking. Default: 1
Christian Rohlfing's avatar
Christian Rohlfing committed
28
    -d, --dpi       dpi parameter for pdf to image conversion. Default: 250
29
30
31
    -q, --quality   quality parameter for jpeg. Default: 25
    -t, --tmp       tmp folder. Default: ./tmp
      ''')
Christian Rohlfing's avatar
Christian Rohlfing committed
32
    parser.add_argument("-i", "--infolder", default="./pdfs",
33
                        help="Input folder with PDFs. Default: ./pdfs")
Christian Rohlfing's avatar
Christian Rohlfing committed
34
    parser.add_argument("-c", "--csv", default="Bewertungen.csv",
35
36
                        help="Moodle grading CSV file, needed to construct " +
                        "folder names for moodle zip")
Christian Rohlfing's avatar
Christian Rohlfing committed
37
    parser.add_argument("-o", "--outfolder", default="./out",
38
39
                        help="output folder containing passwords.csv and " +
                        "moodle_feedbacks.zip. Default: ./out")
Christian Rohlfing's avatar
Christian Rohlfing committed
40
    parser.add_argument("-e", "--cores", default="2",
41
42
                        help="Number of cores for parallel processing. " +
                        "Default: 2")
Christian Rohlfing's avatar
Christian Rohlfing committed
43
    parser.add_argument("-p", "--password", default="",
44
45
                        help="sets global password. Default: empty, " +
                        "such that each PDF gets custom password")
Christian Rohlfing's avatar
Christian Rohlfing committed
46
    parser.add_argument("-d", "--dpi", default="250",
47
48
                        help="DPI parameter for pdf to image conversion. " +
                        "Default: 250")
Christian Rohlfing's avatar
Christian Rohlfing committed
49
    parser.add_argument("-t", "--tmp", default="./tmp",
50
                        help="tmp folder. Default: ./tmp/")
51
52
53
54
55
    parser.add_argument("-u", "--supinfolder", default="./supplements",
                        help="Input folder with sample solutions. Default: ./supplements")
    parser.add_argument("-w", "--sup", default="0",
                        help="Flag for watermarking sample solutions. 0 means no 1 means yes. Default: 0")

56

57
    args = parser.parse_args(args)
Christian Rohlfing's avatar
Christian Rohlfing committed
58
    infolder = args.infolder
59
60
61
62
63
    csv = args.csv
    outfolder = args.outfolder
    cores = args.cores
    dpi = args.dpi
    tmp = args.tmp
Christian Rohlfing's avatar
Christian Rohlfing committed
64
    password = args.password
65
66
    sup = int(args.sup)
    supinfolder = args.supinfolder
67

Christian Rohlfing's avatar
Christian Rohlfing committed
68
    starttime = time.time()
69

70
    # Watermarking
Christian Rohlfing's avatar
Christian Rohlfing committed
71
    watermark_outfolder = os.path.join(tmp, 'pdfs_watermarked')
72
73
    if not os.path.exists(watermark_outfolder):
        os.makedirs(watermark_outfolder)
Christian Rohlfing's avatar
Christian Rohlfing committed
74
    watermark.main(['--in', infolder, '--out', watermark_outfolder,
75
                    '--cores', cores, '--dpi', dpi])
76
    if sup == 1:
Amrita Deb's avatar
Amrita Deb committed
77
78
79
        supoutfolder = os.path.join(tmp, 'supplements_out')
        if not os.path.exists(supoutfolder):
            os.makedirs(supoutfolder)
80
        supplements.main(['--in', supinfolder, '--out', supoutfolder])
Amrita Deb's avatar
Amrita Deb committed
81
        watermark.main(['--in', supoutfolder, '--out', watermark_outfolder,
82
                    '--cores', cores, '--dpi', dpi])
Christian Rohlfing's avatar
Christian Rohlfing committed
83

84
    # Encryption
Christian Rohlfing's avatar
Christian Rohlfing committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    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')
    encrypt.main(['--in', watermark_outfolder, '--out', enc_out,
                  '--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)
    preparemoodle.main(['--in', enc_out, '--csv', csv, '--tmp', moodle_tmp,
                        '--out', moodle_out])
100
101
102

    endtime = time.time()
    print(f'\nTotal time taken: {endtime-starttime:.2f}s\n')
103
104
105
106


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