Commit 6670d2df authored by Christian Rohlfing's avatar Christian Rohlfing
Browse files

supplementy.py uses moodle utils

parent 18584c4b
...@@ -13,9 +13,9 @@ import os # path listing/manipulation/... ...@@ -13,9 +13,9 @@ import os # path listing/manipulation/...
import time # keep track of time import time # keep track of time
import argparse # handle command line arguments import argparse # handle command line arguments
import shutil # copy import shutil # copy
import csv # handle CSV files
import utils.matnum as matnum_utils import utils.matnum as matnum_utils
import utils.moodle as moodle
def copy_supplements(supp_dir, supp_files, prefixes, output_dir, dry=False): def copy_supplements(supp_dir, supp_files, prefixes, output_dir, dry=False):
...@@ -84,16 +84,21 @@ def main(args): ...@@ -84,16 +84,21 @@ def main(args):
help="Folder with supplements. Default: ./supplements") help="Folder with supplements. Default: ./supplements")
parser.add_argument("-p", "--prefix", default="./pdfs", parser.add_argument("-p", "--prefix", default="./pdfs",
help="Provides information to construct prefixes. " + help="Provides information to construct prefixes. " +
"Either PDF folder with scanned PDFs or " + "Either PDF folder with scanned PDFs or " +
"Moodle grading CSV file. Default: ./pdfs") "Moodle grading CSV file. Default: ./pdfs")
parser.add_argument("-o", "--outfolder", default="./supplements_out", parser.add_argument("-o", "--outfolder", default="./supplements_out",
help="Output folder. Default: ./supplements_out") help="Output folder. Default: ./supplements_out")
parser.add_argument("--csvprefixformat", default="{matnum}_{fullname[0]}",
help="Format of CSV prefix. Available keywords: " +
"{{matnum}}, {{fullname}}, {{lastname}}, " +
"{{firstname}}. Default: '{{matnum}}_{{fullname[0]}}'")
parser.add_argument("-d", "--dry", action='store_true', parser.add_argument("-d", "--dry", action='store_true',
help="Flag for dry run") help="Flag for dry run")
args = parser.parse_args(args) args = parser.parse_args(args)
supp_dir = args.infolder supp_dir = args.infolder
prefixinfo = args.prefix prefixinfo = args.prefix
prefixformat = args.csvprefixformat
output_dir = args.outfolder output_dir = args.outfolder
dry = args.dry dry = args.dry
...@@ -114,7 +119,7 @@ def main(args): ...@@ -114,7 +119,7 @@ def main(args):
# Print status # Print status
starttime = time.time() starttime = time.time()
supp_folder = os.listdir(supp_dir) supp_folder = os.listdir(supp_dir)
supp_files = [_ for _ in supp_folder if _.endswith(".pdf")] supp_files = [_ for _ in supp_folder if _.lower().endswith(".pdf")]
print(""" print("""
Available supplement PDFs to be copied: Available supplement PDFs to be copied:
- {} - {}
...@@ -127,7 +132,7 @@ Files in output folder {} will be overwritten during this process. ...@@ -127,7 +132,7 @@ Files in output folder {} will be overwritten during this process.
if pdf_dir != "": # Take prefixes from pdf directory if pdf_dir != "": # Take prefixes from pdf directory
pdf_folder = os.listdir(pdf_dir) pdf_folder = os.listdir(pdf_dir)
pdf_files = [_ for _ in pdf_folder pdf_files = [_ for _ in pdf_folder
if _.endswith(".pdf") and if _.lower().endswith(".pdf") and
matnum_utils.starts_with_matnum(_)] matnum_utils.starts_with_matnum(_)]
prefixes = [] prefixes = []
for pdf_file in pdf_files: for pdf_file in pdf_files:
...@@ -135,21 +140,13 @@ Files in output folder {} will be overwritten during this process. ...@@ -135,21 +140,13 @@ Files in output folder {} will be overwritten during this process.
prefixes.append(prefix) prefixes.append(prefix)
else: # Take prefixes from CSV file else: # Take prefixes from CSV file
prefixes = [] prefixes = []
# Open CSV file infos = moodle.extract_info(csvfilename)
with open(csvfilename, newline='') as csvfile: for info in infos:
# Loop over all lines in CSV file prefix = prefixformat.format(matnum=info['matnum'],
reader = csv.reader(csvfile, delimiter=',', quotechar='"') fullname=info['fullname'],
next(reader) # skip header CSV line lastname=info['lastname'],
for row in reader: firstname=info['firstname'])
# Parse required fields from CSV line prefixes.append(prefix) # save prefix
name = row[1] # [Lastname], [Firstname]
name = name[0] # Take only first letter of lastname
matnum = row[2] # matriculation number (6-digit)
if not matnum_utils.check_matnum(matnum):
raise Exception("Invalid matriculation number found")
prefix = matnum + "_" + name
prefixes.append(prefix) # save prefix
# Copy supplements to output dir and prepend prefixes # Copy supplements to output dir and prepend prefixes
copied_files = copy_supplements(supp_dir, supp_files, prefixes, copied_files = copy_supplements(supp_dir, supp_files, prefixes,
......
...@@ -85,7 +85,7 @@ class MainTest(unittest.TestCase): ...@@ -85,7 +85,7 @@ class MainTest(unittest.TestCase):
# Prepare parameter # Prepare parameter
supp_dir = './supplements' supp_dir = './supplements'
pdf_dir = './pdfs' pdf_dir = './pdfs'
dpi = 150 dpi = 100
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)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment