Skip to content

Commit 173a15d

Browse files
committed
refactor: Сделал классы для управления txt и json файлами
1 parent 987936d commit 173a15d

File tree

3 files changed

+72
-67
lines changed

3 files changed

+72
-67
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
ui/add_game_dialog.ui
33
ui/mainWindow.ui
44
venv
5+
main.spec
6+
build
7+
dist

main.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
import dialogs
77
from db import database
88
from ui.mainWindow_ui import Ui_MainWindow
9-
from utils import create_json, get_theme, update_setting, add_game_to_history, \
10-
get_last_games, create_txt
9+
from utils import txt_editor, json_editor
1110

1211

1312
class QtLauncher(QMainWindow, Ui_MainWindow):
@@ -18,12 +17,12 @@ def __init__(self):
1817
self.setupUi(self)
1918
self.update_game_list()
2019

21-
create_txt()
20+
txt_editor.create_txt()
2221
self.update_last_game_list()
2322

24-
create_json()
23+
json_editor.create_json()
2524

26-
with open(f'style/{get_theme()}.qss') as qss:
25+
with open(f'style/{json_editor.get_theme()}.qss') as qss:
2726
self.setStyleSheet(qss.read())
2827

2928
self.add_game.clicked.connect(self.open_dialog)
@@ -39,7 +38,7 @@ def __init__(self):
3938
def set_theme(self, theme):
4039
with open(f'style/{theme}.qss') as qss:
4140
self.setStyleSheet(qss.read())
42-
update_setting("theme", theme)
41+
json_editor.update_setting("theme", theme)
4342

4443
def update_game_list(self):
4544
games = database.get_games()
@@ -48,7 +47,7 @@ def update_game_list(self):
4847
self.list_games.addItem(game)
4948

5049
def update_last_game_list(self):
51-
games = get_last_games()
50+
games = txt_editor.get_last_games()
5251
self.last_games.clear()
5352
for game in games:
5453
self.last_games.addItem(game)
@@ -72,7 +71,7 @@ def delete_game_from_list(self):
7271
def open_game(self, item):
7372
game = database.get_game(item.text())
7473
try:
75-
add_game_to_history(game[1])
74+
txt_editor.add_game_to_history(game[1])
7675
os.startfile(game[2])
7776
except Exception as e:
7877
QMessageBox.warning(self, "Ошибка!", str(e))

utils.py

Lines changed: 62 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,73 +2,76 @@
22

33
from 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

Comments
 (0)