-
Notifications
You must be signed in to change notification settings - Fork 1
/
pystatemachine.py
214 lines (166 loc) · 7.88 KB
/
pystatemachine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
The MIT License (MIT)
Copyright (c) 2015 Christian Maugg
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
CHANGELOG
=========
1.2
===
* exceptions in an event-decorated function are now reraised when no transition failure handler was
registered
1.1
===
* added a decorator for registering a class' method as exception handler when an 'event'-decorated method
fails. multiple methods may be registered as transition failure handler: they are invoked in the order
given by the optional 'calling_sequence' keyword
1.0
===
* first public release
"""
import functools
import inspect
__author__ = 'Christian Maugg <software@christianmaugg.de>'
__version__ = version = '1.2'
class InvalidStateTransition(Exception):
pass
class StateInfo(object):
@staticmethod
def get_states(cls):
if not inspect.isclass(cls):
raise TypeError('"{0}" is no class object!'.format(cls))
if not hasattr(cls, '___pystatemachine_cls_states'):
states = tuple(state for _, state in inspect.getmembers(cls, lambda member: isinstance(member, State)))
setattr(cls, '___pystatemachine_cls_states', states)
return getattr(cls, '___pystatemachine_cls_states')
@staticmethod
def get_initial_state(cls):
if not inspect.isclass(cls):
raise TypeError('"{0}" is no class object!'.format(cls))
if not hasattr(cls, '___pystatemachine_cls_initial_state'):
states = StateInfo.get_states(cls)
initial_states = [state for state in states if state.is_initial]
assert initial_states, 'no initial state found!'
assert len(initial_states) == 1, 'found multiple initial states! initial states: {0}'.format(
' ,'.join(str(state) for state in initial_states))
initial_state = initial_states[0]
setattr(cls, '___pystatemachine_cls_initial_state', initial_state)
return getattr(cls, '___pystatemachine_cls_initial_state')
@staticmethod
def get_current_state(obj):
if not hasattr(obj, '___pystatemachine_obj_current_state'):
initial_state = StateInfo.get_initial_state(obj.__class__)
setattr(obj, '___pystatemachine_obj_current_state', initial_state)
return getattr(obj, '___pystatemachine_obj_current_state')
@staticmethod
def set_current_state(obj, state):
assert isinstance(state, State), 'invalid state type!'
setattr(obj, '___pystatemachine_obj_current_state', state)
class State(object):
def __init__(self, name, initial=False):
super(State, self).__init__()
self.is_initial = True if initial else False
self.name = name.upper()
def __str__(self):
return '<{0}.State[{1}] object at 0x{2:X}>'.format(__name__, self.name, id(self))
def event(from_states=None, to_state=None):
""" a decorator for transitioning from certain states to a target state. must be used on bound methods of a class
instance, only. """
from_states_tuple = (from_states, ) if isinstance(from_states, State) else tuple(from_states or [])
if not len(from_states_tuple) >= 1:
raise ValueError()
if not all(isinstance(state, State) for state in from_states_tuple):
raise TypeError()
if not isinstance(to_state, State):
raise TypeError()
def wrapper(wrapped):
@functools.wraps(wrapped)
def transition(instance, *a, **kw):
if instance.current_state not in from_states_tuple:
raise InvalidStateTransition()
try:
result = wrapped(instance, *a, **kw)
except Exception as error:
error_handlers = getattr(instance, '___pystatemachine_transition_failure_handlers', [])
for error_handler in error_handlers:
error_handler(instance, wrapped, instance.current_state, to_state, error)
if not error_handlers:
raise error
else:
StateInfo.set_current_state(instance, to_state)
return result
return transition
return wrapper
def transition_failure_handler(calling_sequence=0):
def wrapper(wrapped):
setattr(wrapped, '___pystatemachine_is_transition_failure_handler', True)
setattr(wrapped, '___pystatemachine_transition_failure_handler_calling_sequence', int(calling_sequence))
return wrapped
return wrapper
def acts_as_state_machine(cls):
"""
a decorator which sets two properties on a class:
* the 'current_state' property: a read-only property, returning the state machine's current state, as 'State' object
* the 'states' property: a tuple of all valid state machine states, as 'State' objects
class objects may use current_state and states freely
:param cls:
:return:
"""
assert not hasattr(cls, 'current_state'), '{0} already has a "current_state" attribute!'.format(cls)
assert not hasattr(cls, 'states'), '{0} already has a "states" attribute!'.format(cls)
def get_states(obj):
return StateInfo.get_states(obj.__class__)
def is_transition_failure_handler(obj):
return all([
any([
inspect.ismethod(obj), # python2
inspect.isfunction(obj), # python3
]),
getattr(obj, '___pystatemachine_is_transition_failure_handler', False),
])
transition_failure_handlers = sorted(
[value for name, value in inspect.getmembers(cls, is_transition_failure_handler)],
key=lambda m: getattr(m, '___pystatemachine_transition_failure_handler_calling_sequence', 0),
)
setattr(cls, '___pystatemachine_transition_failure_handlers', transition_failure_handlers)
cls.current_state = property(fget=StateInfo.get_current_state)
cls.states = property(fget=get_states)
return cls
if __name__ == '__main__':
@acts_as_state_machine
class Turnstile(object):
locked = State('locked', initial=True)
unlocked = State('unlocked')
@event(from_states=(locked, unlocked), to_state=unlocked)
def coin(self):
assert random.random() > .5, 'failing for demonstration purposes, only ..'
print('*blingbling* .. unlocked!')
@event(from_states=(locked, unlocked), to_state=locked)
def push(self):
print('*push* .. locked!')
@transition_failure_handler(calling_sequence=2)
def turnstile_malfunction(self, method, from_state, to_state, error):
print('state transition from {0.name} to {1.name} failed. Reason: {2}'.format(from_state, to_state, error))
@transition_failure_handler(calling_sequence=1)
def before_turnstile_malfunction(self, method, from_state, to_state, error):
print('before state transition failure handler ..')
import random
turnstile = Turnstile()
for _ in range(10):
handler = random.choice([turnstile.coin, turnstile.push])
handler()