-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_utils.py
148 lines (106 loc) · 4.23 KB
/
config_utils.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import json
import tkinter as tk
import ttkbootstrap as tb
from ttkbootstrap.constants import *
from ttkbootstrap.scrolled import ScrolledFrame
def build_config_frame(parent, config_path):
"""
Creates a Frame for modifying the config inside the main window.
"""
frame = tb.Frame(parent, bootstyle="dark")
title_label = tb.Label(frame, text="Modify Config", font=("Helvetica", 18, "bold"))
title_label.pack(pady=10)
try:
with open(config_path, "r") as f:
config = json.load(f)
except FileNotFoundError:
config = {}
container = ScrolledFrame(frame, autohide=True, bootstyle="round")
container.pack(fill="both", expand=True, padx=10, pady=(0, 10))
content = container
entries = {}
for key, val in config.items():
row = tb.Frame(content, bootstyle="dark")
row.pack(fill="x", pady=5)
lbl = tb.Label(row, text=f"{key}:", width=20, anchor="w")
lbl.pack(side="left")
entry = tb.Entry(row)
entry.insert(0, str(val))
entry.pack(side="left", fill="x", expand=True)
entries[key] = entry
def save_config():
for key, entry in entries.items():
config[key] = entry.get()
with open(config_path, "w") as f:
json.dump(config, f, indent=4)
save_button = tb.Button(frame, text="Save", bootstyle="success", command=save_config)
save_button.pack(pady=10)
return frame
def build_urls_frame(parent, urls_file):
"""
Creates a Frame for changing the URL file inside the main window.
Always re-reads the file each time we refresh the list, ensuring the UI
is in sync with the current state of urls.txt.
"""
frame = tb.Frame(parent, bootstyle="dark")
title_label = tb.Label(frame, text="Change URL File", font=("Helvetica", 18, "bold"))
title_label.pack(pady=10)
container = ScrolledFrame(frame, autohide=True, bootstyle="round")
container.pack(fill="both", expand=True, padx=10, pady=(0, 10))
content = container
row_frames = []
def get_urls_from_file():
"""Read the current URLs from disk, stripping empty lines."""
try:
with open(urls_file, "r") as f:
return [line.strip() for line in f if line.strip()]
except FileNotFoundError:
return []
def refresh_list():
for rf in row_frames:
rf.destroy()
row_frames.clear()
loaded_urls = get_urls_from_file()
for i, url in enumerate(loaded_urls):
row = tb.Frame(content, bootstyle="dark")
row.pack(fill="x", pady=3)
row_frames.append(row)
lbl = tb.Label(row, text=url, anchor="w")
lbl.pack(side="left", padx=5, fill="x", expand=True)
delete_btn = tb.Button(
row,
text="Delete Link",
bootstyle="danger",
command=lambda idx=i: remove_url(idx)
)
delete_btn.pack(side="right", padx=5)
def remove_url(index):
loaded_urls = get_urls_from_file()
if 0 <= index < len(loaded_urls):
loaded_urls.pop(index)
with open(urls_file, "w") as f:
for u in loaded_urls:
f.write(u + "\n")
refresh_list()
def add_url():
new_url = new_url_entry.get().strip()
if new_url:
loaded_urls = get_urls_from_file()
loaded_urls.append(new_url)
with open(urls_file, "w") as f:
for u in loaded_urls:
f.write(u + "\n")
new_url_entry.delete(0, "end")
refresh_list()
add_row = tb.Frame(frame, bootstyle="dark")
add_row.pack(fill="x", pady=(0, 10))
add_label = tb.Label(add_row, text="Add New URL:", anchor="w")
add_label.pack(side="left", padx=5)
new_url_entry = tb.Entry(add_row)
new_url_entry.pack(side="left", fill="x", expand=True, padx=5)
new_url_entry.bind("<Return>", lambda event: add_url())
add_button = tb.Button(add_row, text="Add", bootstyle="info", command=add_url)
add_button.pack(side="right", padx=5)
refresh_list()
frame.refresh_list = refresh_list
return frame