import logging logger = logging.getLogger(__name__) def find_mapping_for_element(svg_type, label_prefix, element_mappings, debug=False): """Find the appropriate element mapping based on SVG type and label prefix. Prioritizes exact prefix match over fallback (no prefix). Args: svg_type (str): The type of the SVG element (e.g., 'rect', 'path'). label_prefix (str): The extracted label prefix (can be empty). element_mappings (list): The list of mapping dictionaries from config. debug (bool): Whether to enable debug logging. Returns: tuple: (dict or None, str) containing the matching mapping dictionary (or None if no match) and the match type ('exact', 'fallback', 'none'). """ exact_match = None fallback_match = None match_type = 'none' # Default match type if debug: logger.debug(f"MAPPER: Looking for mapping: svg_type={svg_type}, label_prefix='{label_prefix}'") logger.debug(f"MAPPER: Available mappings: {len(element_mappings)}") # Optional: Log details of available mappings if needed # for i, m in enumerate(element_mappings): # logger.debug(f" Mapping #{i+1}: svg_type={m.get('svg_type')}, prefix={m.get('label_prefix')}") # First pass: look for an exact match (case-sensitive for now, consider options) if label_prefix: for mapping in element_mappings: # Ensure keys exist before accessing map_svg_type = mapping.get('svg_type', '') map_label_prefix = mapping.get('label_prefix', '') if map_svg_type == svg_type and map_label_prefix == label_prefix: exact_match = mapping if debug: logger.debug(f"MAPPER: Found exact prefix match: {mapping}") break # Second pass: look for a fallback match (empty prefix for the same svg_type) if not exact_match: # Only look for fallback if no exact match was found for mapping in element_mappings: map_svg_type = mapping.get('svg_type', '') map_label_prefix = mapping.get('label_prefix', '') if map_svg_type == svg_type and not map_label_prefix: fallback_match = mapping if debug and not exact_match: # Log fallback only if no exact match was found logger.debug(f"MAPPER: Found fallback match (no prefix): {mapping}") break # Found the fallback for this type, no need to check further # Determine final mapping and match type selected_mapping = None if exact_match: selected_mapping = exact_match match_type = 'exact' elif fallback_match: selected_mapping = fallback_match match_type = 'fallback' # else: match_type remains 'none' if debug and not selected_mapping: logger.debug(f"MAPPER: No mapping found for svg_type={svg_type}, label_prefix='{label_prefix}'") return selected_mapping, match_type # --- Potentially deprecated functions below, kept for reference --- # These might be replaced by directly using find_mapping_for_element # and getting the specific property (e.g., 'element_type') from the result. def get_element_type_for_svg_type_and_label(svg_type, label_prefix, element_mappings, default_type="ia.display.view"): """Get the appropriate element type for an SVG type and label. Uses find_mapping_for_element internally. """ # Use _ to ignore the match_type returned by find_mapping_for_element mapping, _ = find_mapping_for_element(svg_type, label_prefix, element_mappings) if mapping: return mapping.get('element_type', default_type) return default_type def get_element_type_for_svg_type(svg_type, element_type_mapping, default_type="ia.display.view"): """ (Potentially deprecated) Determine the element type based *only* on SVG element type. This logic seems superseded by the prefix-based mapping system. Kept for reference if needed for older config formats or specific use cases. """ # This function relies on a different structure in custom_options ('element_type_mapping') # which might be outdated compared to the list-based 'element_mappings'. logger.warning("get_element_type_for_svg_type might be deprecated, relying on potentially old config format.") if svg_type in element_type_mapping and element_type_mapping[svg_type]: return element_type_mapping[svg_type] return default_type