"""Validation helpers for versioned board-viewer annotations.""" from __future__ import annotations import copy from collections.abc import Mapping import re TEMPLATE_VARIABLE = re.compile(r"\$\{([A-Za-z_][A-Za-z0-9_]*)\}") class AnnotationError(ValueError): """Raised when an annotation points outside the parsed board.""" def _substitute_template( value: object, variables: Mapping[str, object], *, context: str, ) -> object: if isinstance(value, str): def replace(match: re.Match[str]) -> str: name = match.group(1) if name not in variables: raise AnnotationError( f"missing template variable {name} for {context}" ) return str(variables[name]) return TEMPLATE_VARIABLE.sub(replace, value) if isinstance(value, list): return [ _substitute_template(item, variables, context=context) for item in value ] if isinstance(value, Mapping): return { str(key): _substitute_template(item, variables, context=context) for key, item in value.items() } return copy.deepcopy(value) def _deep_merge(base: object, override: object) -> object: if isinstance(base, Mapping) and isinstance(override, Mapping): result = {str(key): copy.deepcopy(value) for key, value in base.items()} for key, value in override.items(): name = str(key) result[name] = _deep_merge(result[name], value) if name in result else copy.deepcopy(value) return result return copy.deepcopy(override) def expand_annotation_templates(annotations: Mapping[str, object]) -> dict[str, object]: """Expand versioned repeated annotations without mutating the source mapping.""" result = {str(key): copy.deepcopy(value) for key, value in annotations.items()} templates = annotations.get("annotation_templates", {}) instances = annotations.get("template_instances", {}) touchpoints = annotations.get("touchpoints", {}) if not isinstance(templates, Mapping): raise AnnotationError("annotation_templates must be an object") if not isinstance(instances, Mapping): raise AnnotationError("template_instances must be an object") if not isinstance(touchpoints, Mapping): raise AnnotationError("touchpoints annotation must be an object") expanded_touchpoints = { str(reference): copy.deepcopy(value) for reference, value in touchpoints.items() } for reference_value, instance in instances.items(): reference = str(reference_value) if not isinstance(instance, Mapping): raise AnnotationError(f"template instance {reference} must be an object") template_name = str(instance.get("template") or "") template = templates.get(template_name) if not template_name or not isinstance(template, Mapping): raise AnnotationError( f"unknown annotation template {template_name or ''} for {reference}" ) variables = instance.get("variables", {}) if not isinstance(variables, Mapping): raise AnnotationError(f"template variables {reference} must be an object") expanded = _substitute_template( template, variables, context=f"{reference} via {template_name}", ) explicit = expanded_touchpoints.get(reference, {}) expanded_touchpoints[reference] = _deep_merge(expanded, explicit) result["touchpoints"] = expanded_touchpoints return result def validate_annotation_references( annotations: Mapping[str, object], touchpoints: Mapping[str, Mapping[str, object]] ) -> None: annotated_touchpoints = annotations.get("touchpoints", {}) if not isinstance(annotated_touchpoints, Mapping): raise AnnotationError("touchpoints annotation must be an object") for reference, annotation in annotated_touchpoints.items(): if reference not in touchpoints: raise AnnotationError(f"unknown touchpoint {reference}") if not isinstance(annotation, Mapping): raise AnnotationError(f"touchpoint annotation {reference} must be an object") pins = annotation.get("pins", {}) if not isinstance(pins, Mapping): raise AnnotationError(f"pins annotation {reference} must be an object") actual_pins = touchpoints[reference].get("pins", []) actual_numbers = { str(pin.get("number")) for pin in actual_pins if isinstance(pin, Mapping) and pin.get("number") is not None } for number in pins: if str(number) not in actual_numbers: raise AnnotationError(f"unknown pin {reference}.{number}") connector_profiles = annotations.get("connector_profiles", {}) if not isinstance(connector_profiles, Mapping): raise AnnotationError("connector_profiles annotation must be an object") owners: dict[str, str] = {} for profile_name, profile in connector_profiles.items(): if not isinstance(profile, Mapping): raise AnnotationError(f"connector profile {profile_name} must be an object") references = profile.get("references", []) if not isinstance(references, list): raise AnnotationError(f"connector profile {profile_name} references must be an array") for reference_value in references: reference = str(reference_value) if reference in owners: raise AnnotationError(f"connector {reference} appears in multiple profiles") owners[reference] = str(profile_name) for profile_name, profile in connector_profiles.items(): if not isinstance(profile, Mapping): continue contact_values = profile.get("contact_pins", []) shield_values = profile.get("shield_pads", []) references = profile.get("references", []) if not isinstance(contact_values, list) or not isinstance(shield_values, list): raise AnnotationError(f"connector profile {profile_name} pin classifications must be arrays") contacts = {str(value) for value in contact_values} shields = {str(value) for value in shield_values} overlap = sorted(contacts.intersection(shields)) if overlap: raise AnnotationError( f"connector profile {profile_name} has overlapping contact and shield pads: {', '.join(overlap)}" ) for reference_value in references if isinstance(references, list) else []: reference = str(reference_value) if reference not in touchpoints: raise AnnotationError(f"unknown connector {reference}") touchpoint = touchpoints[reference] expected_component_value = str(profile.get("component_value", profile_name)) if str(touchpoint.get("component_value") or "") != expected_component_value: raise AnnotationError( f"component value mismatch {reference}: " f"{touchpoint.get('component_value')} != {expected_component_value}" ) actual_pins = touchpoint.get("pins", []) actual_numbers = { str(pin.get("number")) for pin in actual_pins if isinstance(pin, Mapping) and str(pin.get("number") or "") } classified = contacts.union(shields) unknown = sorted(classified.difference(actual_numbers)) if unknown: raise AnnotationError(f"unknown classified pads {reference}: {', '.join(unknown)}") unclassified = sorted(actual_numbers.difference(classified)) if unclassified: raise AnnotationError(f"unclassified numbered pads {reference}: {', '.join(unclassified)}")