-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
197 lines (169 loc) · 4.74 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
189
190
191
192
193
194
195
196
197
import os, sys
from PyQt6.QtGui import QIcon
from PyQt6.QtWidgets import QApplication
from Models.SessionData import SessionData
from Data.Input import (
backup_file,
get_toml_files,
get_toml_patches,
parse_toml,
save_toml,
)
from UI.MainWindow import MainWindow
from UI.Components import (
Dialog,
PatchToggle,
Button,
FilesHeader,
FilesCombo,
DirectoryInput,
)
session_data = SessionData()
WINDOW = None
def render_current_toml_data(filename: str):
_ELEMENTS_PER_COLUMN = 8
WINDOW.sections['body'].clear_content()
_current_file_path = f'{session_data.get_parent_directory()}/{filename}'
session_data.set_toml(
parse_toml(_current_file_path)
)
session_data.set_current_file(_current_file_path)
session_data.set_patches(
_generate_patch_toggles(
data= get_toml_patches(
toml= session_data.get_toml()
)
)
)
print('Current session data:', session_data)
# Place elements in grid (n x 8)
for index, patch in enumerate(
session_data.get_patches()
):
_grid_position = reversed(
divmod(index, _ELEMENTS_PER_COLUMN)
)
WINDOW.sections['body'].add_child(
patch.widget,
coords= tuple(_grid_position)
)
print('Loaded toml:', filename)
print(
'Parsed data:',
f'{str(session_data.get_toml())[:24]}...'
)
def update_patch_directory(header: FilesHeader, directory: str):
session_data.set_parent_directory(directory)
session_data.set_current_file(None)
header.combo.update_data(
get_toml_files(
path= session_data.get_parent_directory()
)
)
header.update()
# Then overwrite saved path
if os.path.exists('path.txt'):
with open('path.txt', 'w') as path_file:
path_file.write(
f'{session_data.get_parent_directory()}\n'
)
def _generate_patch_toggles(data) -> tuple:
stack = []
for key in data:
stack.append(
PatchToggle(data[key])
)
return tuple(stack)
def _save_changes_handler():
if not session_data.current_file:
return
saved = save_toml(
path= session_data.get_current_file(),
data= session_data.get_toml()
)
Dialog(
title= 'Success',
body= 'Configuration updated successfully'
).show() if saved else Dialog(
title= 'Error',
body= 'Configuration couldn\'t be updated',
icon= 'error'
).show()
def _backup_conf_handler():
if not session_data.current_file:
return
backup_path = backup_file(
filepath= session_data.get_current_file()
)
if not backup_path:
Dialog(
title= 'Error',
body= "Backup couldn't be created",
icon= 'error'
).show()
return
Dialog(
title= 'Success',
body= f'Backup created at {backup_path}'
).show()
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setWindowIcon(QIcon('assets/icon.png'))
# Get route from file as fallback
if os.path.exists('path.txt'):
with open('path.txt', 'r') as path_file:
line = path_file.readline().strip()
if line:
sys.argv.append(line)
# Exit if there's no directory
if (
(len(sys.argv) < 2) or
not (os.path.exists(sys.argv[1]))
):
print(sys.argv)
Dialog(
title= 'Error',
body= 'Invalid directory provided',
icon= 'error'
).show()
sys.exit(0)
session_data.set_parent_directory(
os.path.abspath(sys.argv[1])
)
files_header = FilesHeader(
combo= FilesCombo(
tomls= get_toml_files(
session_data.get_parent_directory()
),
action= render_current_toml_data
)
)
directory_input = DirectoryInput(
session_data.get_parent_directory()
)
directory_input.set_on_change(
lambda: update_patch_directory(header= files_header, directory= directory_input.get_directory())
)
WINDOW = MainWindow('Xenia Patch Manager')
WINDOW.sections['header'].add_child(
directory_input,
coords= (0,0)
).add_child(
files_header,
coords= (1,0)
)
WINDOW.sections['footer'].add_child(
Button(
text= 'Save Changes',
action= lambda: _save_changes_handler(),
)
).add_child(
Button(
text= 'Backup configuration',
action= lambda: _backup_conf_handler(),
)
)
with open('assets/styles.css', 'r') as stylesheet:
app.setStyleSheet(stylesheet.read())
WINDOW.show()
app.exec()