From 522298a6ec813484afe21617dc334a8ecb06e041 Mon Sep 17 00:00:00 2001 From: rnsrk Date: Mon, 5 May 2025 14:21:54 +0200 Subject: [PATCH] better toggle view --- config_selector.py | 119 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 115 insertions(+), 4 deletions(-) diff --git a/config_selector.py b/config_selector.py index 3de2831..132240b 100755 --- a/config_selector.py +++ b/config_selector.py @@ -80,19 +80,43 @@ class ConfigSelector: exit(1) def copy_matched_files(self) -> None: - """Copy all matched files from original_config to config.""" + """Copy all matched files from original_config to config, removing UUID and _core structures.""" if not os.path.exists(self.config_path): os.makedirs(self.config_path) copied_count = 0 + processed_count = 0 for prefix in self.prefixes: for filename in self.matched_files[prefix]: src = os.path.join(self.original_config_path, filename) dst = os.path.join(self.config_path, filename) - shutil.copy2(src, dst) + + # Read the YAML file + try: + with open(src, 'r') as file: + content = file.read() + + # Process the content to remove UUID and _core structures + # 1. Remove uuid key-value pair (ensuring we start at beginning of line) + content = re.sub(r'^uuid: [a-f0-9\-]+\n', '', content, flags=re.MULTILINE) + + # 2. Remove _core structure (careful not to remove wisski_core or similar) + # Match exactly '_core:' at the start of a line + content = re.sub(r'^_core:\n([ \t]+[^\n]+\n)+', '', content, flags=re.MULTILINE) + + # Write the modified content + with open(dst, 'w') as file: + file.write(content) + + processed_count += 1 + except Exception as e: + print(f"Error processing {filename}: {e}") + # Fall back to direct copy if processing fails + shutil.copy2(src, dst) + copied_count += 1 - print(f"Copied {copied_count} files to {self.config_path}") + print(f"Copied {copied_count} files to {self.config_path} ({processed_count} files were processed to remove UUIDs and _core structures)") def add_prefix(self, prefix: str) -> None: """Add a new prefix to the list if it doesn't exist.""" @@ -694,6 +718,93 @@ def has_child_segments(item, mode, selector): return True return False +def preview_yaml_cleaning(stdscr, content): + """Show a simplified schematic preview of the YAML cleaning process.""" + h, w = stdscr.getmaxyx() + + # Calculate preview window size + preview_h = min(12, h - 8) # Smaller height + preview_w = min(70, w - 4) + preview_y = (h - preview_h) // 2 + preview_x = (w - preview_w) // 2 + + # Create window + preview_win = curses.newwin(preview_h, preview_w, preview_y, preview_x) + preview_win.box() + preview_win.addstr(1, 2, "YAML Cleaning Preview:", curses.A_BOLD) + + # Create simple schematic example + original = """langcode: en +status: true +uuid: cc09dc7f-ec98-4e4e-ae38-4fe7e8676aae +dependencies: + module: + - node +_core: + default_config_hash: fUksROt4FfkAU9BV4hV2XvhTBSS2nTNrZS4U7S-tKrs +id: example +name: Example""" + + cleaned = """langcode: en +status: true +dependencies: + module: + - node +id: example +name: Example""" + + # Display side by side + # Show original + preview_win.addstr(3, 2, "Original:", curses.A_UNDERLINE) + preview_win.addstr(4, 2, "uuid: cc09dc7f-ec98-4e4e-ae38-4fe7e8676aae", curses.A_BOLD) + preview_win.addstr(5, 2, "_core:", curses.A_BOLD) + preview_win.addstr(6, 2, " default_config_hash: fUksROt4F...", curses.A_BOLD) + preview_win.addstr(7, 2, "other YAML content...") + + # Show arrow + arrow_x = preview_w // 2 - 2 + for i in range(3, preview_h - 3): + preview_win.addstr(i, arrow_x, "→") + + # Show cleaned + preview_win.addstr(3, arrow_x + 4, "Cleaned:", curses.A_UNDERLINE) + preview_win.addstr(5, arrow_x + 4, "UUID and _core removed") + preview_win.addstr(7, arrow_x + 4, "other YAML content preserved") + + # Instructions + preview_win.addstr(preview_h - 2, 2, "Press any key to continue...", curses.A_DIM) + preview_win.refresh() + + # Wait for key + stdscr.getch() + +def confirm_yaml_cleaning(stdscr, selector): + """Show a confirmation dialog for YAML cleaning during copy.""" + h, w = stdscr.getmaxyx() + + # Get a sample file to preview + sample_file = None + for prefix in selector.prefixes: + if selector.matched_files[prefix]: + sample_file = os.path.join(selector.original_config_path, selector.matched_files[prefix][0]) + break + + if sample_file and os.path.exists(sample_file): + try: + with open(sample_file, 'r') as file: + content = file.read() + preview_yaml_cleaning(stdscr, content) + except Exception as e: + # In case of error, just show a simple message + error_msg = f"Error reading sample file: {str(e)}" + message_win = curses.newwin(3, min(len(error_msg) + 4, w - 4), (h - 3) // 2, (w - min(len(error_msg) + 4, w - 4)) // 2) + message_win.box() + message_win.addstr(1, 2, error_msg[:w-8]) + message_win.refresh() + message_win.getch() + + return confirm_dialog(stdscr, "Copy all matched files to config directory and remove UUID and _core?") + def main(stdscr): # Set paths relative to current directory json_path = os.path.join(BASE_DIR, "config_prefixes.json") @@ -862,7 +973,7 @@ def main(stdscr): path_history = [] selected_idx = 0 elif key == ord('c') or key == ord('C'): - if confirm_dialog(stdscr, "Copy all matched files to config directory?"): + if confirm_yaml_cleaning(stdscr, selector): selector.copy_matched_files() elif key == ord('s') or key == ord('S'): selector.save_prefixes()