7 Commits

7 changed files with 60 additions and 21 deletions

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"

View File

@@ -1,11 +1,12 @@
import configparser
import os import os
from configparser import ConfigParser from configparser import ConfigParser, DuplicateSectionError
class Config: class Config:
parser: ConfigParser parser: ConfigParser
def __init__(self): def __init__(self, path: str = None, filename: str = "config.ini"):
""" """
Config parser reading config.ini Config parser reading config.ini
@@ -14,12 +15,18 @@ class Config:
self.__filename: Path and name to the config file self.__filename: Path and name to the config file
""" """
self.parser = ConfigParser() self.parser = ConfigParser()
self.path = path
self.filename = filename
if self.path is None:
home_path = os.environ["HOME"] home_path = os.environ["HOME"]
full_path = os.path.join(home_path, ".config", "brovski-adress-etiketten" ) full_path = os.path.join(home_path, ".config", "brovski-adress-etiketten" )
else:
full_path = self.path
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, self.filename)
self._load() self._load()
@@ -32,11 +39,15 @@ class Config:
def add_section(self, section): def add_section(self, section):
self._load() self._load()
try:
self.parser.add_section(section) self.parser.add_section(section)
except DuplicateSectionError:
pass
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()

View File

@@ -1,13 +1,12 @@
import json import json
import os import os
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from config import 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,9 +34,8 @@ 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")

View File

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

View File

@@ -3,7 +3,7 @@ from configparser import NoSectionError, NoOptionError
from tkinter import font, filedialog, messagebox from tkinter import font, filedialog, messagebox
from config import Config from config import Config
from connector import JSONConnector from model import Model
def show_error(message_title: str, message: str, parent: tk.Tk | tk.Toplevel): def show_error(message_title: str, message: str, parent: tk.Tk | tk.Toplevel):
@@ -18,6 +18,7 @@ class Window(tk.Toplevel):
def __init__(self, parent, root: tk.Tk): def __init__(self, parent, root: tk.Tk):
super().__init__(root) super().__init__(root)
self.parent = parent self.parent = parent
self.config = parent.config
self.root = root self.root = root
self.protocol("WM_DELETE_WINDOW", self.close_window) self.protocol("WM_DELETE_WINDOW", self.close_window)
self.bind("<Escape>", self.close_window) self.bind("<Escape>", self.close_window)
@@ -36,7 +37,7 @@ class EditRecord(Window):
super().__init__(parent, root) super().__init__(parent, root)
self.bind("<Return>", self._update) self.bind("<Return>", self._update)
self.model = JSONConnector() self.model = Model(self.config)
record = self.model.get_by_id(record_id) record = self.model.get_by_id(record_id)

0
tests/__init__.py Normal file
View File

30
tests/test_config.py Normal file
View File

@@ -0,0 +1,30 @@
import os
from typing import assert_type
import pytest
from src.config import Config
@pytest.fixture
def config() -> Config:
config = Config(path="testfiles", filename="configtest.ini")
return config
def teardown_config():
print("tearing down config test")
def test_construction(config):
assert_type(config, Config)
def test_file_creation(config):
config._save()
assert os.path.isfile(os.path.join(config.path, config.filename))
def test_add_section(config):
config.add_section("test_section")
assert "test_section" in config.parser.sections()
def test_set_and_get(config):
config.set(section="section", option="option", value="value")
assert config.get(section="section", option="option") == "value"