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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
|
"""
transitions.core
----------------
This module contains the central parts of transitions which are the state machine logic, state
and transition concepts.
"""
try:
from builtins import object
except ImportError: # pragma: no cover
# python2
pass
try:
# Enums are supported for Python 3.4+ and Python 2.7 with enum34 package installed
from enum import Enum, EnumMeta
except ImportError: # pragma: no cover
# If enum is not available, create dummy classes for type checks
class Enum: # type:ignore
"""This is just an Enum stub for Python 2 and Python 3.3 and before without Enum support."""
class EnumMeta: # type:ignore
"""This is just an EnumMeta stub for Python 2 and Python 3.3 and before without Enum support."""
import inspect
import itertools
import logging
import warnings
from collections import OrderedDict, defaultdict, deque
from functools import partial
_LOGGER = logging.getLogger(__name__)
_LOGGER.addHandler(logging.NullHandler())
warnings.filterwarnings(action='default', message=r".*transitions version.*", category=DeprecationWarning)
def listify(obj):
"""Wraps a passed object into a list in case it has not been a list, tuple before.
Returns an empty list in case ``obj`` is None.
Args:
obj: instance to be converted into a list.
Returns:
list: May also return a tuple in case ``obj`` has been a tuple before.
"""
if obj is None:
return []
try:
return obj if isinstance(obj, (list, tuple, EnumMeta)) else [obj]
except ReferenceError:
# obj is an empty weakref
return [obj]
def _prep_ordered_arg(desired_length, arguments=None):
"""Ensure list of arguments passed to add_ordered_transitions has the proper length.
Expands the given arguments and apply same condition, callback
to all transitions if only one has been given.
Args:
desired_length (int): The size of the resulting list
arguments (optional[str, reference or list]): Parameters to be expanded.
Returns:
list: Parameter sets with the desired length.
"""
arguments = listify(arguments) if arguments is not None else [None]
if len(arguments) != desired_length and len(arguments) != 1:
raise ValueError("Argument length must be either 1 or the same length as "
"the number of transitions.")
if len(arguments) == 1:
return arguments * desired_length
return arguments
class State(object):
"""A persistent representation of a state managed by a ``Machine``.
Attributes:
name (str): State name which is also assigned to the model(s).
on_enter (list): Callbacks executed when a state is entered.
on_exit (list): Callbacks executed when a state is exited.
ignore_invalid_triggers (bool): Indicates if unhandled/invalid triggers should raise an exception.
"""
# A list of dynamic methods which can be resolved by a ``Machine`` instance for convenience functions.
# Dynamic methods for states must always start with `on_`!
dynamic_methods = ['on_enter', 'on_exit']
def __init__(self, name, on_enter=None, on_exit=None,
ignore_invalid_triggers=None, final=False):
"""
Args:
name (str or Enum): The name of the state
on_enter (str or list): Optional callable(s) to trigger when a
state is entered. Can be either a string providing the name of
a callable, or a list of strings.
on_exit (str or list): Optional callable(s) to trigger when a
state is exited. Can be either a string providing the name of a
callable, or a list of strings.
ignore_invalid_triggers (Boolean): Optional flag to indicate if
unhandled/invalid triggers should raise an exception
"""
self._name = name
self.final = final
self.ignore_invalid_triggers = ignore_invalid_triggers
self.on_enter = listify(on_enter) if on_enter else []
self.on_exit = listify(on_exit) if on_exit else []
@property
def name(self):
"""The name of the state."""
if isinstance(self._name, Enum):
return self._name.name
return self._name
@property
def value(self):
"""The state's value. For string states this will be equivalent to the name attribute."""
return self._name
def enter(self, event_data):
"""Triggered when a state is entered."""
_LOGGER.debug("%sEntering state %s. Processing callbacks...", event_data.machine.name, self.name)
event_data.machine.callbacks(self.on_enter, event_data)
_LOGGER.info("%sFinished processing state %s enter callbacks.", event_data.machine.name, self.name)
def exit(self, event_data):
"""Triggered when a state is exited."""
_LOGGER.debug("%sExiting state %s. Processing callbacks...", event_data.machine.name, self.name)
event_data.machine.callbacks(self.on_exit, event_data)
_LOGGER.info("%sFinished processing state %s exit callbacks.", event_data.machine.name, self.name)
def add_callback(self, trigger, func):
"""Add a new enter or exit callback.
Args:
trigger (str): The type of triggering event. Must be one of
'enter' or 'exit'.
func (str): The name of the callback function.
"""
callback_list = getattr(self, 'on_' + trigger)
callback_list.append(func)
def __repr__(self):
return "<%s('%s')@%s>" % (type(self).__name__, self.name, id(self))
class Condition(object):
"""A helper class to call condition checks in the intended way.
Attributes:
func (str or callable): The function to call for the condition check
target (bool): Indicates the target state--i.e., when True,
the condition-checking callback should return True to pass,
and when False, the callback should return False to pass.
"""
def __init__(self, func, target=True):
"""
Args:
func (str or callable): Name of the condition-checking callable
target (bool): Indicates the target state--i.e., when True,
the condition-checking callback should return True to pass,
and when False, the callback should return False to pass.
Notes:
This class should not be initialized or called from outside a
Transition instance, and exists at module level (rather than
nesting under the transition class) only because of a bug in
dill that prevents serialization under Python 2.7.
"""
self.func = func
self.target = target
def check(self, event_data):
"""Check whether the condition passes.
Args:
event_data (EventData): An EventData instance to pass to the
condition (if event sending is enabled) or to extract arguments
from (if event sending is disabled). Also contains the data
model attached to the current machine which is used to invoke
the condition.
"""
predicate = event_data.machine.resolve_callable(self.func, event_data)
if event_data.machine.send_event:
return predicate(event_data) == self.target
return predicate(*event_data.args, **event_data.kwargs) == self.target
def __repr__(self):
return "<%s(%s)@%s>" % (type(self).__name__, self.func, id(self))
class Transition(object):
"""Representation of a transition managed by a ``Machine`` instance.
Attributes:
source (str): Source state of the transition.
dest (str): Destination state of the transition.
prepare (list): Callbacks executed before conditions checks.
conditions (list): Callbacks evaluated to determine if
the transition should be executed.
before (list): Callbacks executed before the transition is executed
but only if condition checks have been successful.
after (list): Callbacks executed after the transition is executed
but only if condition checks have been successful.
"""
dynamic_methods = ['before', 'after', 'prepare']
""" A list of dynamic methods which can be resolved by a ``Machine`` instance for convenience functions. """
condition_cls = Condition
""" The class used to wrap condition checks. Can be replaced to alter condition resolution behaviour
(e.g. OR instead of AND for 'conditions' or AND instead of OR for 'unless') """
def __init__(self, source, dest, conditions=None, unless=None, before=None,
after=None, prepare=None):
"""
Args:
source (str): The name of the source State.
dest (str): The name of the destination State.
conditions (optional[str, callable or list]): Condition(s) that must pass in order for
the transition to take place. Either a string providing the
name of a callable, or a list of callables. For the transition
to occur, ALL callables must return True.
unless (optional[str, callable or list]): Condition(s) that must return False in order
for the transition to occur. Behaves just like conditions arg
otherwise.
before (optional[str, callable or list]): callbacks to trigger before the
transition.
after (optional[str, callable or list]): callbacks to trigger after the transition.
prepare (optional[str, callable or list]): callbacks to trigger before conditions are checked
"""
self.source = source
self.dest = dest
self.prepare = [] if prepare is None else listify(prepare)
self.before = [] if before is None else listify(before)
self.after = [] if after is None else listify(after)
self.conditions = []
if conditions is not None:
for cond in listify(conditions):
self.conditions.append(self.condition_cls(cond))
if unless is not None:
for cond in listify(unless):
self.conditions.append(self.condition_cls(cond, target=False))
def _eval_conditions(self, event_data):
for cond in self.conditions:
if not cond.check(event_data):
_LOGGER.debug("%sTransition condition failed: %s() does not return %s. Transition halted.",
event_data.machine.name, cond.func, cond.target)
return False
return True
def execute(self, event_data):
"""Execute the transition.
Args:
event_data: An instance of class EventData.
Returns: boolean indicating whether the transition was
successfully executed (True if successful, False if not).
"""
_LOGGER.debug("%sInitiating transition from state %s to state %s...",
event_data.machine.name, self.source, self.dest)
event_data.machine.callbacks(self.prepare, event_data)
_LOGGER.debug("%sExecuted callbacks before conditions.", event_data.machine.name)
if not self._eval_conditions(event_data):
return False
event_data.machine.callbacks(itertools.chain(event_data.machine.before_state_change, self.before), event_data)
_LOGGER.debug("%sExecuted callback before transition.", event_data.machine.name)
if self.dest: # if self.dest is None this is an internal transition with no actual state change
self._change_state(event_data)
event_data.machine.callbacks(itertools.chain(self.after, event_data.machine.after_state_change), event_data)
_LOGGER.debug("%sExecuted callback after transition.", event_data.machine.name)
return True
def _change_state(self, event_data):
event_data.machine.get_state(self.source).exit(event_data)
event_data.machine.set_state(self.dest, event_data.model)
event_data.update(getattr(event_data.model, event_data.machine.model_attribute))
dest = event_data.machine.get_state(self.dest)
dest.enter(event_data)
if dest.final:
event_data.machine.callbacks(event_data.machine.on_final, event_data)
def add_callback(self, trigger, func):
"""Add a new before, after, or prepare callback.
Args:
trigger (str): The type of triggering event. Must be one of
'before', 'after' or 'prepare'.
func (str or callable): The name of the callback function or a callable.
"""
callback_list = getattr(self, trigger)
callback_list.append(func)
def __repr__(self):
return "<%s('%s', '%s')@%s>" % (type(self).__name__,
self.source, self.dest, id(self))
class EventData(object):
"""Collection of relevant data related to the ongoing transition attempt.
Attributes:
state (State): The State from which the Event was triggered.
event (Event): The triggering Event.
machine (Machine): The current Machine instance.
model (object): The model/object the machine is bound to.
args (list): Optional positional arguments from trigger method
to store internally for possible later use.
kwargs (dict): Optional keyword arguments from trigger method
to store internally for possible later use.
transition (Transition): Currently active transition. Will be assigned during triggering.
error (Exception): In case a triggered event causes an Error, it is assigned here and passed on.
result (bool): True in case a transition has been successful, False otherwise.
"""
def __init__(self, state, event, machine, model, args, kwargs):
"""
Args:
state (State): The State from which the Event was triggered.
event (Event): The triggering Event.
machine (Machine): The current Machine instance.
model (object): The model/object the machine is bound to.
args (tuple): Optional positional arguments from trigger method
to store internally for possible later use.
kwargs (dict): Optional keyword arguments from trigger method
to store internally for possible later use.
"""
self.state = state
self.event = event
self.machine = machine
self.model = model
self.args = args
self.kwargs = kwargs
self.transition = None
self.error = None
self.result = False
def update(self, state):
"""Updates the EventData object with the passed state.
Attributes:
state (State, str or Enum): The state object, enum member or string to assign to EventData.
"""
if not isinstance(state, State):
self.state = self.machine.get_state(state)
def __repr__(self):
return "<%s(%s, %s, %s)@%s>" % (type(self).__name__, self.event, self.state,
getattr(self, 'transition'), id(self))
class Event(object):
"""A collection of transitions assigned to the same trigger
"""
def __init__(self, name, machine):
"""
Args:
name (str): The name of the event, which is also the name of the
triggering callable (e.g., 'advance' implies an advance()
method).
machine (Machine): The current Machine instance.
"""
self.name = name
self.machine = machine
self.transitions = defaultdict(list)
def add_transition(self, transition):
"""Add a transition to the list of potential transitions.
Args:
transition (Transition): The Transition instance to add to the
list.
"""
self.transitions[transition.source].append(transition)
def trigger(self, model, *args, **kwargs):
"""Executes all transitions that match the current state,
halting as soon as one successfully completes. More precisely, it prepares a partial
of the internal ``_trigger`` function, passes this to ``Machine._process``.
It is up to the machine's configuration of the Event whether processing happens queued (sequentially) or
whether further Events are processed as they occur.
Args:
model (object): The currently processed model
args and kwargs: Optional positional or named arguments that will
be passed onto the EventData object, enabling arbitrary state
information to be passed on to downstream triggered functions.
Returns: boolean indicating whether a transition was
successfully executed (True if successful, False if not).
"""
func = partial(self._trigger, EventData(None, self, self.machine, model, args=args, kwargs=kwargs))
# pylint: disable=protected-access
# noinspection PyProtectedMember
# Machine._process should not be called somewhere else. That's why it should not be exposed
# to Machine users.
return self.machine._process(func)
def _trigger(self, event_data):
"""Internal trigger function called by the ``Machine`` instance. This should not
be called directly but via the public method ``Machine.process``.
Args:
event_data (EventData): The currently processed event. State, result and (potentially) error might be
overridden.
Returns: boolean indicating whether a transition was
successfully executed (True if successful, False if not).
"""
event_data.state = self.machine.get_model_state(event_data.model)
try:
if self._is_valid_source(event_data.state):
self._process(event_data)
except BaseException as err: # pylint: disable=broad-except; Exception will be handled elsewhere
event_data.error = err
if self.machine.on_exception:
self.machine.callbacks(self.machine.on_exception, event_data)
else:
raise
finally:
try:
self.machine.callbacks(self.machine.finalize_event, event_data)
_LOGGER.debug("%sExecuted machine finalize callbacks", self.machine.name)
except BaseException as err: # pylint: disable=broad-except; Exception will be handled elsewhere
_LOGGER.error("%sWhile executing finalize callbacks a %s occurred: %s.",
self.machine.name,
type(err).__name__,
str(err))
return event_data.result
def _process(self, event_data):
self.machine.callbacks(self.machine.prepare_event, event_data)
_LOGGER.debug("%sExecuted machine preparation callbacks before conditions.", self.machine.name)
for trans in self.transitions[event_data.state.name]:
event_data.transition = trans
if trans.execute(event_data):
event_data.result = True
break
def _is_valid_source(self, state):
if state.name not in self.transitions:
msg = "%sCan't trigger event %s from state %s!" % (self.machine.name, self.name,
state.name)
ignore = state.ignore_invalid_triggers if state.ignore_invalid_triggers is not None \
else self.machine.ignore_invalid_triggers
if ignore:
_LOGGER.warning(msg)
return False
raise MachineError(msg)
return True
def __repr__(self):
return "<%s('%s')@%s>" % (type(self).__name__, self.name, id(self))
def add_callback(self, trigger, func):
"""Add a new before or after callback to all available transitions.
Args:
trigger (str): The type of triggering event. Must be one of
'before', 'after' or 'prepare'.
func (str): The name of the callback function.
"""
for trans in itertools.chain(*self.transitions.values()):
trans.add_callback(trigger, func)
class Machine(object):
"""Machine manages states, transitions and models. In case it is initialized without a specific model
(or specifically no model), it will also act as a model itself. Machine takes also care of decorating
models with conveniences functions related to added transitions and states during runtime.
Attributes:
states (OrderedDict): Collection of all registered states.
events (dict): Collection of transitions ordered by trigger/event.
models (list): List of models attached to the machine.
initial (str): Name of the initial state for new models.
prepare_event (list): Callbacks executed when an event is triggered.
before_state_change (list): Callbacks executed after condition checks but before transition is conducted.
Callbacks will be executed BEFORE the custom callbacks assigned to the transition.
after_state_change (list): Callbacks executed after the transition has been conducted.
Callbacks will be executed AFTER the custom callbacks assigned to the transition.
finalize_event (list): Callbacks will be executed after all transitions callbacks have been executed.
Callbacks mentioned here will also be called if a transition or condition check raised an error.
queued (bool): Whether transitions in callbacks should be executed immediately (False) or sequentially.
send_event (bool): When True, any arguments passed to trigger methods will be wrapped in an EventData
object, allowing indirect and encapsulated access to data. When False, all positional and keyword
arguments will be passed directly to all callback methods.
auto_transitions (bool): When True (default), every state will automatically have an associated
to_{state}() convenience trigger in the base model.
ignore_invalid_triggers (bool): When True, any calls to trigger methods that are not valid for the
present state (e.g., calling an a_to_b() trigger when the current state is c) will be silently
ignored rather than raising an invalid transition exception.
name (str): Name of the ``Machine`` instance mainly used for easier log message distinction.
"""
separator = '_' # separates callback type from state/transition name
wildcard_all = '*' # will be expanded to ALL states
wildcard_same = '=' # will be expanded to source state
state_cls = State
transition_cls = Transition
event_cls = Event
self_literal = 'self'
def __init__(self, model=self_literal, states=None, initial='initial', transitions=None,
send_event=False, auto_transitions=True,
ordered_transitions=False, ignore_invalid_triggers=None,
before_state_change=None, after_state_change=None, name=None,
queued=False, prepare_event=None, finalize_event=None, model_attribute='state', model_override=False,
on_exception=None, on_final=None, **kwargs):
"""
Args:
model (object or list): The object(s) whose states we want to manage. If set to `Machine.self_literal`
(default value), the current Machine instance will be used as the model (i.e., all
triggering events will be attached to the Machine itself). Note that an empty list
is treated like no model.
states (list or Enum): A list or enumeration of valid states. Each list element can be either a
string, an enum member or a State instance. If string or enum member, a new generic State
instance will be created that is named according to the string or enum member's name.
initial (str, Enum or State): The initial state of the passed model[s].
transitions (list): An optional list of transitions. Each element
is a dictionary of named arguments to be passed onto the
Transition initializer.
send_event (boolean): When True, any arguments passed to trigger
methods will be wrapped in an EventData object, allowing
indirect and encapsulated access to data. When False, all
positional and keyword arguments will be passed directly to all
callback methods.
auto_transitions (boolean): When True (default), every state will
automatically have an associated to_{state}() convenience
trigger in the base model.
ordered_transitions (boolean): Convenience argument that calls
add_ordered_transitions() at the end of initialization if set
to True.
ignore_invalid_triggers: when True, any calls to trigger methods
that are not valid for the present state (e.g., calling an
a_to_b() trigger when the current state is c) will be silently
ignored rather than raising an invalid transition exception.
before_state_change: A callable called on every change state before
the transition happened. It receives the very same args as normal
callbacks.
after_state_change: A callable called on every change state after
the transition happened. It receives the very same args as normal
callbacks.
name: If a name is set, it will be used as a prefix for logger output
queued (boolean): When True, processes transitions sequentially. A trigger
executed in a state callback function will be queued and executed later.
Due to the nature of the queued processing, all transitions will
_always_ return True since conditional checks cannot be conducted at queueing time.
prepare_event: A callable called on for before possible transitions will be processed.
It receives the very same args as normal callbacks.
finalize_event: A callable called on for each triggered event after transitions have been processed.
This is also called when a transition raises an exception.
on_exception: A callable called when an event raises an exception. If not set,
the exception will be raised instead.
**kwargs additional arguments passed to next class in MRO. This can be ignored in most cases.
"""
# calling super in case `Machine` is used as a mix in
# all keyword arguments should be consumed by now if this is not the case
try:
super(Machine, self).__init__(**kwargs)
except TypeError as err:
raise ValueError('Passing arguments {0} caused an inheritance error: {1}'.format(kwargs.keys(), err))
# initialize protected attributes first
self._queued = queued
self._transition_queue = deque()
self._before_state_change = []
self._after_state_change = []
self._prepare_event = []
self._finalize_event = []
self._on_exception = []
self._on_final = []
self._initial = None
self.states = OrderedDict()
self.events = OrderedDict()
self.send_event = send_event
self.auto_transitions = auto_transitions
self.ignore_invalid_triggers = ignore_invalid_triggers
self.prepare_event = prepare_event
self.before_state_change = before_state_change
self.after_state_change = after_state_change
self.finalize_event = finalize_event
self.on_exception = on_exception
self.on_final = on_final
self.name = name + ": " if name is not None else ""
self.model_attribute = model_attribute
self.model_override = model_override
self.models = []
if states is not None:
self.add_states(states)
if initial is not None:
self.initial = initial
if transitions is not None:
self.add_transitions(transitions)
if ordered_transitions:
self.add_ordered_transitions()
if model:
self.add_model(model)
def add_model(self, model, initial=None):
"""Register a model with the state machine, initializing triggers and callbacks."""
models = listify(model)
if initial is None:
if self.initial is None:
raise ValueError("No initial state configured for machine, must specify when adding model.")
initial = self.initial
for mod in models:
mod = self if mod is self.self_literal else mod
if mod not in self.models:
self._checked_assignment(mod, 'trigger', partial(self._get_trigger, mod))
self._checked_assignment(mod, 'may_trigger', partial(self._can_trigger, mod))
for trigger in self.events:
self._add_trigger_to_model(trigger, mod)
for state in self.states.values():
self._add_model_to_state(state, mod)
self.set_state(initial, model=mod)
self.models.append(mod)
def remove_model(self, model):
"""Remove a model from the state machine. The model will still contain all previously added triggers
and callbacks, but will not receive updates when states or transitions are added to the Machine.
If an event queue is used, all queued events of that model will be removed."""
models = listify(model)
for mod in models:
self.models.remove(mod)
if len(self._transition_queue) > 0:
# the first element of the list is currently executed. Keeping it for further Machine._process(ing)
self._transition_queue = deque(
[self._transition_queue[0]] + [e for e in self._transition_queue if e.args[0].model not in models])
@classmethod
def _create_transition(cls, *args, **kwargs):
return cls.transition_cls(*args, **kwargs)
@classmethod
def _create_event(cls, *args, **kwargs):
return cls.event_cls(*args, **kwargs)
@classmethod
def _create_state(cls, *args, **kwargs):
return cls.state_cls(*args, **kwargs)
@property
def initial(self):
"""Return the initial state."""
return self._initial
@initial.setter
def initial(self, value):
if isinstance(value, State):
if value.name not in self.states:
self.add_states(value)
else:
_ = self._has_state(value, raise_error=True)
self._initial = value.name
else:
state_name = value.name if isinstance(value, Enum) else value
if state_name not in self.states:
self.add_states(state_name)
self._initial = state_name
@property
def has_queue(self):
"""Return boolean indicating if machine has queue or not"""
return self._queued
@property
def model(self):
"""List of models attached to the machine. For backwards compatibility, the property will
return the model instance itself instead of the underlying list if there is only one attached
to the machine.
"""
if len(self.models) == 1:
return self.models[0]
return self.models
@property
def before_state_change(self):
"""Callbacks executed after condition checks but before transition is conducted.
Callbacks will be executed BEFORE the custom callbacks assigned to the transition."""
return self._before_state_change
# this should make sure that _before_state_change is always a list
@before_state_change.setter
def before_state_change(self, value):
self._before_state_change = listify(value)
@property
def after_state_change(self):
"""Callbacks executed after the transition has been conducted.
Callbacks will be executed AFTER the custom callbacks assigned to the transition."""
return self._after_state_change
# this should make sure that _after_state_change is always a list
@after_state_change.setter
def after_state_change(self, value):
self._after_state_change = listify(value)
@property
def prepare_event(self):
"""Callbacks executed when an event is triggered."""
return self._prepare_event
# this should make sure that prepare_event is always a list
@prepare_event.setter
def prepare_event(self, value):
self._prepare_event = listify(value)
@property
def finalize_event(self):
"""Callbacks will be executed after all transitions callbacks have been executed.
Callbacks mentioned here will also be called if a transition or condition check raised an error."""
return self._finalize_event
# this should make sure that finalize_event is always a list
@finalize_event.setter
def finalize_event(self, value):
self._finalize_event = listify(value)
@property
def on_exception(self):
"""Callbacks will be executed when an Event raises an Exception."""
return self._on_exception
# this should make sure that finalize_event is always a list
@on_exception.setter
def on_exception(self, value):
self._on_exception = listify(value)
@property
def on_final(self):
"""Callbacks will be executed when the reached state is tagged with 'final'"""
return self._on_final
# this should make sure that finalize_event is always a list
@on_final.setter
def on_final(self, value):
self._on_final = listify(value)
def get_state(self, state):
"""Return the State instance with the passed name."""
if isinstance(state, Enum):
state = state.name
if state not in self.states:
raise ValueError("State '%s' is not a registered state." % state)
return self.states[state]
# In theory this function could be static. This however causes some issues related to inheritance and
# pickling down the chain.
def is_state(self, state, model):
"""Check whether the current state matches the named state. This function is not called directly
but assigned as partials to model instances (e.g. is_A -> partial(_is_state, 'A', model)).
Args:
state (str or Enum): name of the checked state or Enum
model: model to be checked
Returns:
bool: Whether the model's current state is state.
"""
return getattr(model, self.model_attribute) == state
def get_model_state(self, model):
"""
Get the state of a model
Args:
model (object): the stateful model
Returns:
State: The State object related to the model's state
"""
return self.get_state(getattr(model, self.model_attribute))
def set_state(self, state, model=None):
"""
Set the current state.
Args:
state (str or Enum or State): value of state to be set
model (optional[object]): targeted model; if not set, all models will be set to 'state'
"""
if not isinstance(state, State):
state = self.get_state(state)
models = self.models if model is None else listify(model)
for mod in models:
setattr(mod, self.model_attribute, state.value)
def add_state(self, states, on_enter=None, on_exit=None, ignore_invalid_triggers=None, **kwargs):
"""Alias for add_states."""
self.add_states(states=states, on_enter=on_enter, on_exit=on_exit,
ignore_invalid_triggers=ignore_invalid_triggers, **kwargs)
def add_states(self, states, on_enter=None, on_exit=None,
ignore_invalid_triggers=None, **kwargs):
"""Add new state(s).
Args:
states (list, str, dict, Enum or State): a list, a State instance, the
name of a new state, an enumeration (member) or a dict with keywords to pass on to the
State initializer. If a list, each element can be a string, State or enumeration member.
on_enter (str or list): callbacks to trigger when the state is
entered. Only valid if first argument is string.
on_exit (str or list): callbacks to trigger when the state is
exited. Only valid if first argument is string.
ignore_invalid_triggers: when True, any calls to trigger methods
that are not valid for the present state (e.g., calling an
a_to_b() trigger when the current state is c) will be silently
ignored rather than raising an invalid transition exception.
Note that this argument takes precedence over the same
argument defined at the Machine level, and is in turn
overridden by any ignore_invalid_triggers explicitly
passed in an individual state's initialization arguments.
**kwargs additional keyword arguments used by state mixins.
"""
ignore = ignore_invalid_triggers
if ignore is None:
ignore = self.ignore_invalid_triggers
states = listify(states)
for state in states:
if isinstance(state, (str, Enum)):
state = self._create_state(
state, on_enter=on_enter, on_exit=on_exit,
ignore_invalid_triggers=ignore, **kwargs)
elif isinstance(state, dict):
if 'ignore_invalid_triggers' not in state:
state['ignore_invalid_triggers'] = ignore
state = self._create_state(**state)
self.states[state.name] = state
for model in self.models:
self._add_model_to_state(state, model)
if self.auto_transitions:
for a_state in self.states.keys():
# add all states as sources to auto transitions 'to_<state>' with dest <state>
if a_state == state.name:
if self.model_attribute == 'state':
method_name = 'to_%s' % a_state
else:
method_name = 'to_%s_%s' % (self.model_attribute, a_state)
self.add_transition(method_name, self.wildcard_all, a_state)
# add auto transition with source <state> to <a_state>
else:
if self.model_attribute == 'state':
method_name = 'to_%s' % a_state
else:
method_name = 'to_%s_%s' % (self.model_attribute, a_state)
self.add_transition(method_name, state.name, a_state)
def _add_model_to_state(self, state, model):
# Add convenience function 'is_<state_name>' (e.g. 'is_A') to the model.
# When model_attribute has been customized, add 'is_<model_attribute>_<state_name>' instead
# to potentially support multiple states on one model (e.g. 'is_custom_state_A' and 'is_my_state_B').
func = partial(self.is_state, state.value, model)
if self.model_attribute == 'state':
method_name = 'is_%s' % state.name
else:
method_name = 'is_%s_%s' % (self.model_attribute, state.name)
self._checked_assignment(model, method_name, func)
# Add dynamic method callbacks (enter/exit) if there are existing bound methods in the model
# except if they are already mentioned in 'on_enter/exit' of the defined state
for callback in self.state_cls.dynamic_methods:
method = "{0}_{1}".format(callback, state.name)
if hasattr(model, method) and inspect.ismethod(getattr(model, method)) and \
method not in getattr(state, callback):
state.add_callback(callback[3:], method)
def _checked_assignment(self, model, name, func):
bound_func = getattr(model, name, None)
if (bound_func is None) ^ self.model_override:
setattr(model, name, func)
else:
_LOGGER.warning("%sSkip binding of '%s' to model due to model override policy.", self.name, name)
def _can_trigger(self, model, trigger, *args, **kwargs):
state = self.get_model_state(model)
event_data = EventData(state, Event(name=trigger, machine=self), self, model, args, kwargs)
for trigger_name in self.get_triggers(state):
if trigger_name != trigger:
continue
for transition in self.events[trigger_name].transitions[state.name]:
try:
_ = self.get_state(transition.dest) if transition.dest is not None else transition.source
except ValueError:
continue
event_data.transition = transition
try:
self.callbacks(self.prepare_event, event_data)
self.callbacks(transition.prepare, event_data)
if all(c.check(event_data) for c in transition.conditions):
return True
except BaseException as err:
event_data.error = err
if self.on_exception:
self.callbacks(self.on_exception, event_data)
else:
raise
return False
def _add_may_transition_func_for_trigger(self, trigger, model):
self._checked_assignment(model, "may_%s" % trigger, partial(self._can_trigger, model, trigger))
def _add_trigger_to_model(self, trigger, model):
self._checked_assignment(model, trigger, partial(self.events[trigger].trigger, model))
self._add_may_transition_func_for_trigger(trigger, model)
def _get_trigger(self, model, trigger_name, *args, **kwargs):
"""Convenience function added to the model to trigger events by name.
Args:
model (object): Model with assigned event trigger.
trigger_name (str): Name of the trigger to be called.
*args: Variable length argument list which is passed to the triggered event.
**kwargs: Arbitrary keyword arguments which is passed to the triggered event.
Returns:
bool: True if a transitions has been conducted or the trigger event has been queued.
"""
try:
event = self.events[trigger_name]
except KeyError:
state = self.get_model_state(model)
ignore = state.ignore_invalid_triggers if state.ignore_invalid_triggers is not None \
else self.ignore_invalid_triggers
if not ignore:
raise AttributeError("Do not know event named '%s'." % trigger_name)
return False
return event.trigger(model, *args, **kwargs)
def get_triggers(self, *args):
"""Collects all triggers FROM certain states.
Args:
*args: Tuple of source states.
Returns:
list of transition/trigger names.
"""
names = {state.name if hasattr(state, 'name') else state for state in args}
return [t for (t, ev) in self.events.items() if any(name in ev.transitions for name in names)]
def add_transition(self, trigger, source, dest, conditions=None,
unless=None, before=None, after=None, prepare=None, **kwargs):
"""Create a new Transition instance and add it to the internal list.
Args:
trigger (str): The name of the method that will trigger the
transition. This will be attached to the currently specified
model (e.g., passing trigger='advance' will create a new
advance() method in the model that triggers the transition.)
source(str, Enum or list): The name of the source state--i.e., the state we
are transitioning away from. This can be a single state, a
list of states or an asterisk for all states.
dest (str or Enum): The name of the destination State--i.e., the state
we are transitioning into. This can be a single state or an
equal sign to specify that the transition should be reflexive
so that the destination will be the same as the source for
every given source. If dest is None, this transition will be
an internal transition (exit/enter callbacks won't be processed).
conditions (str or list): Condition(s) that must pass in order
for the transition to take place. Either a list providing the
name of a callable, or a list of callables. For the transition
to occur, ALL callables must return True.
unless (str or list): Condition(s) that must return False in order
for the transition to occur. Behaves just like conditions arg
otherwise.
before (str or list): Callables to call before the transition.
after (str or list): Callables to call after the transition.
prepare (str or list): Callables to call when the trigger is activated
**kwargs: Additional arguments which can be passed to the created transition.
This is useful if you plan to extend Machine.Transition and require more parameters.
"""
if trigger == self.model_attribute:
raise ValueError("Trigger name cannot be same as model attribute name.")
if trigger not in self.events:
self.events[trigger] = self._create_event(trigger, self)
for model in self.models:
self._add_trigger_to_model(trigger, model)
if source == self.wildcard_all:
source = list(self.states.keys())
else:
# states are checked lazily which means we will only raise exceptions when the passed state
# is a State object because of potential confusion (see issue #155 for more details)
source = [s.name if isinstance(s, State) and self._has_state(s, raise_error=True) or hasattr(s, 'name') else
s for s in listify(source)]
for state in source:
if dest == self.wildcard_same:
_dest = state
elif dest is not None:
if isinstance(dest, State):
_ = self._has_state(dest, raise_error=True)
_dest = dest.name if hasattr(dest, 'name') else dest
else:
_dest = None
_trans = self._create_transition(state, _dest, conditions, unless, before,
after, prepare, **kwargs)
self.events[trigger].add_transition(_trans)
def add_transitions(self, transitions):
"""Add several transitions.
Args:
transitions (list): A list of transitions.
"""
for trans in listify(transitions):
if isinstance(trans, list):
self.add_transition(*trans)
else:
self.add_transition(**trans)
def add_ordered_transitions(self, states=None, trigger='next_state',
loop=True, loop_includes_initial=True,
conditions=None, unless=None, before=None,
after=None, prepare=None, **kwargs):
"""Add a set of transitions that move linearly from state to state.
Args:
states (list): A list of state names defining the order of the
transitions. E.g., ['A', 'B', 'C'] will generate transitions
for A --> B, B --> C, and C --> A (if loop is True). If states
is None, all states in the current instance will be used.
trigger (str): The name of the trigger method that advances to
the next state in the sequence.
loop (boolean): Whether to add a transition from the last
state to the first state.
loop_includes_initial (boolean): If no initial state was defined in
the machine, setting this to True will cause the _initial state
placeholder to be included in the added transitions. This argument
has no effect if the states argument is passed without the
initial state included.
conditions (str or list): Condition(s) that must pass in order
for the transition to take place. Either a list providing the
name of a callable, or a list of callables. For the transition
to occur, ALL callables must return True.
unless (str or list): Condition(s) that must return False in order
for the transition to occur. Behaves just like conditions arg
otherwise.
before (str or list): Callables to call before the transition.
after (str or list): Callables to call after the transition.
prepare (str or list): Callables to call when the trigger is activated
**kwargs: Additional arguments which can be passed to the created transition.
This is useful if you plan to extend Machine.Transition and require more parameters.
"""
if states is None:
states = list(self.states.keys()) # need to listify for Python3
len_transitions = len(states)
if len_transitions < 2:
raise ValueError("Can't create ordered transitions on a Machine "
"with fewer than 2 states.")
if not loop:
len_transitions -= 1
# ensure all args are the proper length
conditions = _prep_ordered_arg(len_transitions, conditions)
unless = _prep_ordered_arg(len_transitions, unless)
before = _prep_ordered_arg(len_transitions, before)
after = _prep_ordered_arg(len_transitions, after)
prepare = _prep_ordered_arg(len_transitions, prepare)
# reorder list so that the initial state is actually the first one
try:
idx = states.index(self._initial)
states = states[idx:] + states[:idx]
first_in_loop = states[0 if loop_includes_initial else 1]
except ValueError:
# since initial is not part of states it shouldn't be part of the loop either
first_in_loop = states[0]
for i in range(0, len(states) - 1):
self.add_transition(trigger, states[i], states[i + 1],
conditions=conditions[i],
unless=unless[i],
before=before[i],
after=after[i],
prepare=prepare[i],
**kwargs)
if loop:
self.add_transition(trigger, states[-1],
# omit initial if not loop_includes_initial
first_in_loop,
conditions=conditions[-1],
unless=unless[-1],
before=before[-1],
after=after[-1],
prepare=prepare[-1],
**kwargs)
def get_transitions(self, trigger="", source="*", dest="*"):
"""Return the transitions from the Machine.
Args:
trigger (str): Trigger name of the transition.
source (str, Enum or State): Limits list to transitions from a certain state.
dest (str, Enum or State): Limits list to transitions to a certain state.
"""
if trigger:
try:
events = (self.events[trigger], )
except KeyError:
return []
else:
events = self.events.values()
transitions = []
for event in events:
transitions.extend(
itertools.chain.from_iterable(event.transitions.values()))
target_source = source.name if hasattr(source, 'name') else source if source != "*" else ""
target_dest = dest.name if hasattr(dest, 'name') else dest if dest != "*" else ""
return [transition
for transition in transitions
if (transition.source, transition.dest) == (target_source or transition.source,
target_dest or transition.dest)]
def remove_transition(self, trigger, source="*", dest="*"):
"""Removes a transition from the Machine and all models.
Args:
trigger (str): Trigger name of the transition.
source (str, Enum or State): Limits removal to transitions from a certain state.
dest (str, Enum or State): Limits removal to transitions to a certain state.
"""
source = listify(source) if source != "*" else source
dest = listify(dest) if dest != "*" else dest
# outer comprehension, keeps events if inner comprehension returns lists with length > 0
tmp = {key: value for key, value in
{k: [t for t in v
# keep entries if source should not be filtered; same for dest.
if (source != "*" and t.source not in source) or (dest != "*" and t.dest not in dest)]
# }.items() takes the result of the inner comprehension and uses it
# for the outer comprehension (see first line of comment)
for k, v in self.events[trigger].transitions.items()}.items()
if len(value) > 0}
# convert dict back to defaultdict in case tmp is not empty
if tmp:
self.events[trigger].transitions = defaultdict(list, **tmp)
# if no transition is left remove the trigger from the machine and all models
else:
for model in self.models:
delattr(model, trigger)
del self.events[trigger]
def dispatch(self, trigger, *args, **kwargs):
"""Trigger an event on all models assigned to the machine.
Args:
trigger (str): Event name
*args (list): List of arguments passed to the event trigger
**kwargs (dict): Dictionary of keyword arguments passed to the event trigger
Returns:
bool The truth value of all triggers combined with AND
"""
return all(getattr(model, trigger)(*args, **kwargs) for model in self.models)
def callbacks(self, funcs, event_data):
"""Triggers a list of callbacks"""
for func in funcs:
self.callback(func, event_data)
_LOGGER.info("%sExecuted callback '%s'", self.name, func)
def callback(self, func, event_data):
"""Trigger a callback function with passed event_data parameters. In case func is a string,
the callable will be resolved from the passed model in event_data. This function is not intended to
be called directly but through state and transition callback definitions.
Args:
func (str or callable): The callback function.
1. First, if the func is callable, just call it
2. Second, we try to import string assuming it is a path to a func
3. Fallback to a model attribute
event_data (EventData): An EventData instance to pass to the
callback (if event sending is enabled) or to extract arguments
from (if event sending is disabled).
"""
func = self.resolve_callable(func, event_data)
if self.send_event:
func(event_data)
else:
func(*event_data.args, **event_data.kwargs)
@staticmethod
def resolve_callable(func, event_data):
"""Converts a model's property name, method name or a path to a callable into a callable.
If func is not a string it will be returned unaltered.
Args:
func (str or callable): Property name, method name or a path to a callable
event_data (EventData): Currently processed event
Returns:
callable function resolved from string or func
"""
if isinstance(func, str):
try:
func = getattr(event_data.model, func)
if not callable(func): # if a property or some other not callable attribute was passed
def func_wrapper(*_, **__): # properties cannot process parameters
return func
return func_wrapper
except AttributeError:
try:
module_name, func_name = func.rsplit('.', 1)
module = __import__(module_name)
for submodule_name in module_name.split('.')[1:]:
module = getattr(module, submodule_name)
func = getattr(module, func_name)
except (ImportError, AttributeError, ValueError):
raise AttributeError("Callable with name '%s' could neither be retrieved from the passed "
"model nor imported from a module." % func)
return func
def _has_state(self, state, raise_error=False):
found = state in self.states.values()
if not found and raise_error:
msg = 'State %s has not been added to the machine' % (state.name if hasattr(state, 'name') else state)
raise ValueError(msg)
return found
def _process(self, trigger):
# default processing
if not self.has_queue:
if not self._transition_queue:
# if trigger raises an Error, it has to be handled by the Machine.process caller
return trigger()
raise MachineError("Attempt to process events synchronously while transition queue is not empty!")
# process queued events
self._transition_queue.append(trigger)
# another entry in the queue implies a running transition; skip immediate execution
if len(self._transition_queue) > 1:
return True
# execute as long as transition queue is not empty
while self._transition_queue:
try:
self._transition_queue[0]()
self._transition_queue.popleft()
except BaseException:
# if a transition raises an exception, clear queue and delegate exception handling
self._transition_queue.clear()
raise
return True
def _identify_callback(self, name):
# Does the prefix match a known callback?
for callback in itertools.chain(self.state_cls.dynamic_methods, self.transition_cls.dynamic_methods):
if name.startswith(callback):
callback_type = callback
break
else:
return None, None
# Extract the target by cutting the string after the type and separator
target = name[len(callback_type) + len(self.separator):]
# Make sure there is actually a target to avoid index error and enforce _ as a separator
if target == '' or name[len(callback_type)] != self.separator:
return None, None
return callback_type, target
def __getattr__(self, name):
# Machine.__dict__ does not contain double underscore variables.
# Class variables will be mangled.
if name.startswith('__'):
raise AttributeError("'{}' does not exist on <Machine@{}>"
.format(name, id(self)))
# Could be a callback
callback_type, target = self._identify_callback(name)
if callback_type is not None:
if callback_type in self.transition_cls.dynamic_methods:
if target not in self.events:
raise AttributeError("event '{}' is not registered on <Machine@{}>"
.format(target, id(self)))
return partial(self.events[target].add_callback, callback_type)
if callback_type in self.state_cls.dynamic_methods:
state = self.get_state(target)
return partial(state.add_callback, callback_type[3:])
try:
return self.__getattribute__(name)
except AttributeError:
# Nothing matched
raise AttributeError("'{}' does not exist on <Machine@{}>".format(name, id(self)))
class MachineError(Exception):
"""MachineError is used for issues related to state transitions and current states.
For instance, it is raised for invalid transitions or machine configuration issues.
"""
def __init__(self, value):
super(MachineError, self).__init__(value)
self.value = value
def __str__(self):
return repr(self.value)
|