83 lines
2.5 KiB
Python
83 lines
2.5 KiB
Python
from dataclasses import dataclass
|
|
from typing import Optional
|
|
import json
|
|
import os
|
|
|
|
@dataclass
|
|
class HanaConfig:
|
|
server: str
|
|
port: int
|
|
database: str
|
|
user: str
|
|
password: str
|
|
|
|
@dataclass
|
|
class OutlookConfig:
|
|
client_id: str
|
|
client_secret: str
|
|
tenant_id: str
|
|
from_email: str
|
|
to_email: list[str]
|
|
attachments_folder: str = "./attachments"
|
|
verify_ssl: bool = True
|
|
ca_bundle: Optional[str] = None
|
|
|
|
@dataclass
|
|
class CrystalConfig:
|
|
path: str
|
|
version: str
|
|
|
|
@dataclass
|
|
class PowerbiConfig:
|
|
url: str
|
|
|
|
@dataclass
|
|
class Config:
|
|
hana: HanaConfig
|
|
outlook: OutlookConfig
|
|
crystal: CrystalConfig
|
|
powerbi: PowerbiConfig
|
|
|
|
@staticmethod
|
|
def load(path: str = "config.json") -> 'Config':
|
|
if not os.path.exists(path):
|
|
raise FileNotFoundError(f"No se encontró el archivo de configuración: {path}")
|
|
|
|
with open(path, 'r', encoding='utf-8') as f:
|
|
data = json.load(f)
|
|
|
|
hana_data = data.get('hana', {})
|
|
outlook_data = data.get('outlook', {})
|
|
crystal_data = data.get('crystal', {})
|
|
|
|
# Validar campos requeridos para HANA
|
|
required_hana = ['server', 'port', 'database', 'user', 'password']
|
|
for field in required_hana:
|
|
if field not in hana_data:
|
|
raise KeyError(f"Falta el campo requerido '{field}' en la sección 'hana' de {path}")
|
|
|
|
# Validar campos requeridos para Outlook
|
|
required_outlook = ['client_id', 'client_secret', 'tenant_id', 'from_email', 'to_email']
|
|
for field in required_outlook:
|
|
if field not in outlook_data:
|
|
raise KeyError(f"Falta el campo requerido '{field}' en la sección 'outlook' de {path}")
|
|
|
|
# Validar campos requeridos para Crystal
|
|
required_crystal = ['path', 'version']
|
|
for field in required_crystal:
|
|
if field not in crystal_data:
|
|
raise KeyError(f"Falta el campo requerido '{field}' en la sección 'crystal' de {path}")
|
|
|
|
powerbi_data = data.get('powerbi', {})
|
|
required_powerbi = ['url']
|
|
for field in required_powerbi:
|
|
if field not in powerbi_data:
|
|
raise KeyError(f"Falta el campo requerido '{field}' en la sección 'powerbi' de {path}")
|
|
|
|
hana = HanaConfig(**hana_data)
|
|
outlook = OutlookConfig(**outlook_data)
|
|
crystal = CrystalConfig(**crystal_data)
|
|
powerbi=PowerbiConfig(**powerbi_data)
|
|
|
|
return Config(hana=hana, outlook=outlook, crystal=crystal,powerbi=powerbi)
|