From c89959673b5bc3bb50f7aa4fb222867d329020f3 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Sat, 9 Apr 2022 22:46:09 +0200 Subject: [PATCH] refact: move planners to own directory --- bopytex/planner.py | 115 --------------------------- bopytex/planner/__init__.py | 2 + bopytex/planner/default_planner.py | 62 +++++++++++++++ bopytex/planner/exceptions.py | 2 + bopytex/planner/naming.py | 15 ++++ bopytex/planner/only_corr_planner.py | 44 ++++++++++ test/test_planner.py | 27 ++++--- 7 files changed, 139 insertions(+), 128 deletions(-) delete mode 100644 bopytex/planner.py create mode 100644 bopytex/planner/__init__.py create mode 100644 bopytex/planner/default_planner.py create mode 100644 bopytex/planner/exceptions.py create mode 100644 bopytex/planner/naming.py create mode 100644 bopytex/planner/only_corr_planner.py diff --git a/bopytex/planner.py b/bopytex/planner.py deleted file mode 100644 index 2104028..0000000 --- a/bopytex/planner.py +++ /dev/null @@ -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 diff --git a/bopytex/planner/__init__.py b/bopytex/planner/__init__.py new file mode 100644 index 0000000..9ceb248 --- /dev/null +++ b/bopytex/planner/__init__.py @@ -0,0 +1,2 @@ +from .default_planner import default_planner +from .only_corr_planner import only_corr_planner diff --git a/bopytex/planner/default_planner.py b/bopytex/planner/default_planner.py new file mode 100644 index 0000000..9255ae1 --- /dev/null +++ b/bopytex/planner/default_planner.py @@ -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 diff --git a/bopytex/planner/exceptions.py b/bopytex/planner/exceptions.py new file mode 100644 index 0000000..65c7a29 --- /dev/null +++ b/bopytex/planner/exceptions.py @@ -0,0 +1,2 @@ +class PlannerMissingOption(Exception): + pass diff --git a/bopytex/planner/naming.py b/bopytex/planner/naming.py new file mode 100644 index 0000000..bf022b4 --- /dev/null +++ b/bopytex/planner/naming.py @@ -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:]) + diff --git a/bopytex/planner/only_corr_planner.py b/bopytex/planner/only_corr_planner.py new file mode 100644 index 0000000..4656659 --- /dev/null +++ b/bopytex/planner/only_corr_planner.py @@ -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 diff --git a/test/test_planner.py b/test/test_planner.py index 0306805..0317abf 100644 --- a/test/test_planner.py +++ b/test/test_planner.py @@ -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 -def test_planner_generate(): - tasks = default_planner( +def test_tasks_builder_generate(): + tasks = default_tasks_builder( options={ "template": "tpl_source.tex", "subjects": [{"number": "01"}, {"number": "02"}], @@ -26,8 +27,8 @@ def test_planner_generate(): ] -def test_planner_generate_compile(): - tasks = default_planner( +def test_tasks_builder_generate_compile(): + tasks = default_tasks_builder( options={ "template": "tpl_source.tex", "subjects": [{"number": "01"}, {"number": "02"}], @@ -62,8 +63,8 @@ def test_planner_generate_compile(): ] -def test_planner_generate_compile_join(): - tasks = default_planner( +def test_tasks_builder_generate_compile_join(): + tasks = default_tasks_builder( options={ "template": "tpl_source.tex", "subjects": [{"number": "01"}, {"number": "02"}], @@ -103,8 +104,8 @@ def test_planner_generate_compile_join(): ] -def test_planner_generate_compile_corr(): - tasks = default_planner( +def test_tasks_builder_generate_compile_corr(): + tasks = default_tasks_builder( options={ "template": "tpl_source.tex", "subjects": [{"number": "01"}, {"number": "02"}], @@ -164,8 +165,8 @@ def test_planner_generate_compile_corr(): ] -def test_planner_generate_compile_corr_joined(): - tasks = default_planner( +def test_tasks_builder_generate_compile_corr_joined(): + tasks = default_tasks_builder( options={ "template": "tpl_source.tex", "subjects": [{"number": "01"}, {"number": "02"}], @@ -237,8 +238,8 @@ def test_planner_generate_compile_corr_joined(): ] -def test_only_corr_planner(): - tasks = only_corr_planner( +def test_only_corr_tasks_builder(): + tasks = only_corr_tasks_builder( options={ "sources": ["01_source.tex", "02_source.tex"], }