Ferramentas/AjustaFundos/actions/file/input_base_resolver.py

88 lines
3.1 KiB
Python

import platform
from typing import Dict, Tuple
from actions.env.mirror_sync_env import MirrorSyncEnv
from actions.ui.ui import warn, fail
from packages.v1.parametros.repositories.g_config.g_config_show_by_nome_repository import (
GConfigShowByNomeRepository,
)
from packages.v1.parametros.schemas.g_config_schema import GConfigNomeSchema
class InputBaseResolver:
"""
Resolve dinamicamente caminhos base do GED sem precisar passar nada externo.
Apenas:
get_input_base(nome_variavel, sistema_id)
Tudo o resto — carregar MirrorSyncEnv, decrypt, buscar LOCAL_IMAGEM —
é feito internamente.
"""
def __init__(self):
# Carrega ENV automaticamente
self.env = MirrorSyncEnv().as_object()
# GConfig carregado internamente
self.g_config_repo = GConfigShowByNomeRepository()
# Cache por chave (env_var_name, sistema_id)
self._cache: Dict[Tuple[str, int], str] = {}
# -------------------------------------------------------
def get_input_base(self, env_var_name: str, sistema_id: int) -> str:
"""
Obtém dinamicamente o caminho do GED:
- Se Windows + ged_local=True → retorna variável do .env
- Se Windows + ged_local=False → busca LOCAL_IMAGEM no GConfig
- Se Linux → sempre retorna variável do .env
Parâmetros:
env_var_name: nome da variável de ambiente (ex.: "ged_tabelionato")
sistema_id: ID do sistema (ex.: 2)
"""
cache_key = (env_var_name, sistema_id)
if cache_key in self._cache:
return self._cache[cache_key]
resolved = self._resolve_path(env_var_name, sistema_id)
self._cache[cache_key] = resolved or ""
return resolved
# -------------------------------------------------------
def _resolve_path(self, env_var_name: str, sistema_id: int) -> str:
system = platform.system()
ged_local = str(getattr(self.env, "ged_local", "false")).lower() == "true"
dynamic_path = getattr(self.env, env_var_name, "")
# ---------------- WINDOWS ----------------
if system == "Windows":
if ged_local:
# Usa o caminho vindo do .env (dinâmico)
return dynamic_path
# Busca LOCAL_IMAGEM no GConfig
return self._resolve_from_gconfig(sistema_id)
# ---------------- LINUX ------------------
if system == "Linux":
return dynamic_path
# ---------------- OUTROS -----------------
warn(f"Sistema operacional não suportado: {system}")
return ""
# -------------------------------------------------------
def _resolve_from_gconfig(self, sistema_id: int) -> str:
"""Busca LOCAL_IMAGEM no GConfig automaticamente."""
try:
result = self.g_config_repo.execute(
GConfigNomeSchema(nome="LOCAL_IMAGEM", sistema_id=sistema_id)
)
return result.valor
except Exception as e:
fail(f"Erro no GConfig (sistema_id={sistema_id}): {e}")
return ""