-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
161 lines (139 loc) · 5.95 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# utils.py
import os
import subprocess
import sys
import pathlib
import tkinter as tk
from tkinter import filedialog
import logging
def initialize_button_states(app):
"""
Initialize the button states with default values in the application,
including text fields in the main window.
"""
app.button_states = {
'start_button': {'enabled': True, 'default': True, 'visible': True},
'cancel_button': {'enabled': False, 'default': False, 'visible': True},
'quit_button': {'enabled': True, 'default': True, 'visible': True},
'save_button': {'enabled': True, 'default': True, 'visible': True},
'restore_defaults_button': {'enabled': True, 'default': True, 'visible': True},
'import_oscar_checkbox': {'enabled': True, 'default': True, 'visible': True},
'quit_checkbox': {'enabled': True, 'default': True, 'visible': True},
'select_folder_button': {'enabled': True, 'default': True, 'visible': True},
'configure_wifi_button': {'enabled': True, 'default': True, 'visible': True},
'local_directory_path': {'enabled': True, 'default': True, 'visible': True},
'url_entry': {'enabled': True, 'default': True, 'visible': True},
'ssid_entry': {'enabled': True, 'default': True, 'visible': True},
'psk_entry': {'enabled': True, 'default': True, 'visible': True},
}
def update_button_state(app, button_name, enabled=None, is_default=None, visible=None):
"""
Update the state, visibility, and default status of a button or text field in the UI and its state in the global dictionary.
"""
widget = app.builder.get_object(button_name)
state_info = app.button_states[button_name]
if enabled is not None:
state_info['enabled'] = enabled
widget.config(state=tk.NORMAL if enabled else tk.DISABLED)
# Apply the default option only if the widget is a tk.Button
if isinstance(widget, tk.Button) and is_default is not None:
state_info['default'] = is_default
widget.config(default=tk.ACTIVE if is_default else tk.NORMAL)
if visible is not None:
state_info['visible'] = visible
if visible:
widget.pack() # Show the widget using pack()
else:
widget.pack_forget() # Hide the widget using pack_forget()
logging.info(f"Updated widget '{button_name}' - Enabled: {state_info['enabled']}, Default: {state_info.get('default')}, Visible: {state_info['visible']}")
def set_default_button_states(app):
"""
Reset all buttons and text fields to their default states as defined in the global dictionary.
"""
logging.info("Setting all widget states to their default values.")
for widget_name, state_info in app.button_states.items():
update_button_state(app, widget_name, enabled=state_info['default'], is_default=state_info['default'])
def set_process_button_states(app):
"""
Set all buttons and text fields to their states during a major process, disabling all but the cancel button.
"""
logging.info("Setting all widget states for a process (disabling most except the cancel button).")
for widget_name in app.button_states:
if widget_name == 'cancel_button':
update_button_state(app, widget_name, enabled=True, is_default=True)
else:
update_button_state(app, widget_name, enabled=False, is_default=False)
def get_button_state(app, button_name):
"""
Retrieve the current state of a button or text field from the global dictionary.
Parameters:
button_name (str): The name of the button or text field.
Returns:
dict: The current state of the button or text field.
"""
return app.button_states.get(button_name, {'enabled': False, 'default': False, 'visible': True})
def ensure_and_check_disk_access(directory, parent=None):
"""
Ensure the specified directory exists and is accessible.
Parameters:
directory (str): The directory path to check.
parent (tk.Widget): The parent widget (optional).
Returns:
bool: True if the directory exists and is accessible, False otherwise.
"""
expanded_directory = pathlib.Path(directory).expanduser()
if not expanded_directory.exists():
if parent:
try:
expanded_directory.mkdir(parents=True)
except PermissionError:
request_disk_access(parent)
return False
else:
return False
try:
os.listdir(expanded_directory)
return True
except PermissionError:
return False
def request_disk_access(parent):
"""
Request access to a directory if the current one is inaccessible.
Parameters:
parent (tk.Widget): The parent widget.
"""
options = {'initialdir': '/'}
directory = filedialog.askdirectory(**options)
if directory:
parent.config_manager.set_setting('Settings', 'path', directory)
parent.save_config()
print(f"Directory selected: {directory}")
else:
print("No directory selected")
def check_oscar_installed():
"""
Check if the OSCAR application is installed.
Returns:
bool: True if OSCAR is installed, False otherwise.
"""
try:
oscar_installed = subprocess.run(["osascript", "-e", 'id of application "OSCAR"'], capture_output=True, text=True, check=True)
return oscar_installed.returncode == 0
except subprocess.CalledProcessError as e:
print(f"Error checking OSCAR installation: {e}")
return False
def resource_path(relative_path):
"""
Get the absolute path to the resource, works for dev and for PyInstaller.
Parameters:
relative_path (str): The relative path to the resource.
Returns:
str: The absolute path to the resource.
"""
if hasattr(sys, '_MEIPASS'):
# Running in a PyInstaller bundle
base_path = sys._MEIPASS
else:
# Running in a normal Python environment
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)