@@ -97,6 +97,8 @@ def get_files(self):
97
97
def load_json_schemas (self , schema_dir ):
98
98
schemas = {}
99
99
# todo: check if folder exist
100
+ if not os .path .isdir (schema_dir ):
101
+ return schemas
100
102
for file in os .listdir (schema_dir ):
101
103
if file .endswith ('.json' ):
102
104
with open (os .path .join (schema_dir , file )) as schema_file :
@@ -173,57 +175,42 @@ def show_json_dialog(self, json_data, schema, file_path):
173
175
dialog = ctk .CTkToplevel (self .root )
174
176
dialog .title (schema .get ('title' , 'JSON Data' ))
175
177
176
- # Create a canvas and a scrollbar
177
- canvas = ctk .CTkCanvas (dialog )
178
- scrollbar = ctk .CTkScrollbar (dialog , command = canvas .yview )
179
- scrollable_frame = ctk .CTkFrame (canvas )
180
-
181
- # Configure canvas
182
- canvas .create_window ((0 , 0 ), window = scrollable_frame , anchor = "nw" )
183
- canvas .configure (yscrollcommand = scrollbar .set )
184
-
185
- def on_configure (event ):
186
- canvas .configure (scrollregion = canvas .bbox ("all" ))
187
-
188
- scrollable_frame .bind ("<Configure>" , on_configure )
189
-
190
178
self .widget_references = {}
191
179
192
180
row = 0
193
181
for prop , details in schema .get ('properties' , {}).items ():
194
- label = ctk .CTkLabel (scrollable_frame , text = prop )
195
- label .grid (row = row , column = 0 , sticky = 'ew' , padx = 10 , pady = 5 )
182
+ label = ctk .CTkLabel (dialog , text = prop )
183
+ label .grid (row = row , column = 0 , padx = 10 , pady = 5 )
196
184
197
185
value = json_data .get (prop , '' )
198
186
199
187
if isinstance (value , dict ):
200
188
nested_row = 0
201
- frame = ctk .CTkFrame (scrollable_frame )
189
+ frame = ctk .CTkFrame (dialog )
202
190
frame .grid (row = row , column = 1 , padx = 10 , pady = 5 , sticky = 'ew' )
203
191
for nested_prop , nested_details in details ['properties' ].items ():
204
192
nested_label = ctk .CTkLabel (frame , text = nested_prop )
205
- nested_label .grid (row = nested_row , column = 0 , sticky = 'w' , padx = 10 , pady = 2 )
206
- nested_text = ctk .CTkTextbox (frame , height = 60 , wrap = 'word' )
207
- nested_text .insert ('end' , str (value .get (nested_prop , '' )))
193
+ nested_label .grid (row = nested_row , column = 0 , padx = 10 , pady = 2 )
194
+ nested_text = ctk .CTkEntry (frame )
195
+ nested_text .insert (0 , str (value .get (nested_prop , '' )))
208
196
nested_text .grid (row = nested_row , column = 1 , padx = 10 , pady = 2 , sticky = 'ew' )
209
197
frame .grid_columnconfigure (1 , weight = 1 )
198
+ self .widget_references [f"{ prop } .{ nested_prop } " ] = nested_text
210
199
nested_row += 1
211
- self .widget_references [prop ] = nested_text
212
200
else :
213
- text = ctk .CTkTextbox ( scrollable_frame , height = 60 , wrap = 'word' )
214
- text .insert ('end' , str (value ))
201
+ text = ctk .CTkEntry ( dialog )
202
+ text .insert (0 , str (value ))
215
203
text .grid (row = row , column = 1 , padx = 10 , pady = 5 , sticky = 'ew' )
216
- scrollable_frame .grid_columnconfigure (1 , weight = 1 )
217
204
self .widget_references [prop ] = text
218
-
205
+
219
206
row += 1
220
207
221
- # Save button
222
- save_button = ctk .CTkButton (dialog , text = "Save" , command = lambda : self .save_json_data (json_data , schema , file_path ))
223
- save_button .pack ()
208
+ dialog .protocol ("WM_DELETE_WINDOW" , lambda : self .on_dialog_close (dialog , json_data , schema , file_path ))
224
209
225
- canvas .pack (side = "left" , fill = "both" , expand = True )
226
- scrollbar .pack (side = "right" , fill = "y" )
210
+ def on_dialog_close (self , dialog , original_data , schema , file_path ):
211
+ # Attempt to save the data when dialog is closed
212
+ if self .save_json_data (original_data , schema , file_path ):
213
+ dialog .destroy () # Only close the dialog if save was successful
227
214
228
215
def save_json_data (self , original_data , schema , file_path ):
229
216
def parse_input (data , schema_properties , parent = None ):
@@ -238,7 +225,7 @@ def parse_input(data, schema_properties, parent=None):
238
225
widget = self .widget_references .get (widget_key )
239
226
240
227
if widget :
241
- input_value = widget .get ("1.0" , "end-1c" ) # Get text from Text widget
228
+ input_value = widget .get ()
242
229
243
230
# Convert input value to the correct type based on the schema
244
231
if details .get ('type' ) == 'number' :
@@ -267,12 +254,13 @@ def parse_input(data, schema_properties, parent=None):
267
254
# Validate updated data
268
255
try :
269
256
validate (instance = updated_data , schema = schema )
270
- # Write back to file if validation passes
271
257
with open (file_path , 'w' ) as json_file :
272
258
json .dump (updated_data , json_file , indent = 4 )
273
259
# messagebox.showinfo("Success", "Data saved successfully.")
260
+ return True
274
261
except ValidationError as e :
275
262
messagebox .showerror ("Validation Error" , str (e ))
263
+ return False
276
264
277
265
278
266
def save_files (self ):
0 commit comments