Commit 6a45642b authored by Amrita Deb's avatar Amrita Deb
Browse files

Merge branch 'rohlfing-docstring-argument-handling' into 'master'

Automated documentation build

See merge request !39
parents 1661db24 9f907fd7
Pipeline #457905 passed with stages
in 7 minutes and 25 seconds
...@@ -19,12 +19,23 @@ class MainTest(unittest.TestCase): ...@@ -19,12 +19,23 @@ class MainTest(unittest.TestCase):
def test_batch(self): def test_batch(self):
import batch import batch
expected_files = ['moodle_feedbacks.zip', 'passwords.csv'] expected_files = [
expected_folders = ['Four, Student_107_assignsubmission_file_'] 'moodle_feedbacks.zip',
expected_pdfs = ['123456_Nachname_w_aes.pdf'] 'passwords.csv']
expected_folders = [
'LastnameA, FirstnameA_1519321_assignsubmission_file_',
'LastnameB, FirstnameB_1519322_assignsubmission_file_',
'LastnameJ, FirstnameJ_1519330_assignsubmission_file_',
'LastnameK, FirstnameK_1519331_assignsubmission_file_'
]
expected_pdfs = [
'123001_LastnameA_w_aes.pdf']
# Prepare parameter # Prepare parameter
in_dir = './pdfs' in_dir = './tests/assets/pdfs'
csv = './tests/assets/Grades.csv'
dpi = 50
out_dir = os.path.join(self.test_dir, 'out') out_dir = os.path.join(self.test_dir, 'out')
os.mkdir(out_dir) os.mkdir(out_dir)
...@@ -35,8 +46,9 @@ class MainTest(unittest.TestCase): ...@@ -35,8 +46,9 @@ class MainTest(unittest.TestCase):
os.mkdir(zipout_dir) os.mkdir(zipout_dir)
# Copy supplements file # Copy supplements file
batch.main(["-i", in_dir, "-o", out_dir, "-t", tmp_dir, batch.main([
"--cores", "1", "--dpi", "100"]) in_dir, csv, out_dir,
"-t", tmp_dir, "--cores", "1", "--dpi", str(dpi)])
# Assert output # Assert output
created_files = os.listdir(out_dir) created_files = os.listdir(out_dir)
......
...@@ -18,22 +18,45 @@ class MainTest(unittest.TestCase): ...@@ -18,22 +18,45 @@ class MainTest(unittest.TestCase):
# Clean up # Clean up
shutil.rmtree(self.test_dir) shutil.rmtree(self.test_dir)
def test_encrypt_scan_single(self):
import encrypt
expected_files = [
'123001_LastnameA_aes.pdf']
# Prepare parameter
in_pdf = './tests/assets/pdfs/123001_LastnameA.pdf'
enc_pdf = '123001_LastnameA_aes.pdf'
out_dir = os.path.join(self.test_dir, 'out')
os.mkdir(out_dir)
# Encrypt files
encrypt.encrypt(
pdf_file=in_pdf, enc_file=os.path.join(out_dir, enc_pdf),
password='tests_are_fun')
created_files = os.listdir(out_dir)
created_files.sort()
self.assertEqual(expected_files, created_files)
def test_encrypt_scans(self): def test_encrypt_scans(self):
import encrypt import encrypt
expected_files = ['123456_Nachname_aes.pdf', expected_files = ["123001_LastnameA_aes.pdf",
'456789_Lastname_aes.pdf', "123002_LastnameB_aes.pdf",
'567891_Student_aes.pdf', "123010_LastnameJ_aes.pdf",
"123011_LastnameK_aes.pdf",
'passwords.csv'] 'passwords.csv']
# Prepare parameter # Prepare parameter
in_dir = './pdfs' in_dir = './tests/assets/pdfs'
out_dir = os.path.join(self.test_dir, 'out') out_dir = os.path.join(self.test_dir, 'out')
os.mkdir(out_dir) os.mkdir(out_dir)
# Encrypt files # Encrypt files
encrypt.main(["-i", in_dir, "-o", out_dir]) encrypt.main([in_dir, out_dir])
created_files = os.listdir(out_dir) created_files = os.listdir(out_dir)
created_files.sort() created_files.sort()
...@@ -46,13 +69,13 @@ class MainTest(unittest.TestCase): ...@@ -46,13 +69,13 @@ class MainTest(unittest.TestCase):
'passwords.csv'] 'passwords.csv']
# Prepare parameter # Prepare parameter
in_dir = './supplements' in_dir = './tests/assets/supplements'
out_dir = os.path.join(self.test_dir, 'out') out_dir = os.path.join(self.test_dir, 'out')
os.mkdir(out_dir) os.mkdir(out_dir)
# Encrypt files # Encrypt files
encrypt.main(["-i", in_dir, "-o", out_dir]) encrypt.main([in_dir, out_dir])
created_files = os.listdir(out_dir) created_files = os.listdir(out_dir)
created_files.sort() created_files.sort()
......
...@@ -14,15 +14,15 @@ class MainTest(unittest.TestCase): ...@@ -14,15 +14,15 @@ class MainTest(unittest.TestCase):
def test_extract_grading_info(self): def test_extract_grading_info(self):
from utils import moodle as moodle_utils from utils import moodle as moodle_utils
csv_file = "./Bewertungen.csv" csv_file = "./tests/assets/Grades.csv"
gis = moodle_utils.extract_info(sheet_csv=csv_file) gis = moodle_utils.extract_info(sheet_csv=csv_file)
self.assertEqual(gis[2]['lastname'], "Three") self.assertEqual(gis[2]['lastname'], "LastnameJ")
num_students = moodle_utils.get_student_number(sheet_csv=csv_file) num_students = moodle_utils.get_student_number(sheet_csv=csv_file)
self.assertEqual(num_students, 3) self.assertEqual(num_students, 5)
csv_file = "./Grades.csv" csv_file = "./tests/assets/Grades.csv"
gis = moodle_utils.extract_info(sheet_csv=csv_file) gis = moodle_utils.extract_info(sheet_csv=csv_file)
# Check that "d'Lastname" gets whitened to "dLastname" # Check that "d'Lastname" gets whitened to "dLastname"
self.assertEqual(gis[3]['lastname'], "dLastname") self.assertEqual(gis[0]['lastname'], "dLastname")
...@@ -18,15 +18,16 @@ class MainTest(unittest.TestCase): ...@@ -18,15 +18,16 @@ class MainTest(unittest.TestCase):
# Clean up # Clean up
shutil.rmtree(self.test_dir) shutil.rmtree(self.test_dir)
def test_prepare_moodle(self): def test_preparemoodle_single(self):
import preparemoodle import preparemoodle
expected_feedback_folder = 'Four, Student_107_assignsubmission_file_' expected_feedback_folder =\
expected_feedback_file = '123456_Nachname.pdf' 'LastnameB, FirstnameB_1519322_assignsubmission_file_'
expected_feedback_file = '123002_LastnameB.pdf'
# Prepare parameter # Prepare parameter
in_dir = './pdfs' in_dir = './tests/assets/pdfs'
sheet_csv = "./Bewertungen.csv" sheet_csv = "./tests/assets/Grades.csv"
feedback_zip = 'feedbacks.zip' feedback_zip = 'feedbacks.zip'
tmp_dir = os.path.join(self.test_dir, 'tmp') tmp_dir = os.path.join(self.test_dir, 'tmp')
...@@ -35,8 +36,8 @@ class MainTest(unittest.TestCase): ...@@ -35,8 +36,8 @@ class MainTest(unittest.TestCase):
out_zip = os.path.join(self.test_dir, feedback_zip) out_zip = os.path.join(self.test_dir, feedback_zip)
# Call function # Call function
preparemoodle.main(["-i", in_dir, "--csv", sheet_csv, preparemoodle.main([in_dir, sheet_csv, out_zip,
"-t", tmp_dir, "-o", out_zip]) "-t", tmp_dir])
# Unpack feedbacks # Unpack feedbacks
shutil.unpack_archive(out_zip, tmp_dir) shutil.unpack_archive(out_zip, tmp_dir)
......
...@@ -19,13 +19,14 @@ class MainTest(unittest.TestCase): ...@@ -19,13 +19,14 @@ class MainTest(unittest.TestCase):
import preparepdf import preparepdf
expected_files = [ expected_files = [
'123456_F.pdf', '123001_L.pdf',
'123457_O.pdf', '123002_L.pdf',
'125412_T.pdf'] '123010_L.pdf',
'123011_L.pdf']
# Prepare parameter # Prepare parameter
in_zip = './submissions.zip' in_zip = './tests/assets/submissions.zip'
sheet_csv = "./Bewertungen.csv" sheet_csv = "./tests/assets/Grades.csv"
out_dir = os.path.join(self.test_dir, 'out') out_dir = os.path.join(self.test_dir, 'out')
os.mkdir(out_dir) os.mkdir(out_dir)
...@@ -35,8 +36,7 @@ class MainTest(unittest.TestCase): ...@@ -35,8 +36,7 @@ class MainTest(unittest.TestCase):
# Call function # Call function
preparepdf.main([ preparepdf.main([
"-i", in_zip, "-o", out_dir, "-c", sheet_csv, in_zip, sheet_csv, out_dir, "-t", tmp_dir])
"-t", tmp_dir])
# Assert output # Assert output
created_files = os.listdir(out_dir) created_files = os.listdir(out_dir)
......
...@@ -16,7 +16,7 @@ class MainTest(unittest.TestCase): ...@@ -16,7 +16,7 @@ class MainTest(unittest.TestCase):
expected_qr = "23-16" expected_qr = "23-16"
pdf_file = "./pdfs/456789_Lastname.pdf" pdf_file = "./tests/assets/pdfs/123001_LastnameA.pdf"
qr = qr_utils.first_qr_from_first_pdf_page(pdf_file=pdf_file) qr = qr_utils.first_qr_from_first_pdf_page(pdf_file=pdf_file)
self.assertEqual(qr, expected_qr) self.assertEqual(qr, expected_qr)
...@@ -44,7 +44,7 @@ class MainTest(unittest.TestCase): ...@@ -44,7 +44,7 @@ class MainTest(unittest.TestCase):
# Parameters # Parameters
dpi = 200 dpi = 200
pdf_file = "./pdfs/456789_Lastname.pdf" pdf_file = "./tests/assets/pdfs/123001_LastnameA.pdf"
# Decode all QRs # Decode all QRs
qrs, _ = qr_utils.qrs_from_pdf(pdf_file=pdf_file, dpi=dpi) qrs, _ = qr_utils.qrs_from_pdf(pdf_file=pdf_file, dpi=dpi)
......
...@@ -19,8 +19,8 @@ class MainTest(unittest.TestCase): ...@@ -19,8 +19,8 @@ class MainTest(unittest.TestCase):
import renamescans import renamescans
# Prepare parameter # Prepare parameter
in_dir = './pdfs_scan' in_dir = './tests/assets/pdfs_scan'
sheet_csv = "./Bewertungen.csv" sheet_csv = "./tests/assets/Grades.csv"
out_dir = os.path.join(self.test_dir, 'out') out_dir = os.path.join(self.test_dir, 'out')
os.mkdir(out_dir) os.mkdir(out_dir)
...@@ -28,7 +28,7 @@ class MainTest(unittest.TestCase): ...@@ -28,7 +28,7 @@ class MainTest(unittest.TestCase):
# Call function # Call function
try: try:
renamescans.main([ renamescans.main([
"-i", in_dir, "-o", out_dir, "-c", sheet_csv, in_dir, sheet_csv, out_dir,
"--dry", "--checkqr"]) "--dry", "--checkqr"])
except Exception: except Exception:
pass pass
...@@ -37,19 +37,21 @@ class MainTest(unittest.TestCase): ...@@ -37,19 +37,21 @@ class MainTest(unittest.TestCase):
import renamescans import renamescans
expected_files = [ expected_files = [
'123456_F.pdf', '123001_L.pdf',
'123457_O.pdf', '123002_L.pdf',
'125412_T.pdf'] '123010_L.pdf',
'123011_L.pdf',
'123012_d.pdf']
# Prepare parameter # Prepare parameter
in_dir = './pdfs_scan' in_dir = './tests/assets/pdfs_scan'
sheet_csv = "./Bewertungen.csv" sheet_csv = "./tests/assets/Grades.csv"
out_dir = os.path.join(self.test_dir, 'out') out_dir = os.path.join(self.test_dir, 'out')
os.mkdir(out_dir) os.mkdir(out_dir)
# Call function # Call function
renamescans.main(["-i", in_dir, "-o", out_dir, "-c", sheet_csv]) renamescans.main([in_dir, sheet_csv, out_dir])
# Assert output # Assert output
created_files = os.listdir(out_dir) created_files = os.listdir(out_dir)
......
...@@ -18,16 +18,19 @@ class MainTest(unittest.TestCase): ...@@ -18,16 +18,19 @@ class MainTest(unittest.TestCase):
def test_supplements_from_pdf_folder(self): def test_supplements_from_pdf_folder(self):
import supplements import supplements
expected_files = ['123456_Nachname_GDET3_20H.pdf', expected_files = [
'123456_Nachname_GDET3_20H_loes.pdf', '123001_LastnameA_GDET3_20H.pdf',
'456789_Lastname_GDET3_20H.pdf', '123001_LastnameA_GDET3_20H_loes.pdf',
'456789_Lastname_GDET3_20H_loes.pdf', '123002_LastnameB_GDET3_20H.pdf',
'567891_Student_GDET3_20H.pdf', '123002_LastnameB_GDET3_20H_loes.pdf',
'567891_Student_GDET3_20H_loes.pdf'] '123010_LastnameJ_GDET3_20H.pdf',
'123010_LastnameJ_GDET3_20H_loes.pdf',
'123011_LastnameK_GDET3_20H.pdf',
'123011_LastnameK_GDET3_20H_loes.pdf']
# Prepare parameter # Prepare parameter
supp_dir = './supplements' supp_dir = './tests/assets/supplements'
pdf_dir = './pdfs' pdf_dir = './tests/assets/pdfs'
supp_out_dir = os.path.join(self.test_dir, 'supplements_out') supp_out_dir = os.path.join(self.test_dir, 'supplements_out')
os.mkdir(supp_out_dir) os.mkdir(supp_out_dir)
...@@ -36,7 +39,7 @@ class MainTest(unittest.TestCase): ...@@ -36,7 +39,7 @@ class MainTest(unittest.TestCase):
os.mkdir(tmp_dir) os.mkdir(tmp_dir)
# Copy supplements file # Copy supplements file
supplements.main(["-i", supp_dir, "-p", pdf_dir, "-o", supp_out_dir]) supplements.main([supp_dir, pdf_dir, supp_out_dir])
# Assert output # Assert output
created_files = os.listdir(supp_out_dir) created_files = os.listdir(supp_out_dir)
...@@ -46,16 +49,21 @@ class MainTest(unittest.TestCase): ...@@ -46,16 +49,21 @@ class MainTest(unittest.TestCase):
def test_supplements_from_csv(self): def test_supplements_from_csv(self):
import supplements import supplements
expected_files = ['123456_F_GDET3_20H.pdf', expected_files = [
'123456_F_GDET3_20H_loes.pdf', '123001_L_GDET3_20H.pdf',
'123457_O_GDET3_20H.pdf', '123001_L_GDET3_20H_loes.pdf',
'123457_O_GDET3_20H_loes.pdf', '123002_L_GDET3_20H.pdf',
'125412_T_GDET3_20H.pdf', '123002_L_GDET3_20H_loes.pdf',
'125412_T_GDET3_20H_loes.pdf'] '123010_L_GDET3_20H.pdf',
'123010_L_GDET3_20H_loes.pdf',
'123011_L_GDET3_20H.pdf',
'123011_L_GDET3_20H_loes.pdf',
'123012_d_GDET3_20H.pdf',
'123012_d_GDET3_20H_loes.pdf']
# Prepare parameter # Prepare parameter
supp_dir = './supplements' supp_dir = './tests/assets/supplements'
csv = 'Bewertungen.csv' csv = './tests/assets/Grades.csv'
supp_out_dir = os.path.join(self.test_dir, 'supplements_out') supp_out_dir = os.path.join(self.test_dir, 'supplements_out')
os.mkdir(supp_out_dir) os.mkdir(supp_out_dir)
...@@ -64,7 +72,7 @@ class MainTest(unittest.TestCase): ...@@ -64,7 +72,7 @@ class MainTest(unittest.TestCase):
os.mkdir(tmp_dir) os.mkdir(tmp_dir)
# Copy supplements file # Copy supplements file
supplements.main(["-i", supp_dir, "-p", csv, "-o", supp_out_dir]) supplements.main([supp_dir, csv, supp_out_dir])
# Assert output # Assert output
created_files = os.listdir(supp_out_dir) created_files = os.listdir(supp_out_dir)
...@@ -75,16 +83,19 @@ class MainTest(unittest.TestCase): ...@@ -75,16 +83,19 @@ class MainTest(unittest.TestCase):
import supplements import supplements
import watermark import watermark
expected_files = ['123456_Nachname_GDET3_20H_loes_w.pdf', expected_files = [
'123456_Nachname_GDET3_20H_w.pdf', '123001_LastnameA_GDET3_20H_loes_w.pdf',
'456789_Lastname_GDET3_20H_loes_w.pdf', '123001_LastnameA_GDET3_20H_w.pdf',
'456789_Lastname_GDET3_20H_w.pdf', '123002_LastnameB_GDET3_20H_loes_w.pdf',
'567891_Student_GDET3_20H_loes_w.pdf', '123002_LastnameB_GDET3_20H_w.pdf',
'567891_Student_GDET3_20H_w.pdf'] '123010_LastnameJ_GDET3_20H_loes_w.pdf',
'123010_LastnameJ_GDET3_20H_w.pdf',
'123011_LastnameK_GDET3_20H_loes_w.pdf',
'123011_LastnameK_GDET3_20H_w.pdf']
# Prepare parameter # Prepare parameter
supp_dir = './supplements' supp_dir = './tests/assets/supplements'
pdf_dir = './pdfs' pdf_dir = './tests/assets/pdfs'
dpi = 100 dpi = 100
supp_out_dir = os.path.join(self.test_dir, 'supplements_out') supp_out_dir = os.path.join(self.test_dir, 'supplements_out')
...@@ -97,11 +108,11 @@ class MainTest(unittest.TestCase): ...@@ -97,11 +108,11 @@ class MainTest(unittest.TestCase):
os.mkdir(out_dir) os.mkdir(out_dir)
# Copy supplements file # Copy supplements file
supplements.main(["-i", supp_dir, "-p", pdf_dir, "-o", supp_out_dir]) supplements.main([supp_dir, pdf_dir, supp_out_dir])
# Watermark files # Watermark files
watermark.main(["-i", supp_out_dir, "-o", out_dir, watermark.main(
"-t", tmp_dir, "--dpi", str(dpi)]) [supp_out_dir, out_dir, "-t", tmp_dir, "--dpi", str(dpi)])
# Assert output # Assert output
created_files = os.listdir(out_dir) created_files = os.listdir(out_dir)
......
...@@ -22,11 +22,11 @@ class MainTest(unittest.TestCase): ...@@ -22,11 +22,11 @@ class MainTest(unittest.TestCase):
import watermark import watermark
# Prepare parameter # Prepare parameter
in_dir = './pdfs' in_dir = './tests/assets/pdfs'
dpi = 150 dpi = 150
quality = 75 quality = 75
fontsize = 75 fontsize = 75
pdf_file = '123456_Nachname.pdf' pdf_file = '123001_LastnameA.pdf'
tmp_dir = os.path.join(self.test_dir, 'tmp') tmp_dir = os.path.join(self.test_dir, 'tmp')
os.mkdir(tmp_dir) os.mkdir(tmp_dir)
...@@ -39,17 +39,18 @@ class MainTest(unittest.TestCase): ...@@ -39,17 +39,18 @@ class MainTest(unittest.TestCase):
output_dir=out_dir, fontsize=fontsize, dpi=dpi, output_dir=out_dir, fontsize=fontsize, dpi=dpi,
quality=quality, pdf_file=pdf_file) quality=quality, pdf_file=pdf_file)
self.assertTrue(os.listdir(out_dir)[0], '123456_Nachname_w.pdf') self.assertTrue(os.listdir(out_dir)[0], '123001_LastnameA_w.pdf')
def test_watermark_pdfs(self): def test_watermark_pdfs(self):
import watermark import watermark
expected_files = ['123456_Nachname_w.pdf', expected_files = ["123001_LastnameA_w.pdf",
'456789_Lastname_w.pdf', "123002_LastnameB_w.pdf",
'567891_Student_w.pdf'] "123010_LastnameJ_w.pdf",
"123011_LastnameK_w.pdf"]
# Prepare parameter # Prepare parameter
in_dir = './pdfs' in_dir = './tests/assets/pdfs'
dpi = 150 dpi = 150
tmp_dir = os.path.join(self.test_dir, 'tmp') tmp_dir = os.path.join(self.test_dir, 'tmp')
...@@ -59,9 +60,10 @@ class MainTest(unittest.TestCase): ...@@ -59,9 +60,10 @@ class MainTest(unittest.TestCase):
os.mkdir(out_dir) os.mkdir(out_dir)
# Watermark files # Watermark files
watermark.main(["-i", in_dir, "-o", out_dir, watermark.main([in_dir, out_dir,
"-t", tmp_dir, "--dpi", str(dpi)]) "-t", tmp_dir, "--dpi", str(dpi)])
created_files = os.listdir(out_dir) created_files = os.listdir(out_dir)
created_files.sort() created_files.sort()
self.assertEqual(expected_files, created_files) self.assertEqual(expected_files, created_files)
"""Common functions for handling Moodle grading sheet CSV
"""
import csv # handles csv import csv # handles csv
import argparse # argument parser for CSV files
def get_student_number(sheet_csv, csv_enc='utf-8'): def get_student_number(sheet_csv, csv_enc='utf-8'):
...@@ -46,8 +51,7 @@ def extract_info(sheet_csv, csv_delim=',', csv_quote='"', csv_enc='utf-8'): ...@@ -46,8 +51,7 @@ def extract_info(sheet_csv, csv_delim=',', csv_quote='"', csv_enc='utf-8'):
csv_delim (str, optional): CSV delimiter. Defaults to ','. csv_delim (str, optional): CSV delimiter. Defaults to ','.
csv_quote (str, optional): CSV quote char. Defaults to '"'. csv_quote (str, optional): CSV quote char. Defaults to '"'.
csv_enc (str, optional): CSV encoding. Defaults to 'utf-8'. csv_enc (str, optional): CSV encoding. Defaults to 'utf-8'.
Typical values: "'utf-8', 'utf-8-sig', Typical values: "'utf-8', 'utf-8-sig', or 'cp1252' (Windows). "
or 'cp1252' (Windows). "
Returns: Returns:
list of dicts: grading information with following info per student: list of dicts: grading information with following info per student:
...@@ -98,3 +102,16 @@ def extract_info(sheet_csv, csv_delim=',', csv_quote='"', csv_enc='utf-8'): ...@@ -98,3 +102,16 @@ def extract_info(sheet_csv, csv_delim=',', csv_quote='"', csv_enc='utf-8'):
gi['fullname'] = fullname gi['fullname'] = fullname
return grading_infos return grading_infos
def get_moodle_csv_parser():
csv_parser = argparse.ArgumentParser(add_help=False)
csv_parser.add_argument(
"--csvdelim", default=",", help="CSV delimiter character.")
csv_parser.add_argument(
"--csvquote", default='"', help="CSV quote character.")
csv_parser.add_argument(
"--csvenc", default="utf-8", help="CSV encoding scheme. " +
"Typical encodings:'utf-8', 'utf-8-sig', or 'cp1252' (Windows).")
return csv_parser
...@@ -2,12 +2,17 @@ ...@@ -2,12 +2,17 @@
"""Watermarks each page of PDF with matriculation number """Watermarks each page of PDF with matriculation number