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:
parent
37d779c0ab
commit
67656c2cf1
@ -41,7 +41,7 @@ def tasks_builder(
|
||||
|
||||
for source in sources:
|
||||
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:
|
||||
corr_pdf = naming.source2pdf(corr_source)
|
||||
|
@ -69,7 +69,10 @@ def tasks_builder(
|
||||
|
||||
for subject in subjects:
|
||||
source = naming.template2source(template, subject)
|
||||
args = {**subject, **options}
|
||||
args = {
|
||||
"subject": subject,
|
||||
"options": options
|
||||
}
|
||||
|
||||
tasks.append(generate(template, args, source))
|
||||
|
||||
|
@ -62,6 +62,8 @@ def config_from_file(filename: str) -> dict:
|
||||
|
||||
def build_config(options: dict) -> dict:
|
||||
"""Look for options["configfile"] to load it with default_config and options"""
|
||||
config = clean_vars_keys(vars(default_config))
|
||||
|
||||
configfile = ""
|
||||
try:
|
||||
configfile = options["configfile"]
|
||||
@ -71,12 +73,12 @@ def build_config(options: dict) -> dict:
|
||||
configfile = os.environ["BOPYTEXCONFIG"]
|
||||
except KeyError:
|
||||
pass
|
||||
if configfile:
|
||||
local_config = config_from_file(configfile)
|
||||
config.update(local_config)
|
||||
|
||||
local_config = config_from_file(configfile)
|
||||
|
||||
config = clean_vars_keys(vars(default_config))
|
||||
config.update(local_config)
|
||||
config.update(options)
|
||||
|
||||
return config
|
||||
|
||||
|
||||
|
@ -4,12 +4,26 @@ from bopytex.message import Message
|
||||
|
||||
|
||||
def generate(args, deps, output):
|
||||
env = args["jinja2"]["environment"]
|
||||
env = args["options"]["jinja2"]["environment"]
|
||||
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:
|
||||
with open(output, "w") as out:
|
||||
fed = template.render(args)
|
||||
fed = template.render(variables)
|
||||
out.write(fed)
|
||||
|
||||
return Message(0, [f"GENERATE - {deps[0]} to {output}"], [])
|
||||
|
@ -15,10 +15,12 @@ def test_tasks_builder_generate():
|
||||
Task(
|
||||
action="GENERATE",
|
||||
args={
|
||||
"number": "01",
|
||||
"template": "tpl_source.tex",
|
||||
"subjects": [{"number": "01"}, {"number": "02"}],
|
||||
"no_pdf": True,
|
||||
"options": {
|
||||
"no_pdf": True,
|
||||
"subjects": [{"number": "01"}, {"number": "02"}],
|
||||
'template': 'tpl_source.tex',
|
||||
},
|
||||
"subject": {"number": "01"}
|
||||
},
|
||||
deps=["tpl_source.tex"],
|
||||
output="01_source.tex",
|
||||
@ -26,10 +28,12 @@ def test_tasks_builder_generate():
|
||||
Task(
|
||||
action="GENERATE",
|
||||
args={
|
||||
"number": "02",
|
||||
"template": "tpl_source.tex",
|
||||
"subjects": [{"number": "01"}, {"number": "02"}],
|
||||
"no_pdf": True,
|
||||
"options": {
|
||||
"no_pdf": True,
|
||||
"subjects": [{"number": "01"}, {"number": "02"}],
|
||||
'template': 'tpl_source.tex',
|
||||
},
|
||||
"subject": {"number": "02"}
|
||||
},
|
||||
deps=["tpl_source.tex"],
|
||||
output="02_source.tex",
|
||||
@ -49,10 +53,12 @@ def test_tasks_builder_generate_compile():
|
||||
Task(
|
||||
action="GENERATE",
|
||||
args={
|
||||
"number": "01",
|
||||
"template": "tpl_source.tex",
|
||||
"subjects": [{"number": "01"}, {"number": "02"}],
|
||||
"no_join": True,
|
||||
"options": {
|
||||
"no_join": True,
|
||||
"subjects": [{"number": "01"}, {"number": "02"}],
|
||||
'template': 'tpl_source.tex',
|
||||
},
|
||||
"subject": {"number": "01"}
|
||||
},
|
||||
deps=["tpl_source.tex"],
|
||||
output="01_source.tex",
|
||||
@ -66,10 +72,12 @@ def test_tasks_builder_generate_compile():
|
||||
Task(
|
||||
action="GENERATE",
|
||||
args={
|
||||
"number": "02",
|
||||
"template": "tpl_source.tex",
|
||||
"subjects": [{"number": "01"}, {"number": "02"}],
|
||||
"no_join": True,
|
||||
"options": {
|
||||
"no_join": True,
|
||||
"subjects": [{"number": "01"}, {"number": "02"}],
|
||||
'template': 'tpl_source.tex',
|
||||
},
|
||||
"subject": {"number": "02"}
|
||||
},
|
||||
deps=["tpl_source.tex"],
|
||||
output="02_source.tex",
|
||||
@ -94,9 +102,11 @@ def test_tasks_builder_generate_compile_join():
|
||||
Task(
|
||||
action="GENERATE",
|
||||
args={
|
||||
"number": "01",
|
||||
"template": "tpl_source.tex",
|
||||
"subjects": [{"number": "01"}, {"number": "02"}],
|
||||
"options": {
|
||||
"subjects": [{"number": "01"}, {"number": "02"}],
|
||||
'template': 'tpl_source.tex',
|
||||
},
|
||||
"subject": {"number": "01"}
|
||||
},
|
||||
deps=["tpl_source.tex"],
|
||||
output="01_source.tex",
|
||||
@ -110,9 +120,11 @@ def test_tasks_builder_generate_compile_join():
|
||||
Task(
|
||||
action="GENERATE",
|
||||
args={
|
||||
"number": "02",
|
||||
"template": "tpl_source.tex",
|
||||
"subjects": [{"number": "01"}, {"number": "02"}],
|
||||
"options": {
|
||||
"subjects": [{"number": "01"}, {"number": "02"}],
|
||||
'template': 'tpl_source.tex',
|
||||
},
|
||||
"subject": {"number": "02"}
|
||||
},
|
||||
deps=["tpl_source.tex"],
|
||||
output="02_source.tex",
|
||||
@ -145,11 +157,13 @@ def test_tasks_builder_generate_compile_corr():
|
||||
Task(
|
||||
action="GENERATE",
|
||||
args={
|
||||
"number": "01",
|
||||
"template": "tpl_source.tex",
|
||||
"subjects": [{"number": "01"}, {"number": "02"}],
|
||||
"corr": True,
|
||||
"no_join": True,
|
||||
"options": {
|
||||
"subjects": [{"number": "01"}, {"number": "02"}],
|
||||
"corr": True,
|
||||
"no_join": True,
|
||||
'template': 'tpl_source.tex',
|
||||
},
|
||||
"subject": {"number": "01"}
|
||||
},
|
||||
deps=["tpl_source.tex"],
|
||||
output="01_source.tex",
|
||||
@ -162,7 +176,13 @@ def test_tasks_builder_generate_compile_corr():
|
||||
),
|
||||
Task(
|
||||
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"],
|
||||
output="corr_01_source.tex",
|
||||
),
|
||||
@ -175,11 +195,13 @@ def test_tasks_builder_generate_compile_corr():
|
||||
Task(
|
||||
action="GENERATE",
|
||||
args={
|
||||
"number": "02",
|
||||
"template": "tpl_source.tex",
|
||||
"subjects": [{"number": "01"}, {"number": "02"}],
|
||||
"corr": True,
|
||||
"no_join": True,
|
||||
"options": {
|
||||
"subjects": [{"number": "01"}, {"number": "02"}],
|
||||
"corr": True,
|
||||
"no_join": True,
|
||||
'template': 'tpl_source.tex',
|
||||
},
|
||||
"subject": {"number": "02"}
|
||||
},
|
||||
deps=["tpl_source.tex"],
|
||||
output="02_source.tex",
|
||||
@ -192,7 +214,13 @@ def test_tasks_builder_generate_compile_corr():
|
||||
),
|
||||
Task(
|
||||
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"],
|
||||
output="corr_02_source.tex",
|
||||
),
|
||||
@ -218,11 +246,13 @@ def test_tasks_builder_generate_compile_corr_joined():
|
||||
Task(
|
||||
action="GENERATE",
|
||||
args={
|
||||
"number": "01",
|
||||
"corr": True,
|
||||
"no_join": False,
|
||||
"template": "tpl_source.tex",
|
||||
"subjects": [{"number": "01"}, {"number": "02"}],
|
||||
"options": {
|
||||
"subjects": [{"number": "01"}, {"number": "02"}],
|
||||
"corr": True,
|
||||
"no_join": False,
|
||||
'template': 'tpl_source.tex',
|
||||
},
|
||||
"subject": {"number": "01"}
|
||||
},
|
||||
deps=["tpl_source.tex"],
|
||||
output="01_source.tex",
|
||||
@ -235,7 +265,13 @@ def test_tasks_builder_generate_compile_corr_joined():
|
||||
),
|
||||
Task(
|
||||
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"],
|
||||
output="corr_01_source.tex",
|
||||
),
|
||||
@ -248,11 +284,13 @@ def test_tasks_builder_generate_compile_corr_joined():
|
||||
Task(
|
||||
action="GENERATE",
|
||||
args={
|
||||
"number": "02",
|
||||
"template": "tpl_source.tex",
|
||||
"subjects": [{"number": "01"}, {"number": "02"}],
|
||||
"corr": True,
|
||||
"no_join": False,
|
||||
"options": {
|
||||
"subjects": [{"number": "01"}, {"number": "02"}],
|
||||
"corr": True,
|
||||
"no_join": False,
|
||||
'template': 'tpl_source.tex',
|
||||
},
|
||||
"subject": {"number": "02"}
|
||||
},
|
||||
deps=["tpl_source.tex"],
|
||||
output="02_source.tex",
|
||||
@ -265,7 +303,13 @@ def test_tasks_builder_generate_compile_corr_joined():
|
||||
),
|
||||
Task(
|
||||
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"],
|
||||
output="corr_02_source.tex",
|
||||
),
|
||||
@ -299,7 +343,11 @@ def test_only_corr_tasks_builder():
|
||||
assert tasks == [
|
||||
Task(
|
||||
action="ACTIVATE_CORR",
|
||||
args={},
|
||||
args={
|
||||
'no_join': False,
|
||||
'no_pdf': False,
|
||||
'sources': ['01_source.tex', '02_source.tex']
|
||||
},
|
||||
deps=["01_source.tex"],
|
||||
output="corr_01_source.tex",
|
||||
),
|
||||
@ -311,7 +359,11 @@ def test_only_corr_tasks_builder():
|
||||
),
|
||||
Task(
|
||||
action="ACTIVATE_CORR",
|
||||
args={},
|
||||
args={
|
||||
'no_join': False,
|
||||
'no_pdf': False,
|
||||
'sources': ['01_source.tex', '02_source.tex']
|
||||
},
|
||||
deps=["02_source.tex"],
|
||||
output="corr_02_source.tex",
|
||||
),
|
||||
|
@ -49,7 +49,8 @@ def test_get_config_with_configfile(config_file, tmp_path):
|
||||
"planner",
|
||||
"pdflatex",
|
||||
"clean",
|
||||
"j2",
|
||||
"latex",
|
||||
"activate_corr",
|
||||
"dispatcher",
|
||||
"pdfjam",
|
||||
"jinja2",
|
||||
|
@ -11,7 +11,7 @@ def test_build_task_generate():
|
||||
|
||||
def test_build_task_activate_corr_on():
|
||||
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.args == {}
|
||||
assert task.deps == [src]
|
||||
|
@ -27,7 +27,13 @@ def test_generate(template_path, jinja2_env):
|
||||
output = "output"
|
||||
|
||||
message = generate(
|
||||
args={"a": 2, "jinja2": {"environment": jinja2_env}},
|
||||
args={
|
||||
"options": {
|
||||
"direct_access": {"a": 2},
|
||||
"jinja2": {"environment": jinja2_env},
|
||||
},
|
||||
"subject": {},
|
||||
},
|
||||
deps=[template],
|
||||
output=output,
|
||||
)
|
||||
@ -59,10 +65,18 @@ def test_generate_with_random(template_path_with_random, jinja2_env):
|
||||
import random
|
||||
|
||||
message = generate(
|
||||
args={"random": random, "jinja2": {"environment": jinja2_env}},
|
||||
args={
|
||||
"options": {
|
||||
"jinja2": {"environment": jinja2_env},
|
||||
"direct_access": {
|
||||
"random": random,
|
||||
}
|
||||
},
|
||||
"subject":{},
|
||||
},
|
||||
deps=[template],
|
||||
output=output,
|
||||
)
|
||||
)
|
||||
print(message.err)
|
||||
assert message.status == 0
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user