-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
100 lines (76 loc) · 2.75 KB
/
main.py
File metadata and controls
100 lines (76 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
Ponto de entrada principal
"""
import sys
import os
import logging
from datetime import datetime
from src.config import gerenciador_config
from src.automacao import AutomatorSEFAZ
from src.utils.login_helper import LoggingConfig
from src.automacao.timeout_manager import TimeoutManager
sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
def limpar_logs_antigos(max_logs=5):
try:
logs_dir = "logs"
if not os.path.exists(logs_dir):
return
log_files = [f for f in os.listdir(logs_dir) if f.endswith('.log')]
log_files.sort(key=lambda x: os.path.getmtime(os.path.join(logs_dir, x)))
if len(log_files) > max_logs:
for old_log in log_files[:-max_logs]:
os.remove(os.path.join(logs_dir, old_log))
print(f"Removido log antigo: {old_log}")
except Exception as e:
print(f"Erro ao limpar logs antigos: {e}")
def main():
limpar_logs_antigos(max_logs=3)
if not LoggingConfig.setup(log_level=logging.DEBUG, log_file="logs/nfe_automation.log", verbose=False):
return 1
logger = logging.getLogger(__name__)
print("\n" + "="*50)
print("AUTOMACAO SEFAZ NFe - INICIANDO")
print("="*50)
automator = None
try:
logger.info("Carregando configuracoes...")
config = gerenciador_config.carregar_config()
if not config:
logger.error("Falha: Configuracoes nao carregadas")
return 1
logger.info("Validando credenciais...")
erros = config.validar_formatos()
if erros:
logger.error("Erros nas credenciais:")
for erro in erros:
logger.error(f" - {erro}")
return 1
logger.info("Inicializando automator...")
automator = AutomatorSEFAZ()
if not automator.inicializar(config):
logger.error("Falha: Automator nao inicializado")
return 1
logger.info("Executando fluxo...")
sucesso = automator.executar_fluxo()
print("\n" + "="*50)
if sucesso:
print("SUCESSO: Processo concluido")
else:
print("ERRO: Processo interrompido")
print("="*50)
return 0 if sucesso else 1
except KeyboardInterrupt:
print("\nProcesso interrompido pelo usuario")
return 1
except Exception as e:
logger.error(f"Erro nao tratado: {e}")
return 1
finally:
if automator:
automator.limpar_recursos()
try:
input("\nPressione ENTER para sair...")
except:
pass
if __name__ == "__main__":
sys.exit(main())