feat: planner for normal workflow

This commit is contained in:
Bertrand Benjamin 2022-04-09 15:33:28 +02:00
parent 1865f9ec63
commit f9dd70a2f1
2 changed files with 290 additions and 0 deletions

67
bopytex/planner.py Normal file
View File

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

223
test/test_planner.py Normal file
View File

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