22
33from db import get_data_path
44
5- PATH_JSON = get_data_path () / "settings.json"
6- PATH_LAST_GAME = get_data_path () / "last_games.txt"
75
8-
9- def create_json ():
10- if not PATH_JSON .exists ():
11- default_settings = {
12- "theme" : "dark"
13- }
14- with open (PATH_JSON , 'w' , encoding = 'utf-8' ) as file :
15- json .dump (default_settings , file , indent = 4 , ensure_ascii = False )
16-
17-
18- def create_txt ():
19- if not PATH_LAST_GAME .exists ():
20- with open (PATH_LAST_GAME , 'w' , encoding = 'utf-8' ) as file :
6+ class TXTEditor :
7+ def __init__ (self ):
8+ self .path = get_data_path () / "last_games.txt"
9+
10+ def create_txt (self ):
11+ if not self .path .exists ():
12+ with open (self .path , 'w' , encoding = 'utf-8' ) as file :
13+ file .write ('' )
14+
15+ def get_last_games (self ):
16+ try :
17+ with open (self .path , 'r' , encoding = 'utf-8' ) as file :
18+ file_data = file .read ()
19+ return file_data .split ("\n " )
20+ except FileNotFoundError :
21+ return []
22+
23+ def add_game_to_history (self , game_name ):
24+ current_games = self .get_last_games ()
25+ current_games = [game for game in current_games if
26+ game != game_name and game .strip ()]
27+ current_games .insert (0 , game_name )
28+ current_games = current_games [:5 ]
29+ self .save_last_games (current_games )
30+
31+ def save_last_games (self , games_list ):
32+ with open (self .path , 'w' , encoding = 'utf-8' ) as file :
33+ for game in games_list :
34+ if game .strip ():
35+ file .write (game + '\n ' )
36+
37+ def clear_game_history (self ):
38+ with open (self .path , 'w' , encoding = 'utf-8' ) as file :
2139 file .write ('' )
2240
2341
24- def get_theme ():
25- try :
26- with open (PATH_JSON , 'r' , encoding = 'utf-8' ) as file :
27- settings = json .load (file )
28- return settings .get ("theme" ,
29- "dark" )
30- except (FileNotFoundError , json .JSONDecodeError ):
31- return "dark"
32-
33-
34- def get_last_games ():
35- try :
36- with open (PATH_LAST_GAME , 'r' , encoding = 'utf-8' ) as file :
37- file_data = file .read ()
38- return file_data .split ("\n " )
39- except FileNotFoundError :
40- return []
41-
42-
43- def update_setting (key , value ):
44- try :
45- with open (PATH_JSON , 'r' , encoding = 'utf-8' ) as file :
46- settings = json .load (file )
47- except (FileNotFoundError , json .JSONDecodeError ):
48- settings = {}
49-
50- settings [key ] = value
42+ class JSONEditor :
43+ def __init__ (self ):
44+ self .path = get_data_path () / "settings.json"
5145
52- with open (PATH_JSON , 'w' , encoding = 'utf-8' ) as file :
53- json .dump (settings , file , indent = 4 , ensure_ascii = False )
46+ def create_json (self ):
47+ if not self .path .exists ():
48+ default_settings = {
49+ "theme" : "dark"
50+ }
51+ with open (self .path , 'w' , encoding = 'utf-8' ) as file :
52+ json .dump (default_settings , file , indent = 4 , ensure_ascii = False )
5453
54+ def get_theme (self ):
55+ try :
56+ with open (self .path , 'r' , encoding = 'utf-8' ) as file :
57+ settings = json .load (file )
58+ return settings .get ("theme" ,
59+ "dark" )
60+ except (FileNotFoundError , json .JSONDecodeError ):
61+ return "dark"
5562
56- def add_game_to_history (game_name ):
57- current_games = get_last_games ()
58- current_games = [game for game in current_games if
59- game != game_name and game .strip ()]
60- current_games .insert (0 , game_name )
61- current_games = current_games [:5 ]
62- save_last_games (current_games )
63+ def update_setting (self , key , value ):
64+ try :
65+ with open (self .path , 'r' , encoding = 'utf-8' ) as file :
66+ settings = json .load (file )
67+ except (FileNotFoundError , json .JSONDecodeError ):
68+ settings = {}
6369
70+ settings [key ] = value
6471
65- def save_last_games (games_list ):
66- with open (PATH_LAST_GAME , 'w' , encoding = 'utf-8' ) as file :
67- for game in games_list :
68- if game .strip ():
69- file .write (game + '\n ' )
72+ with open (self .path , 'w' , encoding = 'utf-8' ) as file :
73+ json .dump (settings , file , indent = 4 , ensure_ascii = False )
7074
7175
72- def clear_game_history ():
73- with open (PATH_LAST_GAME , 'w' , encoding = 'utf-8' ) as file :
74- file .write ('' )
76+ txt_editor = TXTEditor ()
77+ json_editor = JSONEditor ()
0 commit comments