Skip to content

Commit 29c22fb

Browse files
committed
fix: Reading from file on <scxml.datamodel.data.src> tag
1 parent 705ecb3 commit 29c22fb

4 files changed

Lines changed: 26 additions & 34 deletions

File tree

statemachine/io/scxml/parser.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import xml.etree.ElementTree as ET
33
from typing import Iterable
44
from typing import Set
5+
from urllib.parse import urlparse
56

67
from .schema import Action
78
from .schema import AssignAction
@@ -106,12 +107,19 @@ def parse_datamodel(root: ET.Element) -> "DataModel | None":
106107

107108
for datamodel_elem in root.findall(".//datamodel"):
108109
for data_elem in datamodel_elem.findall("data"):
110+
content = data_elem.text and re.sub(r"\s+", " ", data_elem.text).strip() or None
111+
src = data_elem.attrib.get("src")
112+
src_parsed = urlparse(src) if src else None
113+
if src_parsed and src_parsed.scheme == "file" and content is None:
114+
with open(src_parsed.path) as f:
115+
content = f.read()
116+
109117
data_model.data.append(
110118
DataItem(
111119
id=data_elem.attrib["id"],
112-
src=data_elem.attrib.get("src"),
120+
src=src_parsed,
113121
expr=data_elem.attrib.get("expr"),
114-
content=data_elem.text and re.sub(r"\s+", " ", data_elem.text).strip() or None,
122+
content=content,
115123
)
116124
)
117125

statemachine/io/scxml/processor.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
from contextlib import contextmanager
13
from dataclasses import dataclass
24
from pathlib import Path
35
from typing import Any
@@ -19,6 +21,16 @@
1921
from .schema import Transition
2022

2123

24+
@contextmanager
25+
def temporary_directory(new_current_dir):
26+
original_dir = os.getcwd()
27+
try:
28+
os.chdir(new_current_dir)
29+
yield
30+
finally:
31+
os.chdir(original_dir)
32+
33+
2234
class IOProcessor:
2335
def __init__(self, processor: "SCXMLProcessor", machine: StateMachine):
2436
self.scxml_processor = processor
@@ -52,7 +64,8 @@ def __init__(self):
5264

5365
def parse_scxml_file(self, path: Path):
5466
scxml_content = path.read_text()
55-
return self.parse_scxml(path.stem, scxml_content)
67+
with temporary_directory(path.parent):
68+
return self.parse_scxml(path.stem, scxml_content)
5669

5770
def parse_scxml(self, sm_name: str, scxml_content: str):
5871
definition = parse_scxml(scxml_content)

statemachine/io/scxml/schema.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Dict
44
from typing import List
55
from typing import Set
6+
from urllib.parse import ParseResult
67

78

89
@dataclass
@@ -125,7 +126,7 @@ class State:
125126
@dataclass
126127
class DataItem:
127128
id: str
128-
src: "str | None"
129+
src: "ParseResult | None"
129130
expr: "str | None"
130131
content: "str | None"
131132

tests/scxml/w3c/mandatory/test552.fail.md

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)