Ferramentas/AjustaFundos/actions/firebird/parse_firebird_path.py

80 lines
2.6 KiB
Python

import re
from typing import Any, Dict, Union
class ParseFirebirdPath:
DEFAULT_HOST = "localhost"
DEFAULT_PORT = 3050
@classmethod
def execute(cls, value: Union[str, Dict[str, Any]]) -> Dict[str, Any]:
# -------------------------------------------
# Se for dicionário, já padroniza
# -------------------------------------------
if isinstance(value, dict):
return {
"host": value.get("host", cls.DEFAULT_HOST),
"port": int(value.get("port", cls.DEFAULT_PORT)),
"path": value.get("path"),
}
if not isinstance(value, str):
raise TypeError("BaseDados deve ser string ou dict.")
raw = value.strip().strip('"').strip("'")
if not raw:
raise ValueError("BaseDados está vazio.")
# -------------------------------------------
# 1) host/port:path
# -------------------------------------------
m = re.match(r"^(?P<host>[^:/]+)\/(?P<port>\d+)\:(?P<path>.+)$", raw)
if m:
return {
"host": m.group("host"),
"port": int(m.group("port")),
"path": m.group("path").strip(),
}
# -------------------------------------------
# 2) host:path (ex: 127.0.0.1:D:\Banco.fdb)
# -------------------------------------------
m = re.match(r"^(?P<host>[^:\/]+)\:(?P<path>[A-Za-z]:\\.+)$", raw)
if m:
return {
"host": m.group("host"),
"port": cls.DEFAULT_PORT,
"path": m.group("path"),
}
# -------------------------------------------
# 3) Caminho local absoluto (D:\, E:\)
# -------------------------------------------
if re.match(r"^[A-Za-z]:\\", raw):
return {
"host": cls.DEFAULT_HOST,
"port": cls.DEFAULT_PORT,
"path": raw,
}
# -------------------------------------------
# 4) UNC path (\\servidor\pasta\arquivo)
# -------------------------------------------
if raw.startswith("\\\\"):
return {
"host": cls.DEFAULT_HOST,
"port": cls.DEFAULT_PORT,
"path": raw,
}
# -------------------------------------------
# 5) Alias puro — retorna padrão
# -------------------------------------------
return {
"host": cls.DEFAULT_HOST,
"port": cls.DEFAULT_PORT,
"path": raw,
}