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/...
import time # keep track of time
import argparse # handle command line arguments
import shutil # copy
import csv # handle CSV files
import utils.matnum as matnum_utils
import utils.moodle as moodle
def copy_supplements(supp_dir, supp_files, prefixes, output_dir, dry=False):
......@@ -88,12 +88,17 @@ def main(args):
"Moodle grading CSV file. Default: ./pdfs")
parser.add_argument("-o", "--outfolder", 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',
help="Flag for dry run")
args = parser.parse_args(args)
supp_dir = args.infolder
prefixinfo = args.prefix
prefixformat = args.csvprefixformat
output_dir = args.outfolder
dry = args.dry
......@@ -114,7 +119,7 @@ def main(args):
# Print status
starttime = time.time()
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("""
Available supplement PDFs to be copied:
- {}
......@@ -127,7 +132,7 @@ Files in output folder {} will be overwritten during this process.
if pdf_dir != "": # Take prefixes from pdf directory
pdf_folder = os.listdir(pdf_dir)
pdf_files = [_ for _ in pdf_folder
if _.endswith(".pdf") and
if _.lower().endswith(".pdf") and
matnum_utils.starts_with_matnum(_)]
prefixes = []
for pdf_file in pdf_files:
......@@ -135,20 +140,12 @@ Files in output folder {} will be overwritten during this process.
prefixes.append(prefix)
else: # Take prefixes from CSV file
prefixes = []
# Open CSV file
with open(csvfilename, newline='') as csvfile:
# Loop over all lines in CSV file
reader = csv.reader(csvfile, delimiter=',', quotechar='"')
next(reader) # skip header CSV line
for row in reader:
# Parse required fields from CSV line
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
infos = moodle.extract_info(csvfilename)
for info in infos:
prefix = prefixformat.format(matnum=info['matnum'],
fullname=info['fullname'],
lastname=info['lastname'],
firstname=info['firstname'])
prefixes.append(prefix) # save prefix
# Copy supplements to output dir and prepend prefixes
......
......@@ -85,7 +85,7 @@ class MainTest(unittest.TestCase):
# Prepare parameter
supp_dir = './supplements'
pdf_dir = './pdfs'
dpi = 150
dpi = 100
supp_out_dir = os.path.join(self.test_dir, 'supplements_out')
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