[docs]classEvent(str):"""An event is triggers a signal that something has happened. They are send to a state machine and allow the state machine to react. An event starts a :ref:`Transition`, which can be thought of as a “cause” that initiates a change in the state of the system. See also :ref:`events`. """id:str"""The event identifier."""name:str"""The event name."""_sm:"StateMachine | None"=None"""The state machine instance."""_transitions:"TransitionList | None"=None_has_real_id=Falsedef__new__(cls,transitions:"str | TransitionList | None"=None,id:"str | None"=None,name:"str | None"=None,_sm:"StateMachine | None"=None,):ifisinstance(transitions,str):id=transitionstransitions=None_has_real_id=idisnotNoneid=str(id)if_has_real_idelsef"__event__{uuid4().hex}"instance=super().__new__(cls,id)instance.id=idifname:instance.name=nameelif_has_real_id:instance.name=str(id).replace("_"," ").capitalize()else:instance.name=""iftransitions:instance._transitions=transitionsinstance._has_real_id=_has_real_idinstance._sm=_smreturninstancedef__repr__(self):returnf"{type(self).__name__}({self.id!r})"defis_same_event(self,*_args,event:"str | None"=None,**_kwargs)->bool:returnself==eventdef__get__(self,instance,owner):"""By implementing this method `Event` can be used as a property descriptor When attached to a SM class, if the user tries to get the `Event` instance, we intercept here and return a `BoundEvent` instance, so the user can call it as a method with the correct SM instance. """ifinstanceisNone:returnselfreturnBoundEvent(id=self.id,name=self.name,_sm=instance)
[docs]def__call__(self,*args,**kwargs):"""Send this event to the current state machine. Triggering an event on a state machine means invoking or sending a signal, initiating the process that may result in executing a transition. """# The `__call__` is declared here to help IDEs knowing that an `Event`# can be called as a method. But it is not meant to be called without# an SM instance. Such SM instance is provided by `__get__` method when# used as a property descriptor.machine=self._smkwargs={k:vfork,vinkwargs.items()ifknotin_event_data_kwargs}trigger_data=TriggerData(machine=machine,event=self,args=args,kwargs=kwargs,)machine._put_nonblocking(trigger_data)result=machine._processing_loop()ifnotisawaitable(result):returnresultreturnrun_async_from_sync(result)