batch.py 3.5 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

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

10
11
12

def main(args):
    # Argument handling
13
    parser = argparse.ArgumentParser(description='''
Christian Rohlfing's avatar
Christian Rohlfing committed
14
      Watermark and encrypts exams and prepares everything for moodle upload.
15
16
17
18
19
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
20
21
22
23
24
25
    -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
26
    -e, --cores     number of cores for watermarking. Default: 1
Christian Rohlfing's avatar
Christian Rohlfing committed
27
    -d, --dpi       dpi parameter for pdf to image conversion. Default: 250
28
29
30
    -q, --quality   quality parameter for jpeg. Default: 25
    -t, --tmp       tmp folder. Default: ./tmp
      ''')
Christian Rohlfing's avatar
Christian Rohlfing committed
31
    parser.add_argument("-i", "--infolder", default="./pdfs",
32
                        help="Input folder with PDFs. Default: ./pdfs")
Christian Rohlfing's avatar
Christian Rohlfing committed
33
    parser.add_argument("-c", "--csv", default="Bewertungen.csv",
34
35
                        help="Moodle grading CSV file, needed to construct " +
                        "folder names for moodle zip")
Christian Rohlfing's avatar
Christian Rohlfing committed
36
    parser.add_argument("-o", "--outfolder", default="./out",
37
38
                        help="output folder containing passwords.csv and " +
                        "moodle_feedbacks.zip. Default: ./out")
Christian Rohlfing's avatar
Christian Rohlfing committed
39
    parser.add_argument("-e", "--cores", default="2",
40
41
                        help="Number of cores for parallel processing. " +
                        "Default: 2")
Christian Rohlfing's avatar
Christian Rohlfing committed
42
    parser.add_argument("-p", "--password", default="",
43
44
                        help="sets global password. Default: empty, " +
                        "such that each PDF gets custom password")
Christian Rohlfing's avatar
Christian Rohlfing committed
45
    parser.add_argument("-d", "--dpi", default="250",
46
47
                        help="DPI parameter for pdf to image conversion. " +
                        "Default: 250")
Christian Rohlfing's avatar
Christian Rohlfing committed
48
    parser.add_argument("-t", "--tmp", default="./tmp",
49
                        help="tmp folder. Default: ./tmp/")
50

51
    args = parser.parse_args(args)
Christian Rohlfing's avatar
Christian Rohlfing committed
52
    infolder = args.infolder
53
54
55
56
57
    csv = args.csv
    outfolder = args.outfolder
    cores = args.cores
    dpi = args.dpi
    tmp = args.tmp
Christian Rohlfing's avatar
Christian Rohlfing committed
58
    password = args.password
59

Christian Rohlfing's avatar
Christian Rohlfing committed
60
    starttime = time.time()
61

62
    # Watermarking
Christian Rohlfing's avatar
Christian Rohlfing committed
63
    watermark_outfolder = os.path.join(tmp, 'pdfs_watermarked')
64
65
    if not os.path.exists(watermark_outfolder):
        os.makedirs(watermark_outfolder)
Christian Rohlfing's avatar
Christian Rohlfing committed
66
    watermark.main(['--in', infolder, '--out', watermark_outfolder,
67
                    '--cores', cores, '--dpi', dpi])
Christian Rohlfing's avatar
Christian Rohlfing committed
68

69
    # Encryption
Christian Rohlfing's avatar
Christian Rohlfing committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    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])
85
86
87

    endtime = time.time()
    print(f'\nTotal time taken: {endtime-starttime:.2f}s\n')
88
89
90
91


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