11# module import
22from json import loads
33from 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
78from PySide6 .QtCore import Slot
1617
1718
1819class 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