Skip to content

Commit 56e9825

Browse files
committed
[0.7.1] optimize const path
1 parent e7630c5 commit 56e9825

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

models/window/main_window.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,7 @@ def setup_ui(self, *, is_new: bool = False):
246246
self.logger.info("Constant update disabled.")
247247
app_state.scan_status["const_updated"] = True
248248
elif not app_state.scan_status["const_updated"]:
249-
const_updater = ConstantUpdateWorker(self._login_state,
250-
self._base_path)
249+
const_updater = ConstantUpdateWorker(self._login_state)
251250
self.add_thread(const_updater,
252251
on_finished=const_updater.on_finished)
253252
version_check = VersionCheckerWorker(self._login_state)

models/workers/const/const_update.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# module import
22
from json import loads
33
from os import makedirs
4-
from os.path import join, isdir
4+
from os.path import join, isdir, expanduser, abspath
5+
from platform import system
56

67
# package import
78
from PySide6.QtCore import Slot
@@ -16,13 +17,34 @@
1617

1718

1819
class ConstantUpdateWorker(BaseWorker):
19-
def __init__(self, state: LoginState, base_dir: str):
20+
def __init__(self, state: LoginState):
2021
super().__init__(name="配置更新")
2122
self._state = state
22-
self._base_dir = join(base_dir, "config")
2323
self.logger = get_logger(self.__class__.__name__)
24+
self._get_const_path()
2425
self._session.cookies.clear()
2526

27+
def _get_const_path(self, *, is_makedir: bool = True) -> None:
28+
if (_arch := system()) == "Windows":
29+
try:
30+
self._base_dir = abspath(__compiled__.containing_dir)
31+
except NameError:
32+
self._base_dir = abspath(".")
33+
self._base_dir = join(self._base_dir, "config")
34+
self._const_path = join(self._base_dir, "version.json")
35+
elif _arch == "Linux":
36+
self._base_dir = join(expanduser("~"), ".cache", "StartLive",
37+
"config")
38+
self._const_path = join(self._base_dir, "version.json")
39+
elif _arch == "Darwin":
40+
self._base_dir = join(expanduser("~"), "Library",
41+
"Application Support", "StartLive")
42+
self._const_path = join(self._base_dir, "version.json")
43+
else:
44+
raise ValueError("Unsupported system")
45+
if is_makedir:
46+
makedirs(self._base_dir, exist_ok=True)
47+
2648
@Slot()
2749
@run_wrapper
2850
def run(self, /) -> None:
@@ -42,14 +64,11 @@ def run(self, /) -> None:
4264
def _load_from_file(self):
4365
if not isdir(self._base_dir):
4466
return
45-
with open(join(self._base_dir, "version.json"), "r",
46-
encoding="utf-8") as f:
67+
with open(self._const_path, "r", encoding="utf-8") as f:
4768
self._update_const(loads(f.read()))
4869

4970
def _save_to_file(self, response):
50-
makedirs(self._base_dir, exist_ok=True)
51-
with open(join(self._base_dir, "version.json"), "w",
52-
encoding="utf-8") as f:
71+
with open(self._const_path, "w", encoding="utf-8") as f:
5372
f.write(dumps(response))
5473

5574
@staticmethod

0 commit comments

Comments
 (0)