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:
@@ -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}"], [])
|
||||
|
Reference in New Issue
Block a user