Feat: Enable configfile loading

This commit is contained in:
Bertrand Benjamin 2022-05-05 14:35:01 +02:00
parent e4f234d241
commit 90ae3e936e
5 changed files with 109 additions and 21 deletions

View File

@ -2,14 +2,3 @@ import bopytex.default_config as DEFAULT
from bopytex.service import orcherstrator from bopytex.service import orcherstrator
def bopytex(**options):
config = options.copy()
config["jinja2"] = {}
config["jinja2"]["environment"] = DEFAULT.jinja2_env
orcherstre = orcherstrator(
config, planner=DEFAULT.planner, dispatcher=DEFAULT.dispatcher
)
for message in orcherstre:
yield message

View File

@ -1,4 +1,4 @@
import jinja2 import jinja2 as j2
from bopytex.planner.generate_compile_join_planner import planner from bopytex.planner.generate_compile_join_planner import planner
from bopytex.worker import Dispatcher from bopytex.worker import Dispatcher
from bopytex.worker.clean import clean from bopytex.worker.clean import clean
@ -6,7 +6,9 @@ from bopytex.worker.compile import latexmk
from bopytex.worker.generate import generate from bopytex.worker.generate import generate
from bopytex.worker.join_pdf import pdfjam from bopytex.worker.join_pdf import pdfjam
jinja2_env = jinja2.Environment(loader=jinja2.FileSystemLoader("./")) jinja2 = {
"environment": j2.Environment(loader=j2.FileSystemLoader("./")),
}
dispatcher = Dispatcher( dispatcher = Dispatcher(
{"GENERATE": generate, "COMPILE": latexmk, "JOIN": pdfjam, "CLEAN": clean} {"GENERATE": generate, "COMPILE": latexmk, "JOIN": pdfjam, "CLEAN": clean}

View File

@ -71,14 +71,21 @@ logger.addHandler(steam_handler)
help="Activate correction and compile only from existing subjects", help="Activate correction and compile only from existing subjects",
) )
@click.option( @click.option(
"-c", "-C",
"--corr", "--corr",
is_flag=True, is_flag=True,
default=False, default=False,
help="Create and compile correction while making subjects", help="Create and compile correction while making subjects",
) )
@click.option(
"-c",
"--configfile",
type=str,
default="bopyptex_config.py",
help="Config file path",
)
def new(**options): def new(**options):
for message in main(**options): for message in main(options):
try: try:
assert message.status == 0 assert message.status == 0
except AssertionError: except AssertionError:

View File

@ -5,8 +5,10 @@
Producing then compiling templates Producing then compiling templates
""" """
import importlib.util
from pathlib import Path
from bopytex.scheduler import Scheduler from bopytex.scheduler import Scheduler
import bopytex.default_config as DEFAULT from bopytex import default_config
def orcherstrator( def orcherstrator(
@ -22,18 +24,63 @@ def orcherstrator(
for message in scheduler.backlog(): for message in scheduler.backlog():
yield message yield message
def load_module(modulefile: str):
spec = importlib.util.spec_from_file_location("module.name", modulefile)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def clean_vars_keys(
vars: dict,
keys: list[str] = [
"__name__",
"__doc__",
"__package__",
"__loader__",
"__spec__",
"__file__",
"__cached__",
"__builtins__",
],
) -> dict:
new_dict = vars.copy()
for k in keys:
del new_dict[k]
return new_dict
def get_config(options: dict) -> dict:
""" Look for options["configfile"] to load it with default_config and options """
try:
options["configfile"]
except KeyError:
local_config = {}
else:
if Path(options["configfile"]).exists():
local_config = vars(load_module(options["configfile"]))
local_config = clean_vars_keys(local_config)
else:
local_config = {}
config = clean_vars_keys(vars(default_config))
config.update(local_config)
config.update(options)
return config
def main(**options): def main(**options):
config = options.copy() config = get_config(options)
config["jinja2"] = {}
config["jinja2"]["environment"] = DEFAULT.jinja2_env
orcherstre = orcherstrator( orcherstre = orcherstrator(
config, planner=DEFAULT.planner, dispatcher=DEFAULT.dispatcher config, planner=default_config.planner, dispatcher=default_config.dispatcher
) )
for message in orcherstre: for message in orcherstre:
yield message yield message
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4: # vim:set autoindent expandtab tabstop=4 shiftwidth=4:

View File

@ -1,5 +1,8 @@
import os
import pytest
from bopytex.planner import fake_planner from bopytex.planner import fake_planner
from bopytex.service import orcherstrator from bopytex.service import get_config, orcherstrator
from bopytex.tasks import Task from bopytex.tasks import Task
from bopytex.worker import Dispatcher from bopytex.worker import Dispatcher
from .fakes.workers import fake_worker from .fakes.workers import fake_worker
@ -14,3 +17,43 @@ def test_service():
for i, message in enumerate(service): for i, message in enumerate(service):
assert message.status == 0 assert message.status == 0
assert message.out == [f"FAKE - {{'number': {i}}} - [] - {i}"] assert message.out == [f"FAKE - {{'number': {i}}} - [] - {i}"]
def test_get_config_no_configfile():
pass
@pytest.fixture
def config_file(tmp_path):
config_file = tmp_path / "bopytex_config.py"
with open(config_file, "w") as f:
f.write(
"""
from bopytex.jinja2_env.texenv import texenv
jinja2 = {
"environment": texenv,
}
"""
)
return config_file
def test_get_config_with_configfile(config_file, tmp_path):
os.chdir(tmp_path)
config = get_config({"from_option": "config", "configfile": str(config_file)})
assert type(config) == dict
assert set(config.keys()) == {
"generate",
"configfile",
"Dispatcher",
"planner",
"latexmk",
"clean",
"j2",
"dispatcher",
"pdfjam",
"jinja2",
"texenv",
"from_option",
}
assert list(config["jinja2"].keys()) == ["environment"]