Passed
Push — main ( ee2af1...03b1dc )
by Sat CFDI
01:44
created

satdigitalinvoice.FacturacionLauncher.run()   A

Complexity

Conditions 2

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 20
ccs 0
cts 0
cp 0
rs 9.55
c 0
b 0
f 0
cc 2
nop 1
crap 6
1 1
import logging
2
import os
3 1
import sys
4
from zipfile import ZipFile
5
import PySimpleGUI as sg
6
7
8
9
SOURCE_DIRECTORY = os.path.dirname(__file__)
10
11
DATA_DIRECTORY = ".data"
12
ARCHIVOS_DIRECTORY = "archivos"
13
TEMPLATES_DIRECTORY = "templates"
14
TEMP_DIRECTORY = ".data/temp"
15
16
PPD = "PPD"
17
PUE = "PUE"
18
19
20
def add_file_handler():
21
    os.makedirs(DATA_DIRECTORY, exist_ok=True)
22
    fh = logging.FileHandler(
23
        os.path.join(DATA_DIRECTORY, 'errors.log'),
24
        mode='a',
25
        encoding='utf-8',
26
    )
27
    fh.setLevel(logging.ERROR)
28
    formatter = logging.Formatter('%(asctime)s - %(message)s')
29
    fh.setFormatter(formatter)
30
    logging.root.addHandler(fh)
31
32
33
class FacturacionLauncher:
34
    def __init__(self, cwd=None):
35
        self.app_dir = os.getcwd()
36
        if cwd:
37
            os.chdir(cwd)
38
        add_file_handler()
39
40
        # layout
41
        layout = [
42
            [sg.Column([[
43
                sg.Image(source=os.path.join(SOURCE_DIRECTORY, "images", "logo.png"), pad=2),
44
            ]], justification='center', pad=0, background_color='black')],
45
            [
46
                sg.Multiline(
47
                    "Cargando Aplicación...",
48
                    expand_x=True,
49
                    expand_y=True,
50
                    key="console",
51
                    background_color='#a0dbd9',
52
                    no_scrollbar=True,
53
                    border_width=0,
54
                    write_only=True,
55
                    disabled=True,
56
                )
57
            ]
58
        ]
59
60
        self.window = sg.Window(
61
            f"Facturación Masiva CFDI 4.0",
62
            layout,
63
            size=(640, 480),
64
            resizable=True,
65
            font=("Courier New", 11, "bold"),
66
            no_titlebar=True,
67
            modal=True,
68
            background_color='#a0dbd9',  # sg.theme_background_color(),
69
            auto_close=True,
70
            auto_close_duration=10,  # seconds
71
        )
72
73
    @staticmethod
74
    def read_config():
75
        from satdigitalinvoice.file_data_managers import ConfigManager
76
        return ConfigManager()
77
78
    def load_sample_files(self):
79
        # loading the sample.zip
80
        try:
81
            with ZipFile(os.path.join(self.app_dir, 'sample.zip'), 'r') as zf:
82
                for member in zf.infolist():
83
                    if not os.path.exists(member.filename):
84
                        zf.extract(member)
85
        except FileNotFoundError:
86
            pass
87
88
    def run(self):
89
        self.window.finalize()
90
        self.window.read(timeout=0)
91
92
        try:
93
            # check if another directory is configured
94
            from satdigitalinvoice.file_data_managers import InitManager
95
            if cwd := InitManager().get('cwd'):
96
                os.chdir(cwd)
97
            self.load_sample_files()
98
99
            from satdigitalinvoice.facturacion import FacturacionGUI
100
            app = FacturacionGUI(
101
                config=self.read_config()
102
            )
103
        except Exception as ex:
104
            logging.exception(ex)
105
            self.window['console'].update(
106
                f"Error al cargar la aplicación. {ex.__class__.__name__}: {ex}",
107
                text_color='red4',
108
            )
109
            self.window.read()
110
            self.window.close()
111
            return
112
113
        self.window.close()
114
        app.run()
115