-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariable_reader_gui.py
More file actions
305 lines (244 loc) · 14.4 KB
/
variable_reader_gui.py
File metadata and controls
305 lines (244 loc) · 14.4 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
import threading
import keyring
# Ensure 'variable_reader_engine.py' is in the same directory.
from variable_reader_engine import read_from_ssh_server, read_from_database
# Helper class for placeholder text in Entry widgets
class PlaceholderEntry(ttk.Entry):
def __init__(self, container, placeholder, *args, show=None, **kwargs):
super().__init__(container, *args, style="Placeholder.TEntry", **kwargs)
self.placeholder = placeholder
self.show_char = show
self.bind("<FocusIn>", self._focus_in)
self.bind("<FocusOut>", self._focus_out)
self.put_placeholder()
def put_placeholder(self):
self.insert(0, self.placeholder)
self.config(style="Placeholder.TEntry", show="")
def _focus_in(self, *args):
if self.get() == self.placeholder:
self.delete('0', 'end')
self.config(style="TEntry")
if self.show_char:
self.config(show=self.show_char)
def _focus_out(self, *args):
if not self.get():
if self.show_char:
self.config(show="")
self.put_placeholder()
# Main Application Class
class IntegratedReaderApp:
def __init__(self, root):
self.root = root
self.root.title("Variable Reader - Integrated Control Panel")
self.root.geometry("1100x700")
style = ttk.Style(root)
style.configure("Placeholder.TEntry", foreground="grey")
self.sources = {}
# Main Frame
main_frame = ttk.Frame(root, padding="10")
main_frame.pack(fill="both", expand=True)
self._create_widgets(main_frame)
self.load_all_credentials()
def _create_widgets(self, parent):
# --- Search Parameters ---
search_frame = ttk.LabelFrame(parent, text="Global Search Parameters", padding="10")
search_frame.pack(fill="x", pady=5)
ttk.Label(search_frame, text="Site Name (for 'Surveys' only):").grid(row=0, column=0, padx=5, pady=5, sticky="w")
self.site_entry = ttk.Entry(search_frame)
self.site_entry.grid(row=0, column=1, padx=5, pady=5, sticky="ew")
ttk.Label(search_frame, text="Subject ID:").grid(row=1, column=0, padx=5, pady=5, sticky="w")
self.subject_entry = ttk.Entry(search_frame)
self.subject_entry.grid(row=1, column=1, padx=5, pady=5, sticky="ew")
ttk.Label(search_frame, text="Variable Name(s) (comma separated):").grid(row=2, column=0, padx=5, pady=5, sticky="w")
self.variable_entry = ttk.Entry(search_frame)
self.variable_entry.grid(row=2, column=1, padx=5, pady=5, sticky="ew")
search_frame.columnconfigure(1, weight=1)
# --- Data Sources Area ---
sources_frame = ttk.LabelFrame(parent, text="Data Sources", padding="10")
sources_frame.pack(fill="both", expand=True, pady=10)
# --- Source Group 1 (e.g., Network A) ---
ttk.Label(sources_frame, text="Network A:").grid(row=0, column=0, padx=5, pady=10, sticky="nw")
user_entry_a = PlaceholderEntry(sources_frame, "Enter User ID", width=20)
user_entry_a.grid(row=0, column=1, padx=5, pady=10, sticky="n")
find_button_a = ttk.Button(sources_frame, text="Find from Surveys", command=lambda: self.find_value("NetworkA", "Surveys"))
find_button_a.grid(row=0, column=2, padx=10, pady=10, sticky="n")
text_widget_a = self.create_result_box(sources_frame, row=0, col=3, colspan=4)
self.sources["NetworkA"] = {"user": user_entry_a, "pw": None, "result_widget": text_widget_a, "remember": None, "auth_type": "user_only"}
# --- Source Group 2 (e.g., Network B) ---
ttk.Label(sources_frame, text="Network B:").grid(row=1, column=0, padx=5, pady=10, sticky="nw")
ttk.Label(sources_frame, text="Uses SSH Key").grid(row=1, column=1, padx=5, pady=10, sticky="n")
find_button_b1 = ttk.Button(sources_frame, text="Find from Surveys", command=lambda: self.find_value("NetworkB", "Surveys"))
find_button_b1.grid(row=1, column=2, padx=10, pady=10, sticky="n")
find_button_b2 = ttk.Button(sources_frame, text="Find from RPMS", command=lambda: self.find_value("NetworkB", "RPMS"))
find_button_b2.grid(row=1, column=3, padx=10, pady=10, sticky="n")
text_widget_b = self.create_result_box(sources_frame, row=1, col=4, colspan=3)
self.sources["NetworkB"] = {"user": None, "pw": None, "result_widget": text_widget_b, "remember": None, "auth_type": "key_only"}
# --- Source Group 3 (e.g., Internal Server) ---
ttk.Label(sources_frame, text="Internal Server:").grid(row=2, column=0, padx=5, pady=10, sticky="nw")
user_entry_c = PlaceholderEntry(sources_frame, "Enter User ID", width=20)
pw_entry_c = PlaceholderEntry(sources_frame, "Enter Password", width=20, show="*")
remember_var_c = tk.BooleanVar()
remember_check_c = ttk.Checkbutton(sources_frame, text="Remember", variable=remember_var_c)
user_entry_c.grid(row=2, column=1, padx=5, pady=10, sticky="n")
pw_entry_c.grid(row=2, column=2, padx=5, pady=10, sticky="n")
remember_check_c.grid(row=2, column=3, padx=5, pady=10, sticky="n")
find_button_c1 = ttk.Button(sources_frame, text="Find from Surveys", command=lambda: self.find_value("Internal", "Surveys"))
find_button_c1.grid(row=2, column=4, padx=10, pady=10, sticky="n")
find_button_c2 = ttk.Button(sources_frame, text="Find Combined Data (A)", command=lambda: self.find_value("Internal", "Combined_A"))
find_button_c2.grid(row=2, column=5, padx=10, pady=10, sticky="n")
find_button_c3 = ttk.Button(sources_frame, text="Find Combined Data (B)", command=lambda: self.find_value("Internal", "Combined_B"))
find_button_c3.grid(row=2, column=6, padx=10, pady=10, sticky="n")
text_widget_c = self.create_result_box(sources_frame, row=3, col=1, colspan=6)
self.sources["Internal"] = {"user": user_entry_c, "pw": pw_entry_c, "result_widget": text_widget_c, "remember": remember_var_c, "auth_type": "user_pass"}
# --- Source Group 4: Database ---
ttk.Label(sources_frame, text="Database:").grid(row=4, column=0, padx=5, pady=10, sticky="nw")
user_entry_d = PlaceholderEntry(sources_frame, "Enter User ID", width=20)
pw_entry_d = PlaceholderEntry(sources_frame, "Enter Password", width=20, show="*")
remember_var_d = tk.BooleanVar()
remember_check_d = ttk.Checkbutton(sources_frame, text="Remember", variable=remember_var_d)
user_entry_d.grid(row=4, column=1, padx=5, pady=10, sticky="n")
pw_entry_d.grid(row=4, column=2, padx=5, pady=10, sticky="n")
remember_check_d.grid(row=4, column=3, padx=5, pady=10, sticky="n")
find_button_d = ttk.Button(sources_frame, text="Find Value from DB", command=lambda: self.find_value("DB", "DB"))
find_button_d.grid(row=4, column=4, padx=10, pady=10, sticky="n")
text_widget_d = self.create_result_box(sources_frame, row=5, col=1, colspan=6)
self.sources["DB"] = {"user": user_entry_d, "pw": pw_entry_d, "result_widget": text_widget_d, "remember": remember_var_d, "auth_type": "user_pass"}
sources_frame.columnconfigure(7, weight=1)
# --- Action Frame ---
action_frame = ttk.Frame(parent)
action_frame.pack(pady=10)
setup_button = ttk.Button(action_frame, text="Connection Setup Instructions", command=self.show_setup_instructions)
setup_button.pack()
def create_result_box(self, parent, row, col, colspan):
"""Creates a Scrollable Text Widget for results"""
frame = ttk.Frame(parent)
frame.grid(row=row, column=col, columnspan=colspan, padx=5, pady=5, sticky="ew")
text_widget = tk.Text(frame, height=5, width=40, state="disabled", wrap="word", font=("Consolas", 9))
text_widget.pack(side="left", fill="both", expand=True)
scrollbar = ttk.Scrollbar(frame, orient="vertical", command=text_widget.yview)
scrollbar.pack(side="right", fill="y")
text_widget.config(yscrollcommand=scrollbar.set)
return text_widget
def update_result_text(self, widget, text):
"""Updates the text widget in a thread-safe way"""
widget.config(state="normal")
widget.delete("1.0", tk.END)
widget.insert(tk.END, text)
widget.config(state="disabled")
def show_setup_instructions(self):
setup_window = tk.Toplevel(self.root)
setup_window.title("Connection Setup Instructions")
setup_window.geometry("800x600")
instructions = """
Before using the Variable Reader, please ensure you have the correct setup.
[ Network A ]
- Requires SSH key registered with administrator.
- Ensure your institution's VPN is active.
[ Network B ]
- Requires SSH Key configured via ~/.ssh/config.
- Example config:
Host your.network.domain.com
User YOUR_ID
IdentityFile /path/to/key
[ Internal Server & Database ]
- Requires User ID and Password.
- VPN access to your institution's network is mandatory.
"""
instruction_text = tk.Text(setup_window, wrap="word", padx=10, pady=10, font=("Courier New", 10))
instruction_text.insert("1.0", instructions)
instruction_text.config(state="disabled")
instruction_text.pack(expand=True, fill="both")
ttk.Button(setup_window, text="Close", command=setup_window.destroy).pack(pady=10)
def get_service_name(self, source):
return f"VariableReader_{source}"
def save_credentials(self, source, username, password):
if username and password:
service = self.get_service_name(source)
try:
keyring.set_password(service, "last_username", username)
keyring.set_password(service, username, password)
except Exception as e:
print(f"Error saving credentials for {source}: {e}")
def clear_credentials(self, source, username):
if username:
service = self.get_service_name(source)
try:
keyring.delete_password(service, "last_username")
keyring.delete_password(service, username)
except Exception: pass
def load_all_credentials(self):
for source, widgets in self.sources.items():
if widgets.get('auth_type') == "user_pass":
service = self.get_service_name(source)
try:
username = keyring.get_password(service, "last_username")
if username:
password = keyring.get_password(service, username)
if password:
widgets['user'].delete(0, tk.END)
widgets['user'].insert(0, username)
widgets['user'].config(style="TEntry")
widgets['pw'].delete(0, tk.END)
widgets['pw'].insert(0, password)
widgets['pw'].config(style="TEntry", show="*")
widgets['remember'].set(True)
except Exception: pass
def find_value(self, source, search_type):
site = self.site_entry.get()
subject = self.subject_entry.get()
variable = self.variable_entry.get()
user, password = "", ""
source_widgets = self.sources[source]
auth_type = source_widgets["auth_type"]
result_widget = source_widgets["result_widget"]
if search_type == "Surveys" and not (site and site != "Enter Site Name"):
messagebox.showerror("Input Error", "Please fill in the 'Site Name' for a 'Surveys' search.")
return
if not all([subject, variable]):
messagebox.showerror("Input Error", "Please fill in 'Subject ID' and 'Variable Name'.")
return
# Institutional VPN Check
if not messagebox.askokcancel("VPN Check", "Ensure you are connected to the required institutional VPN."):
return
# Authentication
if auth_type in ["user_pass", "user_only"]:
user_widget = source_widgets["user"]
user = user_widget.get() if user_widget.get() != user_widget.placeholder else ""
if not user:
messagebox.showerror("Input Error", f"Please enter a User ID for {source}.")
return
if auth_type == "user_pass":
pw_widget = source_widgets["pw"]
password = pw_widget.get() if pw_widget.get() != pw_widget.placeholder else ""
if not password:
messagebox.showerror("Input Error", f"Please enter a Password for {source}.")
return
if source_widgets["remember"].get():
self.save_credentials(source, user, password)
else:
self.clear_credentials(source, user)
self.update_result_text(result_widget, "Searching...")
self.root.update_idletasks()
threading.Thread(target=self.run_search, args=(source, search_type, site, user, password, subject, variable, result_widget)).start()
def run_search(self, source, search_type, site, user, password, subject, variable, result_widget):
# MASKED HOST ADDRESSES
host_map = {
"NetworkA": "YOUR_NETWORK_A_IP",
"NetworkB": "your.network.b.domain.com",
"Internal": "your.internal.server.com"
}
if source in host_map:
host = host_map.get(source)
result = read_from_ssh_server(source, host, user, password, site, subject, variable, search_type)
elif source == 'DB':
result = read_from_database(user, password, subject, variable)
else:
result = "Error: Invalid source."
self.root.after(0, lambda: self.update_result_text(result_widget, str(result)))
if __name__ == "__main__":
app_root = tk.Tk()
app = IntegratedReaderApp(app_root)
app_root.mainloop()