Ejemplo n.º 1
0
    def _create_contexts(app_crs, rcns_to_details):
        """
        Returns a list of contexts, AppContexts based on 'executable', FuncContext, one for each
        app rule, and if more than zero app rules, the negation context for the
        global ccr rule. (Global rule should be active when none of the other
        contexts are.) If there are zero app rules, [None] will be returned
        so the result can be zipped.

        :param app_crs: list of CompatibilityResult for app rules
        :param rcns_to_details: map of {rule class name: rule details}
        :return:
        """
        contexts = []
        context_evaluations = {}

        def wrap_context(context):
            old_matches = context.matches
            context_evaluations[context] = (False, False)

            def matches(executable, title, handle):
                result, valid = context_evaluations[context]
                if valid:
                    context_evaluations[context] = (result, False)
                else:
                    try:
                        result = old_matches(executable, title, handle)
                    except:
                        result = True
                        traceback.print_exc()
                    context_evaluations[context] = (result, True)
                return result

            context.matches = matches
            return context

        for cr in app_crs:
            details = rcns_to_details[cr.rule_class_name()]
            context = AppContext(executable=details.executable,
                                 title=details.title)
            if details.function_context is not None:
                context &= FuncContext(function=details.function_context)
            contexts.append(wrap_context(context))

        negation_context = FuncContext(
            lambda **kw: [x for x in context_evaluations
                          if x.matches(**kw)] == [])

        contexts.insert(0, negation_context)
        return contexts
Ejemplo n.º 2
0
    def _create_contexts(app_crs, rcns_to_details):
        """
        Returns a list of contexts, AppContexts based on 'executable', FuncContext, one for each
        app rule, and if more than zero app rules, the negation context for the
        global ccr rule. (Global rule should be active when none of the other
        contexts are.) If there are zero app rules, [None] will be returned
        so the result can be zipped.

        :param app_crs: list of CompatibilityResult for app rules
        :param rcns_to_details: map of {rule class name: rule details}
        :return:
        """
        contexts = []
        negation_context = None
        for cr in app_crs:
            details = rcns_to_details[cr.rule_class_name()]
            context = AppContext(executable=details.executable,
                                 title=details.title)
            if details.function_context is not None:
                funkcontext = context
                funkcontext &= FuncContext(function=details.function_context)
                contexts.append(funkcontext)
            else:
                contexts.append(context)
            if details.function_context is None:
                if negation_context is None:
                    negation_context = ~context
                else:
                    negation_context &= ~context
        contexts.insert(0, negation_context)
        return contexts
Ejemplo n.º 3
0
    def create_non_ccr_grammar(self, managed_rule):
        details = managed_rule.get_details()
        rule_instance = managed_rule.get_rule_class()(name=details.name)

        if not details.transformer_exclusion:
            rule_instance = self._transformers_runner.transform_rule(
                rule_instance)

        self._smr_configurer.configure(rule_instance)

        context = None
        if details.function_context is not None:
            context = FuncContext(function=details.function_context,
                                  executable=details.executable,
                                  title=details.title)
        else:
            if details.executable is not None or details.title is not None:
                context = AppContext(executable=details.executable,
                                     title=details.title)
        self._name_uniquefier += 1
        counter = "g" + str(self._name_uniquefier)
        grammar_name = counter if details.grammar_name is None else details.grammar_name + counter
        grammar = Grammar(name=grammar_name, context=context)
        grammar.add_rule(rule_instance)
        return grammar
Ejemplo n.º 4
0
class ExclusiveManager:
    """
    Loads and switches exclusivity for caster modes
    :param mode: str
    :param modetype: 'mic_mode' or 'engine_mode' str
    """
    # TODO: Implement set_engine_mode exclusivity with mode rules.
    # TODO: Implement timer for sleep mode.
    # TODO: Implement hotkey for microphone on-off
    sleep_grammar = None
    sleeping = False

    sleep_rule = MappingRule(
        name="sleep_rule",
        mapping={
            "caster <mic_mode>":
            Function(lambda mic_mode: EngineModesManager.set_mic_mode(
                mode=mic_mode)),
            "<text>":
            Function(lambda text: False)
        },
        extras=[
            Choice("mic_mode", {
                "off": "off",
                "on": "on",
                "sleep": "sleeping",
            }),
            Dictation("text")
        ],
        context=FuncContext(lambda: ExclusiveManager.sleeping),
    )

    def __init__(self, mode, modetype):
        if modetype == "mic_mode":
            if not isinstance(ExclusiveManager.sleep_grammar, Grammar):
                grammar = ExclusiveManager.sleep_grammar = Grammar("sleeping")
                grammar.add_rule(self.sleep_rule)
                grammar.load()
            if mode == "sleeping":
                self.set_exclusive(state=True)
                printer.out("Caster: Microphone is sleeping")
            if mode == "on":
                self.set_exclusive(state=False)
                printer.out("Caster: Microphone is on")
            if mode == "off":
                printer.out("Caster: Microphone is off")
        else:
            printer.out("{}, {} not implemented".format(mode, modetype))

    def set_exclusive(self, state):
        grammar = ExclusiveManager.sleep_grammar
        ExclusiveManager.sleeping = state
        grammar.set_exclusive(state)
        get_engine().process_grammars_context()
Ejemplo n.º 5
0
def load_sleep_wake_grammar(initial_awake):
    sleep_grammar = Grammar("sleep")

    def sleep(force=False):
        global sleeping
        if not sleeping or force:
            sleeping = True
            sleep_grammar.set_exclusiveness(True)
        notify('Sleeping...')

    def wake(force=False):
        global sleeping
        if sleeping or force:
            sleeping = False
            sleep_grammar.set_exclusiveness(False)
        notify('Awake...')

    def halt(force=False):
        exit(0)

    class SleepRule(MappingRule):
        mapping = {
            'dragonfly exit':
            Function(halt),
            "start listening":
            Function(wake) +
            Function(lambda: get_engine().start_saving_adaptation_state()),
            "stop listening":
            Function(lambda: get_engine().stop_saving_adaptation_state()) +
            Function(sleep),
            "halt listening":
            Function(lambda: get_engine().stop_saving_adaptation_state()) +
            Function(sleep),
        }

    sleep_grammar.add_rule(SleepRule())

    sleep_noise_rule = MappingRule(
        name="sleep_noise_rule",
        mapping={"<text>": Function(lambda text: False and print(text))},
        extras=[Dictation("text")],
        context=FuncContext(lambda: sleeping),
    )
    sleep_grammar.add_rule(sleep_noise_rule)

    sleep_grammar.load()

    if initial_awake:
        wake(force=True)
    else:
        sleep(force=True)
Ejemplo n.º 6
0
def load_sleep_wake_grammar(initial_awake):
    sleep_grammar = Grammar("sleep")

    def sleep(force=False):
        global sleeping
        if not sleeping or force:
            sleeping = True
            sleep_grammar.set_exclusiveness(True)
        notify('sleep')

    def wake(force=False):
        global sleeping
        if sleeping or force:
            sleeping = False
            sleep_grammar.set_exclusiveness(False)
        notify('wake')

    class SleepRule(MappingRule):
        mapping = {
            "listen to me":
            Function(wake) +
            Function(lambda: get_engine().start_saving_adaptation_state()),
            "(go to sleep)|(stop listening)":
            Function(lambda: get_engine().stop_saving_adaptation_state()) +
            Function(sleep),

            # "halt listening":   Function(lambda: get_engine().stop_saving_adaptation_state()) + Function(sleep),
        }

    sleep_grammar.add_rule(SleepRule())

    sleep_noise_rule = MappingRule(
        name="sleep_noise_rule",
        mapping={
            "<text>":
            Function(lambda text: False and print("(asleep) " + text))
        },
        extras=[Dictation("text")],
        context=FuncContext(lambda: sleeping),
    )
    sleep_grammar.add_rule(sleep_noise_rule)

    sleep_grammar.load()

    if initial_awake:
        wake(force=True)
    else:
        sleep(force=True)
Ejemplo n.º 7
0
def load_sleep_wake_grammar(initial_awake):
    sleep_grammar = Grammar("sleep")

    def sleep(force=False):
        global sleeping
        if not sleeping or force:
            sleeping = True
            sleep_grammar.set_exclusiveness(True)
            get_engine().process_grammars_context()
        notify('sleep')

    def wake(force=False):
        global sleeping
        if sleeping or force:
            sleeping = False
            sleep_grammar.set_exclusiveness(False)
            get_engine().process_grammars_context()
        notify('wake')

    class SleepRule(MappingRule):
        mapping = {
            "start listening": Function(wake),
            "stop listening": Function(sleep),
            "halt listening": Function(sleep),
        }

    sleep_grammar.add_rule(SleepRule())

    sleep_noise_rule = MappingRule(
        name="sleep_noise_rule",
        mapping={"<text>": Function(lambda text: False and print(text))},
        extras=[Dictation("text")],
        context=FuncContext(lambda: sleeping),
    )
    sleep_grammar.add_rule(sleep_noise_rule)

    sleep_grammar.load()

    if initial_awake:
        wake(force=True)
    else:
        sleep(force=True)
Ejemplo n.º 8
0
def build_grammars():
    putty_context = AppContext(title="bash")
    extraterm_context = AppContext(executable="extraterm")
    netbeans_context = AppContext(executable="netbeans64")
    intellij_context = AppContext(executable="idea64")
    vs_context = AppContext(executable="devenv")
    eclipse_context = AppContext(executable="eclipse")

    bash_context = putty_context | extraterm_context

    vim_context = bash_context | netbeans_context | intellij_context | vs_context | eclipse_context

    java_context = vim_context & (build_form_context(L_JAVA)
                                  | netbeans_context
                                  | intellij_context)

    new_grammars = [
        vim.build_grammar(vim_context),
        forms.default.build_grammar(vim_context
                                    & build_form_context(L_DEFAULT)),
        forms.bash.build_grammar(vim_context & build_form_context(L_BASH)),
        forms.rust.build_grammar(vim_context & build_form_context(L_RUST)),
        forms.cpp.build_grammar(vim_context & build_form_context(L_CPP)),
        forms.cs.build_grammar(vim_context & build_form_context(L_CS)),
        #forms.java  .build_grammar(vim_context & build_form_context(L_JAVA)),
        forms.java.build_grammar(java_context),
        forms.python.build_grammar(vim_context & build_form_context(L_PYTHON)),
        forms.unity.build_grammar(vim_context & build_form_context(L_UNITY)),
        #forms.unreal.build_grammar(vim_context & build_form_context(L_UNREAL)),

        # DO NOT REMOVE THIS
        # The last grammar to load doesn't do anything for some reason
        forms.default.build_grammar(FuncContext(lambda: False)),
    ]

    return new_grammars
Ejemplo n.º 9
0
def load_sleep_wake_grammar(initial_awake):
    sleep_grammar = Grammar("sleep")
    def sleep(force=False):
        get_engine().stop_saving_adaptation_state()
        global sleeping
        if not sleeping or force:
            sleeping = True
            sleep_grammar.set_exclusiveness(True)
            notify('sleep')

    def wake(force=False):
        get_engine().start_saving_adaptation_state()
        global sleeping
        if sleeping or force:
            sleeping = False
            sleep_grammar.set_exclusiveness(False)
            notify('wake')

    class SleepRule(MappingRule):
        mapping = {
            "go to sleep":Function(sleep),
            "bake up": Function(noop),
            "rake up":Function(noop),
            "shake up":Function(noop),
            "lake up":Function(noop),
            "nake up":Function(noop),
            "wake tup":Function(noop),
            "wake sup":Function(noop),
            "whey grub":Function(noop),
            "wake":Function(noop),
            "wake up":Function(wake),
        }
    sleep_grammar.add_rule(SleepRule())

    sleep_noise_rule = MappingRule(
        name = "sleep_noise_rule",
        mapping = { "<text>": Function(lambda text: False and print("(asleep) " + text)) },
        extras = [ Dictation("text") ],
        context = FuncContext(lambda: sleeping),
    )
    sleep_grammar.add_rule(sleep_noise_rule)

    sleep_grammar.load()

    # def checkIniFile():
    #     global sleeping
    #     global sleepOverride
    #     voice = readIni('voice')
    #     new_value = (voice=="off")
    #     if new_value:
    #         sleep()
    #     else:
    #         wake()
            
    # set_interval(checkIniFile,3)
    # watchingIniFile = True
        
    



    if initial_awake:
        wake(force=True)
    else:
        sleep(force=True)
Ejemplo n.º 10
0
import os
import sys
from dragonfly import FuncContext
from castervoice.lib.context import AppContext

WINDOWS_CONTEXT = FuncContext(lambda: sys.platform == "win32")
MACOS_CONTEXT = FuncContext(lambda: sys.platform == "darwin")
LINUX_CONTEXT = FuncContext(lambda: sys.platform.startswith("linux"))
X11_CONTEXT = FuncContext(lambda: os.environ.get("XDG_SESSION_TYPE") == "x11")

TERMINAL_CONTEXT = AppContext(executable=[
    "\\sh.exe", "\\bash.exe", "\\cmd.exe", "\\mintty.exe", "\\powershell.exe",
    "gnome-terminal"
])

JETBRAINS_CONTEXT = AppContext(executable="idea", title="IntelliJ") \
          | AppContext(executable="idea64", title="IntelliJ") \
          | AppContext(executable="studio64") \
          | AppContext(executable="pycharm")

DIALOGUE_CONTEXT = AppContext(title=[
    "open",
    "save",
    "select",
])
Ejemplo n.º 11
0
def build_form_context(form):
    def is_in_form():
        return active_form() == form

    return FuncContext(is_in_form)
                print("Nothing in scratch memory for %r window "
                      "(title=%r, id=%d)" % (exe, title, handle))
                break

    def clear_formatting_state(self, option):
        if option == "current":
            stack = self._get_window_stack()
            while stack:
                stack.pop()
        elif option == "all":
            self._window_stacks.clear()


# Initialize the grammar here so we can use it to keep track of state.
grammar = DictationModeGrammar()
enabled_context = FuncContext(lambda: grammar.status)
disabled_context = FuncContext(lambda: not grammar.status)


class EnableRule(CompoundRule):
    spec = "enable <mode>"
    extras = [
        Choice("mode", {
            "command [only] mode": 0,
            "dictation plus command mode": 1,
            "command plus dictation mode": 1,
            "dictation [only] mode ": 2,
        },
               default=1)
    ]
Ejemplo n.º 13
0
    return sys.platform == "win32"


def is_macos():
    return sys.platform == "darwin"


def is_linux():
    return sys.platform.startswith("linux")


def is_x11():
    return os.environ.get("XDG_SESSION_TYPE") == "x11"


WINDOWS_CONTEXT = FuncContext(is_windows)
MACOS_CONTEXT = FuncContext(is_macos)
LINUX_CONTEXT = FuncContext(is_linux)
X11_CONTEXT = FuncContext(is_x11)

TERMINAL_CONTEXT = AppContext(executable=[
    "\\Terminus.exe", "\\sh.exe", "\\bash.exe", "\\cmd.exe", "\\mintty.exe",
    "\\powershell.exe", "gnome-terminal"
])

JETBRAINS_CONTEXT = AppContext(executable="idea", title="IntelliJ") \
          | AppContext(executable="idea64", title="IntelliJ") \
          | AppContext(executable="studio64") \
          | AppContext(executable="pycharm")

DIALOGUE_CONTEXT = AppContext(title=[