satdigitalinvoice.FacturacionLauncher.run()   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 22
rs 9.5
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 4
nop 2
crap 20
1 1
import logging
2
import os
3 1
4
import PySimpleGUI as sg
5
6
SOURCE_DIRECTORY = os.path.dirname(__file__)
7
8
LOGS_DIRECTORY = ".data/logs"
9
10
DATA_DIRECTORY = ".data"
11
ARCHIVOS_DIRECTORY = "archivos"
12
METADATA_FILE = os.path.join(ARCHIVOS_DIRECTORY, "metadata.csv")
13
TEMPLATES_DIRECTORY = "templates"
14
TEMP_DIRECTORY = ".data/temp"
15
16
17
def add_file_handler():
18
    os.makedirs(LOGS_DIRECTORY, exist_ok=True)
19
    fh = logging.FileHandler(
20
        os.path.join(LOGS_DIRECTORY, 'errors.log'),
21
        mode='a',
22
        encoding='utf-8',
23
    )
24
    fh.setLevel(logging.ERROR)
25
    formatter = logging.Formatter('%(asctime)s - %(message)s')
26
    fh.setFormatter(formatter)
27
    logging.root.addHandler(fh)
28
29
30
class FacturacionLauncher:
31
    def __init__(self):
32
        # sg.theme('Reddit')
33
        add_file_handler()
34
35
        # layout
36
        layout = [
37
            [sg.Column([[
38
                sg.Image(source=os.path.join(SOURCE_DIRECTORY, "images", "logo.png"), pad=2),
39
            ]], justification='center', pad=0, background_color='black')],
40
            [
41
                sg.Multiline(
42
                    "Cargando Aplicación...",
43
                    expand_x=True,
44
                    expand_y=True,
45
                    key="console",
46
                    background_color='#a0dbd9',
47
                    no_scrollbar=True,
48
                    border_width=0,
49
                    write_only=True,
50
                    disabled=True,
51
                )
52
            ]
53
        ]
54
55
        self.window = sg.Window(
56
            f"Facturación Masiva CFDI 4.0",
57
            layout,
58
            size=(640, 480),
59
            resizable=True,
60
            font=("Courier New", 11, "bold"),
61
            no_titlebar=True,
62
            modal=True,
63
            background_color='#a0dbd9',  # sg.theme_background_color(),
64
            auto_close=True,
65
            auto_close_duration=10,  # seconds
66
        )
67
68
    def run(self, config=None):
69
        self.window.finalize()
70
        self.window.read(timeout=0)
71
72
        try:
73
            from satdigitalinvoice.facturacion import FacturacionGUI
74
            app = FacturacionGUI()
75
            if config:
76
                app.read_config = lambda : config
77
78
        except Exception as ex:
79
            logging.exception(ex)
80
            self.window['console'].update(
81
                f"Error al cargar la aplicación. {ex.__class__.__name__}: {ex}",
82
                text_color='red4',
83
            )
84
            self.window.read(close=True)
85
            return
86
87
        self.window.read(timeout=0, close=True)
88
        self.window.close()
89
        app.run()
90