Feat: use and test csv files

This commit is contained in:
Bertrand Benjamin 2019-12-23 16:14:56 +01:00
parent 9d5c231c9c
commit 1ba29c057b
3 changed files with 78 additions and 34 deletions

View File

@ -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"]:

6
test/students.csv Normal file
View File

@ -0,0 +1,6 @@
nom,classe,elo
Bob,1ST,1000
Pipo,1ST,1300
Popi,1ST,100
Boule,1ST,4000
Bill,1ST,1300
1 nom classe elo
2 Bob 1ST 1000
3 Pipo 1ST 1300
4 Popi 1ST 100
5 Boule 1ST 4000
6 Bill 1ST 1300

View File

@ -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,
# }
# )
# -----------------------------