pyfsm icon indicating copy to clipboard operation
pyfsm copied to clipboard

pre and post hooks when state includes invalid function name characters

Open ewolz3 opened this issue 4 years ago • 3 comments

I'm unable to create pre_<state_name> and post_<state_name> call-backs for states that have characters not allowed in function names.

For example, a state named "in-progress" would look for a call-back pre_in-progress which is not allowed.

ewolz3 avatar Oct 22 '21 16:10 ewolz3

You are right, I wonder what can we do to prevent this issue, restrict the allowed values or convert to from - to _?

woile avatar Oct 23 '21 09:10 woile

Converting invalid characters to _ would be the more flexible approach. There are a lot of use cases for having hyphens (or even spaces) in state values.

One possible implementation would be to add a method to BaseFiniteStateMachineMixin with a default implementation that converts invalid identifier characters to _ (i.e. anything that's not small case (a-z), upper case (A-Z), digit (0-9), or an underscore). Your change_state method could then call this method when determining the name of the call-back.

This lets someone who doesn't like the package's opinionated default override the method that converts the state value to one that can be used in a Python identifier.

ewolz3 avatar Oct 24 '21 21:10 ewolz3

Sounds good to me, would you mind submitting a PR?

woile avatar Oct 25 '21 06:10 woile