Compare commits
7 Commits
7e9d5bec9b
...
implement-
| Author | SHA1 | Date | |
|---|---|---|---|
| 6a22ecb591 | |||
| 233f81c33e | |||
| e939c97474 | |||
| 7028c7ce69 | |||
| 2a25111c0a | |||
| 8125d88695 | |||
| b8581b34cc |
@@ -1,5 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
if [ "$VIRTUAL_ENV" == "" ]
|
||||
if [ $VIRTUAL_ENV=="" ]
|
||||
then
|
||||
source venv/bin/activate
|
||||
fi
|
||||
|
||||
@@ -264,7 +264,6 @@ class Application:
|
||||
if reload:
|
||||
self.address_list = self.model.get_all()
|
||||
if len(self.address_list) == 0:
|
||||
self.delete_all_table_items()
|
||||
return
|
||||
self.delete_all_table_items()
|
||||
for address in self.address_list:
|
||||
@@ -290,14 +289,14 @@ class Application:
|
||||
self.table.delete(item)
|
||||
|
||||
def open_window_settings(self):
|
||||
window = SettingsWindow(self, self.root, self.config)
|
||||
window = SettingsWindow(self, self.root)
|
||||
window.wm_transient(self.root)
|
||||
window.wait_visibility()
|
||||
window.grab_set()
|
||||
return window
|
||||
|
||||
def open_window_edit_records(self, record_id: int):
|
||||
window = EditRecord(self, self.root, record_id, self.config)
|
||||
window = EditRecord(self, self.root, record_id)
|
||||
window.wm_transient(self.root)
|
||||
window.wait_visibility()
|
||||
window.grab_set()
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import configparser
|
||||
import os
|
||||
from configparser import ConfigParser, NoOptionError, NoSectionError, DuplicateSectionError
|
||||
from configparser import ConfigParser, DuplicateSectionError
|
||||
|
||||
|
||||
class Config:
|
||||
parser: ConfigParser
|
||||
|
||||
def __init__(self, filename: str = "config.ini"):
|
||||
def __init__(self, path: str = None, filename: str = "config.ini"):
|
||||
"""
|
||||
Config parser reading config.ini
|
||||
|
||||
@@ -14,12 +15,18 @@ class Config:
|
||||
self.__filename: Path and name to the config file
|
||||
"""
|
||||
self.parser = ConfigParser()
|
||||
self.path = path
|
||||
self.filename = filename
|
||||
|
||||
if self.path is None:
|
||||
home_path = os.environ["HOME"]
|
||||
full_path = os.path.join(home_path, ".config", "brovski-adress-etiketten" )
|
||||
else:
|
||||
full_path = self.path
|
||||
|
||||
if not os.path.exists(full_path):
|
||||
os.makedirs(full_path)
|
||||
self.config_file = os.path.join(full_path, filename)
|
||||
self.config_file = os.path.join(full_path, self.filename)
|
||||
|
||||
self._load()
|
||||
|
||||
@@ -35,8 +42,7 @@ class Config:
|
||||
try:
|
||||
self.parser.add_section(section)
|
||||
except DuplicateSectionError:
|
||||
return
|
||||
|
||||
pass
|
||||
self._save()
|
||||
|
||||
def set(self, section: str, option: str, value: str):
|
||||
@@ -47,10 +53,4 @@ class Config:
|
||||
|
||||
def get(self, section: str, option: str):
|
||||
self._load()
|
||||
try:
|
||||
option = self.parser.get(section, option)
|
||||
except NoOptionError:
|
||||
option = ""
|
||||
except NoSectionError:
|
||||
option = ""
|
||||
return option
|
||||
return self.parser.get(section, option)
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import json
|
||||
import os
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
import config
|
||||
from config import Config
|
||||
|
||||
|
||||
@@ -36,17 +34,14 @@ class Connector(ABC):
|
||||
|
||||
|
||||
class JSONConnector(Connector):
|
||||
def __init__(self, config: Config):
|
||||
def __init__(self, config: Config, ):
|
||||
super().__init__(config)
|
||||
self.json_path = self.config.get("json", "path")
|
||||
self.json_file = os.path.join(self.json_path, "brovski-adress-etiketten-verwaltung-v7.json")
|
||||
|
||||
def get_all(self) -> list:
|
||||
try:
|
||||
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:
|
||||
with open(self.json_file, "r") as f:
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
from connector import Connector, JSONConnector
|
||||
from config import Config
|
||||
from connector import JSONConnector
|
||||
|
||||
|
||||
class Model:
|
||||
def __init__(self, config: Config):
|
||||
self.connector = JSONConnector(config)
|
||||
self.config = config
|
||||
|
||||
def get_all(self):
|
||||
return self.connector.get_all()
|
||||
|
||||
@@ -3,7 +3,7 @@ from configparser import NoSectionError, NoOptionError
|
||||
from tkinter import font, filedialog, messagebox
|
||||
|
||||
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):
|
||||
@@ -18,6 +18,7 @@ class Window(tk.Toplevel):
|
||||
def __init__(self, parent, root: tk.Tk):
|
||||
super().__init__(root)
|
||||
self.parent = parent
|
||||
self.config = parent.config
|
||||
self.root = root
|
||||
self.protocol("WM_DELETE_WINDOW", self.close_window)
|
||||
self.bind("<Escape>", self.close_window)
|
||||
@@ -32,11 +33,11 @@ class Window(tk.Toplevel):
|
||||
|
||||
|
||||
class EditRecord(Window):
|
||||
def __init__(self, parent, root: tk.Tk, record_id: int, config: Config):
|
||||
def __init__(self, parent, root: tk.Tk, record_id: int):
|
||||
super().__init__(parent, root)
|
||||
self.bind("<Return>", self._update)
|
||||
|
||||
self.model = JSONConnector(config)
|
||||
self.model = Model(self.config)
|
||||
|
||||
record = self.model.get_by_id(record_id)
|
||||
|
||||
@@ -86,10 +87,10 @@ class EditRecord(Window):
|
||||
|
||||
|
||||
class SettingsWindow(Window):
|
||||
def __init__(self, parent, root: tk.Tk, config: Config):
|
||||
def __init__(self, parent, root: tk.Tk):
|
||||
super().__init__(parent, root)
|
||||
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.csv_file = tk.StringVar()
|
||||
|
||||
|
||||
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
30
tests/test_config.py
Normal file
30
tests/test_config.py
Normal 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"
|
||||
Reference in New Issue
Block a user