Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
satcfdi committed Mar 14, 2024
1 parent 7fd50ce commit 38436d1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
12 changes: 4 additions & 8 deletions satdigitalinvoice/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import logging
import os
from zipfile import ZipFile

import PySimpleGUI as sg

SOURCE_DIRECTORY = os.path.dirname(__file__)

LOGS_DIRECTORY = "logs"

DATA_DIRECTORY = ".data"
ARCHIVOS_DIRECTORY = "archivos"
METADATA_FILE = os.path.join(ARCHIVOS_DIRECTORY, "metadata.csv")
Expand All @@ -14,9 +15,9 @@


def add_file_handler():
os.makedirs(DATA_DIRECTORY, exist_ok=True)
os.makedirs(LOGS_DIRECTORY, exist_ok=True)
fh = logging.FileHandler(
os.path.join(DATA_DIRECTORY, 'errors.log'),
os.path.join(LOGS_DIRECTORY, 'errors.log'),
mode='a',
encoding='utf-8',
)
Expand Down Expand Up @@ -69,11 +70,6 @@ def run(self, config=None):
self.window.read(timeout=0)

try:
# check if another directory is configured
from satdigitalinvoice.file_data_managers import InitManager
if cwd := InitManager().get('cwd'):
os.chdir(cwd)

from satdigitalinvoice.facturacion import FacturacionGUI
app = FacturacionGUI()
if config:
Expand Down
25 changes: 18 additions & 7 deletions satdigitalinvoice/facturacion.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from .gui_functions import generate_ingresos, pago_factura, exportar_facturas, archivos_folder, period_desc, parse_fecha_pago, parse_importe_pago, preview_cfdis, center_location, \
CALENDAR_FECHA_FMT, ConsoleErrors, \
generate_ajustes, generar_depositos, calculate_declaracion_provisional, calculate_diot
from .initdb import InitDB
from .layout import make_layout, ActionButtonManager, TipoRecuperar, SearchOptions
from .localdb import LocalDBSatCFDI, StatusState
from .log_tools import header_line, print_yaml, to_yaml
Expand Down Expand Up @@ -74,7 +75,8 @@ def get_directory():

class FacturacionGUI:
def __init__(self):
os.makedirs(TEMP_DIRECTORY, exist_ok=True)
self.init_db = InitDB()
self.init_db.set_cwd()

self.email_manager = None
self._all_invoices = None
Expand Down Expand Up @@ -129,13 +131,13 @@ def read_config():
from satdigitalinvoice.file_data_managers import ConfigManager
return ConfigManager()

def load_config(self):
def load_config(self, force=False):
config = self.read_config()
if isinstance(config, dict):
last_modified = True
else:
last_modified = config.last_modified()
if self.config_last_modified == last_modified:
if self.config_last_modified == last_modified and not force:
return
self.config_last_modified = last_modified

Expand Down Expand Up @@ -819,6 +821,15 @@ def nuevos_depositos(self, values, force=False):
values=depositos,
)

def initialize(self, clear=False):
self.window['projecto_dir'].update(value=os.getcwd())
self.load_config(force=True)
self.set_inputs()
if clear:
self._all_invoices = None
for t in ('facturas_table', 'clientes_table', 'emitidas_table', 'recibidas_table', 'correos_table', 'ajustes_table', 'depositos_table', 'solicitudes_table'):
self.window[t].update(values=[])

def main_tab_group(self, values):
self.action_button_manager.clear()

Expand Down Expand Up @@ -909,14 +920,13 @@ def action(self, event, values):
try:
match event:
case '_initialize':
self.load_config()
self.set_inputs()
self.initialize()

case '_focus_in':
if not self.has_focus:
self.has_focus = True
self.load_config()
if values["main_tab_group"] in ("clientes_tab", "facturas_tab", "ajustes_tab", "correos_tab"):
if values["main_tab_group"] in ("clientes_tab", "facturas_tab", "ajustes_tab", "correos_tab", "depositos_tab"):
self.main_tab_group(values)

case '_focus_out':
Expand Down Expand Up @@ -1235,9 +1245,10 @@ def action(self, event, values):
)

case "projecto_dir_selected":
self.window['projecto_dir'].update(
self.init_db.update_cwd(
values["projecto_dir_browse"]
)
self.initialize(clear=True)

case _:
logger.error(f"Unknown event '{event}'")
Expand Down
8 changes: 0 additions & 8 deletions satdigitalinvoice/file_data_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,6 @@ def last_modified(self):
return os.path.getmtime(self.file_source)


class InitManager(LocalData):
file_source = "init.yaml"

def __init__(self):
try:
super().__init__()
except FileNotFoundError:
pass


class ConfigManager(LocalData):
Expand Down
18 changes: 18 additions & 0 deletions satdigitalinvoice/initdb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os

import diskcache

WORKING_DIR = 'working_dir'


class InitDB(diskcache.Cache):
def __init__(self):
super().__init__(directory=os.path.join(os.getcwd(), 'cache'))

def set_cwd(self):
cwd = self.get('working_dir', os.getcwd())
os.chdir(cwd)

def update_cwd(self, cwd):
self['working_dir'] = cwd
self.set_cwd()

0 comments on commit 38436d1

Please sign in to comment.