-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
188 lines (163 loc) · 7.11 KB
/
main.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import logging
import os
import sys
from PySide6.QtWidgets import QApplication
from PySide6.QtCore import QUrl
from PySide6.QtGui import QDesktopServices
from PySide6.QtWidgets import QDialog
from snapcast_gui.windows.combined_window import CombinedWindow
from snapcast_gui.fileactions.file_folder_checks import FileFolderChecks
from snapcast_gui.windows.main_window import MainWindow
from snapcast_gui.misc.notifications import Notifications
from snapcast_gui.windows.client_window import ClientWindow
from snapcast_gui.windows.server_window import ServerWindow
from snapcast_gui.windows.settings_window import SettingsWindow
from snapcast_gui.fileactions.snapcast_settings import SnapcastSettings
from snapcast_gui.misc.snapcast_gui_variables import SnapcastGuiVariables
from snapcast_gui.misc.logger_setup import LoggerSetup
from snapcast_gui.dialogs.path_input_dialog import PathInputDialog
def read_log_level(log_level_file_path: str) -> int:
"""
Reads the log level from a file and returns the corresponding logging level.
Args:
log_level_file_path (str): The path to the log level file.
Raises:
FileNotFoundError: If the log level file is not found.
Exception: If there is an error opening the log level file.
"""
try:
with open(log_level_file_path, "r+") as file:
first_line = file.readline().strip().upper()
if first_line == "":
log_level = logging.INFO
file.write("INFO\n")
else:
log_level = {
"DEBUG": logging.DEBUG,
"INFO": logging.INFO,
"WARNING": logging.WARNING,
"ERROR": logging.ERROR,
"CRITICAL": logging.CRITICAL,
}.get(first_line, logging.INFO)
except FileNotFoundError:
with open(log_level_file_path, "w") as file:
log_level = logging.INFO
file.write("INFO\n")
except IsADirectoryError:
os.removedirs(os.path.dirname(log_level_file_path))
print("File path is a directory. Removing directory.")
log_level = logging.INFO
except Exception as e:
Notifications.send_notify("Error", f"Error opening log level file: {e}")
log_level = logging.DEBUG
return log_level
def open_file(file_path: str) -> None:
"""
Opens a file with the default application.
Args:
file_path (str): The path to the file.
"""
from PySide6.QtGui import QGuiApplication
app = QGuiApplication([])
url = QUrl.fromLocalFile(file_path)
QDesktopServices.openUrl(url)
app.exit()
FileFolderChecks.ensure_folder_creation()
FileFolderChecks.create_missing_files()
FileFolderChecks.set_file_permission()
log_level = read_log_level(SnapcastGuiVariables.log_level_file_path)
log_file_path = SnapcastGuiVariables.log_file_path
LoggerSetup.setup_logging(log_file_path, log_level)
logger = LoggerSetup.get_logger("main")
def main():
"""
The main function of the Snapcast-Gui application.
It handles command line arguments, initializes the logging system,
creates the application and window objects, and starts the event loop.
"""
if len(sys.argv) > 1:
option = sys.argv[1].lower()
if option == "-h" or option == "--help":
logger.debug("Showing help message")
print("Usage: python main.py")
print("Options:")
print(" -h / --help Show this help message and exit")
print(" -v / --version Show version and exit")
print(" -c / --config Open settings file")
print(" -l / --log Open log file")
print(" -i / --ip Open ip file")
sys.exit(0)
elif option == "-v" or option == "--version":
logger.debug("Showing version")
print("Snapcast-Gui version: {}".format(SnapcastGuiVariables.snapcast_gui_version))
elif option == "-c" or option == "--config":
open_file(SnapcastGuiVariables.settings_file_path)
logger.debug("Opening settings file with open_file")
sys.exit(0)
elif option == "-l" or option == "--log":
open_file(SnapcastGuiVariables.log_file_path)
logger.debug("Opening log file with open_file")
sys.exit(0)
elif option == "-i" or option == "--ip":
open_file(SnapcastGuiVariables.log_level_file_path)
logger.debug("Opening log level file with open_file")
sys.exit(0)
else:
logger.debug("Invalid argument")
print("Invalid argument")
print("")
print("Usage: python main.py")
print("Options:")
print(" -h / --help Show this help message and exit")
print(" -v / --version Show version and exit")
print(" -c / --config Open settings file")
print(" -l / --log Open log file")
print(" -i / --ip Open ip file")
sys.exit(1)
logger.info("Starting Snapcast-Gui")
logger.debug("sys.platform: {}".format(sys.platform))
snapcast_settings = SnapcastSettings(log_level)
app = QApplication(sys.argv)
client_window = ClientWindow(snapcast_settings, log_level)
main_window = MainWindow(snapcast_settings, client_window, log_level)
server_window = ServerWindow(snapcast_settings, log_level)
settings_window = SettingsWindow(snapcast_settings, main_window, log_level)
combined_window = CombinedWindow(
main_window,
client_window,
server_window,
settings_window,
snapcast_settings,
log_level,
)
main_window.create_tray_icon(
main_window,
client_window,
server_window,
settings_window,
combined_window,
snapcast_settings,
log_level,
)
def is_executable(file_path):
return os.path.isfile(file_path) and os.access(file_path, os.X_OK)
snapclient_path = snapcast_settings.read_setting("snapclient/custom_path")
snapserver_path = snapcast_settings.read_setting("snapserver/custom_path")
if not snapcast_settings.read_setting("snapclient/ignore_popup"):
if not is_executable(snapclient_path):
dialog = PathInputDialog("snapclient", log_level)
if dialog.exec() == QDialog.Accepted:
snapcast_settings.set_ignore_popup(dialog.ignore_popup_checkbox.isChecked())
new_path = dialog.get_path()
snapcast_settings.write_setting("snapclient/custom_path", new_path)
if not snapcast_settings.read_setting("snapserver/ignore_popup"):
if not is_executable(snapserver_path):
dialog = PathInputDialog("snapserver", log_level)
if dialog.exec() == QDialog.Accepted:
snapcast_settings.set_ignore_popup(dialog.ignore_popup_checkbox.isChecked())
new_path = dialog.get_path()
snapcast_settings.write_setting("snapserver/custom_path", new_path)
combined_window.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()