Skip to content

Commit

Permalink
Merge pull request #9 from t0mmili/feature/6-dc-config-version-compat…
Browse files Browse the repository at this point in the history
…ibility

feature/6-dc-config-version-compatibility
  • Loading branch information
t0mmili authored Aug 26, 2024
2 parents fee6726 + 2533523 commit 8f69147
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 25 deletions.
4 changes: 1 addition & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@
| Documentation - user || <span style="color:orange">Medium</span> |
| User config | - ~~Implement config in json format.~~<br>- ~~Create default, if doesn't exist, on app start.~~<br>- In-app window to modify.<br>- ~~Versioning, in case new config values appear in the future.~~ | <span style="color:orange">Medium</span> |
| Detailed message after completing scheme apply | Should mention what was done, e.g. DC config backup. | <span style="color:yellow">Low</span> |
| DC config compatibility | - Check scheme version against config version.<br>- Display warning if version mismatch. | <span style="color:yellow">Low</span> |
| Scheme export | Allow to export scheme from current DC config. | <span style="color:yellow">Low</span> |
| License link | Link in About window. | <span style="color:yellow">Low</span> |
| Scheme export | Allow to export scheme from current DC config. | <span style="color:yellow">Low</span> |
2 changes: 1 addition & 1 deletion app/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# General information
APP_AUTHOR = 't0mmili'
APP_NAME = 'DC Themer'
APP_VERSION = '0.3.1'
APP_VERSION = '0.4.0'
DEV_YEARS = '2024'
LICENSE_PATH = 'LICENSE'
REPO_URL = 'https://github.com/t0mmili/dc-themer'
Expand Down
61 changes: 41 additions & 20 deletions app/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ class AppFrame(ttk.Frame):
scheme_selector (ttk.OptionMenu): Dropdown menu to select a scheme.
dark_mode_tick (ttk.Checkbutton): Checkbox to enable or disable auto
dark mode.
apply_button (ttk.Button): Button to apply the selected scheme.
apply_button (ttk.Button): Button to verify and apply the selected
scheme.
Args:
container (tk.Tk): The parent widget, typically an instance of Tk or
Expand All @@ -155,6 +156,37 @@ def __init__(self, container: tk.Tk, user_config: dict) -> None:

self.setup_widgets()
self.grid(padx=10, pady=10, sticky=tk.NSEW)
self.initialize_scheme()

def initialize_scheme(self) -> None:
"""
Initialize object of Scheme class.
"""
self.scheme = Scheme(
self.scheme_var.get(), self.user_config['schemes']['path'],
self.user_config['doubleCommander']['configPaths'],
self.user_config['doubleCommander']['backupConfigs'],
self.dark_mode_var.get(),
self.user_config['schemes']['xmlTags']
)

def modify_scheme(self) -> None:
"""
Applies the selected scheme and updates the configuration accordingly.
"""
try:
self.scheme.apply_scheme()
showinfo(
title='Info',
message=(
f'Scheme \'{self.scheme_var.get()}\' applied successfully.'
)
)
except Exception as e:
showerror(
title='Error',
message=str(e)
)

def setup_widgets(self) -> None:
"""
Expand Down Expand Up @@ -189,34 +221,23 @@ def setup_widgets(self) -> None:
column=0, row=1, columnspan=2, sticky=tk.W, **options
)

# Apply Scheme button
# Initialize, verify and apply scheme
self.apply_button: ttk.Button = ttk.Button(
self, text='Apply', command=self.modify_scheme
self, text='Apply', command=lambda: (
self.initialize_scheme(), self.verify_scheme(),
self.modify_scheme()
)
)
self.apply_button.grid(
column=0, row=2, columnspan=2, sticky=tk.W, **options
)

def modify_scheme(self) -> None:
def verify_scheme(self) -> None:
"""
Applies the selected scheme and updates the configuration accordingly.
Verifies the selected scheme version against target scheme version.
"""
try:
scheme = Scheme(
self.scheme_var.get(), self.user_config['schemes']['path'],
self.user_config['doubleCommander']['configPaths'],
self.user_config['doubleCommander']['backupConfigs'],
self.dark_mode_var.get(),
self.user_config['schemes']['xmlTags']
)
scheme.apply_scheme()
showinfo(
title='Info',
message=(
f'Scheme \'{self.scheme_var.get()}\' '
f'applied successfully.'
)
)
self.scheme.verify_scheme()
except Exception as e:
showerror(
title='Error',
Expand Down
44 changes: 43 additions & 1 deletion app/scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from defusedxml.ElementTree import parse, tostring
from defusedxml.minidom import parseString
from os import path
from tkinter.messagebox import showwarning
from utils import DCFileManager, SchemeFileManager

class Scheme:
Expand All @@ -28,6 +29,10 @@ class Scheme:
configuration file.
apply_scheme_xml(): Applies the scheme specifically to the xml
configuration file.
verify_scheme(): Verifies the scheme version of all configuration files
(cfg, json, xml).
verify_scheme_version_xml(): Verifies the scheme version of xml
configuration file specifically.
"""
def __init__(
self, scheme: str, scheme_path: str, dc_configs: dict[str, str],
Expand Down Expand Up @@ -152,4 +157,41 @@ def apply_scheme_xml(self) -> None:
)

# Save modified DC xml config file
SchemeFileManager.set_xml(pretty_xml, target_file)
SchemeFileManager.set_xml(pretty_xml, target_file)

def verify_scheme(self) -> None:
"""
Verifies the scheme version of all configuration files
(cfg, json, xml).
"""
self.verify_scheme_version_xml()

def verify_scheme_version_xml(self) -> None:
"""
Verifies the scheme version of xml configuration file specifically.
"""
source_file: str = path.join(self.scheme_path, f'{self.scheme}.xml')
target_file: str = DCFileManager.get_config(self.dc_configs['xml'])

source_tree = parse(source_file)
target_tree = parse(target_file)

source_config_version: str | None = (
source_tree.getroot().attrib.get('ConfigVersion')
)
target_config_version: str | None = (
target_tree.getroot().attrib.get('ConfigVersion')
)

if source_config_version != target_config_version:
showwarning(
title='Warning',
message=(
'XML configuration scheme version mismatch:\n\n'
f'Source scheme: {source_config_version}\n'
f'Target scheme: {target_config_version}\n\n'
'The apply process will continue.\n'
'In case of any issues, please verify your configuration '
'files.'
)
)

0 comments on commit 8f69147

Please sign in to comment.