Feat: arrange variables for templates

Split in 3 categories
- from "options" key build with default_config, file_config and cli options
- from "subject" key build csv or quantity of subject
- other in direct access from "direct_access" dictionnary inside the
  file_config
This commit is contained in:
Bertrand Benjamin 2022-07-20 16:16:09 +02:00
parent 37d779c0ab
commit 67656c2cf1
8 changed files with 147 additions and 61 deletions

View File

@ -41,7 +41,7 @@ def tasks_builder(
for source in sources: for source in sources:
corr_source = naming.corr(source) corr_source = naming.corr(source)
tasks.append(activate_corr_on(source, corr_source)) tasks.append(activate_corr_on(source, opt, corr_source))
if not no_pdf: if not no_pdf:
corr_pdf = naming.source2pdf(corr_source) corr_pdf = naming.source2pdf(corr_source)

View File

@ -69,7 +69,10 @@ def tasks_builder(
for subject in subjects: for subject in subjects:
source = naming.template2source(template, subject) source = naming.template2source(template, subject)
args = {**subject, **options} args = {
"subject": subject,
"options": options
}
tasks.append(generate(template, args, source)) tasks.append(generate(template, args, source))

View File

@ -62,6 +62,8 @@ def config_from_file(filename: str) -> dict:
def build_config(options: dict) -> dict: def build_config(options: dict) -> dict:
"""Look for options["configfile"] to load it with default_config and options""" """Look for options["configfile"] to load it with default_config and options"""
config = clean_vars_keys(vars(default_config))
configfile = "" configfile = ""
try: try:
configfile = options["configfile"] configfile = options["configfile"]
@ -71,12 +73,12 @@ def build_config(options: dict) -> dict:
configfile = os.environ["BOPYTEXCONFIG"] configfile = os.environ["BOPYTEXCONFIG"]
except KeyError: except KeyError:
pass pass
if configfile:
local_config = config_from_file(configfile) local_config = config_from_file(configfile)
config = clean_vars_keys(vars(default_config))
config.update(local_config) config.update(local_config)
config.update(options) config.update(options)
return config return config

View File

@ -4,12 +4,26 @@ from bopytex.message import Message
def generate(args, deps, output): def generate(args, deps, output):
env = args["jinja2"]["environment"] env = args["options"]["jinja2"]["environment"]
template = env.get_template(deps[0]) template = env.get_template(deps[0])
variables = {
"options":args["options"],
"subject":args["subject"],
}
try:
args["options"]["direct_access"]
except KeyError:
pass
else:
for (k,v) in args["options"]["direct_access"].items():
if k not in ["options", "subject"]:
variables[k] = v
try: try:
with open(output, "w") as out: with open(output, "w") as out:
fed = template.render(args) fed = template.render(variables)
out.write(fed) out.write(fed)
return Message(0, [f"GENERATE - {deps[0]} to {output}"], []) return Message(0, [f"GENERATE - {deps[0]} to {output}"], [])

View File

@ -15,10 +15,12 @@ def test_tasks_builder_generate():
Task( Task(
action="GENERATE", action="GENERATE",
args={ args={
"number": "01", "options": {
"template": "tpl_source.tex",
"subjects": [{"number": "01"}, {"number": "02"}],
"no_pdf": True, "no_pdf": True,
"subjects": [{"number": "01"}, {"number": "02"}],
'template': 'tpl_source.tex',
},
"subject": {"number": "01"}
}, },
deps=["tpl_source.tex"], deps=["tpl_source.tex"],
output="01_source.tex", output="01_source.tex",
@ -26,10 +28,12 @@ def test_tasks_builder_generate():
Task( Task(
action="GENERATE", action="GENERATE",
args={ args={
"number": "02", "options": {
"template": "tpl_source.tex",
"subjects": [{"number": "01"}, {"number": "02"}],
"no_pdf": True, "no_pdf": True,
"subjects": [{"number": "01"}, {"number": "02"}],
'template': 'tpl_source.tex',
},
"subject": {"number": "02"}
}, },
deps=["tpl_source.tex"], deps=["tpl_source.tex"],
output="02_source.tex", output="02_source.tex",
@ -49,10 +53,12 @@ def test_tasks_builder_generate_compile():
Task( Task(
action="GENERATE", action="GENERATE",
args={ args={
"number": "01", "options": {
"template": "tpl_source.tex",
"subjects": [{"number": "01"}, {"number": "02"}],
"no_join": True, "no_join": True,
"subjects": [{"number": "01"}, {"number": "02"}],
'template': 'tpl_source.tex',
},
"subject": {"number": "01"}
}, },
deps=["tpl_source.tex"], deps=["tpl_source.tex"],
output="01_source.tex", output="01_source.tex",
@ -66,10 +72,12 @@ def test_tasks_builder_generate_compile():
Task( Task(
action="GENERATE", action="GENERATE",
args={ args={
"number": "02", "options": {
"template": "tpl_source.tex",
"subjects": [{"number": "01"}, {"number": "02"}],
"no_join": True, "no_join": True,
"subjects": [{"number": "01"}, {"number": "02"}],
'template': 'tpl_source.tex',
},
"subject": {"number": "02"}
}, },
deps=["tpl_source.tex"], deps=["tpl_source.tex"],
output="02_source.tex", output="02_source.tex",
@ -94,9 +102,11 @@ def test_tasks_builder_generate_compile_join():
Task( Task(
action="GENERATE", action="GENERATE",
args={ args={
"number": "01", "options": {
"template": "tpl_source.tex",
"subjects": [{"number": "01"}, {"number": "02"}], "subjects": [{"number": "01"}, {"number": "02"}],
'template': 'tpl_source.tex',
},
"subject": {"number": "01"}
}, },
deps=["tpl_source.tex"], deps=["tpl_source.tex"],
output="01_source.tex", output="01_source.tex",
@ -110,9 +120,11 @@ def test_tasks_builder_generate_compile_join():
Task( Task(
action="GENERATE", action="GENERATE",
args={ args={
"number": "02", "options": {
"template": "tpl_source.tex",
"subjects": [{"number": "01"}, {"number": "02"}], "subjects": [{"number": "01"}, {"number": "02"}],
'template': 'tpl_source.tex',
},
"subject": {"number": "02"}
}, },
deps=["tpl_source.tex"], deps=["tpl_source.tex"],
output="02_source.tex", output="02_source.tex",
@ -145,11 +157,13 @@ def test_tasks_builder_generate_compile_corr():
Task( Task(
action="GENERATE", action="GENERATE",
args={ args={
"number": "01", "options": {
"template": "tpl_source.tex",
"subjects": [{"number": "01"}, {"number": "02"}], "subjects": [{"number": "01"}, {"number": "02"}],
"corr": True, "corr": True,
"no_join": True, "no_join": True,
'template': 'tpl_source.tex',
},
"subject": {"number": "01"}
}, },
deps=["tpl_source.tex"], deps=["tpl_source.tex"],
output="01_source.tex", output="01_source.tex",
@ -162,7 +176,13 @@ def test_tasks_builder_generate_compile_corr():
), ),
Task( Task(
action="ACTIVATE_CORR", action="ACTIVATE_CORR",
args={}, args={
'corr': True,
'no_join': True,
'no_pdf': False,
'template': 'tpl_source.tex',
"subjects": [{'number': '01'}, {'number': '02'}]
},
deps=["01_source.tex"], deps=["01_source.tex"],
output="corr_01_source.tex", output="corr_01_source.tex",
), ),
@ -175,11 +195,13 @@ def test_tasks_builder_generate_compile_corr():
Task( Task(
action="GENERATE", action="GENERATE",
args={ args={
"number": "02", "options": {
"template": "tpl_source.tex",
"subjects": [{"number": "01"}, {"number": "02"}], "subjects": [{"number": "01"}, {"number": "02"}],
"corr": True, "corr": True,
"no_join": True, "no_join": True,
'template': 'tpl_source.tex',
},
"subject": {"number": "02"}
}, },
deps=["tpl_source.tex"], deps=["tpl_source.tex"],
output="02_source.tex", output="02_source.tex",
@ -192,7 +214,13 @@ def test_tasks_builder_generate_compile_corr():
), ),
Task( Task(
action="ACTIVATE_CORR", action="ACTIVATE_CORR",
args={}, args={
'corr': True,
'no_join': True,
'no_pdf': False,
'template': 'tpl_source.tex',
"subjects": [{'number': '01'}, {'number': '02'}]
},
deps=["02_source.tex"], deps=["02_source.tex"],
output="corr_02_source.tex", output="corr_02_source.tex",
), ),
@ -218,11 +246,13 @@ def test_tasks_builder_generate_compile_corr_joined():
Task( Task(
action="GENERATE", action="GENERATE",
args={ args={
"number": "01", "options": {
"subjects": [{"number": "01"}, {"number": "02"}],
"corr": True, "corr": True,
"no_join": False, "no_join": False,
"template": "tpl_source.tex", 'template': 'tpl_source.tex',
"subjects": [{"number": "01"}, {"number": "02"}], },
"subject": {"number": "01"}
}, },
deps=["tpl_source.tex"], deps=["tpl_source.tex"],
output="01_source.tex", output="01_source.tex",
@ -235,7 +265,13 @@ def test_tasks_builder_generate_compile_corr_joined():
), ),
Task( Task(
action="ACTIVATE_CORR", action="ACTIVATE_CORR",
args={}, args={
'corr': True,
'no_join': False,
'no_pdf': False,
'template': 'tpl_source.tex',
"subjects": [{'number': '01'}, {'number': '02'}]
},
deps=["01_source.tex"], deps=["01_source.tex"],
output="corr_01_source.tex", output="corr_01_source.tex",
), ),
@ -248,11 +284,13 @@ def test_tasks_builder_generate_compile_corr_joined():
Task( Task(
action="GENERATE", action="GENERATE",
args={ args={
"number": "02", "options": {
"template": "tpl_source.tex",
"subjects": [{"number": "01"}, {"number": "02"}], "subjects": [{"number": "01"}, {"number": "02"}],
"corr": True, "corr": True,
"no_join": False, "no_join": False,
'template': 'tpl_source.tex',
},
"subject": {"number": "02"}
}, },
deps=["tpl_source.tex"], deps=["tpl_source.tex"],
output="02_source.tex", output="02_source.tex",
@ -265,7 +303,13 @@ def test_tasks_builder_generate_compile_corr_joined():
), ),
Task( Task(
action="ACTIVATE_CORR", action="ACTIVATE_CORR",
args={}, args={
'corr': True,
'no_join': False,
'no_pdf': False,
'template': 'tpl_source.tex',
"subjects": [{'number': '01'}, {'number': '02'}]
},
deps=["02_source.tex"], deps=["02_source.tex"],
output="corr_02_source.tex", output="corr_02_source.tex",
), ),
@ -299,7 +343,11 @@ def test_only_corr_tasks_builder():
assert tasks == [ assert tasks == [
Task( Task(
action="ACTIVATE_CORR", action="ACTIVATE_CORR",
args={}, args={
'no_join': False,
'no_pdf': False,
'sources': ['01_source.tex', '02_source.tex']
},
deps=["01_source.tex"], deps=["01_source.tex"],
output="corr_01_source.tex", output="corr_01_source.tex",
), ),
@ -311,7 +359,11 @@ def test_only_corr_tasks_builder():
), ),
Task( Task(
action="ACTIVATE_CORR", action="ACTIVATE_CORR",
args={}, args={
'no_join': False,
'no_pdf': False,
'sources': ['01_source.tex', '02_source.tex']
},
deps=["02_source.tex"], deps=["02_source.tex"],
output="corr_02_source.tex", output="corr_02_source.tex",
), ),

View File

@ -49,7 +49,8 @@ def test_get_config_with_configfile(config_file, tmp_path):
"planner", "planner",
"pdflatex", "pdflatex",
"clean", "clean",
"j2", "latex",
"activate_corr",
"dispatcher", "dispatcher",
"pdfjam", "pdfjam",
"jinja2", "jinja2",

View File

@ -11,7 +11,7 @@ def test_build_task_generate():
def test_build_task_activate_corr_on(): def test_build_task_activate_corr_on():
src = "source.tex" src = "source.tex"
task = activate_corr_on(src, output="corr_source.tex") task = activate_corr_on(src, meta={}, output="corr_source.tex")
assert task.action == "ACTIVATE_CORR" assert task.action == "ACTIVATE_CORR"
assert task.args == {} assert task.args == {}
assert task.deps == [src] assert task.deps == [src]

View File

@ -27,7 +27,13 @@ def test_generate(template_path, jinja2_env):
output = "output" output = "output"
message = generate( message = generate(
args={"a": 2, "jinja2": {"environment": jinja2_env}}, args={
"options": {
"direct_access": {"a": 2},
"jinja2": {"environment": jinja2_env},
},
"subject": {},
},
deps=[template], deps=[template],
output=output, output=output,
) )
@ -59,7 +65,15 @@ def test_generate_with_random(template_path_with_random, jinja2_env):
import random import random
message = generate( message = generate(
args={"random": random, "jinja2": {"environment": jinja2_env}}, args={
"options": {
"jinja2": {"environment": jinja2_env},
"direct_access": {
"random": random,
}
},
"subject":{},
},
deps=[template], deps=[template],
output=output, output=output,
) )