You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.7 KiB
57 lines
1.7 KiB
import logging
|
|
from pathlib import Path
|
|
from pprint import pformat
|
|
|
|
from dependency_injector import containers
|
|
from dependency_injector import providers
|
|
from serial import Serial
|
|
|
|
from .util import log_function_call
|
|
|
|
DEFAULTS_DIR = Path(__file__).parent / "defaults"
|
|
CONFIG_FILE = DEFAULTS_DIR / "config.yml"
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
|
class Container(containers.DeclarativeContainer):
|
|
config = providers.Configuration("config")
|
|
|
|
serial = providers.Factory(
|
|
Serial,
|
|
port=config.device_id[config.role].required(),
|
|
baudrate=config.baudrate.required(),
|
|
)
|
|
|
|
|
|
@log_function_call
|
|
def get_initialize_container() -> Container:
|
|
logger = _logger.getChild("initialize_container")
|
|
|
|
logger.debug("initialize container...")
|
|
container = Container()
|
|
|
|
logger.debug(f"initialize container with defaults from config file: {CONFIG_FILE}")
|
|
container.config.from_yaml(CONFIG_FILE, required=True)
|
|
|
|
current_dir = Path.cwd()
|
|
is_searching = True
|
|
while is_searching:
|
|
config_file = current_dir / "monsun_config.yml"
|
|
if config_file.is_file():
|
|
logger.debug(f"initialize container from user config file: {config_file}")
|
|
container.config.from_yaml(config_file, required=True)
|
|
break
|
|
else:
|
|
current_dir = current_dir.parent
|
|
|
|
if str(current_dir) == current_dir.anchor:
|
|
is_searching = False
|
|
|
|
config_file = Path("/var/config/monsun_config.yml")
|
|
if config_file.is_file():
|
|
logger.debug(f"initialize container from config file: {config_file}")
|
|
container.config.from_yaml(config_file, required=True)
|
|
|
|
logger.debug(f"container config:\n{pformat(container.config())}")
|
|
return container
|
|
|