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

platform context helper functions? #293

Closed kendonB closed 4 years ago

kendonB commented 4 years ago

I'm starting to do some cross platforming for Caster. There are many places where the same action just has a slightly different key combination depending on the platform. Something like WindowsContext, MacOSContext, and LinuxContext functions would be really helpful. I've been trying to write these based on FuncContext and sys.platform == "win32", sys.platform == "darwin" and sys.platform == "linux" but haven't figured it out. Any help?

kendonB commented 4 years ago

Ok funccontext was not it. This seems to work. Is this right?

class LinuxContext(Context):
    def __init__(self):
        self._str = "Test for linux"

    def matches(self, executable, title, handle):
        return sys.platform == "linux"
drmfinlay commented 4 years ago

Hi @kendonB. It's good you're working on cross-platforming Caster.

Your Context class should work I think. You can do platform contexts with FuncContext like this:

import os
import sys

from dragonfly import FuncContext

windows_context = FuncContext(lambda: sys.platform == "win32")
darwin_context = FuncContext(lambda: sys.platform == "darwin")  # Darwin == MacOS
linux_context = FuncContext(lambda: sys.platform.startswith("linux"))
x11_context = FuncContext(lambda: os.environ.get("XDG_SESSION_TYPE") == "x11")

As for the slightly different key combinations, this has come up before in this discussion in the Caster gitter channel involving @amatveie, @LexiconCode and myself. Implementing an extensible way of solving this problem is quite complicated, as cross-platform support usually is. I have some notes on it, but I can't really commit to developing it myself. I'll try to put my notes up here soon.

kendonB commented 4 years ago

Those commands above will work fine - will close. Thanks!