-
Notifications
You must be signed in to change notification settings - Fork 523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic on_enter/exit_* methods #76
Comments
Hi, they are created in def __getattr__(self, name):
if name.startswith('__'):
if name in self.__dict__:
return self.__dict__[name]
else:
raise AttributeError("{} does not exist".format(name))
terms = name.split('_')
if terms[0] in ['before', 'after']:
name = '_'.join(terms[1:])
if name not in self.events:
raise MachineError('Event "%s" is not registered.' % name)
return partial(self.events[name].add_callback, terms[0])
# that's where the magic happens
# on_enter / on_exit queries are concatenated with the current state
# and predefined with partial
elif name.startswith('on_enter') or name.startswith('on_exit'):
state = self.get_state('_'.join(terms[2:]))
return partial(state.add_callback, terms[1])
else:
if name in self.__dict__:
return self.__dict__[name]
else:
raise AttributeError("{} does not exist".format(name))
machine.on_exit_solid('say_goodbye') should work. |
Okay, after some more careful reading what you said and what the documentation states, I guess you are right. Afaik, it does not work the way its stated in the documentation. |
Good catch. I'll fix this right now. Thanks @TheMysteriousX! |
in # Add enter/exit callbacks if there are existing bound methods
enter_callback = 'on_enter_' + state_name
if hasattr(self.model, enter_callback) and \
inspect.ismethod(getattr(self.model, enter_callback)):
state.add_callback('enter', enter_callback)
exit_callback = 'on_exit_' + state_name
if hasattr(self.model, exit_callback) and \
inspect.ismethod(getattr(self.model, exit_callback)):
state.add_callback('exit', exit_callback) |
I assume we don't want to allow on_enter_* to work on the model itself, right? I mean, we definitely don't want to start patching an arbitrary model class's getattr method. So I'm just going to update the README to be clear about this. |
👍 agree |
The usage of the on_exit_* and on_enter_* callbacks was previously described incorrectly in the README.
The documentation says:
I haven't looked into this in detail yet but a quick scan doesn't show a route through the code that creates on_enter/on_exit methods for each state after init has run.
Has this ever worked?
The text was updated successfully, but these errors were encountered: