refact: move planners to own directory

This commit is contained in:
Bertrand Benjamin 2022-04-09 22:46:09 +02:00
parent dfcc48dd20
commit c89959673b
7 changed files with 139 additions and 128 deletions

View File

@ -1,115 +0,0 @@
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:]
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 default_planner(
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 = []
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(
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 = []
for source in sources:
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 = "joined.pdf"
if corr_pdfs:
corr_joined = naming_corr(joined)
tasks.append(join_pdfs(corr_pdfs, corr_joined))
return tasks

View File

@ -0,0 +1,2 @@
from .default_planner import default_planner
from .only_corr_planner import only_corr_planner

View File

@ -0,0 +1,62 @@
from bopytex.tasks import Task, activate_corr_on, compile_pdf, generate, join_pdfs
import bopytex.planner.naming as naming
def default_planner(options: dict) -> list[Task]:
pass
def default_tasks_builder(
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 = []
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

View File

@ -0,0 +1,2 @@
class PlannerMissingOption(Exception):
pass

15
bopytex/planner/naming.py Normal file
View File

@ -0,0 +1,15 @@
def template2source(template: str, metadatas: dict):
return metadatas["number"] + template[3:]
def corr(source):
return "corr_" + source
def source2pdf(source):
return source[:-4] + ".pdf"
def join(template):
return source2pdf("joined" + template[3:])

View File

@ -0,0 +1,44 @@
from bopytex.tasks import Task, activate_corr_on, compile_pdf, generate, join_pdfs
import bopytex.planner.naming as naming
def only_corr_planner(options: dict) -> list[Task]:
pass
def only_corr_tasks_builder(
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 = []
for source in sources:
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 = "joined.pdf"
if corr_pdfs:
corr_joined = naming.corr(joined)
tasks.append(join_pdfs(corr_pdfs, corr_joined))
return tasks

View File

@ -1,9 +1,10 @@
from bopytex.planner import only_corr_planner, default_planner from bopytex.planner.default_planner import default_tasks_builder
from bopytex.planner.only_corr_planner import only_corr_tasks_builder
from bopytex.tasks import Task from bopytex.tasks import Task
def test_planner_generate(): def test_tasks_builder_generate():
tasks = default_planner( tasks = default_tasks_builder(
options={ options={
"template": "tpl_source.tex", "template": "tpl_source.tex",
"subjects": [{"number": "01"}, {"number": "02"}], "subjects": [{"number": "01"}, {"number": "02"}],
@ -26,8 +27,8 @@ def test_planner_generate():
] ]
def test_planner_generate_compile(): def test_tasks_builder_generate_compile():
tasks = default_planner( tasks = default_tasks_builder(
options={ options={
"template": "tpl_source.tex", "template": "tpl_source.tex",
"subjects": [{"number": "01"}, {"number": "02"}], "subjects": [{"number": "01"}, {"number": "02"}],
@ -62,8 +63,8 @@ def test_planner_generate_compile():
] ]
def test_planner_generate_compile_join(): def test_tasks_builder_generate_compile_join():
tasks = default_planner( tasks = default_tasks_builder(
options={ options={
"template": "tpl_source.tex", "template": "tpl_source.tex",
"subjects": [{"number": "01"}, {"number": "02"}], "subjects": [{"number": "01"}, {"number": "02"}],
@ -103,8 +104,8 @@ def test_planner_generate_compile_join():
] ]
def test_planner_generate_compile_corr(): def test_tasks_builder_generate_compile_corr():
tasks = default_planner( tasks = default_tasks_builder(
options={ options={
"template": "tpl_source.tex", "template": "tpl_source.tex",
"subjects": [{"number": "01"}, {"number": "02"}], "subjects": [{"number": "01"}, {"number": "02"}],
@ -164,8 +165,8 @@ def test_planner_generate_compile_corr():
] ]
def test_planner_generate_compile_corr_joined(): def test_tasks_builder_generate_compile_corr_joined():
tasks = default_planner( tasks = default_tasks_builder(
options={ options={
"template": "tpl_source.tex", "template": "tpl_source.tex",
"subjects": [{"number": "01"}, {"number": "02"}], "subjects": [{"number": "01"}, {"number": "02"}],
@ -237,8 +238,8 @@ def test_planner_generate_compile_corr_joined():
] ]
def test_only_corr_planner(): def test_only_corr_tasks_builder():
tasks = only_corr_planner( tasks = only_corr_tasks_builder(
options={ options={
"sources": ["01_source.tex", "02_source.tex"], "sources": ["01_source.tex", "02_source.tex"],
} }