From dfcc48dd20e3b25aa47a0111bc3c46b26e426bbb Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Sat, 9 Apr 2022 22:29:58 +0200 Subject: [PATCH] refact: planners accept only options in parameters --- bopytex/planner.py | 44 +++++++++++++++++++++++++++++++++++-------- test/test_planner.py | 45 ++++++++++++++++++++++++++++---------------- 2 files changed, 65 insertions(+), 24 deletions(-) diff --git a/bopytex/planner.py b/bopytex/planner.py index 28fea3b..2104028 100644 --- a/bopytex/planner.py +++ b/bopytex/planner.py @@ -1,6 +1,10 @@ from bopytex.tasks import Task, activate_corr_on, compile_pdf, generate, join_pdfs +class PlannerMissingOption(Exception): + pass + + def naming_template2source(template: str, metadatas: dict): return metadatas["number"] + template[3:] @@ -18,12 +22,25 @@ def naming_join(template): def default_planner( - template: str, - subjects: list[dict], - corr: bool = False, - no_join: bool = False, - no_pdf: bool = False, + options: dict, ) -> list[Task]: + + opt = { + "corr": False, + "no_join": False, + "no_pdf": False, + } + opt.update(options) + + try: + template = opt["template"] + subjects = opt["subjects"] + corr = opt["corr"] + no_join = opt["no_join"] + no_pdf = opt["no_pdf"] + except KeyError: + raise PlannerMissingOption("An option is missing") + tasks = [] pdfs = [] @@ -61,10 +78,21 @@ def default_planner( def only_corr_planner( - sources=[], - no_pdf: bool = False, - no_join: bool = False, + options: dict, ) -> list[Task]: + opt = { + "no_join": False, + "no_pdf": False, + } + opt.update(options) + + try: + sources = opt["sources"] + no_join = opt["no_join"] + no_pdf = opt["no_pdf"] + except KeyError: + raise PlannerMissingOption("An option is missing") + tasks = [] corr_pdfs = [] diff --git a/test/test_planner.py b/test/test_planner.py index 41c542f..0306805 100644 --- a/test/test_planner.py +++ b/test/test_planner.py @@ -4,9 +4,11 @@ from bopytex.tasks import Task def test_planner_generate(): tasks = default_planner( - template="tpl_source.tex", - subjects=[{"number": "01"}, {"number": "02"}], - no_pdf=1, + options={ + "template": "tpl_source.tex", + "subjects": [{"number": "01"}, {"number": "02"}], + "no_pdf": True, + } ) assert tasks == [ Task( @@ -26,9 +28,11 @@ def test_planner_generate(): def test_planner_generate_compile(): tasks = default_planner( - template="tpl_source.tex", - subjects=[{"number": "01"}, {"number": "02"}], - no_join=1, + options={ + "template": "tpl_source.tex", + "subjects": [{"number": "01"}, {"number": "02"}], + "no_join": True, + } ) assert tasks == [ Task( @@ -60,8 +64,10 @@ def test_planner_generate_compile(): def test_planner_generate_compile_join(): tasks = default_planner( - template="tpl_source.tex", - subjects=[{"number": "01"}, {"number": "02"}], + options={ + "template": "tpl_source.tex", + "subjects": [{"number": "01"}, {"number": "02"}], + } ) assert tasks == [ Task( @@ -99,10 +105,12 @@ def test_planner_generate_compile_join(): def test_planner_generate_compile_corr(): tasks = default_planner( - template="tpl_source.tex", - subjects=[{"number": "01"}, {"number": "02"}], - corr=1, - no_join=1, + options={ + "template": "tpl_source.tex", + "subjects": [{"number": "01"}, {"number": "02"}], + "corr": 1, + "no_join": 1, + } ) assert tasks == [ Task( @@ -158,9 +166,12 @@ def test_planner_generate_compile_corr(): def test_planner_generate_compile_corr_joined(): tasks = default_planner( - template="tpl_source.tex", - subjects=[{"number": "01"}, {"number": "02"}], - corr=1, + options={ + "template": "tpl_source.tex", + "subjects": [{"number": "01"}, {"number": "02"}], + "corr": True, + "no_join": False, + } ) assert tasks == [ Task( @@ -228,7 +239,9 @@ def test_planner_generate_compile_corr_joined(): def test_only_corr_planner(): tasks = only_corr_planner( - sources=["01_source.tex", "02_source.tex"], + options={ + "sources": ["01_source.tex", "02_source.tex"], + } ) assert tasks == [ Task(