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
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 = []

View File

@ -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(