-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathsettings_gui.py
117 lines (98 loc) · 4.69 KB
/
settings_gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import json
import tkinter as tk
from pathlib import Path
from tkinter import ttk, messagebox
class EngineSettingsGUI:
def __init__(self, engine_name: str):
self.config = dict()
self.cfg_file = Path(engine_name + ".json")
if self.cfg_file.exists():
self.config = json.loads(self.cfg_file.read_text())
self.window = tk.Tk()
self.window.title(engine_name.capitalize() + " Settings")
mainframe = ttk.Frame(self.window, padding="10")
mainframe.grid(column=0, row=0, sticky=tk.N)
self.window.columnconfigure(0, weight=1)
self.window.rowconfigure(0, weight=1)
self.username = tk.StringVar(value=self.config.get("username", ""))
self.password = tk.StringVar(value=self.config.get("password", ""))
self.proxy_http = tk.StringVar(
value=self.config.get("proxies", dict()).get("http", "")
)
self.proxy_https = tk.StringVar(
value=self.config.get("proxies", dict()).get("https", "")
)
self.date = tk.BooleanVar(value=self.config.get("torrentDate", True))
self.magnet = tk.BooleanVar(value=self.config.get("magnet", False))
self.proxy = tk.BooleanVar(value=self.config.get("proxy", False))
ttk.Label(mainframe, text="Username:").grid(
column=0, row=0, sticky=tk.W)
ttk.Label(mainframe, text="Password:").grid(
column=0, row=1, sticky=tk.W, rowspan=2)
ttk.Entry(mainframe, width=25, textvariable=self.username, state=(
("!" if self.config.get("username") else "") + tk.DISABLED)
).grid(column=1, row=0, sticky=tk.EW, padx=(0, 5))
ttk.Entry(mainframe, width=25, textvariable=self.password, state=(
("!" if self.config.get("password") else "") + tk.DISABLED)
).grid(column=1, row=1, rowspan=2, sticky=tk.EW, padx=(0, 5))
ttk.Checkbutton(
mainframe, text="Date before torrent", variable=self.date,
onvalue=True
).grid(column=2, row=0, sticky=tk.W)
ttk.Checkbutton(
mainframe, text="Use magnet link", variable=self.magnet,
onvalue=True, state=(
("!" if self.config.get("magnet") else "") + tk.DISABLED)
).grid(column=2, row=1, sticky=tk.W)
ttk.Checkbutton(
mainframe, text="Proxy", variable=self.proxy, onvalue=True,
command=self.proxy_action
).grid(column=2, row=2, sticky=tk.W)
ttk.Label(mainframe, text="HTTP:").grid(column=0, row=3, sticky=tk.W)
ttk.Label(mainframe, text="HTTPS:").grid(column=0, row=4, sticky=tk.W)
proxy_state = tk.NORMAL if self.proxy.get() else tk.DISABLED
self.http_entry = ttk.Entry(
mainframe, textvariable=self.proxy_http, state=proxy_state
)
self.http_entry.grid(column=1, row=3, sticky=tk.EW,
padx=(0, 5), pady=(0, 5))
self.https_entry = ttk.Entry(
mainframe, textvariable=self.proxy_https, state=proxy_state
)
self.https_entry.grid(column=1, row=4, sticky=tk.EW, padx=(0, 5))
ttk.Button(
mainframe, text="Save", command=self.close
).grid(column=2, row=3, rowspan=2)
self.window.mainloop()
def proxy_action(self) -> None:
state = ("!" if self.proxy.get() else "") + tk.DISABLED
self.http_entry.state([state])
self.https_entry.state([state])
def close(self) -> None:
if self.config.get("username") and self.config.get("password"):
if not (self.username.get() or self.password.get()):
messagebox.showinfo("Error", "Some fields is empty!")
return None
if self.proxy.get() and not (self.http_entry.get()
or self.https_entry.get()):
messagebox.showinfo("Error", "Some fields is empty!")
return None
if self.config.get("username") and self.config.get("password"):
self.config["username"] = self.username.get()
self.config["password"] = self.password.get()
self.config["proxy"] = self.proxy.get()
if self.config["proxy"]:
self.config["proxies"] = {
"http": self.http_entry.get(),
"https": self.https_entry.get()
}
self.config["torrentDate"] = self.date.get()
if self.config.get("magnet"):
self.config["magnet"] = self.magnet.get()
self.cfg_file.write_text(
json.dumps(self.config, indent=4, sort_keys=False)
)
self.window.destroy()
if __name__ == "__main__":
settings = EngineSettingsGUI("engines/kinozal")
print(settings.config)