|
| 1 | +""" |
| 2 | +Simple SCXML parser that converts SCXML documents to state machine definitions. |
| 3 | +""" |
| 4 | + |
| 5 | +import xml.etree.ElementTree as ET |
| 6 | +from functools import partial |
| 7 | +from typing import Any |
| 8 | +from typing import Dict |
| 9 | +from typing import List |
| 10 | + |
| 11 | +from statemachine.statemachine import StateMachine |
| 12 | + |
| 13 | + |
| 14 | +def send_event(machine: StateMachine, event_to_send: str) -> None: |
| 15 | + machine.send(event_to_send) |
| 16 | + |
| 17 | + |
| 18 | +def assign(model, location, expr): |
| 19 | + pass |
| 20 | + |
| 21 | + |
| 22 | +def strip_namespaces(tree): |
| 23 | + """Remove all namespaces from tags and attributes in place. |
| 24 | +
|
| 25 | + Leaves only the local names in the subtree. |
| 26 | + """ |
| 27 | + for el in tree.iter(): |
| 28 | + tag = el.tag |
| 29 | + if tag and isinstance(tag, str) and tag[0] == "{": |
| 30 | + el.tag = tag.partition("}")[2] |
| 31 | + attrib = el.attrib |
| 32 | + if attrib: |
| 33 | + for name, value in list(attrib.items()): |
| 34 | + if name and isinstance(name, str) and name[0] == "{": |
| 35 | + del attrib[name] |
| 36 | + attrib[name.partition("}")[2]] = value |
| 37 | + |
| 38 | + |
| 39 | +def parse_scxml(scxml_content: str) -> Dict[str, Any]: # noqa: C901 |
| 40 | + """ |
| 41 | + Parse SCXML content and return a dictionary definition compatible with |
| 42 | + create_machine_class_from_definition. |
| 43 | +
|
| 44 | + The returned dictionary has the format: |
| 45 | + { |
| 46 | + "states": { |
| 47 | + "state_id": {"initial": True}, |
| 48 | + ... |
| 49 | + }, |
| 50 | + "events": { |
| 51 | + "event_name": [ |
| 52 | + {"from": "source_state", "to": "target_state"}, |
| 53 | + ... |
| 54 | + ] |
| 55 | + } |
| 56 | + } |
| 57 | + """ |
| 58 | + # Parse XML content |
| 59 | + root = ET.fromstring(scxml_content) |
| 60 | + strip_namespaces(root) |
| 61 | + |
| 62 | + # Find the scxml element (it might be the root or a child) |
| 63 | + scxml = root if "scxml" in root.tag else root.find(".//scxml") |
| 64 | + if scxml is None: |
| 65 | + raise ValueError("No scxml element found in document") |
| 66 | + |
| 67 | + # Get initial state from scxml element |
| 68 | + initial_state = scxml.get("initial") |
| 69 | + |
| 70 | + # Build states dictionary |
| 71 | + states = {} |
| 72 | + events: Dict[str, List[Dict[str, str]]] = {} |
| 73 | + |
| 74 | + def _parse_state(state_elem, final=False): # noqa: C901 |
| 75 | + state_id = state_elem.get("id") |
| 76 | + if not state_id: |
| 77 | + raise ValueError("All states must have an id") |
| 78 | + |
| 79 | + # Mark as initial if specified |
| 80 | + states[state_id] = {"initial": state_id == initial_state, "final": final} |
| 81 | + |
| 82 | + # Process transitions |
| 83 | + for trans_elem in state_elem.findall("transition"): |
| 84 | + event = trans_elem.get("event") or None |
| 85 | + target = trans_elem.get("target") |
| 86 | + |
| 87 | + if target: |
| 88 | + if event not in events: |
| 89 | + events[event] = [] |
| 90 | + |
| 91 | + if target not in states: |
| 92 | + states[target] = {} |
| 93 | + |
| 94 | + events[event].append( |
| 95 | + { |
| 96 | + "from": state_id, |
| 97 | + "to": target, |
| 98 | + } |
| 99 | + ) |
| 100 | + |
| 101 | + for onentry_elem in state_elem.findall("onentry"): |
| 102 | + for raise_elem in onentry_elem.findall("raise"): |
| 103 | + event = raise_elem.get("event") |
| 104 | + if event: |
| 105 | + state = states[state_id] |
| 106 | + if "enter" not in state: |
| 107 | + state["enter"] = [] |
| 108 | + state["enter"].append(partial(send_event, event_to_send=event)) |
| 109 | + |
| 110 | + # First pass: collect all states and mark initial |
| 111 | + for state_elem in scxml.findall(".//state"): |
| 112 | + _parse_state(state_elem) |
| 113 | + |
| 114 | + # Second pass: collect final states |
| 115 | + for state_elem in scxml.findall(".//final"): |
| 116 | + _parse_state(state_elem, final=True) |
| 117 | + |
| 118 | + # If no initial state was specified, mark the first state as initial |
| 119 | + if not initial_state and states: |
| 120 | + first_state = next(iter(states)) |
| 121 | + states[first_state]["initial"] = True |
| 122 | + |
| 123 | + return { |
| 124 | + "states": states, |
| 125 | + "events": events, |
| 126 | + } |
0 commit comments