dictation-toolbox / dragonfly

Speech recognition framework allowing powerful Python-based scripting and extension of Dragon NaturallySpeaking (DNS), Windows Speech Recognition (WSR), Kaldi and CMU Pocket Sphinx
GNU Lesser General Public License v3.0
388 stars 75 forks source link

ContextAction runs both the default and a context if the default action switches into that context #301

Closed kendonB closed 4 years ago

kendonB commented 4 years ago

example.py

from dragonfly import ContextAction, BringApp, AppContext, Key, Text, Pause

def bring_and_pause(app):
    BringApp("firefox").execute()
    Pause("300").execute()

ContextAction(default=bring_and_pause("firefox"), actions=[
                (AppContext("firefox"), Key("c-l") + Text("Hello!"))
            ]).execute()

Then run python example.py

kendonB commented 4 years ago

This was on Ubuntu 20.04 with the latest pip dragonfly and python3

drmfinlay commented 4 years ago

Thanks for opening this issue. This is not really a bug with ContextAction, but with your default argument. I'm guessing this is what you wanted to do:

from dragonfly import ContextAction, BringApp, AppContext, Key, Text, Pause, Function

def bring_and_pause(app):
    BringApp("firefox").execute()
    Pause("300").execute()

ContextAction(default=Function(lambda: bring_and_pause("firefox")), actions=[
                (AppContext("firefox"), Key("c-l") + Text("Hello!"))
            ]).execute()

So, bring_and_pause() had already run before your ContextAction had been initialised. You could also do something like this instead, with the same ContextAction part:

def bring_and_pause(app):
    return BringApp(app) + Pause("300")

This should be much clearer in any case. I will add some type checking into ContextAction so that it rejects "actions" that do not inherit from the ActionBase class. That way you will get an error running your original code when bring_and_pause() returns None :-)

kendonB commented 4 years ago

awesome! thanks so much

kendonB commented 4 years ago

Agree that a useful error message would be best for this case.

drmfinlay commented 4 years ago

No worries. I will add some type checking code soon and release the changes in the next version.