handle-empty-datafile #14

Merged
sroth merged 2 commits from handle-empty-datafile into main 2025-05-10 02:27:34 +02:00
6 changed files with 40 additions and 24 deletions

View File

@@ -1,5 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
if [ $VIRTUAL_ENV=="" ] if [ "$VIRTUAL_ENV" == "" ]
then then
source venv/bin/activate source venv/bin/activate
fi fi

View File

@@ -7,7 +7,6 @@ from tkinter import messagebox
from tkinter import ttk from tkinter import ttk
from config import Config from config import Config
from connector import JSONConnector
from model import Model from model import Model
from windows import SettingsWindow, EditRecord, show_error from windows import SettingsWindow, EditRecord, show_error
@@ -37,7 +36,7 @@ class Application:
self.filter_active = tk.BooleanVar(value=False) self.filter_active = tk.BooleanVar(value=False)
# model connector # model connector
self.model = Model(JSONConnector()) self.model = Model(self.config)
# init paths to json and csv file # init paths to json and csv file
self.json_file_name = "brovski-adress-etiketten-verwaltung.json" self.json_file_name = "brovski-adress-etiketten-verwaltung.json"
@@ -265,6 +264,7 @@ class Application:
if reload: if reload:
self.address_list = self.model.get_all() self.address_list = self.model.get_all()
if len(self.address_list) == 0: if len(self.address_list) == 0:
self.delete_all_table_items()
return return
self.delete_all_table_items() self.delete_all_table_items()
for address in self.address_list: for address in self.address_list:
@@ -290,14 +290,14 @@ class Application:
self.table.delete(item) self.table.delete(item)
def open_window_settings(self): def open_window_settings(self):
window = SettingsWindow(self, self.root) window = SettingsWindow(self, self.root, self.config)
window.wm_transient(self.root) window.wm_transient(self.root)
window.wait_visibility() window.wait_visibility()
window.grab_set() window.grab_set()
return window return window
def open_window_edit_records(self, record_id: int): def open_window_edit_records(self, record_id: int):
window = EditRecord(self, self.root, record_id) window = EditRecord(self, self.root, record_id, self.config)
window.wm_transient(self.root) window.wm_transient(self.root)
window.wait_visibility() window.wait_visibility()
window.grab_set() window.grab_set()

View File

@@ -1,11 +1,11 @@
import os import os
from configparser import ConfigParser from configparser import ConfigParser, NoOptionError, NoSectionError, DuplicateSectionError
class Config: class Config:
parser: ConfigParser parser: ConfigParser
def __init__(self): def __init__(self, filename: str = "config.ini"):
""" """
Config parser reading config.ini Config parser reading config.ini
@@ -19,7 +19,7 @@ class Config:
full_path = os.path.join(home_path, ".config", "brovski-adress-etiketten" ) full_path = os.path.join(home_path, ".config", "brovski-adress-etiketten" )
if not os.path.exists(full_path): if not os.path.exists(full_path):
os.makedirs(full_path) os.makedirs(full_path)
self.config_file = os.path.join(full_path, "config.ini") self.config_file = os.path.join(full_path, filename)
self._load() self._load()
@@ -32,14 +32,25 @@ class Config:
def add_section(self, section): def add_section(self, section):
self._load() self._load()
self.parser.add_section(section) try:
self.parser.add_section(section)
except DuplicateSectionError:
return
self._save() self._save()
def set(self, section: str, option: str, value: str): def set(self, section: str, option: str, value: str):
self._load() self._load()
self.add_section(section)
self.parser.set(section, option, value) self.parser.set(section, option, value)
self._save() self._save()
def get(self, section: str, option: str): def get(self, section: str, option: str):
self._load() self._load()
return self.parser.get(section, option) try:
option = self.parser.get(section, option)
except NoOptionError:
option = ""
except NoSectionError:
option = ""
return option

View File

@@ -3,11 +3,12 @@ import os
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
import config import config
from config import Config
class Connector(ABC): class Connector(ABC):
def __init__(self): def __init__(self, config: Config):
pass self.config = config
@abstractmethod @abstractmethod
def get_all(self) -> list: def get_all(self) -> list:
@@ -35,15 +36,17 @@ class Connector(ABC):
class JSONConnector(Connector): class JSONConnector(Connector):
def __init__(self): def __init__(self, config: Config):
super().__init__() super().__init__(config)
self.config = config.Config()
self.json_path = self.config.get("json", "path") self.json_path = self.config.get("json", "path")
self.json_file = os.path.join(self.json_path, "brovski-adress-etiketten-verwaltung-v7.json") self.json_file = os.path.join(self.json_path, "brovski-adress-etiketten-verwaltung-v7.json")
def get_all(self) -> list: def get_all(self) -> list:
with open(self.json_file, "r") as f: try:
return json.load(f) with open(self.json_file, "r") as f:
return json.load(f)
except FileNotFoundError:
return []
def get_all_sorted_by(self, field: str, reverse=False) -> list: def get_all_sorted_by(self, field: str, reverse=False) -> list:
with open(self.json_file, "r") as f: with open(self.json_file, "r") as f:

View File

@@ -1,9 +1,11 @@
from connector import JSONConnector, Connector from config import Config
from connector import JSONConnector
class Model: class Model:
def __init__(self, connector: Connector): def __init__(self, config: Config):
self.connector = connector self.connector = JSONConnector(config)
self.config = config
def get_all(self): def get_all(self):
return self.connector.get_all() return self.connector.get_all()

View File

@@ -32,11 +32,11 @@ class Window(tk.Toplevel):
class EditRecord(Window): class EditRecord(Window):
def __init__(self, parent, root: tk.Tk, record_id: int): def __init__(self, parent, root: tk.Tk, record_id: int, config: Config):
super().__init__(parent, root) super().__init__(parent, root)
self.bind("<Return>", self._update) self.bind("<Return>", self._update)
self.model = JSONConnector() self.model = JSONConnector(config)
record = self.model.get_by_id(record_id) record = self.model.get_by_id(record_id)
@@ -86,10 +86,10 @@ class EditRecord(Window):
class SettingsWindow(Window): class SettingsWindow(Window):
def __init__(self, parent, root: tk.Tk): def __init__(self, parent, root: tk.Tk, config: Config):
super().__init__(parent, root) super().__init__(parent, root)
self.geometry(f"500x330+{self.root.winfo_x() + 20}+{self.root.winfo_y() + 20}") self.geometry(f"500x330+{self.root.winfo_x() + 20}+{self.root.winfo_y() + 20}")
self.config = Config() self.config = config
self.json_file = tk.StringVar() self.json_file = tk.StringVar()
self.csv_file = tk.StringVar() self.csv_file = tk.StringVar()