Bopytex/bopytex/planner/only_corr_planner.py

59 lines
1.5 KiB
Python
Raw Normal View History

2022-04-09 20:46:09 +00:00
from bopytex.tasks import Task, activate_corr_on, compile_pdf, generate, join_pdfs
import bopytex.planner.naming as naming
import os
def list_files(dir=".", accept=lambda _: True, reject=lambda _: False):
files = []
for file in os.listdir(dir):
if accept(file) and not reject(file):
files.append(file)
return files
2022-04-09 20:46:09 +00:00
def only_corr_planner(options: dict) -> list[Task]:
sources = list_files(
accept=lambda x: x.endswith(".tex"),
reject=lambda x: x.startswith("tpl_"),
)
options["sources"] = sources
return only_corr_tasks_builder(options)
2022-04-09 20:46:09 +00:00
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