Feat: rename get_config

This commit is contained in:
Bertrand Benjamin 2022-07-19 15:15:06 +02:00
parent 01a0f9db17
commit 8c20ff8e4a
2 changed files with 24 additions and 14 deletions

View File

@ -6,6 +6,7 @@ Producing then compiling templates
""" """
import importlib.util import importlib.util
import os
from pathlib import Path from pathlib import Path
from bopytex.scheduler import Scheduler from bopytex.scheduler import Scheduler
from bopytex import default_config from bopytex import default_config
@ -51,18 +52,27 @@ def clean_vars_keys(
return new_dict return new_dict
def get_config(options: dict) -> dict: def config_from_file(filename: str) -> dict:
""" Look for options["configfile"] to load it with default_config and options """ if Path(filename).exists():
local_config = vars(load_module(filename))
return clean_vars_keys(local_config)
else:
return {}
def build_config(options: dict) -> dict:
"""Look for options["configfile"] to load it with default_config and options"""
configfile = ""
try: try:
options["configfile"] configfile = options["configfile"]
except KeyError: except KeyError:
local_config = {} pass
else: try:
if Path(options["configfile"]).exists(): configfile = os.environ["BOPYTEXCONFIG"]
local_config = vars(load_module(options["configfile"])) except KeyError:
local_config = clean_vars_keys(local_config) pass
else:
local_config = {} local_config = config_from_file(configfile)
config = clean_vars_keys(vars(default_config)) config = clean_vars_keys(vars(default_config))
config.update(local_config) config.update(local_config)
@ -72,7 +82,7 @@ def get_config(options: dict) -> dict:
def main(**options): def main(**options):
config = get_config(options) config = build_config(options)
orcherstre = orcherstrator( orcherstre = orcherstrator(
config, planner=default_config.planner, dispatcher=default_config.dispatcher config, planner=default_config.planner, dispatcher=default_config.dispatcher

View File

@ -2,7 +2,7 @@ import os
import pytest import pytest
from bopytex.planner import fake_planner from bopytex.planner import fake_planner
from bopytex.service import get_config, orcherstrator from bopytex.service import build_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
@ -40,7 +40,7 @@ jinja2 = {
def test_get_config_with_configfile(config_file, tmp_path): def test_get_config_with_configfile(config_file, tmp_path):
os.chdir(tmp_path) os.chdir(tmp_path)
config = get_config({"from_option": "config", "configfile": str(config_file)}) config = build_config({"from_option": "config", "configfile": str(config_file)})
assert type(config) == dict assert type(config) == dict
assert set(config.keys()) == { assert set(config.keys()) == {
"generate", "generate",