Skip to content

Commit 1c5104c

Browse files
authored
Merge pull request #35 from ATNoG/feature/states_tags_metadata
Add tags and metadata to the state machine states
2 parents fd4379f + f4a2561 commit 1c5104c

File tree

3 files changed

+353
-195
lines changed

3 files changed

+353
-195
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from transitions.extensions.states import add_state_features, Tags, State
2+
from transitions.extensions import (
3+
HierarchicalMachine,
4+
GraphMachine,
5+
HierarchicalGraphMachine,
6+
)
7+
8+
9+
class Metadata(State):
10+
"""Allows states to have metadata.
11+
Attributes:
12+
metadata (dict): A dictionary with the state metadata.
13+
"""
14+
15+
def __init__(self, *args, **kwargs):
16+
"""
17+
Args:
18+
**kwargs: If kwargs contains `metadata`, assign them to the attribute.
19+
"""
20+
self.metadata = kwargs.pop("metadata", None)
21+
super(Metadata, self).__init__(*args, **kwargs)
22+
23+
def __getattr__(self, key):
24+
if value := self.metadata.get(key) is not None:
25+
return value
26+
return super(Metadata, self).__getattribute__(key)
27+
28+
29+
@add_state_features(Tags, Metadata)
30+
class CustomHierarchicalMachine(HierarchicalMachine):
31+
pass
32+
33+
34+
@add_state_features(Tags, Metadata)
35+
class CustomHierarchicalGraphMachine(HierarchicalGraphMachine):
36+
pass
37+
38+
39+
@add_state_features(Tags, Metadata)
40+
class CustomGraphMachine(GraphMachine):
41+
pass

0 commit comments

Comments
 (0)