Last active
          October 1, 2025 15:44 
        
      - 
      
 - 
        
Save Axelmdez/1ee24c1ebd8a7466fced2354d0840862 to your computer and use it in GitHub Desktop.  
Revisions
- 
        
Axelmdez revised this gist
Oct 1, 2025 . No changes.There are no files selected for viewing
 - 
        
Axelmdez created this gist
Oct 1, 2025 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,96 @@ import json import os import tkinter as tk from tkinter import filedialog, messagebox CONFIG_FILE = "appsettings-saved-config.json" def load_config(): if os.path.exists(CONFIG_FILE): with open(CONFIG_FILE, "r") as f: return json.load(f) return {} def save_config(config): with open(CONFIG_FILE, "w") as f: json.dump(config, f) def select_file(title, initialfile=None, filetypes=None): root = tk.Tk() root.withdraw() file_path = filedialog.askopenfilename(title=title, initialfile=initialfile, filetypes=filetypes) root.destroy() return file_path def parse_target_text(target_path): with open(target_path, "r") as f: content = f.read() # Split by four newlines blocks = [block.strip() for block in content.split('\n\n\n\n\n') if block.strip()] kv = {} for block in blocks: lines = [line for line in block.split('\n') if line.strip()] if len(lines) >= 2: key = lines[0].strip() value = '\n'.join(lines[1:]).strip() kv[key] = value return kv def dot_notation_to_nested_dict(flat_dict): nested = {} for compound_key, value in flat_dict.items(): keys = compound_key.split('.') d = nested for k in keys[:-1]: if k not in d or not isinstance(d[k], dict): d[k] = {} d = d[k] d[keys[-1]] = value return nested def main(): config = load_config() use_previous = False if config.get("source") and config.get("target"): use_previous = messagebox.askyesno("Reuse?", f"Use previous files?\nSource: {config['source']}\nTarget: {config['target']}") if use_previous: source_path = config["source"] target_path = config["target"] else: source_path = select_file("Select source JSON file", filetypes=[("JSON files", "*.json")]) target_path = select_file("Select target text file", filetypes=[("Text files", "*.txt"), ("All files", "*.*")]) config["source"] = source_path config["target"] = target_path save_config(config) # Step 1: Parse the source JSON file and print its contents with open(source_path, "r") as f: source_json = json.load(f) # print(f"\n ({len(source_json)}) key-value pairs from Source JSON file:") # print(json.dumps(source_json, indent=4)) # Step 2: Parse the target text file and print extracted key-value pairs kv_pairs = parse_target_text(target_path) # Step 3: Convert dot-notated key-value pairs to nested dict and print for verification nested_kv = dot_notation_to_nested_dict(kv_pairs) # print("\nkey-value dict derived from target text file:") # print(json.dumps(nested_kv, indent=4)) # Step 4: Update the source JSON with the nested key-value dict and save the updated JSON file def recursive_update(obj, updates): if isinstance(obj, dict) and isinstance(updates, dict): for k, v in updates.items(): if k in obj and isinstance(obj[k], dict) and isinstance(v, dict): recursive_update(obj[k], v) else: obj[k] = v recursive_update(source_json, nested_kv) with open(source_path, "w") as f: json.dump(source_json, f, indent=2) print(f"Updated source JSON saved to {source_path}") if __name__ == "__main__": main()