""" Search for MappingRules in the JSON configuration """ import grpc import sys import io import json sys.path.append(r'C:\DEV\COPILOT\geutebruck-api\src\api') if sys.platform == 'win32': sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8') from protos import configuration_pb2 from protos import configuration_pb2_grpc def find_mapping_rules(node, path=""): """Recursively search for MappingRules folder""" if isinstance(node, dict): # Check if this node is MappingRules if node.get("name") == "MappingRules" and node.get("nodeType") == "folder": return node, path # Check children if this is a folder if "children" in node and isinstance(node["children"], list): for i, child in enumerate(node["children"]): result, result_path = find_mapping_rules(child, f"{path}/{node.get('name', 'unknown')}") if result: return result, result_path return None, "" def find_working_mapping(mapping_rules_node): """Find a working mapping (with CrossSwitch)""" if "children" not in mapping_rules_node: return None for rule in mapping_rules_node["children"]: if rule.get("nodeType") == "folder": # Find the caption (@ field) caption = None if "children" in rule: for field in rule["children"]: if field.get("name") == "@" and field.get("nodeType") == "string": caption = field.get("value") break # Look for CrossSwitch or similar working mappings if caption and ("CrossSwitch" in caption or "C_" in caption): return rule return None print("="*70) print("SEARCHING FOR MAPPING RULES") print("="*70) channel = grpc.insecure_channel('localhost:50051') stub = configuration_pb2_grpc.ConfigurationServiceStub(channel) print("\n1. Exporting configuration as JSON...") export_request = configuration_pb2.ExportJsonRequest() export_response = stub.ExportConfigurationJson(export_request) if not export_response.success: print(f" [ERROR] {export_response.error_message}") sys.exit(1) print(f" JSON size: {export_response.json_size} bytes") config = json.loads(export_response.json_data) print("\n2. Searching for MappingRules folder...") # Search in rootNodes mapping_rules = None if "rootNodes" in config: for node in config["rootNodes"]: result, path = find_mapping_rules(node) if result: mapping_rules = result print(f" Found MappingRules at: {path}/MappingRules") break if not mapping_rules: print(" [ERROR] MappingRules not found!") print("\n Top-level nodes:") if "rootNodes" in config: for i, node in enumerate(config["rootNodes"][:10]): print(f" {i}: {node.get('name', 'unnamed')} ({node.get('nodeType', 'unknown')})") sys.exit(1) print(f"\n3. Found {len(mapping_rules.get('children', []))} rules") # Find a working mapping print("\n4. Looking for working mapping...") working = find_working_mapping(mapping_rules) if not working: print(" [ERROR] No working mapping found!") print("\n Listing all mappings:") for rule in mapping_rules.get("children", []): if rule.get("nodeType") == "folder": caption = None if "children" in rule: for field in rule["children"]: if field.get("name") == "@": caption = field.get("value") break print(f" - Rule {rule.get('name')}: {caption}") sys.exit(1) # Get caption caption = None for field in working.get("children", []): if field.get("name") == "@": caption = field.get("value") break print(f"\n5. Found working mapping: '{caption}'") print(f" Rule ID: {working.get('name')}") print(f"\n6. Complete structure of working mapping:") print(json.dumps(working, indent=2)) print(f"\n7. Field summary:") for field in working.get("children", []): field_type = field.get("nodeType") field_name = field.get("name") if field_type == "folder": num_children = len(field.get("children", [])) print(f" - {field_name}/ (folder with {num_children} children)") # Show children of this folder for child in field.get("children", [])[:5]: # First 5 children child_name = child.get("name") child_type = child.get("nodeType") if child_type == "string": print(f" - {child_name} = \"{child.get('value', '')}\"") elif child_type == "int32": print(f" - {child_name} = {child.get('value', 0)}") elif child_type == "folder": print(f" - {child_name}/ (folder)") elif field_type == "string": print(f" - {field_name} = \"{field.get('value', '')}\"") elif field_type == "int32": print(f" - {field_name} = {field.get('value', 0)}") else: print(f" - {field_name} ({field_type})") print("\n" + "="*70) print("DONE") print("="*70)