Skip to content

Commit

Permalink
Fix backward incompatible change in ruamel.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreDecan committed Oct 30, 2023
1 parent 34e5c31 commit ae48d98
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 26 deletions.
2 changes: 1 addition & 1 deletion sismic/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__package__ = 'sismic'
__version__ = '1.6.5'
__version__ = '1.6.6'
__licence__ = 'LGPL3'
__author__ = 'Alexandre Decan'
__url__ = 'https://github.com/AlexandreDecan/sismic/'
Expand Down
17 changes: 7 additions & 10 deletions sismic/io/datadict.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from collections import OrderedDict
from typing import Any, List, Mapping, MutableMapping, Optional, Tuple, cast

from ..exceptions import StatechartError
Expand Down Expand Up @@ -154,29 +153,27 @@ def _import_state_from_dict(state_d: Mapping[str, Any]) -> StateMixin:
return state


def export_to_dict(statechart: Statechart, ordered=True) -> Mapping[str, Any]:
def export_to_dict(statechart: Statechart) -> Mapping[str, Any]:
"""
Export given StateChart instance to a dict.
:param statechart: a StateChart instance
:param ordered: set to True to use an ordereddict instead of a dict
:return: a dict that can be used in *_import_from_dict*
"""
d = OrderedDict() if ordered else {} # type: MutableMapping
d = dict()
d['name'] = statechart.name
if statechart.description:
d['description'] = statechart.description
if statechart.preamble:
d['preamble'] = statechart.preamble

d['root state'] = _export_state_to_dict(statechart, cast(str, statechart.root), ordered)
d['root state'] = _export_state_to_dict(statechart, cast(str, statechart.root))

return {'statechart': d}


def _export_state_to_dict(statechart: Statechart, state_name: str,
ordered=True) -> Mapping[str, Any]:
data = OrderedDict() if ordered else {}
def _export_state_to_dict(statechart: Statechart, state_name: str) -> Mapping[str, Any]:
data = dict()

state = statechart.state_for(state_name)

Expand Down Expand Up @@ -222,7 +219,7 @@ def _export_state_to_dict(statechart: Statechart, state_name: str,
data['transitions'] = []

for transition in transitions:
transition_data = OrderedDict() if ordered else {}
transition_data = dict()
if transition.event:
transition_data['event'] = transition.event
if transition.guard:
Expand Down Expand Up @@ -257,7 +254,7 @@ def _export_state_to_dict(statechart: Statechart, state_name: str,

if isinstance(state, CompositeStateMixin):
children = statechart.children_for(cast(StateMixin, state).name)
children_data = [_export_state_to_dict(statechart, child, ordered) for child in children]
children_data = [_export_state_to_dict(statechart, child) for child in children]

if isinstance(state, CompoundState):
data['states'] = children_data
Expand Down
19 changes: 10 additions & 9 deletions sismic/io/yaml.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import ruamel.yaml as yaml
import schema

from io import StringIO

from ..exceptions import StatechartError
from ..model import Statechart

Expand Down Expand Up @@ -71,11 +73,8 @@ def import_from_yaml(
with open(filepath, 'r') as f:
text = f.read()

if yaml.version_info < (0, 15):
data = yaml.safe_load(text) # type: dict
else:
yml = yaml.YAML(typ='safe', pure=True)
data = yml.load(text)
yml = yaml.YAML(typ='safe', pure=True)
data = yml.load(text)

if not ignore_schema:
try:
Expand All @@ -99,11 +98,13 @@ def export_to_yaml(statechart: Statechart, filepath: str = None) -> str:
:param filepath: save output to given filepath, if provided
:return: A textual YAML representation
"""
output = yaml.dump(export_to_dict(statechart, ordered=False),
width=1000, default_flow_style=False)
output = StringIO()

yml = yaml.YAML(typ='safe', pure=True)
yml.dump(export_to_dict(statechart), output)

if filepath:
with open(filepath, 'w') as f:
f.write(output)
f.write(output.getvalue())

return output
return output.getvalue()
6 changes: 3 additions & 3 deletions tests/yaml/deep_history.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ statechart:
name: Deep history
description: |
A statechart that makes use of a deep history state.
It represents two concurrent processes that iterate over three states (si1, si2, si3 for i in 1..2) on `next`i event.
The process can be interrupted with a `pause` event.
When paused, the processes can either be continued (`continue` event) or stopped (`stop` event).
It represents two concurrent processes that iterate over three states (si1, si2, si3 for i in 1..2) on "next"i event.
The process can be interrupted with a "pause" event.
When paused, the processes can either be continued ("continue" event) or stopped ("stop" event).
root state:
name: root
initial: active
Expand Down
6 changes: 3 additions & 3 deletions tests/yaml/history.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ statechart:
name: Shallow history
description: |
A statechart that makes use of an history state.
It represents a process that loops over three states (s1, s2, s3) on `next` event.
The process can be interrupted with a `pause` event.
When paused, the process can either be continued (`continue` event) or stopped (`stop` event).
It represents a process that loops over three states (s1, s2, s3) on "next" event.
The process can be interrupted with a "pause" event.
When paused, the process can either be continued ("continue" event) or stopped ("stop" event).
root state:
name: root
initial: loop
Expand Down

0 comments on commit ae48d98

Please sign in to comment.