Source code for config.caris_config

"""
Module de configuration pour l'API Python de Caris.

Ce module contient la classe de configuration pour l'API Python de Caris.
"""

from dataclasses import dataclass
from pathlib import Path

import i18n
from loguru import logger
from pydantic import BaseModel, Field

from .helper import load_config

LOGGER = logger.bind(name="CSB-Processing.Caris.Config")

ConfigDict = dict[str, str | float]


[docs] @dataclass(frozen=True) class CarisConfigError(Exception): config_file: Path def __str__(self) -> str: return i18n.t( "config.caris_config.error_no_config", config_file=self.config_file )
@dataclass(frozen=True) class CarisApiConfigError(Exception): python_path: Path def __str__(self) -> str: return i18n.t( "config.caris_config.error_python_path", python_path=self.python_path ) @dataclass(frozen=True) class CarisBatchConfigError(Exception): caris_batch: Path def __str__(self) -> str: return i18n.t( "config.caris_config.error_caris_batch", caris_batch=self.caris_batch )
[docs] class CarisAPIConfig(BaseModel): """ Classe de configuration pour Caris. :param base_path: Le chemin de base de Caris. :type base_path: str :param software: Le logiciel de Caris. :type software: str :param version: La version du logiciel de Caris. :type version: str :param python_version: La version de Python. :type python_version: str :param python_path: Le chemin de l'API Python de Caris. :type python_path: Path :raises ValueError: Si l'API Python de Caris n'existe pas à l'emplacement spécifié. """ base_path: str software: str version: str python_version: str python_path: Path = Field(default=None) caris_batch: Path = Field(default=None) args: list[str] = Field(default=None) def __init__(self, **values) -> None: super().__init__(**values) self.python_path = ( Path(self.base_path) / self.software / self.version / "python" / self.python_version ) if not self.python_path.exists(): raise CarisApiConfigError(python_path=self.python_path) self.caris_batch = ( Path(self.base_path) / self.software / self.version / "bin" / "carisbatch.exe" ) if not self.caris_batch.exists(): raise CarisBatchConfigError(caris_batch=self.caris_batch)
[docs] def get_caris_api_config( config_file: Path, ) -> CarisAPIConfig: """ Retournes la configuration pour Caris. :param config_file: Le chemin du fichier de configuration. :type config_file: Path :return: La configuration pour Caris. :rtype: CarisAPIConfig """ config_dict = load_config(config_file=config_file) config_caris_api: ConfigDict = ( config_dict.get("CARIS").get("Environment") if "CARIS" in config_dict else None ) if not config_caris_api: raise CarisConfigError(config_file=config_file) LOGGER.debug(i18n.t("config.caris_config.init_config")) return CarisAPIConfig(**config_caris_api)