From f9dd70a2f1c18e4b3db608c397e7781b3d454ee2 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Sat, 9 Apr 2022 15:33:28 +0200 Subject: [PATCH] feat: planner for normal workflow --- bopytex/planner.py | 67 +++++++++++++ test/test_planner.py | 223 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 290 insertions(+) create mode 100644 bopytex/planner.py create mode 100644 test/test_planner.py diff --git a/bopytex/planner.py b/bopytex/planner.py new file mode 100644 index 0000000..1d54f0e --- /dev/null +++ b/bopytex/planner.py @@ -0,0 +1,67 @@ +from bopytex.tasks import Task, activate_corr_on, compile_pdf, generate, join_pdfs + + +def naming_template2source(template: str, metadatas: dict): + return metadatas["number"] + template[3:] + + +def naming_corr(source): + return "corr_" + source + + +def naming_source2pdf(source): + return source[:-4] + ".pdf" + + +def naming_join(template): + return naming_source2pdf("joined" + template[3:]) + + +def planner( + template: str, + subjects: list[dict], + corr: bool = False, + no_join: bool = False, + no_pdf: bool = False, + # dirty: bool = False, +) -> list[Task]: + tasks = [] + + pdfs = [] + corr_pdfs = [] + + for subject in subjects: + source = naming_template2source(template, subject) + + tasks.append(generate(template, subject, source)) + + if not no_pdf: + pdf = naming_source2pdf(source) + tasks.append(compile_pdf(source, pdf)) + pdfs.append(pdf) + + if corr: + corr_source = naming_corr(source) + tasks.append(activate_corr_on(source, corr_source)) + + if not no_pdf: + corr_pdf = naming_source2pdf(corr_source) + tasks.append(compile_pdf(corr_source, corr_pdf)) + corr_pdfs.append(corr_pdf) + + if not no_join: + joined = naming_join(template) + if pdfs: + tasks.append(join_pdfs(pdfs, joined)) + + if corr_pdfs: + corr_joined = naming_corr(joined) + tasks.append(join_pdfs(corr_pdfs, corr_joined)) + + return tasks + + +def only_corr_planner( + no_join: bool = False, + ): + pass diff --git a/test/test_planner.py b/test/test_planner.py new file mode 100644 index 0000000..0aaded4 --- /dev/null +++ b/test/test_planner.py @@ -0,0 +1,223 @@ +from bopytex.planner import planner +from bopytex.tasks import Task + + +def test_planner_generate(): + tasks = planner( + template="tpl_source.tex", + subjects=[{"number": "01"}, {"number": "02"}], + no_pdf=1, + ) + assert tasks == [ + Task( + action="GENERATE", + args={"number": "01"}, + deps=["tpl_source.tex"], + output="01_source.tex", + ), + Task( + action="GENERATE", + args={"number": "02"}, + deps=["tpl_source.tex"], + output="02_source.tex", + ), + ] + +def test_planner_generate_compile(): + tasks = planner( + template="tpl_source.tex", + subjects=[{"number": "01"}, {"number": "02"}], + no_join=1, + ) + assert tasks == [ + Task( + action="GENERATE", + args={"number": "01"}, + deps=["tpl_source.tex"], + output="01_source.tex", + ), + Task( + action="COMPILE", + args={}, + deps=["01_source.tex"], + output="01_source.pdf", + ), + Task( + action="GENERATE", + args={"number": "02"}, + deps=["tpl_source.tex"], + output="02_source.tex", + ), + Task( + action="COMPILE", + args={}, + deps=["02_source.tex"], + output="02_source.pdf", + ), + ] + +def test_planner_generate_compile_join(): + tasks = planner( + template="tpl_source.tex", + subjects=[{"number": "01"}, {"number": "02"}], + ) + assert tasks == [ + Task( + action="GENERATE", + args={"number": "01"}, + deps=["tpl_source.tex"], + output="01_source.tex", + ), + Task( + action="COMPILE", + args={}, + deps=["01_source.tex"], + output="01_source.pdf", + ), + Task( + action="GENERATE", + args={"number": "02"}, + deps=["tpl_source.tex"], + output="02_source.tex", + ), + Task( + action="COMPILE", + args={}, + deps=["02_source.tex"], + output="02_source.pdf", + ), + Task( + action="JOIN", + args={}, + deps=["01_source.pdf", "02_source.pdf"], + output="joined_source.pdf" + ) + ] + +def test_planner_generate_compile_corr(): + tasks = planner( + template="tpl_source.tex", + subjects=[{"number": "01"}, {"number": "02"}], + corr=1, + no_join=1 + ) + assert tasks == [ + Task( + action="GENERATE", + args={"number": "01"}, + deps=["tpl_source.tex"], + output="01_source.tex", + ), + Task( + action="COMPILE", + args={}, + deps=["01_source.tex"], + output="01_source.pdf", + ), + Task( + action="ACTIVATE_CORR", + args={}, + deps=["01_source.tex"], + output="corr_01_source.tex", + ), + Task( + action="COMPILE", + args={}, + deps=["corr_01_source.tex"], + output="corr_01_source.pdf", + ), + Task( + action="GENERATE", + args={"number": "02"}, + deps=["tpl_source.tex"], + output="02_source.tex", + ), + Task( + action="COMPILE", + args={}, + deps=["02_source.tex"], + output="02_source.pdf", + ), + Task( + action="ACTIVATE_CORR", + args={}, + deps=["02_source.tex"], + output="corr_02_source.tex", + ), + Task( + action="COMPILE", + args={}, + deps=["corr_02_source.tex"], + output="corr_02_source.pdf", + ), + ] + +def test_planner_generate_compile_corr_joined(): + tasks = planner( + template="tpl_source.tex", + subjects=[{"number": "01"}, {"number": "02"}], + corr=1, + ) + assert tasks == [ + Task( + action="GENERATE", + args={"number": "01"}, + deps=["tpl_source.tex"], + output="01_source.tex", + ), + Task( + action="COMPILE", + args={}, + deps=["01_source.tex"], + output="01_source.pdf", + ), + Task( + action="ACTIVATE_CORR", + args={}, + deps=["01_source.tex"], + output="corr_01_source.tex", + ), + Task( + action="COMPILE", + args={}, + deps=["corr_01_source.tex"], + output="corr_01_source.pdf", + ), + Task( + action="GENERATE", + args={"number": "02"}, + deps=["tpl_source.tex"], + output="02_source.tex", + ), + Task( + action="COMPILE", + args={}, + deps=["02_source.tex"], + output="02_source.pdf", + ), + Task( + action="ACTIVATE_CORR", + args={}, + deps=["02_source.tex"], + output="corr_02_source.tex", + ), + Task( + action="COMPILE", + args={}, + deps=["corr_02_source.tex"], + output="corr_02_source.pdf", + ), + Task( + action="JOIN", + args={}, + deps=["01_source.pdf", "02_source.pdf"], + output="joined_source.pdf" + ), + Task( + action="JOIN", + args={}, + deps=["corr_01_source.pdf", "corr_02_source.pdf"], + output="corr_joined_source.pdf" + ) + ] +