From 1ba29c057b0a223ea55a615f84ac29a74c40ed24 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Mon, 23 Dec 2019 16:14:56 +0100 Subject: [PATCH] Feat: use and test csv files --- bopytex/bopytex.py | 47 +++++++++++++++++++++++------------ test/students.csv | 6 +++++ test/test_bopytex.py | 59 ++++++++++++++++++++++++++++++-------------- 3 files changed, 78 insertions(+), 34 deletions(-) create mode 100644 test/students.csv diff --git a/bopytex/bopytex.py b/bopytex/bopytex.py index dcb0fdd..645f6a4 100755 --- a/bopytex/bopytex.py +++ b/bopytex/bopytex.py @@ -100,14 +100,33 @@ def pdfjoin(pdf_files, destname, working_dir=".", rm_pdfs=1): def extract_student_csv(csv_filename): - """ Extract student list from csv_filename - - Student identifier is got in the column "Élève". - - """ + """ Extract student list from csv_filename """ with open(csv_filename, "r") as csvfile: reader = csv.DictReader(csvfile) - return [r["Élève"] for r in reader] + return [r for r in reader] + + +def subject_metadata(quantity=0, metacsv=None): + """ Return metadata on subject to produce + + if csv is given it will based on is + otherwise it will be based on quantity + + :example: + >>> subject_metadata(10) + """ + if metacsv: + metadata = [] + for (i, s) in enumerate(extract_student_csv(metacsv)): + d = {"num": f"{i+1:02d}"} + d.update(s) + metadata.append(d) + elif quantity > 0: + metadata = [{"num": f"{i+1:02d}"} for i in range(quantity)] + else: + raise ValueError("Need metacsv or quantity to build subject metadata") + + return metadata def produce_and_compile(options): @@ -122,15 +141,11 @@ def produce_and_compile(options): template = Path(options["template"]).name logger.debug(f"Template will be {template}") - if options["students_csv"]: - list_infos = [ - {"num": f"{i+1:02d}", "name": s} - for (i, s) in enumerate(extract_student_csv(options["students_csv"])) - ] - else: - list_infos = [ - {"num": f"{i+1:02d}"} for i in range(options["number_subjects"]) - ] + list_infos = subject_metadata( + options["number_subjects"], options["students_csv"] + ) + + logger.debug(f"Metadata {list_infos}") tex_files = [] for infos in list_infos: @@ -162,7 +177,7 @@ def produce_and_compile(options): logger.debug(f"Start compiling {texfile}") pytex.pdflatex(texfile) logger.debug(f"End compiling {texfile}") - pdf_files.append(str(texfile).split('.')[0] + ".pdf") + pdf_files.append(str(texfile).split(".")[0] + ".pdf") logger.debug(f"Compiled files : {pdf_files}") if not options["no_join"] and not options["no_compile"]: diff --git a/test/students.csv b/test/students.csv new file mode 100644 index 0000000..5ddf91d --- /dev/null +++ b/test/students.csv @@ -0,0 +1,6 @@ +nom,classe,elo +Bob,1ST,1000 +Pipo,1ST,1300 +Popi,1ST,100 +Boule,1ST,4000 +Bill,1ST,1300 diff --git a/test/test_bopytex.py b/test/test_bopytex.py index 151c270..3857a47 100644 --- a/test/test_bopytex.py +++ b/test/test_bopytex.py @@ -9,7 +9,8 @@ from shutil import copyfile from bopytex import produce_and_compile SNIPPETS_PATH = Path("snippets/") -TEST_TEMPLATE_PATH = Path("test/templates/") +TEST_PATH = Path("test") +TEST_TEMPLATE_PATH = TEST_PATH / "templates/" @pytest.fixture @@ -22,6 +23,9 @@ def prepare_test_template(tmp_path): snippets = TEST_TEMPLATE_PATH.glob("tpl_*.tex") for s in snippets: copyfile(s, tmp / s.name) + csvs = TEST_PATH.glob("*.csv") + for s in csvs: + copyfile(s, tmp / s.name) prev_dir = Path.cwd() os.chdir(tmp) @@ -66,6 +70,25 @@ def test_produce_and_compile_base(prepare_test_template): ) +def test_produce_and_compile_csv(prepare_test_template): + test_tpl = list(Path(".").glob("tpl_*.tex")) + assert [tpl.name for tpl in test_tpl] == ["tpl_test.tex"] + for tpl in test_tpl: + produce_and_compile( + { + "template": tpl, + "working_dir": None, + "only_corr": False, + "students_csv": "students.csv", + "number_subjects": 1, + "dirty": False, + "no_compile": False, + "no_join": False, + "corr": False, + "crazy": False, + } + ) + def test_pdfjoin_current_directory(prepare_test_template): wdir = prepare_test_template pass @@ -99,23 +122,23 @@ def test_activate_solution(): pass -def test_snippets(prepare_snippets): - snippets = list(Path(".").glob("tpl_*.tex")) - for tpl in snippets: - produce_and_compile( - { - "template": tpl, - "working_dir": None, - "only_corr": False, - "students_csv": None, - "number_subjects": 1, - "dirty": False, - "no_compile": False, - "no_join": False, - "corr": False, - "crazy": False, - } - ) +#def test_snippets(prepare_snippets): +# snippets = list(Path(".").glob("tpl_*.tex")) +# for tpl in snippets: +# produce_and_compile( +# { +# "template": tpl, +# "working_dir": None, +# "only_corr": False, +# "students_csv": None, +# "number_subjects": 1, +# "dirty": False, +# "no_compile": False, +# "no_join": False, +# "corr": False, +# "crazy": False, +# } +# ) # -----------------------------