splitguides 0.10.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- splitguides/__init__.py +1 -0
- splitguides/__main__.py +15 -0
- splitguides/_version.py +2 -0
- splitguides/build_ui.py +71 -0
- splitguides/exceptions.py +4 -0
- splitguides/hotkeys.py +54 -0
- splitguides/livesplit_client.py +303 -0
- splitguides/note_parser.py +260 -0
- splitguides/server/__init__.py +1 -0
- splitguides/server/__main__.py +58 -0
- splitguides/server/split_server.py +129 -0
- splitguides/settings.py +231 -0
- splitguides/static/desktop.css +146 -0
- splitguides/static/server.css +160 -0
- splitguides/static/streamlistener.js +18 -0
- splitguides/templates/desktop.html +25 -0
- splitguides/templates/server.html +12 -0
- splitguides/ui/__init__.py +0 -0
- splitguides/ui/color.py +50 -0
- splitguides/ui/custom_elements/__init__.py +18 -0
- splitguides/ui/hotkey_manager.py +92 -0
- splitguides/ui/layouts/__init__.py +8 -0
- splitguides/ui/layouts/build/__init__.py +0 -0
- splitguides/ui/layouts/build/main_window.py +61 -0
- splitguides/ui/layouts/build/server_settings.py +349 -0
- splitguides/ui/layouts/build/settings.py +318 -0
- splitguides/ui/layouts/main_window.ui +64 -0
- splitguides/ui/layouts/server_settings.ui +394 -0
- splitguides/ui/layouts/settings.ui +382 -0
- splitguides/ui/main.pyproject +3 -0
- splitguides/ui/main_window.py +492 -0
- splitguides/ui/server_settings_ui.py +191 -0
- splitguides/ui/settings_ui.py +241 -0
- splitguides-0.10.1.dist-info/METADATA +199 -0
- splitguides-0.10.1.dist-info/RECORD +39 -0
- splitguides-0.10.1.dist-info/WHEEL +5 -0
- splitguides-0.10.1.dist-info/entry_points.txt +3 -0
- splitguides-0.10.1.dist-info/licenses/COPYING +674 -0
- splitguides-0.10.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Handle parsing a notes file into separate pages of notes.
|
|
3
|
+
"""
|
|
4
|
+
import os
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
import bleach
|
|
8
|
+
import bleach.sanitizer
|
|
9
|
+
import bleach.css_sanitizer
|
|
10
|
+
import markdown
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
PERMITTED_TAGS = {
|
|
14
|
+
"p",
|
|
15
|
+
"br",
|
|
16
|
+
"del",
|
|
17
|
+
"ins",
|
|
18
|
+
"mark",
|
|
19
|
+
"small",
|
|
20
|
+
"sub",
|
|
21
|
+
"sup",
|
|
22
|
+
"h1",
|
|
23
|
+
"h2",
|
|
24
|
+
"h3",
|
|
25
|
+
"h4",
|
|
26
|
+
"h5",
|
|
27
|
+
"h6",
|
|
28
|
+
"div",
|
|
29
|
+
"img",
|
|
30
|
+
"video",
|
|
31
|
+
"source",
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
PERMITTED_ATTRIBUTES = {
|
|
35
|
+
"*": ["class", "style", "href"],
|
|
36
|
+
"img": ["src", "alt", "width", "height"],
|
|
37
|
+
"video": [
|
|
38
|
+
"autoplay",
|
|
39
|
+
"controls",
|
|
40
|
+
"height",
|
|
41
|
+
"loop",
|
|
42
|
+
"muted",
|
|
43
|
+
"poster",
|
|
44
|
+
"src",
|
|
45
|
+
"width",
|
|
46
|
+
],
|
|
47
|
+
"source": ["src", "type"],
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
PERMITTED_STYLES = {
|
|
52
|
+
"background-color",
|
|
53
|
+
"color",
|
|
54
|
+
"font-family",
|
|
55
|
+
"font-size",
|
|
56
|
+
"font-weight",
|
|
57
|
+
"text-align",
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class Notes:
|
|
62
|
+
"""
|
|
63
|
+
Class to handle notes and formatting
|
|
64
|
+
|
|
65
|
+
Processing order is:
|
|
66
|
+
Input -> Strip Comment lines (delimited by [ ])
|
|
67
|
+
-> Split by separator
|
|
68
|
+
-> Store
|
|
69
|
+
[Render Call]
|
|
70
|
+
-> Process
|
|
71
|
+
-> Sanitize
|
|
72
|
+
-> Render
|
|
73
|
+
|
|
74
|
+
"""
|
|
75
|
+
|
|
76
|
+
def __init__(self, note_stream, separator="", *, preprocessor=None):
|
|
77
|
+
"""
|
|
78
|
+
Note_stream should be an iterable object providing notes line by line
|
|
79
|
+
|
|
80
|
+
:param note_stream: iterable object providing notes
|
|
81
|
+
:param separator: The separator between split segments (default blank line)
|
|
82
|
+
:param preprocessor: Tool if there is preprocessing to do on the notes
|
|
83
|
+
This should be determined by the file extension
|
|
84
|
+
"""
|
|
85
|
+
if isinstance(note_stream, str):
|
|
86
|
+
raise TypeError(
|
|
87
|
+
"Expected file-like object for note_stream, but received string. "
|
|
88
|
+
"Use Notes.from_file for paths."
|
|
89
|
+
)
|
|
90
|
+
elif isinstance(note_stream, os.PathLike):
|
|
91
|
+
raise TypeError(
|
|
92
|
+
"Expected file-like object for note_stream, but received path-like object. "
|
|
93
|
+
"Use Notes.from_file for paths."
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
self.preprocessor = preprocessor
|
|
97
|
+
self.separator = separator
|
|
98
|
+
|
|
99
|
+
self.notes = []
|
|
100
|
+
self.get_notes(note_stream)
|
|
101
|
+
self.safe_mode = True
|
|
102
|
+
self.cleaner = get_cleaner(
|
|
103
|
+
PERMITTED_TAGS, PERMITTED_ATTRIBUTES, PERMITTED_STYLES
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
def get_notes(self, note_stream):
|
|
107
|
+
"""
|
|
108
|
+
Parse the note stream and obtain the text to be rendered.
|
|
109
|
+
|
|
110
|
+
:param note_stream: iterable containing notes by line
|
|
111
|
+
"""
|
|
112
|
+
split_notes = []
|
|
113
|
+
split = []
|
|
114
|
+
for line in note_stream:
|
|
115
|
+
line = line.rstrip() # remove newlines
|
|
116
|
+
if line.startswith("[") and line.endswith("]"):
|
|
117
|
+
pass # Ignore comment lines
|
|
118
|
+
elif line == self.separator:
|
|
119
|
+
# If the split is empty and the separator is blank
|
|
120
|
+
# Ignore the break
|
|
121
|
+
if not (split or self.separator):
|
|
122
|
+
continue
|
|
123
|
+
# Split segment on separator
|
|
124
|
+
split_notes.append("\n".join(split))
|
|
125
|
+
split = []
|
|
126
|
+
else:
|
|
127
|
+
split.append(line)
|
|
128
|
+
else:
|
|
129
|
+
split_notes.append("\n".join(split))
|
|
130
|
+
|
|
131
|
+
self.notes = split_notes
|
|
132
|
+
|
|
133
|
+
@classmethod
|
|
134
|
+
def from_file(cls, path, separator=""):
|
|
135
|
+
"""
|
|
136
|
+
Helper method to parse a set of notes read from a given file path.
|
|
137
|
+
|
|
138
|
+
:param path: path to notes text file
|
|
139
|
+
:param separator: The separator between split segments (default blank line)
|
|
140
|
+
:return: Instance of Notes parsed from the provided file
|
|
141
|
+
"""
|
|
142
|
+
path = Path(path)
|
|
143
|
+
if path.suffix == ".txt":
|
|
144
|
+
preprocessor = TextProcessor()
|
|
145
|
+
elif path.suffix == ".md":
|
|
146
|
+
preprocessor = MarkdownProcessor()
|
|
147
|
+
else:
|
|
148
|
+
preprocessor = None
|
|
149
|
+
|
|
150
|
+
with open(path, "r", encoding="utf-8") as f:
|
|
151
|
+
notes = Notes(f, separator, preprocessor=preprocessor)
|
|
152
|
+
return notes
|
|
153
|
+
|
|
154
|
+
def render_splits(self, start, end):
|
|
155
|
+
"""
|
|
156
|
+
Render the notes as a list of text items for HTML
|
|
157
|
+
|
|
158
|
+
:param start: Split index to start rendering
|
|
159
|
+
:param end: Split index to end rendering
|
|
160
|
+
:return: notes
|
|
161
|
+
"""
|
|
162
|
+
start = max(start, 0)
|
|
163
|
+
end = min(end, len(self.notes))
|
|
164
|
+
|
|
165
|
+
result = []
|
|
166
|
+
|
|
167
|
+
if start >= end:
|
|
168
|
+
result = ["<h1>End of Splits</h1>"]
|
|
169
|
+
else:
|
|
170
|
+
for idx in range(start, end):
|
|
171
|
+
raw_split = self.notes[idx]
|
|
172
|
+
if self.preprocessor:
|
|
173
|
+
split = self.preprocessor.process(raw_split)
|
|
174
|
+
else:
|
|
175
|
+
split = raw_split
|
|
176
|
+
result.append(split)
|
|
177
|
+
|
|
178
|
+
# If in safe mode clean the HTML of unsafe data
|
|
179
|
+
if self.safe_mode:
|
|
180
|
+
result = [self.cleaner.clean(html) for html in result]
|
|
181
|
+
|
|
182
|
+
return result
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def get_cleaner(extra_tags, extra_attributes, extra_styles):
|
|
186
|
+
"""
|
|
187
|
+
Get a HTML cleaner to remove dangerous tags
|
|
188
|
+
|
|
189
|
+
This handles the external 'bleach' library.
|
|
190
|
+
:param extra_tags:
|
|
191
|
+
:param extra_attributes:
|
|
192
|
+
:param extra_styles:
|
|
193
|
+
:return:
|
|
194
|
+
"""
|
|
195
|
+
valid_tags = set(bleach.sanitizer.ALLOWED_TAGS)
|
|
196
|
+
valid_tags.update(extra_tags)
|
|
197
|
+
valid_attributes = bleach.sanitizer.ALLOWED_ATTRIBUTES.copy()
|
|
198
|
+
valid_attributes.update(extra_attributes)
|
|
199
|
+
|
|
200
|
+
# Bleach 5.0 changed how CSS works, to avoid changing internals we'll wrap around this
|
|
201
|
+
valid_styles = set(bleach.css_sanitizer.ALLOWED_CSS_PROPERTIES)
|
|
202
|
+
valid_styles.update(extra_styles)
|
|
203
|
+
css_sanitizer = bleach.css_sanitizer.CSSSanitizer(allowed_css_properties=valid_tags)
|
|
204
|
+
|
|
205
|
+
cleaner = bleach.sanitizer.Cleaner(
|
|
206
|
+
tags=valid_tags, attributes=valid_attributes, css_sanitizer=css_sanitizer
|
|
207
|
+
)
|
|
208
|
+
return cleaner
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
class TextProcessor:
|
|
212
|
+
def __init__(self, *, continue_char="\\"):
|
|
213
|
+
"""
|
|
214
|
+
Create a basic text processor
|
|
215
|
+
|
|
216
|
+
:param continue_char: Character used to indicate a <br> tag should not be added
|
|
217
|
+
"""
|
|
218
|
+
self.continue_char = continue_char
|
|
219
|
+
|
|
220
|
+
def process(self, raw_text):
|
|
221
|
+
"""
|
|
222
|
+
Basic processing for raw text, add breaks for newlines.
|
|
223
|
+
Don't add a break if the line ends with the continuation char but remove the char
|
|
224
|
+
|
|
225
|
+
:param raw_text: Basic split text
|
|
226
|
+
:return: Text with <br> inserted for new lines
|
|
227
|
+
"""
|
|
228
|
+
split_parts = (
|
|
229
|
+
item[:-1] if item.endswith(self.continue_char) else f"{item}<br>"
|
|
230
|
+
for item in raw_text.split("\n")
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
return "\n".join(split_parts)
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
class MarkdownProcessor:
|
|
237
|
+
default_extensions = ["nl2br", "sane_lists", "tables"]
|
|
238
|
+
|
|
239
|
+
def __init__(self, *, extensions=None):
|
|
240
|
+
"""
|
|
241
|
+
Create a markdown processor using the markdown extension
|
|
242
|
+
By default this uses the extensions for sane lists, tables and new line to break
|
|
243
|
+
|
|
244
|
+
:param extensions: List of specific extensions, if not defined use defailts
|
|
245
|
+
"""
|
|
246
|
+
self.extensions = (
|
|
247
|
+
extensions if extensions is not None else self.default_extensions
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
self.formatter = markdown.Markdown(extensions=self.extensions)
|
|
251
|
+
|
|
252
|
+
def process(self, raw_text):
|
|
253
|
+
"""
|
|
254
|
+
Convert the raw MarkDown input into HTML
|
|
255
|
+
:param raw_text: Markdown input as a string
|
|
256
|
+
:return: processed HTML as a string
|
|
257
|
+
"""
|
|
258
|
+
processed_text = self.formatter.convert(raw_text)
|
|
259
|
+
self.formatter.reset()
|
|
260
|
+
return processed_text
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .split_server import app as app, get_notes as get_notes, settings as settings
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
from PySide6.QtWidgets import QApplication, QMainWindow
|
|
2
|
+
import waitress
|
|
3
|
+
|
|
4
|
+
from splitguides.server import app, get_notes, settings
|
|
5
|
+
from splitguides.ui.server_settings_ui import ServerSettingsDialog
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def launch():
|
|
9
|
+
# Create a base application and main window for the dialogs to use as parent
|
|
10
|
+
qt_app = QApplication()
|
|
11
|
+
main_window = QMainWindow()
|
|
12
|
+
|
|
13
|
+
settings_dialog = ServerSettingsDialog(
|
|
14
|
+
parent=main_window,
|
|
15
|
+
settings=settings,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
result = settings_dialog.exec()
|
|
19
|
+
if result == 0: # Rejected, close without launching server
|
|
20
|
+
print("Settings cancelled, closing application.")
|
|
21
|
+
qt_app.quit()
|
|
22
|
+
return
|
|
23
|
+
|
|
24
|
+
success = get_notes(main_window) # Sets internal 'notes' and 'notefile' variables
|
|
25
|
+
if not success:
|
|
26
|
+
print("No notes file selected, closing application.")
|
|
27
|
+
qt_app.quit()
|
|
28
|
+
return
|
|
29
|
+
|
|
30
|
+
print(
|
|
31
|
+
"This server version of SplitGuides allows you view notes via a browser window "
|
|
32
|
+
"and should work across a local network.\n"
|
|
33
|
+
"This uses a development server and is not intended "
|
|
34
|
+
"to be used over the internet."
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
print(
|
|
38
|
+
f"Connect a browser to "
|
|
39
|
+
f"http://{settings.server_hostname}:{settings.server_port}/ "
|
|
40
|
+
f"in order to view the notes."
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
print("Press ctrl+c to close the server.")
|
|
44
|
+
|
|
45
|
+
try:
|
|
46
|
+
waitress.serve(
|
|
47
|
+
app,
|
|
48
|
+
host=settings.server_hostname,
|
|
49
|
+
port=settings.server_port
|
|
50
|
+
)
|
|
51
|
+
except KeyboardInterrupt:
|
|
52
|
+
print("Interrupt received, closing application.")
|
|
53
|
+
finally:
|
|
54
|
+
qt_app.quit()
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
if __name__ == "__main__":
|
|
58
|
+
launch()
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import random
|
|
2
|
+
import secrets
|
|
3
|
+
import string
|
|
4
|
+
import time
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from flask import Flask, render_template, Response
|
|
8
|
+
from PySide6.QtWidgets import QFileDialog
|
|
9
|
+
|
|
10
|
+
from ..settings import ServerSettings
|
|
11
|
+
from ..livesplit_client import get_client
|
|
12
|
+
from ..note_parser import Notes
|
|
13
|
+
|
|
14
|
+
KEEP_ALIVE = 10
|
|
15
|
+
|
|
16
|
+
settings = ServerSettings.load()
|
|
17
|
+
|
|
18
|
+
app = Flask(
|
|
19
|
+
"splitguides",
|
|
20
|
+
template_folder=settings.html_template_folder,
|
|
21
|
+
static_folder=settings.css_folder,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
notefile: None | Path = None
|
|
25
|
+
notes: None | Notes = None
|
|
26
|
+
|
|
27
|
+
app.secret_key = "".join(
|
|
28
|
+
secrets.choice(string.printable) for _ in range(random.randint(30, 40))
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
# noinspection PyUnresolvedReferences
|
|
33
|
+
@app.route("/")
|
|
34
|
+
def notes_page():
|
|
35
|
+
"""
|
|
36
|
+
Render the basic page as index.
|
|
37
|
+
:return:
|
|
38
|
+
"""
|
|
39
|
+
global notefile
|
|
40
|
+
assert notefile is not None
|
|
41
|
+
return render_template(settings.html_template_file, notefile=notefile.stem)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@app.route("/splits")
|
|
45
|
+
def split():
|
|
46
|
+
"""
|
|
47
|
+
Server-sent events handler
|
|
48
|
+
:return: server-sent events
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
def event_stream():
|
|
52
|
+
"""
|
|
53
|
+
Handle the stream of note updates, when the note index changes - push the update
|
|
54
|
+
otherwise just keep alive every 10s.
|
|
55
|
+
"""
|
|
56
|
+
global notes
|
|
57
|
+
assert notes is not None
|
|
58
|
+
|
|
59
|
+
current_note_index = None
|
|
60
|
+
last_update = 0
|
|
61
|
+
client = get_client(settings.hostname, settings.port)
|
|
62
|
+
connected = client.connect()
|
|
63
|
+
# Note if the previous state was not connected
|
|
64
|
+
disconnected = True
|
|
65
|
+
# Define empty data, used to display the last notes even if disconnected
|
|
66
|
+
data = ""
|
|
67
|
+
|
|
68
|
+
while True:
|
|
69
|
+
if connected:
|
|
70
|
+
now = time.time()
|
|
71
|
+
try:
|
|
72
|
+
new_index = max(client.get_split_index(), 0)
|
|
73
|
+
except (ConnectionError, TimeoutError):
|
|
74
|
+
connected = client.connect()
|
|
75
|
+
yield (
|
|
76
|
+
f"data: <h2>Trying to connect to livesplit.</h2>"
|
|
77
|
+
f"<h3>Make sure Livesplit server is running.</h3>{data}\n\n"
|
|
78
|
+
)
|
|
79
|
+
else:
|
|
80
|
+
if current_note_index != new_index or disconnected:
|
|
81
|
+
disconnected = False
|
|
82
|
+
|
|
83
|
+
last_update = now
|
|
84
|
+
current_note_index = new_index
|
|
85
|
+
split_text = notes.render_splits(
|
|
86
|
+
new_index - settings.previous_splits,
|
|
87
|
+
new_index + settings.next_splits + 1,
|
|
88
|
+
)
|
|
89
|
+
if len(split_text) > 0:
|
|
90
|
+
# Remove newlines from the notes as they break the send
|
|
91
|
+
data = "".join(split_text).replace("\n", "")
|
|
92
|
+
yield f"data: {data}\n\n"
|
|
93
|
+
else:
|
|
94
|
+
yield "data: End of Notes.\n\n"
|
|
95
|
+
elif now - last_update > KEEP_ALIVE:
|
|
96
|
+
last_update = now
|
|
97
|
+
yield ":No update, keep connection\n\n"
|
|
98
|
+
else:
|
|
99
|
+
disconnected = True
|
|
100
|
+
connected = client.connect()
|
|
101
|
+
yield (
|
|
102
|
+
f"data: <h2>Trying to connect to livesplit.</h2>"
|
|
103
|
+
f"<h3>Make sure Livesplit server is running.</h3>{data}\n\n"
|
|
104
|
+
)
|
|
105
|
+
time.sleep(0.5)
|
|
106
|
+
|
|
107
|
+
return Response(event_stream(), mimetype="text/event-stream")
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def get_notes(parent):
|
|
111
|
+
global notes, notefile
|
|
112
|
+
|
|
113
|
+
# noinspection PyTypeChecker
|
|
114
|
+
filepath, _ = QFileDialog.getOpenFileName(
|
|
115
|
+
parent,
|
|
116
|
+
"Open Notes",
|
|
117
|
+
settings.notes_folder,
|
|
118
|
+
"Note Files (*.txt *.md);;All Files (*.*)",
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
if filepath:
|
|
122
|
+
notefile = Path(filepath)
|
|
123
|
+
notes = Notes.from_file(notefile, settings.split_separator)
|
|
124
|
+
|
|
125
|
+
settings.notes_folder = str(notefile.parent)
|
|
126
|
+
settings.save()
|
|
127
|
+
return True
|
|
128
|
+
else:
|
|
129
|
+
return False
|
splitguides/settings.py
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import socket
|
|
3
|
+
import sys
|
|
4
|
+
import json
|
|
5
|
+
|
|
6
|
+
from abc import ABCMeta
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import ClassVar
|
|
9
|
+
|
|
10
|
+
from ducktools.classbuilder.prefab import prefab, attribute, as_dict, is_prefab_instance, get_attributes
|
|
11
|
+
|
|
12
|
+
from .hotkeys import hotkey_or_none, Hotkey
|
|
13
|
+
from .exceptions import UnsupportedPlatformError
|
|
14
|
+
|
|
15
|
+
PROJECT_NAME = "splitguides"
|
|
16
|
+
|
|
17
|
+
match sys.platform:
|
|
18
|
+
case "win32":
|
|
19
|
+
if _local_app_folder := os.environ.get("LOCALAPPDATA"):
|
|
20
|
+
if not os.path.isdir(_local_app_folder):
|
|
21
|
+
raise FileNotFoundError(
|
|
22
|
+
f"Could not find local app data folder {_local_app_folder}"
|
|
23
|
+
)
|
|
24
|
+
else:
|
|
25
|
+
raise EnvironmentError("Environment variable %LOCALAPPDATA% not found")
|
|
26
|
+
SETTINGS_FOLDER = Path(_local_app_folder) / PROJECT_NAME
|
|
27
|
+
case "linux":
|
|
28
|
+
SETTINGS_FOLDER = Path(os.path.expanduser(os.path.join("~", f".{PROJECT_NAME}")))
|
|
29
|
+
case other:
|
|
30
|
+
raise UnsupportedPlatformError(
|
|
31
|
+
f"Platform {other!r} is not currently supported."
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
SETTINGS_FOLDER.mkdir(exist_ok=True)
|
|
35
|
+
|
|
36
|
+
if getattr(sys, "frozen", False): # pragma: nocover
|
|
37
|
+
# Application is .exe, use visible files
|
|
38
|
+
APPLICATION_FOLDER = Path(sys.executable).parent
|
|
39
|
+
else:
|
|
40
|
+
# Running as .py - use standard folder structure
|
|
41
|
+
APPLICATION_FOLDER = Path(__file__).parent
|
|
42
|
+
|
|
43
|
+
DESKTOP_SETTINGS_FILE = SETTINGS_FOLDER / "settings.json"
|
|
44
|
+
SERVER_SETTINGS_FILE = SETTINGS_FOLDER / "server_settings.json"
|
|
45
|
+
|
|
46
|
+
DEFAULT_TEMPLATE_FOLDER = Path(APPLICATION_FOLDER / "templates")
|
|
47
|
+
DEFAULT_STATIC_FOLDER = Path(APPLICATION_FOLDER / "static")
|
|
48
|
+
|
|
49
|
+
USER_PATH = str(Path(os.path.expanduser("~")) / "Documents")
|
|
50
|
+
|
|
51
|
+
try:
|
|
52
|
+
LOCAL_HOSTNAME = socket.gethostname()
|
|
53
|
+
except OSError:
|
|
54
|
+
LOCAL_HOSTNAME = "127.0.0.1"
|
|
55
|
+
print(
|
|
56
|
+
"Could not get local network hostname, using 127.0.0.1. "
|
|
57
|
+
"The server will only be accessible from this machine."
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@prefab
|
|
62
|
+
class BaseSettings(metaclass=ABCMeta):
|
|
63
|
+
# Settings files to use - Default to desktop versions
|
|
64
|
+
SETTINGS_FILE: ClassVar[Path] = DESKTOP_SETTINGS_FILE
|
|
65
|
+
|
|
66
|
+
default_template_filename: ClassVar[str] = "desktop.html"
|
|
67
|
+
default_css_filename: ClassVar[str] = "desktop.css"
|
|
68
|
+
|
|
69
|
+
# Settings save file
|
|
70
|
+
output_file: Path = attribute(serialize=False)
|
|
71
|
+
|
|
72
|
+
# Networking Settings
|
|
73
|
+
hostname: str = "localhost"
|
|
74
|
+
port: int = 16834
|
|
75
|
+
|
|
76
|
+
# Parser Settings
|
|
77
|
+
split_separator: str = ""
|
|
78
|
+
|
|
79
|
+
# Display Settings
|
|
80
|
+
previous_splits: int = 0
|
|
81
|
+
next_splits: int = 2
|
|
82
|
+
font_size: int | float = 20.0
|
|
83
|
+
# Color format: Hex Rgba, NOT argb
|
|
84
|
+
font_color: str = "#000000ff"
|
|
85
|
+
background_color: str = "#f1f8ffff"
|
|
86
|
+
|
|
87
|
+
html_template_folder: Path = DEFAULT_TEMPLATE_FOLDER
|
|
88
|
+
css_folder: Path = DEFAULT_STATIC_FOLDER
|
|
89
|
+
|
|
90
|
+
# Default to desktop versions
|
|
91
|
+
html_template_file: str = "desktop.html"
|
|
92
|
+
css_file: str = "desktop.css"
|
|
93
|
+
|
|
94
|
+
notes_folder: str = USER_PATH
|
|
95
|
+
|
|
96
|
+
# Hotkey Settings
|
|
97
|
+
hotkeys_enabled: bool = False
|
|
98
|
+
increase_offset_hotkey: None | Hotkey = None
|
|
99
|
+
decrease_offset_hotkey: None | Hotkey = None
|
|
100
|
+
|
|
101
|
+
def __prefab_post_init__(
|
|
102
|
+
self,
|
|
103
|
+
output_file,
|
|
104
|
+
html_template_folder,
|
|
105
|
+
css_folder,
|
|
106
|
+
increase_offset_hotkey,
|
|
107
|
+
decrease_offset_hotkey,
|
|
108
|
+
):
|
|
109
|
+
self.output_file = Path(output_file)
|
|
110
|
+
self.html_template_folder = Path(html_template_folder)
|
|
111
|
+
self.css_folder = Path(css_folder)
|
|
112
|
+
|
|
113
|
+
self.increase_offset_hotkey = hotkey_or_none(increase_offset_hotkey)
|
|
114
|
+
self.decrease_offset_hotkey = hotkey_or_none(decrease_offset_hotkey)
|
|
115
|
+
|
|
116
|
+
def save(self):
|
|
117
|
+
"""
|
|
118
|
+
Save settings as JSON
|
|
119
|
+
"""
|
|
120
|
+
|
|
121
|
+
def json_default(o):
|
|
122
|
+
if is_prefab_instance(o):
|
|
123
|
+
return as_dict(o)
|
|
124
|
+
elif isinstance(o, Path):
|
|
125
|
+
return str(o)
|
|
126
|
+
else:
|
|
127
|
+
raise TypeError(
|
|
128
|
+
f"Object of type {o.__class__} is not JSON Serializable"
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
json_str = json.dumps(
|
|
132
|
+
self,
|
|
133
|
+
default=json_default,
|
|
134
|
+
indent=2,
|
|
135
|
+
)
|
|
136
|
+
self.output_file.write_text(json_str)
|
|
137
|
+
|
|
138
|
+
# noinspection PyArgumentList
|
|
139
|
+
@classmethod
|
|
140
|
+
def load(cls, input_filename: None | str | Path = None):
|
|
141
|
+
"""
|
|
142
|
+
Load settings from a file, if the file does not exist
|
|
143
|
+
just use defaults
|
|
144
|
+
|
|
145
|
+
:param input_filename: Saved settings file.
|
|
146
|
+
:return:
|
|
147
|
+
"""
|
|
148
|
+
if input_filename is None:
|
|
149
|
+
input_filename = cls.SETTINGS_FILE
|
|
150
|
+
input_path = Path(input_filename)
|
|
151
|
+
|
|
152
|
+
if input_path.exists():
|
|
153
|
+
new_settings = json.loads(input_path.read_text())
|
|
154
|
+
|
|
155
|
+
valid_attribs = {k for k, v in get_attributes(cls).items() if v.init}
|
|
156
|
+
|
|
157
|
+
# Filter settings from new_settings so there are no invalid keys
|
|
158
|
+
filtered_settings = {
|
|
159
|
+
k: v for k, v in new_settings.items()
|
|
160
|
+
if k in valid_attribs
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
loaded_settings = cls(output_file=input_path, **filtered_settings)
|
|
164
|
+
|
|
165
|
+
# Check that the templates exist, reset otherwise
|
|
166
|
+
# This will happen if the executable folder is moved
|
|
167
|
+
# Absolute path ends up getting used because otherwise launching
|
|
168
|
+
# from an external folder doesn't work
|
|
169
|
+
loaded_settings.fix_template_paths()
|
|
170
|
+
|
|
171
|
+
return loaded_settings
|
|
172
|
+
else:
|
|
173
|
+
return cls(output_file=input_path)
|
|
174
|
+
|
|
175
|
+
def fix_template_paths(self):
|
|
176
|
+
if not self.full_template_path.exists():
|
|
177
|
+
self.html_template_folder = DEFAULT_TEMPLATE_FOLDER
|
|
178
|
+
self.html_template_file = self.default_template_filename
|
|
179
|
+
if not self.full_css_path.exists():
|
|
180
|
+
self.css_folder = DEFAULT_STATIC_FOLDER
|
|
181
|
+
self.css_file = self.default_css_filename
|
|
182
|
+
|
|
183
|
+
@property
|
|
184
|
+
def full_template_path(self):
|
|
185
|
+
return self.html_template_folder / self.html_template_file
|
|
186
|
+
|
|
187
|
+
@property
|
|
188
|
+
def full_css_path(self):
|
|
189
|
+
return self.css_folder / self.css_file
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
@prefab
|
|
193
|
+
class DesktopSettings(BaseSettings):
|
|
194
|
+
"""
|
|
195
|
+
Global persistent settings handler
|
|
196
|
+
"""
|
|
197
|
+
|
|
198
|
+
# Class variables
|
|
199
|
+
SETTINGS_FILE: ClassVar[Path] = DESKTOP_SETTINGS_FILE
|
|
200
|
+
default_template_filename: ClassVar[str] = "desktop.html"
|
|
201
|
+
default_css_filename: ClassVar[str] = "desktop.css"
|
|
202
|
+
|
|
203
|
+
# What file to use
|
|
204
|
+
output_file: Path = attribute(default=DESKTOP_SETTINGS_FILE, serialize=False)
|
|
205
|
+
|
|
206
|
+
# Override Defaults
|
|
207
|
+
html_template_file: str = "desktop.html"
|
|
208
|
+
css_file: str = "desktop.css"
|
|
209
|
+
|
|
210
|
+
# Window Settings
|
|
211
|
+
on_top: bool = False
|
|
212
|
+
transparency: bool = False
|
|
213
|
+
width: int = 800
|
|
214
|
+
height: int = 800
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
@prefab
|
|
218
|
+
class ServerSettings(BaseSettings):
|
|
219
|
+
# Class variables
|
|
220
|
+
SETTINGS_FILE: ClassVar[Path] = SERVER_SETTINGS_FILE
|
|
221
|
+
default_template_filename: ClassVar[str] = "server.html"
|
|
222
|
+
default_css_filename: ClassVar[str] = "server.css"
|
|
223
|
+
|
|
224
|
+
output_file: Path = attribute(default=SERVER_SETTINGS_FILE, serialize=False)
|
|
225
|
+
|
|
226
|
+
# Override defaults
|
|
227
|
+
html_template_file: str = "server.html"
|
|
228
|
+
css_file: str = "server.css"
|
|
229
|
+
|
|
230
|
+
server_hostname: str = LOCAL_HOSTNAME
|
|
231
|
+
server_port: int = 8000
|