Passed
Push — main ( d13aee...fb41de )
by Sat CFDI
01:47
created

FacturacionLauncher.read_config()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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