|
| 1 | +import json |
| 2 | +import re |
| 3 | +from functools import lru_cache |
| 4 | +from pathlib import Path |
| 5 | +from typing import Any, Dict, List, Optional |
| 6 | +from xml.etree import ElementTree |
| 7 | + |
| 8 | + |
| 9 | +def _annex_attr_path() -> Path: |
| 10 | + """Return path to structured Annex attribute XML.""" |
| 11 | + return Path(__file__).resolve().parents[2] / "AttributeDefinitions.xml" |
| 12 | + |
| 13 | + |
| 14 | +def _parse_attribute_definitions_xml(xml_path: Path) -> Dict[str, Any]: |
| 15 | + root = ElementTree.parse(xml_path).getroot() |
| 16 | + |
| 17 | + activation_group_nodes = root.find("ActivationGroups") |
| 18 | + activation_groups = [ |
| 19 | + ag.attrib.get("Name") |
| 20 | + for ag in ( |
| 21 | + activation_group_nodes.findall("ActivationGroup") |
| 22 | + if activation_group_nodes is not None |
| 23 | + else [] |
| 24 | + ) |
| 25 | + if ag.attrib.get("Name") |
| 26 | + ] |
| 27 | + |
| 28 | + feature_group_nodes = root.find("FeatureGroups") |
| 29 | + feature_groups = [] |
| 30 | + if feature_group_nodes is not None: |
| 31 | + for feature_group in feature_group_nodes.findall("FeatureGroup"): |
| 32 | + feature_groups.append( |
| 33 | + { |
| 34 | + "name": feature_group.attrib.get("Name"), |
| 35 | + "pretty": feature_group.attrib.get("Pretty"), |
| 36 | + "features": [ |
| 37 | + feature.attrib.get("Name") |
| 38 | + for feature in feature_group.findall("Feature") |
| 39 | + if feature.attrib.get("Name") |
| 40 | + ], |
| 41 | + } |
| 42 | + ) |
| 43 | + |
| 44 | + attribute_nodes = root.find("Attributes") |
| 45 | + attributes = [] |
| 46 | + if attribute_nodes is not None: |
| 47 | + for attr in attribute_nodes.findall("Attribute"): |
| 48 | + attributes.append( |
| 49 | + { |
| 50 | + "name": attr.attrib.get("Name"), |
| 51 | + "pretty": attr.attrib.get("Pretty"), |
| 52 | + "activation_group": attr.attrib.get("ActivationGroup"), |
| 53 | + "feature": attr.attrib.get("Feature"), |
| 54 | + "main_attribute": attr.attrib.get("MainAttribute"), |
| 55 | + "physical_unit": attr.attrib.get("PhysicalUnit"), |
| 56 | + "color": attr.attrib.get("Color"), |
| 57 | + "subphysical_units": [ |
| 58 | + { |
| 59 | + "Type": spu.attrib.get("Type"), |
| 60 | + "PhysicalUnit": spu.attrib.get("PhysicalUnit"), |
| 61 | + "PhysicalFrom": spu.attrib.get("PhysicalFrom"), |
| 62 | + "PhysicalTo": spu.attrib.get("PhysicalTo"), |
| 63 | + } |
| 64 | + for spu in attr.findall("SubPhysicalUnit") |
| 65 | + ], |
| 66 | + } |
| 67 | + ) |
| 68 | + |
| 69 | + return { |
| 70 | + "activation_groups": activation_groups, |
| 71 | + "feature_groups": feature_groups, |
| 72 | + "attributes": attributes, |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | +def _build_templates_from_data(data: Dict[str, Any]) -> Dict[str, Any]: |
| 77 | + activation_groups = data.get("activation_groups", []) |
| 78 | + feature_groups = data.get("feature_groups", []) |
| 79 | + |
| 80 | + attributes_exact: Dict[str, Dict[str, Any]] = {} |
| 81 | + attributes_wildcard: List[Dict[str, Any]] = [] |
| 82 | + |
| 83 | + for attr in data.get("attributes", []): |
| 84 | + template = { |
| 85 | + "name": attr.get("name"), |
| 86 | + "pretty": attr.get("pretty"), |
| 87 | + "activation_group": attr.get("activation_group"), |
| 88 | + "feature": attr.get("feature"), |
| 89 | + "main_attribute": attr.get("main_attribute"), |
| 90 | + "physical_unit": attr.get("physical_unit"), |
| 91 | + "color": attr.get("color"), |
| 92 | + "subphysical_units": attr.get("subphysical_units", []), |
| 93 | + } |
| 94 | + name = template["name"] |
| 95 | + if not name: |
| 96 | + continue |
| 97 | + if "(n)" in name or "(m)" in name: |
| 98 | + pattern = re.escape(name) |
| 99 | + pattern = pattern.replace("\\(n\\)", "(?P<n>\\d+)") |
| 100 | + pattern = pattern.replace("\\(m\\)", "(?P<m>\\d+)") |
| 101 | + attributes_wildcard.append( |
| 102 | + { |
| 103 | + **template, |
| 104 | + "pattern": re.compile(f"^{pattern}$"), |
| 105 | + } |
| 106 | + ) |
| 107 | + else: |
| 108 | + attributes_exact[name] = template |
| 109 | + |
| 110 | + return { |
| 111 | + "activation_groups": activation_groups, |
| 112 | + "feature_groups": feature_groups, |
| 113 | + "attributes": { |
| 114 | + "exact": attributes_exact, |
| 115 | + "wildcard": attributes_wildcard, |
| 116 | + }, |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | +@lru_cache(maxsize=1) |
| 121 | +def _load_annex_attribute_templates(): |
| 122 | + """Load Annex attribute definitions from generated module or XML.""" |
| 123 | + try: |
| 124 | + from . import attribute_definitions_data as _attr_data_module |
| 125 | + |
| 126 | + raw_data = getattr(_attr_data_module, "ANNEX_ATTRIBUTE_DEFINITIONS", None) |
| 127 | + except Exception: |
| 128 | + raw_data = None |
| 129 | + |
| 130 | + if raw_data is None: |
| 131 | + raw_data = _parse_attribute_definitions_xml(_annex_attr_path()) |
| 132 | + |
| 133 | + return _build_templates_from_data(raw_data) |
| 134 | + |
| 135 | + |
| 136 | +def generate_attribute_definitions_module( |
| 137 | + xml_path: Optional[Path] = None, output_path: Optional[Path] = None |
| 138 | +) -> Path: |
| 139 | + """ |
| 140 | + Generate a Python module with AttributeDefinitions data for zero-IO startup. |
| 141 | +
|
| 142 | + The generated module exports ANNEX_ATTRIBUTE_DEFINITIONS matching the XML content. |
| 143 | + """ |
| 144 | + source = Path(xml_path) if xml_path is not None else _annex_attr_path() |
| 145 | + target = ( |
| 146 | + Path(output_path) |
| 147 | + if output_path is not None |
| 148 | + else Path(__file__).resolve().parent / "attribute_definitions_data.py" |
| 149 | + ) |
| 150 | + |
| 151 | + data = _parse_attribute_definitions_xml(source) |
| 152 | + # Use repr so generated file is valid Python (no JSON null/true/false). |
| 153 | + content = ( |
| 154 | + "# Auto-generated from AttributeDefinitions XML; do not edit by hand.\n" |
| 155 | + f"# Source: {source.name}\n" |
| 156 | + "ANNEX_ATTRIBUTE_DEFINITIONS = " + repr(data) + "\n" |
| 157 | + ) |
| 158 | + target.write_text(content, encoding="utf-8") |
| 159 | + return target |
0 commit comments