/
configuration_display.py
304 lines (241 loc) · 10.1 KB
/
configuration_display.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
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
try:
# Used when executing with Python
from database_maintenance import *
from cache_maintenance import *
from tooltip_creation import *
from metadata_display import *
except ModuleNotFoundError:
# Used when calling as library
from nisaba.database_maintenance import *
from nisaba.cache_maintenance import *
from nisaba.tooltip_creation import *
from nisaba.metadata_display import *
# Import External Libraries
from pathlib import Path
import csv
import yaml
import configparser
import re
# Import TKinter Libraries
from tkinter import *
from tkinter import ttk
from tkinter.ttk import *
from tkinter import scrolledtext
from tkinter import messagebox
class configuration_display(cache_maintenance):
##############################
# Panel Displays #
##############################
def edit_user_frame_displayer(self):
# Displays user metadata in pane two
# Delete Previous Panels and Menus or Create New Window
try:
self.pane_two.destroy()
except (NameError, AttributeError):
pass
# Create User Metadata instance
metadata_window = metadata_display()
# Create Configuration Frame
self.pane_two = ttk.Frame(self.configuration_window)
self.pane_two.place(relx=.55, relwidth=.4, rely =.05, relheight=1)
# Create User Frame
metadata_window.database_metadata_viewer(self.pane_two)
def configuration_textbox_frame_displayer(self,filename):
# Displays configuration file in pane two
# Delete Previous Panels and Menus or Create New Window
try:
self.pane_two.destroy()
except (NameError, AttributeError):
pass
# Create Configuration Frame
self.pane_two = ttk.Frame(self.configuration_window)
self.pane_two.place(relx=.55, relwidth=.4, rely =.05, relheight=1)
# Divider
dividing_label = ttk.Label(self.pane_two, text="Configuration File Editor")
dividing_label.configure(font=(10))
dividing_label.pack(pady = 20)
# Create Loader Frame
self.configuration_textbox_frame = ttk.Frame(self.pane_two)
self.configuration_textbox_frame.pack(side=TOP, fill=X)
self.configuration_textbox =Text(self.configuration_textbox_frame, wrap=WORD)
self.configuration_textbox.pack(expand=YES, fill=BOTH)
self.configuration_textbox.insert("0.0",self.configuration_file_loader(filename))
row = ttk.Frame(self.pane_two)
self.save_button = Button(row, image=self.save_icon, command=(lambda:self.configuration_file_saver(filename)))
save_button_tt = ToolTip(self.save_button, "Save Configuration File",delay=0.01)
self.save_button.pack(side=LEFT)
row.pack(pady=10)
def configurations_frame_displayer(self):
# Loads non-database parameters from Configuration File
# Create Configuration Frame
self.configurations_frame = ttk.Frame(self.pane_one)
# Create List to Hold Widget Pointers (for save)
self.parameter_entries = []
# Display all variable (v_) items in the config file
for key,value in self.config.items():
if key.startswith('v_'):
# Format Label from Key
label_text = re.sub('_',' ',key[1:]).title() + ':'
# Create Row
row = ttk.Frame(self.pane_one)
label =ttk.Label(row, text=label_text, anchor='w', width=30)
entry = ttk.Entry(row)
entry.insert(0,value)
row.pack(side=TOP, fill=X, pady=3)
label.pack(side=LEFT)
entry.pack(side=RIGHT, expand=YES, fill=X)
# Save Label and Pointer
self.parameter_entries.append([key,entry])
# Divider
dividing_label = ttk.Label(self.pane_one, text="Metadata Configuration")
dividing_label.configure(font=(10))
dividing_label.pack(pady = 20)
# Load up Additional Config Files
standard_directories = ['collections','items','segments','users']
directories = []
self.path_dictionary = {}
# Looks up all available configuration directories
for root, dirs, files in os.walk(Path(self.config_path)):
for dir in dirs:
if dir in standard_directories: pass
else: directories.append(dir)
def config_dropdown_displayer():
# Looks up all available type YAML files
types = []
for root, dirs, files in os.walk(Path(self.config_path) / directory):
for file in files:
if file.endswith(".yaml") or file.endswith(".txt"):
filename = re.sub('(?!^)([A-Z]+)', r' \1',os.path.splitext(file)[0])
types.append(filename)
self.path_dictionary[filename] = Path(self.config_path) / directory / file
if len(types) != 0:
types.insert(0,'Choose a Configuration File')
default_type = StringVar(self.pane_one)
# Create Question Row
row = ttk.Frame(self.pane_one)
new_button = Button(row, text="New", command=self.configuration_yaml_creator)
label =ttk.Label(row, text=directory.title() + ':', anchor='w', width=30)
label.pack(side=LEFT)
dropdown = ttk.OptionMenu(row, default_type, *types, command=self.configuration_textbox_frame_displayer)
dropdown.pack(side=LEFT)
new_button.pack(side=RIGHT)
row.pack(side=TOP, fill=X)
# Enlarge Option Menu
self.pane_one.update()
if self.configuration_window.winfo_screenwidth() > 1100:
window_width = row.winfo_width()*.21 - new_button.winfo_width() - 5
else:
window_width = self.configuration_window.winfo_screenwidth()*.05 - new_button.winfo_width() - 5
dropdown.config(width=int(window_width))
for directory in standard_directories:
config_dropdown_displayer()
# Divider
dividing_label = ttk.Label(self.pane_one, text="Additional Configuration Options")
dividing_label.configure(font=(10))
dividing_label.pack(pady = 20)
for directory in directories:
config_dropdown_displayer()
self.button_frame = ttk.Frame(self.pane_one)
self.save_button = Button(self.button_frame, image=self.save_icon, command=self.configuration_defaults_saver)
save_button_tt = ToolTip(self.save_button, "Save Configuration File",delay=0.01)
self.save_button.pack()
self.configurations_frame.pack(pady=5)
self.button_frame.pack(anchor=NW)
def default_taxonomy_frame_displayer(self):
# Creates Fields for Default Taxonomies
# Create Taxonomy Loader Frame
self.taxonomy_loader_frame = ttk.Frame(self.pane_one)
self.taxonomy_loader_frame.pack(side=TOP, fill=X)
# Create Taxonomy Levels
self.taxonomy_levels = ['Collection','Item','Segment']
for level in self.taxonomy_levels:
# Create Taxonmy Loader Row
row = ttk.Frame(self.taxonomy_loader_frame)
row.pack(side=TOP, fill=X)
label =ttk.Label(row, text="Default " + level + " Taxonomy: ", anchor='w', width=30)
label.pack(side=LEFT)
# Load Default Taxonomy Paths
if level == 'Collection':
self.collection_entry = ttk.Entry(row)
self.collection_entry.insert(0,self.taxonomy_level_defaults[0])
self.collection_entry.pack(side=LEFT, expand=YES, fill=X)
button = Button(row, text="Load", command=(lambda: self.database_loader("Collection",self.default_database_panels_displayer)))
if level == 'Item':
self.item_entry = ttk.Entry(row)
self.item_entry.insert(0,self.taxonomy_level_defaults[1])
self.item_entry.pack(side=LEFT, expand=YES, fill=X)
button = Button(row, text="Load" , command=(lambda: self.database_loader("Item",self.default_database_panels_displayer)))
if level == 'Segment':
self.segment_entry = ttk.Entry(row)
self.segment_entry.insert(0,self.taxonomy_level_defaults[2])
self.segment_entry.pack(side=LEFT, expand=YES, fill=X)
button = Button(row, text="Load" , command=(lambda: self.database_loader("Segment",self.default_database_panels_displayer)))
button.pack(side=RIGHT)
# Load other parameters
self.configurations_frame_displayer()
def user_dropdown_displayer(self):
# Creates Dropdown Menu of Possible Users
# Retrieve Existing User List
users =[]
self.current_user = StringVar(self.configuration_window)
try:
for key,value in self.database['users'].items():
users.append(key)
if value['default'] == 1:
self.current_user.set(key)
users.append(key)
except(KeyError):
label = ttk.Label(self.pane_one,text="Invalid Database File")
label.pack()
else:
# Create Default User Row
row = ttk.Frame(self.pane_one)
row.pack(side=TOP, fill=X)
label =ttk.Label(row, text="Default User: ", anchor='w', width=30)
label.pack(side=LEFT)
edit_button = Button(row, text="Edit Users", command=self.edit_user_frame_displayer)
refresh_button = Button(row, text="Refresh Users", command=self.default_database_panels_displayer)
edit_button.pack(side=RIGHT)
refresh_button.pack(side=RIGHT)
# Create Dropdown Menu
users_menu = OptionMenu(row, self.current_user, *users)
# Display Selection ttk.Frame
users_menu.pack(anchor=W)
self.default_taxonomy_frame_displayer()
def default_database_panels_displayer(self):
# Delete Previous Panels and Menus or Create New Window
try:
self.pane_one.destroy()
self.pane_two.destroy()
except (NameError, AttributeError):
pass
# Create Configuration Frame
self.pane_one = ttk.Frame(self.configuration_window)
self.pane_one.place(relwidth=.5, relheight=1, rely =.05)
self.pane_two = ttk.Frame(self.configuration_window)
self.pane_two.place(relx=.5, relwidth=.4, rely =.05, relheight=1)
dividing_label = ttk.Label(self.pane_one, text="Database Configuration")
dividing_label.configure(font=(10))
dividing_label.pack(pady = 10)
# Create Database Loader Row
row = ttk.Frame(self.pane_one)
label =ttk.Label(row, text="Database: ", anchor='w', width=30)
entry = ttk.Entry(row)
entry.insert(0,self.current_database)
button = Button(row, text="Load", command=(lambda: self.database_loader('d',self.default_database_panels_displayer)))
new_button = Button(row, text="New", command=self.database_creator)
row.pack(side=TOP, fill=X)
label.pack(side=LEFT)
button.pack(side=RIGHT)
new_button.pack(side=RIGHT)
entry.pack(side=RIGHT, expand=YES, fill=X)
self.user_dropdown_displayer()
##############################
# Main #
##############################
def configuration_viewer(self,window):
# Display Configuration Panels
self.save_icon=PhotoImage(file=Path(self.assets_path) / 'save.png')
self.configuration_window = window
self.configuration_window.pack_forget
self.default_database_panels_displayer()