refact: planners accept only options in parameters

This commit is contained in:
Bertrand Benjamin 2022-04-09 22:29:58 +02:00
parent 8c9d7bf9a2
commit dfcc48dd20
2 changed files with 65 additions and 24 deletions

View File

@ -1,6 +1,10 @@
from bopytex.tasks import Task, activate_corr_on, compile_pdf, generate, join_pdfs from bopytex.tasks import Task, activate_corr_on, compile_pdf, generate, join_pdfs
class PlannerMissingOption(Exception):
pass
def naming_template2source(template: str, metadatas: dict): def naming_template2source(template: str, metadatas: dict):
return metadatas["number"] + template[3:] return metadatas["number"] + template[3:]
@ -18,12 +22,25 @@ def naming_join(template):
def default_planner( def default_planner(
template: str, options: dict,
subjects: list[dict],
corr: bool = False,
no_join: bool = False,
no_pdf: bool = False,
) -> list[Task]: ) -> 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 = [] tasks = []
pdfs = [] pdfs = []
@ -61,10 +78,21 @@ def default_planner(
def only_corr_planner( def only_corr_planner(
sources=[], options: dict,
no_pdf: bool = False,
no_join: bool = False,
) -> list[Task]: ) -> 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 = [] tasks = []
corr_pdfs = [] corr_pdfs = []

View File

@ -4,9 +4,11 @@ from bopytex.tasks import Task
def test_planner_generate(): def test_planner_generate():
tasks = default_planner( tasks = default_planner(
template="tpl_source.tex", options={
subjects=[{"number": "01"}, {"number": "02"}], "template": "tpl_source.tex",
no_pdf=1, "subjects": [{"number": "01"}, {"number": "02"}],
"no_pdf": True,
}
) )
assert tasks == [ assert tasks == [
Task( Task(
@ -26,9 +28,11 @@ def test_planner_generate():
def test_planner_generate_compile(): def test_planner_generate_compile():
tasks = default_planner( tasks = default_planner(
template="tpl_source.tex", options={
subjects=[{"number": "01"}, {"number": "02"}], "template": "tpl_source.tex",
no_join=1, "subjects": [{"number": "01"}, {"number": "02"}],
"no_join": True,
}
) )
assert tasks == [ assert tasks == [
Task( Task(
@ -60,8 +64,10 @@ def test_planner_generate_compile():
def test_planner_generate_compile_join(): def test_planner_generate_compile_join():
tasks = default_planner( tasks = default_planner(
template="tpl_source.tex", options={
subjects=[{"number": "01"}, {"number": "02"}], "template": "tpl_source.tex",
"subjects": [{"number": "01"}, {"number": "02"}],
}
) )
assert tasks == [ assert tasks == [
Task( Task(
@ -99,10 +105,12 @@ def test_planner_generate_compile_join():
def test_planner_generate_compile_corr(): def test_planner_generate_compile_corr():
tasks = default_planner( tasks = default_planner(
template="tpl_source.tex", options={
subjects=[{"number": "01"}, {"number": "02"}], "template": "tpl_source.tex",
corr=1, "subjects": [{"number": "01"}, {"number": "02"}],
no_join=1, "corr": 1,
"no_join": 1,
}
) )
assert tasks == [ assert tasks == [
Task( Task(
@ -158,9 +166,12 @@ def test_planner_generate_compile_corr():
def test_planner_generate_compile_corr_joined(): def test_planner_generate_compile_corr_joined():
tasks = default_planner( tasks = default_planner(
template="tpl_source.tex", options={
subjects=[{"number": "01"}, {"number": "02"}], "template": "tpl_source.tex",
corr=1, "subjects": [{"number": "01"}, {"number": "02"}],
"corr": True,
"no_join": False,
}
) )
assert tasks == [ assert tasks == [
Task( Task(
@ -228,7 +239,9 @@ def test_planner_generate_compile_corr_joined():
def test_only_corr_planner(): def test_only_corr_planner():
tasks = only_corr_planner( tasks = only_corr_planner(
sources=["01_source.tex", "02_source.tex"], options={
"sources": ["01_source.tex", "02_source.tex"],
}
) )
assert tasks == [ assert tasks == [
Task( Task(