Example #1
0
def refresh(_NEXUS):
    ''' should be able to add new scripts on the fly and then call this '''
    unload()
    global grammar
    grammar = Grammar("si/kuli")

    def refresh_sick_command():
        server_proxy.terminate()
        refresh(_NEXUS)

    mapping = {
        "launch sick IDE": Function(launch_IDE),
        "launch sick server": Function(launch_server),
        "refresh sick you Lee": Function(refresh_sick_command),
        "sick shot": Key("cs-2"),
    }

    rule = MergeRule(name="sik", mapping=mapping)
    gfilter.run_on(rule)
    grammar.add_rule(rule)
    grammar.load()
    # start server
    try:
        # if the server is already running, this should go off without a hitch
        start_server_proxy()
    except Exception:
        launch_server()
        seconds5 = 5
        control.nexus().timer.add_callback(server_proxy_timer_fn, seconds5)
def get_dragonfly_grammar(grammar, target, result_queue):
    global RULES
    RULES = {}

    parser = CFGParser.fromstring(grammar)
    dragonfly_rule_element = _get_dragonfly_rule_element(target, parser)

    dragonfly_grammar = Grammar("G")
    with result_queue.mutex:
        result_queue.queue.clear()

    class GrammarRule(Rule):
        def process_recognition(self, node):
            logger.info('Dragonfly node: %s', str(node))
            result = node.value()
            logger.info('Dragonfly result: %s', str(result))

            flattened_string = result
            if isinstance(result, list):
                flattened_string = " ".join(flatten(result))

            logger.info('Dragonfly flattened result: %s', str(flattened_string))

            if not result_queue.empty():
                logger.warn('There is already a message in the queue! %s', result_queue)
            result_queue.put_nowait(flattened_string)

            logger.debug("Dragonfly thread recognition Q [id=%s, qsize=%d]", id(result_queue), result_queue.qsize())

    rule = GrammarRule(element=dragonfly_rule_element, exported=True)
    dragonfly_grammar.add_rule(rule)

    return dragonfly_grammar
    def test_no_hypothesis(self):
        """
        Check that if something that doesn't match any rule is mimicked, nothing
        gets recognised.
        """
        test1 = self.get_test_function()
        test2 = self.get_test_function()

        class TestRule(MappingRule):
            mapping = {
                "testing": Function(test1),
                "<dictation>": Function(test2)
            }
            extras = [Dictation("dictation")]

        g = Grammar("test")
        g.add_rule(TestRule())
        g.load()
        self.assertTrue(g.loaded)

        self.assert_mimic_failure(None)
        self.assert_test_function_called(test1, 0)
        self.assert_test_function_called(test2, 0)
        self.assert_mimic_failure("")
        self.assert_test_function_called(test1, 0)
        self.assert_test_function_called(test2, 0)
        self.assert_mimic_success("hello")
        self.assert_test_function_called(test1, 0)
        self.assert_test_function_called(test2, 1)
Example #4
0
def generate_commands(list_of_functions):
    global server_proxy
    global grammar
    mapping = {}
    for fname in list_of_functions:
        spec = " ".join(fname.split("_"))
        mapping[spec] = Function(execute, fname=fname)
    grammar.unload()
    grammar = Grammar("sikuli")
    grammar.add_rule(MappingRule(mapping=mapping, name="sikuli server"))
    grammar.load()
Example #5
0
def load():
    global git_grammar
    context = aenea.wrappers.AeneaContext(
        ProxyAppContext(
            match='regex',
            app_id='(?i)(?:(?:DOS|CMD).*)|(?:.*(?:TERM|SHELL).*)',
        ),
        AppContext(title='git'),
    )
    git_grammar = Grammar('git', context=context)
    git_grammar.add_rule(GitRule())
    git_grammar.load()
Example #6
0
    def start(self, queue):
        #instantiating the grammar and rule
        grammar = Grammar("passthrough")
        rule = self.Passthrough()

        #attaching the event
        rule.textrecognition+=queue.put

        #adding and loading rule
        grammar.add_rule(rule)
        grammar.load()

        while 1:
            pythoncom.PumpWaitingMessages()
            time.sleep(.1)
Example #7
0
    def setupFinalDflyGrammar(self):
        log.info("Setting up final grammar.")

        assert not self.dflyGrammar
        self.dflyGrammar = Grammar(self.fullName + "Grammar")
        if self.finalDflyRule:
            self.dflyGrammar.add_rule(self.finalDflyRule)
        for r in self.independentRules:
            self.dflyGrammar.add_rule(self.concreteRules[r])
        loadStart = time.time()
        self.dflyGrammar.load()
        loadEnd = time.time()
        log.info("Grammar load time: %ss" % (loadEnd - loadStart))
        get_engine().set_exclusiveness(self.dflyGrammar, 1)

        # These should never be recognized on their own, only as part of the
        # master rule, quirk of dragonfly that you have to do this even though
        # they're only pulled in by ruleref.
        for r in self.seriesRules:
            self.concreteRules[r].disable()
        if self.terminatorRule:
            self.concreteRules[self.terminatorRule].disable()

        # independent rules only enabled via being a dependency need to have disable
        # called on their dragonfly version so that they don't get recognized by
        # themselves, same quirk.
        notEnabledRules = self.dependencyRuleSet - self.baseRuleSet
        for r in notEnabledRules:
            self.concreteRules[r].disable()

        # they're enabled by default, don't activate until explicitly made to
        self.dflyGrammar.disable()
def recognize(spec, choices_values, timeout):

    global RESULT
    RESULT = None

    grammar = Grammar("grammar")

    extras = []
    for name, choices in choices_values.iteritems():
        extras.append(Choice(name, dict((c,c) for c in choices)))

    Rule = type("Rule", (GrammarRule,),{"spec": spec, "extras": extras})
    grammar.add_rule(Rule())
    grammar.load()   

    future = time.time() + timeout
    while time.time() < future:
        if RESULT is not None:
            break

        pythoncom.PumpWaitingMessages()

        time.sleep(.1)

    grammar.unload()

    print "RESULT:", RESULT

    return RESULT
def reload_grammars():
    unload()
    global grammar
    grammar = Grammar("to rule them all")

    now = datetime.datetime.now()
    print "begun reloading at %s:%s" % (now.hour, now.minute)

    # reload module and re-add the rules imported from that module
    global GRAMMAR_IMPORTS
    for import_name in GRAMMAR_IMPORTS:
        try:
            reloader.reload(sys.modules[import_name])
            import_rule = getattr(__import__(import_name, fromlist=["rules"]), "rules")
            grammar.add_rule(import_rule)
            print "Loaded module %s successfully" % import_name
        except RuntimeError as runtime_error:
            "There was an error in file %s" % import_name
            print runtime_error, '\n', '\n'
        except NameError as nameerror:
            "Forgot something in file %s?" % import_name
            print nameerror, '\n', '\n'

    grammar.add_rule(get_reloader_rules()) # for the "reload grammar module" code in get_reloader_rules
    grammar.load()

    print "reloaded all modules"
    def test_single_dictation(self):
        """
        Test that the engine can handle a dragonfly rule using a Dictation element.
        """
        test = self.get_test_function()

        class TestRule(MappingRule):
            mapping = {"<dictation>": Function(test)}
            extras = [Dictation("dictation")]

        g = Grammar("test")
        g.add_rule(TestRule())
        g.load()
        self.assertTrue(g.loaded)
        self.assert_mimic_success("hello")
        self.assert_test_function_called(test, 1)

        # Test that it works again
        self.assert_mimic_success("hello")
        self.assert_test_function_called(test, 2)

        # Test again with multiple words
        self.assert_mimic_success("hello world")
        self.assert_test_function_called(test, 3)
Example #11
0
              Dictation("text3"),
              Choice("enable_disable",
                    {"enable": 1, "disable": 0
                    }),
              Choice("volume_mode",
                    {"mute": "mute", "up":"up", "down":"down"
                     }),
              generate_CCR_choices.__func__()
             ]
    defaults = {"n": 1, "nnv": 1,
               "text": "", "volume_mode": "setsysvolume",
               "enable":-1
               }


grammar = Grammar('general')
grammar.add_rule(MainRule())
grammar.load()

def unload():
    global grammar
    if grammar: grammar.unload()
    grammar = None
    ccr.unload()
    sikuli.unload()

if settings.SETTINGS["miscellaneous"]["status_window_enabled"]:
    utilities.report("\nWARNING: Status Window is an experimental feature, and there is a known freezing glitch with it.\n")
utilities.report("*- Starting " + settings.SOFTWARE_NAME + " -*")

Example #12
0
class BananaRule(CompoundRule):
    spec = "I like bananas [<text>]"
    extras = [Dictation("text")]
    def _process_recognition(self, node, extras):
        print("I like bananas!  (%s)" % extras.get("text", ""))
        banana_rule.disable()
        apple_rule.enable()

apple_rule = AppleRule()
banana_rule = BananaRule()


#---------------------------------------------------------------------------
# Create this module's grammar.

grammar = Grammar("fruit toggle")
grammar.add_rule(apple_rule)
grammar.add_rule(banana_rule)


#---------------------------------------------------------------------------
# Load the grammar instance and define how to unload it.

grammar.load()

# Unload function which will be called by natlink at unload time.
def unload():
    global grammar
    if grammar: grammar.unload()
    grammar = None
Example #13
0
                    {"enable": True, "disable": False
                    }),
              Choice("volume_mode",
                    {"mute": "mute", "up":"up", "down":"down"
                     }),
              generate_ccr_choices.__func__(_NEXUS),
              generate_sm_ccr_choices.__func__(_NEXUS),
              IntegerRefST("monitor", 1, 10)
             ]
    defaults = {"n": 1, "nnv": 1,
               "text": "", "volume_mode": "setsysvolume",
               "enable":-1
               }


grammar = Grammar('general')
grammar.add_rule(MainRule())
grammar.add_rule(Again(_NEXUS))
grammar.add_rule(VanillaAlias(name="vanilla alias"))
grammar.load()

_NEXUS.merger.update_config()
_NEXUS.merger.merge(Inf.BOOT)

if settings.SETTINGS["miscellaneous"]["status_window_enabled"]:
    print("\nWARNING: Status Window is an experimental feature, and there is a known freezing glitch with it.\n")
    utilities.launch_status()

print("*- Starting " + settings.SOFTWARE_NAME + " -*")

Example #14
0
        IntegerRefST("n", 0, 100),
        IntegerRefST("n1", 0, 100),
        IntegerRefST("n2", 0, 100),
        Choice("action", {
            "kick": 0,
            "psychic": 1,
            "move": 2,
        }),
        Choice("point", {
            "one": 1,
            "two": 2,
        }),
    ]
    defaults = {
        "pre": 0,
        "pre1": 0,
        "pre2": 0,
        "action": 0,
    }


#---------------------------------------------------------------------------

context = AppContext(title="rainbowgrid")
grammar = Grammar("rainbowgrid", context=context)

rule = GridControlRule(name="rainbow")
gfilter.run_on(rule)
grammar.add_rule(rule)
grammar.load()
Example #15
0
        'Open snippets':
        Key('c-k,c-b'),
        'prop snip':
        Text('prop') + Key('tab'),
        'See tour snip':
        Text('ctor') + Key('tab'),

        # Opening solutions
        'Open solution':
        Key("cs-o"),
    }
    extras = [
        Integer('tab', 1, 10),
        Integer('number', 0, 9999),
        Dictation("text"),
        Dictation("dashtext", default="").lower().replace(" ", "-"),
        Dictation("nospace", default="").lower().replace(" ", ""),
    ]


context = AppContext(executable='devenv')
grammar = Grammar('Visual Studio', context=context)
grammar.add_rule(VisualStudioMappings())
grammar.load()


def unload():
    global grammar
    if grammar: grammar.unload()
    grammar = None
Example #16
0
 def __init__(self):
     Grammar.__init__(self, name="config manager", context=None)
    def test_dictation_with_other_elements(self):
        """
        Test that the engine can handle dragonfly rules using dictation with other
        elements.

        This is being tested because the way dictation works for this engine is
        different than how the other engines handle it: Sphinx needs utterance
        pauses between grammar and dictation rule parts.
        """
        test1 = self.get_test_function()
        test2 = self.get_test_function()

        class TestRule(MappingRule):
            mapping = {
                "hello <dictation>": Function(test1),
                "<dictation> testing": Function(test2),
            }
            extras = [Dictation("dictation")]

        g = Grammar("test")
        g.add_rule(TestRule())
        g.load()
        self.assertTrue(g.loaded)

        # Dictation and other elements are processed separately with pauses
        self.assert_mimic_success("hello", "world")  # test mapping 1
        self.assert_test_function_called(test1, 1)
        self.assert_test_function_called(test2, 0)

        # Test with mapping 2
        self.assert_mimic_success("testing", "testing")

        # Test that it works again with mapping 1
        self.reset_test_functions()
        self.assert_mimic_success("hello", "world")
        self.assert_test_function_called(test1, 1)
        self.assert_test_function_called(test2, 0)

        # Test with mapping 2 again
        self.reset_test_functions()
        self.assert_mimic_success("test", "testing")
        self.assert_test_function_called(test1, 0)
        self.assert_test_function_called(test2, 1)

        # Test again with multiple words
        self.reset_test_functions()
        self.assert_mimic_success("hello", "hi there")
        self.assert_test_function_called(test1, 1)
        self.assert_test_function_called(test2, 0)

        # And test again using mapping 2
        self.reset_test_functions()
        self.assert_mimic_success("test test test", "testing")
        self.assert_test_function_called(test2, 1)
        self.assert_test_function_called(test1, 0)

        # Test incomplete sequences
        self.reset_test_functions()
        self.assert_mimic_success("hello")  # start of mapping one
        self.assert_test_function_called(test1, 0)  # incomplete sequence
        self.assert_mimic_success("test", "testing")
        self.assert_test_function_called(test1,
                                         1)  # mapping one completely matched
        # mapping two only partially matched with <dictation> as "testing"
        self.assert_test_function_called(test2, 0)
Example #18
0
        Key("c-w,h"),
        "window right":
        Key("c-w,l"),
        "window up":
        Key("c-w,k"),
        "window down":
        Key("c-w,j"),
        "terminal tabulator next":
        Key("c-w") + Text(":tabn") + Key('enter'),
        "terminal tabulator previous":
        Key("c-w") + Text(":tabp") + Key('enter'),
    }


# The main Python grammar rules are activated here
vimTerminalBootstrap = Grammar("vim terminal bootstrap")
vimTerminalBootstrap.add_rule(TerminalEnabler())
vimTerminalBootstrap.load()

terminalGrammer = Grammar("terminal grammar")
terminalGrammer.add_rule(TerminalCommands())
terminalGrammer.add_rule(TerminalDisabler())
terminalGrammer.load()
terminalGrammer.disable()


# Unload function which will be called by natlink at unload time.
def unload():
    global terminalGrammer
    if terminalGrammer: terminalGrammer.unload()
    terminalGrammer = None
Example #19
0
from dragonfly import (Grammar, AppContext, MappingRule, Dictation, Key, Text,
                       Integer, Mimic)

putty_context = AppContext(executable="putty")
bash_context = AppContext(title="bash")
grammar = Grammar("bash", context=(putty_context | bash_context))
noSpaceNoCaps = Mimic("\\no-caps-on") + Mimic("\\no-space-on")

rules = MappingRule(
    name="bash",
    mapping={
        "term <n>":
        Key('c-b') + Text("%(n)d"),
        "term create":
        Key('c-b, c'),
        "term north":
        Key('c-b, k'),
        "term south":
        Key('c-b, j'),
        "term west":
        Key('c-b, h'),
        "term east":
        Key('c-b, l'),
        "term vertical":
        Key('c-b, v'),
        "term split":
        Key('c-b, s'),
        "term detach":
        Key('c-b, d'),
        "term down [<n>]":
        Key('c-b, colon') + Text("resize-pane -D %(n)d") + Key('enter'),
Example #20
0
                "(variable|variables)": "variable",
                "( parameter | parameters)": "parameter",
                "(module|modules)": "module",
                "(import|imported) (value|item|object|element)":
                "import value",
                "function ( name |names)": "function name",
            }),
    ]
    defaults = {
        "adjective": "None",
        "ndir": 1,
        "level_index": 1,
        "big_roi_sub_index": 0,
        "paste_back_index": 0,
    }


#---------------------------------------------------------------------------

context = AppContext(executable="sublime_text")
grammar = Grammar("pvcp", context=context)

if settings.SETTINGS["apps"]["sublime"]:
    if settings.SETTINGS["miscellaneous"]["rdp_mode"]:
        control.nexus().merger.add_global_rule(NoobRule())
    else:
        rule = NoobRule(name="python voice coding plugin")
        gfilter.run_on(rule)
        grammar.add_rule(rule)
        grammar.load()
Example #21
0
        "greater than or equal to": Text(">="),
    }


class PHPMiscellaneousStuff(MappingRule):
    mapping = {
        "PHP block": Text("<?php  ?>"),
        "require": Text("require ('');"),
        "require once": Text("require_once ('');"),
        "include": Text("include ('');"),
        "include once": Text("require_once ('');"),
        "equals": Text(" = "),
    }


phpBootstrap = Grammar(
    "php bootstrap")  # Create a grammar to contain the command rule.
phpBootstrap.add_rule(PHPEnabler())
phpBootstrap.load()

phpGrammar = Grammar("php grammar")
phpGrammar.add_rule(PHPTestRule())
phpGrammar.add_rule(PHPDataTypes())
phpGrammar.add_rule(PHPVariableDeclarations())
phpGrammar.add_rule(PHPCommentsSyntax())
phpGrammar.add_rule(PHPSuperGlobals())
phpGrammar.add_rule(PHPControlStructures())
phpGrammar.add_rule(PHPAccessModifiers())
phpGrammar.add_rule(PHPUsefulMethods())
phpGrammar.add_rule(PHPLogicalOperators())
phpGrammar.add_rule(PHPAssignmentOperators())
phpGrammar.add_rule(PHPArithmeticOperators())
Example #22
0
from caster.lib.dfplus.state.short import R


class photoshopRule(MergeRule):
    pronunciation = "Photo shop"

    mapping = {
        "new (file | pane)": R(Key("c-n"), rdescript="Photoshop: New File"),
        "open file": R(Key("c-o"), rdescript="Photoshop: Open File"),
        "close file": R(Key("c-w"), rdescript="Photoshop: Open File"),
        "transform": R(Key("c-t"), rdescript="Photoshop: Open File"),
        "new layer": R(Key("cs-n"), rdescript="Photoshop: New File"),
        "open folder": R(Key("cs-o"), rdescript="Photoshop: Open Folder"),
        "save as": R(Key("cs-s"), rdescript="Photoshop: Save As"),
    }

    extras = []
    defaults = {}


context = AppContext(executable="photoshop", title="photoshop")
grammar = Grammar("photoshop", context=context)
if settings.SETTINGS["apps"]["photoshop"]:
    if settings.SETTINGS["miscellaneous"]["rdp_mode"]:
        control.nexus().merger.add_global_rule(photoshopRule())
    else:
        rule = photoshopRule()
        gfilter.run_on(rule)
        grammar.add_rule(photoshopRule(name="photoshop"))
        grammar.load()
    def test_recognition_observer(self):
        """
        Test that the engine's recognition observer manager works correctly.
        """
        on_begin_test = self.get_test_function()
        on_recognition_test = self.get_test_function()
        on_failure_test = self.get_test_function()
        on_next_rule_part_test = self.get_test_function()

        # Set up a custom observer using test methods
        class TestObserver(RecognitionObserver):
            on_begin = on_begin_test
            on_next_rule_part = on_next_rule_part_test
            on_recognition = on_recognition_test
            on_failure = on_failure_test

        # Set up a TestObserver instance and a grammar with multiple rules to use
        observer = TestObserver()
        observer.register()
        grammar = Grammar("test")
        grammar.add_rule(CompoundRule("rule1", "hello world"))
        grammar.add_rule(
            CompoundRule("rule2",
                         "say <dictation>",
                         extras=[Dictation("dictation")]))
        grammar.load()
        self.assertTrue(grammar.loaded)

        # Test that each method is called properly
        self.assert_mimic_success("hello world")

        # on_begin is called during each mimic. on_recognition should be called
        # once per successful and complete recognition. Both on_failure and
        # on_next_rule_part shouldn't have been called yet.
        self.assert_test_function_called(on_begin_test, 1)
        self.assert_test_function_called(on_recognition_test, 1)
        self.assert_test_function_called(on_failure_test, 0)
        self.assert_test_function_called(on_next_rule_part_test, 0)

        # Test with a dictation rule
        self.assert_mimic_success("say")

        # Recognition begins again, is incomplete and no failure yet.
        self.assert_test_function_called(on_begin_test, 2)
        self.assert_test_function_called(on_recognition_test, 1)
        self.assert_test_function_called(on_failure_test, 0)

        # on_next_rule_part should be called because there are more rule parts
        self.assert_test_function_called(on_next_rule_part_test, 1)

        # Test the next part of the dictation rule
        self.assert_mimic_success("testing testing")

        # Recognition begins again, is complete, and no failure yet.
        self.assert_test_function_called(on_begin_test, 3)
        self.assert_test_function_called(on_recognition_test, 2)
        self.assert_test_function_called(on_failure_test, 0)

        # on_next_rule_part shouldn't have been called because this is the last part
        # and on_recognition will be called instead
        self.assert_test_function_called(on_next_rule_part_test, 1)

        # Recognition begins again and failure occurs.
        self.assert_mimic_failure("testing")
        self.assert_test_function_called(on_begin_test, 4)
        self.assert_test_function_called(on_next_rule_part_test,
                                         1)  # no change
        self.assert_test_function_called(on_failure_test, 1)
        self.assert_test_function_called(on_recognition_test, 2)

        # Test that using None or "" also calls the on_failure method
        self.assert_mimic_failure(None)
        self.assert_test_function_called(on_failure_test, 2)
        self.assert_mimic_failure("")
        self.assert_test_function_called(on_failure_test, 3)
        self.assert_test_function_called(on_next_rule_part_test,
                                         1)  # no change

        # Unregister the observer
        observer.unregister()
Example #24
0
        'yarn <text>':
        Key('cs-`') + Pause('10') + Text("yarn %(text)s") +
        Key("enter"),  #install, lint, clean, build, dev
        'build wheel':
        Key('cs-`') + Pause('10') + Text('./buildLoadWheelToCluster.sh') +
        Key('enter'),

        # 'test': bar("text"),
    }
    extras = [
        Integer('tab', 1, 20),
        Integer('number', 1, 9999),
        Integer('n', 1, 9999),
        Dictation("text"),
        Dictation("nocaps", default="").lower(),
        Dictation("camel_text", default="").camel(),
        Dictation("snaketext", default="").lower().replace(" ", "_"),
    ]


context = AppContext(executable='code')
grammar = Grammar('Visual Studio Code', context=context)
grammar.add_rule(CodeMappings())
grammar.load()


def unload():
    global grammar
    if grammar: grammar.unload()
    grammar = None
    for j in xrange(extras["n"]):
      for sequence, count in macros.get(extras["m"], []):
        for i in xrange(count):
          time.sleep(KEYSTROKE_DELAY)
          for action in sequence:
            action.execute()

class MacroBegin(CompoundRule):
  spec = "begin macro <n>"
  extras = [IntegerRef("n", 0, 100)]

  def _process_recognition(self, node, extras):
    global active_macro
    if active_macro is None:
      active_macro = extras["n"]
      macros[extras["n"]] = []

grammar = Grammar("voiceofarmok", context=df_context)
grammar.add_rule(RepeatRule())
grammar.add_rule(MacroBegin())
grammar.add_rule(MacroEnd())
grammar.add_rule(MacroPlay())

grammar.load()

def unload():
  global grammar
  if grammar:
    grammar.unload()
  grammar = None
Example #26
0
from dragonfly import (Grammar, AppContext, MappingRule, Dictation, Integer,
                       Key, Text, Function)

from utils.format import (snake_case, pascal_case, kebab_case, camel_case)

def transform(text, transform_func):
    result = transform_func(text)
    return Text(f'{result}').execute()

grammar = Grammar("general")

class GeneralRule(MappingRule):
    mapping = {
        "tab": Key("tab"),
        "enter": Key("enter"),
        "dictate <text>": Text("%(text)s"),
        "(dictate | type | say) snake [case] <text>": Function(
            lambda text: transform(text, snake_case)),
        "(dictate | type | say) pascal [case] <text>": Function(
            lambda text: transform(text, pascal_case)),
        "(dictate | type | say) kebab [case] <text>": Function(
            lambda text: transform(text, kebab_case)),
        "(dictate | type | say) camel [case] <text>": Function(
            lambda text: transform(text, camel_case)),
    }

    extras = [Dictation("text")]

class SpecialCharacters(MappingRule):
    mapping = {
        "colon [symbol]": Key("colon"),
    def test_long_sequence_rule(self):
        """
        Test that the engine can recognise a long sequence rule and that the
        timeout functionality works correctly.
        """
        timeout = 0.1
        self.engine.config.NEXT_PART_TIMEOUT = timeout

        # Set up a test grammar with some rules
        test1 = self.get_test_function()
        test2 = self.get_test_function()

        class TestRule(MappingRule):
            mapping = {
                "testing": Function(test1),
                "testing <dictation> testing <dictation>": Function(test2),
            }
            extras = [Dictation("dictation")]

        grammar = Grammar("test")
        grammar.add_rule(TestRule())
        grammar.load()
        self.assertTrue(grammar.loaded)

        # Test that mapping 2 can be recognised fully
        self.assert_mimic_success("testing", "hello", "testing")
        self.assert_mimic_success("hello")
        self.assert_test_function_called(test2, 1)
        self.assert_test_function_called(test1, 0)

        # Start recognising mapping 2 again, then sleep for a bit
        self.assert_mimic_success("testing")
        time.sleep(timeout + 0.1)

        # Only mapping 1 should have been processed
        self.assert_test_function_called(test1, 1)
        self.assert_test_function_called(test2, 1)

        # Test that it works with a break shorter than the timeout value
        self.assert_mimic_success("testing")
        time.sleep(timeout / 2)
        self.assert_mimic_success("hello")
        time.sleep(timeout / 2)
        self.assert_mimic_success("testing")
        time.sleep(timeout / 2)
        self.assert_mimic_success("hello")
        self.assert_test_function_called(test2, 2)

        # Test with a timeout of 0 (no timeout)
        timeout = 0
        self.engine.config.NEXT_PART_TIMEOUT = timeout
        self.assert_mimic_success("testing")
        time.sleep(0.1)  # sleep for 100ms
        self.assert_test_function_called(test2, 2)  # no changes

        # Test that mapping 1 won't process any more, even after a time.
        self.assert_test_function_called(test1, 1)

        # Mimicking the rest of the rule parts should make the rule process.
        self.assert_mimic_success("hello", "testing", "hello")
        self.assert_test_function_called(test2, 3)
Example #28
0
from dragonfly import (Grammar, CompoundRule, Config, Section, Item)

import natlink

config = Config("snore");
config.lang = Section("Language section");
config.lang.snore = Item("snore", doc="Put the microphone to sleep")

class SnoreRule(CompoundRule):

  spec = config.lang.snore
  
  def _process_recognition(self, node, extras):
    self._log.debug("sleepy mic")
    natlink.setMicState("sleeping")

grammar = Grammar("snore")
grammar.add_rule(SnoreRule())
grammar.load()

def unload():
  global grammar
  if grammar: grammar.unload()
  grammar = None
Example #29
0
rules = MappingRule(
	name = "general",
	mapping = { 
		"slap": Key("enter"),
		"Max when": Key("w-up"),
		"left when": Key("w-left"),
		"right when": Key("w-right"),
		"min win": Key("w-down"),
    "switch apps": Key("alt:down, tab"),
		"switch app": Key("a-tab"),
    "termi": Key("w-b/10, s-tab/10, enter"),
    "foxy": Key("w-b/10, s-tab/10, right:1/10, enter"),
    "foxy reload": Key("w-b/10, s-tab/10, right:1/10, enter/10, f5"),
    "Jimmy": Key("w-b/10, s-tab/10, right:2/10, enter"), 
    "Heidi": Key("w-b/10, s-tab/10, right:3/10, enter"),
    "chrome": Key("w-b/10, s-tab/10, right:4/10, enter"),
    "chrome reload": Key("w-b/10, s-tab/10, right:4/10, enter/10, f5"),
    "bashing": Key("w-b/10, s-tab/10, right:5/10, enter"),
    "code mode": Mimic("\\no-caps-on") + Mimic("\\no-space-on"),
	}
)

grammar = Grammar("general")
grammar.add_rule(rules)
grammar.load()

def unload():
  global grammar
  if grammar: grammar.unload()
  grammar = None
Example #30
0
from dragonfly import (Key, Grammar, Mimic,
                       Dictation, MappingRule, Text)

class MainRule(MappingRule):
	mapping = {
		"snore": Mimic("stop", "listening"),
	}
 
grammar = Grammar("main")
grammar.add_rule(MainRule())
grammar.load()
Example #31
0
        'next': Key("c-tab"),
        'previous': Key("cs-tab"),
        'close': Key('c-w'),
    }
    extras = [
        IntegerRef("n", 1, 10),
        IntegerRef("int", 1, 10000),
        Dictation("text"),
    ]
    defaults = {
        "n": 1,
    }


#---------------------------------------------------------------------------
# Create and load this module's grammar.

#context = AppContext(executable="Foxit Reader")
#grammar = Grammar("foxit reader", context=context)
grammar = Grammar("foxit reader")
#grammar.add_rule(CommandRule())
grammar.add_rule(StaticRule())
grammar.load()


# Unload function which will be called by natlink at unload time.
def unload():
    global grammar
    if grammar: grammar.unload()
    grammar = None
Example #32
0
from dragonfly import Grammar, CompoundRule

# Voice command rule combining spoken form and recognition processing.
class ExampleRule(CompoundRule):
    spec = "do something computer"                  # Spoken form of command.
    def _process_recognition(self, node, extras):   # Callback when command is spoken.
        print "Voice command spoken."

# Create a grammar which contains and loads the command rule.
grammar = Grammar("example grammar")                # Create a grammar to contain the command rule.
grammar.add_rule(ExampleRule())                     # Add the command rule to the grammar.
grammar.load()                                      # Load the grammar.
Example #33
0
class BaseMode:
    quadrant3x3 = 0
    quadrant4x3 = 0
    speech_commands = {}
    patterns = {}
    toggles = {}
    use_direct_keys = False
    input_release_lag = 0
                
    def __init__(self, modeSwitcher, is_testing=False, repeat_delay=REPEAT_DELAY, repeat_rate=REPEAT_RATE):
        self.inputManager = InputManager(is_testing=is_testing, use_direct_keys=self.use_direct_keys, input_release_lag_ms=self.input_release_lag * 1000)
        self.mode = "regular"
        self.modeSwitcher = modeSwitcher
        self.detector = PatternDetector(self.patterns)
        self.pressed_keys = {}
        self.should_drag = False
        self.ctrlKey = False
        self.shiftKey = False
        self.altKey = False
        
        if( SPEECHREC_ENABLED == True ):
            from dragonfly import Grammar
            from lib.grammar.simple_grammar import SimpleSpeechCommand
            import pythoncom
            self.grammar = Grammar("Simple")
            self.simpleCommandRule = SimpleSpeechCommand(self.speech_commands, callback=self.toggle_speech)
            self.grammar.add_rule( self.simpleCommandRule )
        
    def start( self ):
        update_overlay_image( "default" )
        toggle_eyetracker()
        
    def exit( self ):
        if( self.mode == "speech" ):
            self.toggle_speech()
    
        update_overlay_image( "default" )
        toggle_eyetracker()
                    
    def handle_input( self, dataDicts ):
        self.detector.tick( dataDicts )
        self.quadrant3x3 = self.detector.detect_mouse_quadrant( 3, 3 )
        self.quadrant4x3 = self.detector.detect_mouse_quadrant( 4, 3 )
        
        if( self.detect_silence() ):
            self.stop_drag_mouse()
            self.inputManager.release_non_toggle_keys()
                
        # Recognize speech commands in speech mode
        if( self.mode == "speech" ):
            pythoncom.PumpWaitingMessages()
            self.handle_speech( dataDicts )
            
        # Regular quick command mode
        elif( self.mode == "regular" ):
            self.handle_sounds( dataDicts )
            
        return self.detector.tickActions
                
    def handle_speech( self, dataDicts ):
        print( "No speech handler" )
        return
        
    def handle_sounds( self, dataDicts ):
        print( "No sound handler" )
        return
        
    # Toggle between variables
    # If the value is a list, turn them on in sequence after another
    def toggle( self, value ):
        if (isinstance(value, list)):
            turned_on_index = -1
            for index, key in enumerate(value):
                if (key not in self.toggles):
                    self.toggles[key] = False
                elif (self.toggles[key] == True):
                    turned_on_index = index
                    self.toggles[key] = False
                    
            next_index = turned_on_index + 1
            if (next_index >= len(value)):
                next_index = 0

            self.toggles[value[next_index]] = True
        else:
            if (value not in self.toggles ):
                self.toggles[value] = False
                
            self.toggles[value] = not self.toggles[value]
    
    def enable( self, value ):
        if (isinstance(value, list)):
            for index, key in enumerate(value):
                self.toggles[key] = True
        else:
            self.toggles[value] = True

    def disable( self, value ):
        if (isinstance(value, list)):
            for index, key in enumerate(value):
                self.toggles[key] = False
        else:
            self.toggles[value] = False
        
    def detect( self, key ):
        if (key in self.toggles):
            return self.toggles[key]
    
        return self.detector.detect( key )
        
    def detect_silence( self ):
        return self.detector.detect_silence()        
        
    def drag_mouse( self ):
        self.toggle_drag_mouse( True )

    def stop_drag_mouse( self ):
        self.toggle_drag_mouse( False )
                
    def leftclick( self ):
        self.inputManager.click(button='left')

    def rightclick( self ):
        self.inputManager.click(button='right')
        
    def press( self, key ):
        self.inputManager.press( key )
        
    def hold( self, key, repeat_rate_ms=0 ):
        self.inputManager.hold( key, repeat_rate_ms )
        
    def release( self, key ):
        self.inputManager.release( key )
        
    def release_special_keys( self ):
        self.inputManager.release_special_keys()
        
    def toggle_speech( self ):
        if( self.mode != "speech" ):
            self.mode = "speech"
            self.grammar.load()
            print( "--- TOGGLING SPEECH RECOGNITION ON" )
        else:
            self.mode = "regular"
            self.grammar.unload()
            print( "--- TOGGLING SPEECH RECOGNITION OFF" )
        toggle_speechrec()

    # Drag mouse for selection purposes
    def toggle_drag_mouse( self, should_drag ):
        if( self.should_drag != should_drag ):
            if( should_drag == True ):
                self.inputManager.mouseDown()
            else:
                self.inputManager.mouseUp()
                
        self.should_drag = should_drag
        
    # Detect when the cursor is inside an area
    def detect_inside_area( self, x, y, width, height ):
        return self.detector.detect_inside_minimap( x, y, width, height )

    def update_overlay( self ):
        if( not( self.ctrlKey or self.shiftKey or self.altKey ) ):
            update_overlay_image( "default" )
        else:
            modes = []
            if( self.ctrlKey ):
                modes.append( "ctrl" )
            if( self.shiftKey ):
                modes.append( "shift" )
            if( self.altKey ):
                modes.append( "alt" )
                
            update_overlay_image( "mode-%s" % ( "-".join( modes ) ) )        
        "cap rubber set up remote aliases <text>": Text("RUBBER_ENV=") + Function(lib.format.lowercase_text) + Text(" cap rubber:setup_remote_aliases"),
        "cap rubber set up D N S aliases <text>": Text("RUBBER_ENV=") + Function(lib.format.lowercase_text) + Text(" cap rubber:setup_dns_aliases"),
        "cap rubber add role to <text>": Text("RUBBER_ENV=") + Function(lib.format.lowercase_text) + Text(" cap rubber:roles:add ROLES= ALIAS=") + Function(lib.format.lowercase_text),
        "cap rubber create staging <text>": Text("RUBBER_ENV=") + Function(lib.format.lowercase_text) + Text(" cap rubber:create_staging"),
        "cap rubber destroy staging <text>": Text("RUBBER_ENV=") + Function(lib.format.lowercase_text) + Text(" cap rubber:destroy_staging"),
        "cap rubber monit start": Text("cap rubber:monit:start RUBBER_ENV="),
        "cap rubber monit stop": Text("cap rubber:monit:stop RUBBER_ENV="),
        "cap rubber (postgres|PostgreSQL) start": Text("cap rubber:postgresql:start RUBBER_ENV="),
        "cap rubber (postgres|PostgreSQL) stop": Text("cap rubber:postgresql:stop RUBBER_ENV="),
        }
)

class MyCommandsRule(MappingRule):
    mapping = config.cmd.map

    extras = [
        Dictation("text"),
    ]

global_context = None  # Context is None, so grammar will be globally active.
terminator_grammar = Grammar("Capistrano commands", context=global_context)  # Create this module's grammar.
terminator_grammar.add_rule(MyCommandsRule())  # Add the top-level rule.
terminator_grammar.load()  # Load the grammar.


def unload():
    """Unload function which will be called at unload time."""
    global terminator_grammar
    if grammar:
        grammar.unload()
    grammar = None
Example #35
0
        }
    extras = [
        Dictation("text"),
        IntegerRefST("n", 1, 1000),
        Boolean("back"),
    ]
    defaults = {"n": 1, "back": False}


#---------------------------------------------------------------------------

context = AppContext(executable="javaw", title="Eclipse") | AppContext(
    executable="eclipse",
    title="Eclipse") | AppContext(executable="AptanaStudio3")
grammar = Grammar("Eclipse", context=context)

if settings.SETTINGS["apps"]["eclipse"]:
    #     if settings.SETTINGS["miscellaneous"]["rdp_mode"]:
    #         control.nexus().merger.add_global_rule(EclipseRule())
    #         control.nexus().merger.add_global_rule(EclipseCCR())
    #     else:
    #         control.nexus().merger.add_app_rule(EclipseCCR(), context)

    rule = EclipseRule(name="eclipse")
    gfilter.run_on(rule)
    ccr = EclipseCCR()
    gfilter.run_on(ccr)
    grammar.add_rule(rule)
    grammar.add_rule(ccr)
    grammar.load()
    # Here we define this rule's spoken-form and special elements.
    spec = "<sequence> [[[and] repeat [that]] <n> times]"
    extras = [
        sequence,  # Sequence of actions defined above.
        IntegerRef("n", 1, 100),  # Times to repeat the sequence.
    ]
    defaults = {
        "n": 1,  # Default repeat count.
    }

    def _process_recognition(self, node, extras):  # @UnusedVariable
        sequence = extras["sequence"]  # A sequence of actions.
        count = extras["n"]  # An integer repeat count.
        for i in range(count):  # @UnusedVariable
            for action in sequence:
                action.execute()
        release.execute()


grammar = Grammar("Generic edit", context=GlobalDynamicContext())
grammar.add_rule(RepeatRule())  # Add the top-level rule.
grammar.load()  # Load the grammar.


def unload():
    """Unload function which will be called at unload time."""
    global grammar
    if grammar:
        grammar.unload()
    grammar = None
Example #37
0
    mapping = {
        "address bar":
        R(Key("a-d"), rdescript="Explorer: Address Bar"),
        "new folder":
        R(Key("cs-n"), rdescript="Explorer: New Folder"),
        "new file":
        R(Key("a-f, w, t"), rdescript="Explorer: New File"),
        "(show | file | folder) properties":
        R(Key("a-enter"), rdescript="Explorer: Properties Dialog"),
    }
    extras = [
        Dictation("text"),
        IntegerRefST("n", 1, 1000),
    ]
    defaults = {"n": 1}


#---------------------------------------------------------------------------

context = AppContext(executable="explorer")
grammar = Grammar("Windows Explorer", context=context)

if settings.SETTINGS["apps"]["explorer"]:
    if settings.SETTINGS["miscellaneous"]["rdp_mode"]:
        control.nexus().merger.add_global_rule(IERule())
    else:
        rule = IERule(name="explorer")
        gfilter.run_on(rule)
        grammar.add_rule(rule)
        grammar.load()
Example #38
0
#---------------------------------------------------------------------------
# Create the main command rule.


def swap_macros():
    os.rename("c:/NatLink/NatLink/MacroSystem",
              "c:/NatLink/NatLink/MacroSystem.temp")
    os.rename("c:/NatLink/NatLink/MacroSystem.other",
              "c:/NatLink/NatLink/MacroSystem")
    os.rename("c:/NatLink/NatLink/MacroSystem.temp",
              "c:/NatLink/NatLink/MacroSystem.other")


class CommandRule(MappingRule):
    mapping = {
        "swap macros directory": Function(swap_macros),
    }


grammar = Grammar("safety")
grammar.add_rule(CommandRule())
grammar.load()


# Unload function which will be called by natlink at unload time.
def unload():
    global grammar
    if grammar: grammar.unload()
    grammar = None
  extras = [
    IntegerRef("n", 1, 10),
    Dictation("room"),
    ]

  defaults = {
    "n": 1,
    }

class ChatRule(MappingRule):
  mapping = {
    "at <user>":     Text("@%(user)s "),
    "send":          Key("enter"),
  }

  extras = [
    Choice("user", config.usernames.map),
  ]


context = AppContext(executable="hipchat")
terminator_grammar = Grammar("hipchat_general", context=context)
terminator_grammar.add_rule(NavigationRule())
terminator_grammar.add_rule(ChatRule())
terminator_grammar.load()

# Unload function which will be called by natlink at unload time.
def unload():
  global terminator_grammar
  if grammar: grammar.unload()
  grammar = None
Example #40
0
    "vagrant up [with] virtual box":
    Text("vagrant up --provider=virtualbox"),
    "vagrant up [with] VMware":
    Text("vagrant up --provider=vmware_workstation"),
    "vagrant ssh":
    Text("vagrant ssh"),
})


class MyCommandsRule(MappingRule):
    mapping = config.cmd.map

    extras = [
        Dictation("text"),
    ]


terminator_grammar = Grammar(
    "My commands",
    context=GlobalDynamicContext())  # Create this module's grammar.
terminator_grammar.add_rule(MyCommandsRule())  # Add the top-level rule.
terminator_grammar.load()  # Load the grammar.


def unload():
    """Unload function which will be called at unload time."""
    global terminator_grammar
    if grammar:
        grammar.unload()
    grammar = None
Example #41
0
    nexus.comm.get_com("hmc").do_action("dir")

def hmc_confirm(value, nexus):
    nexus.comm.get_com("hmc").do_action(value)
    
def hmc_settings_complete(nexus):
    nexus.comm.get_com("hmc").complete()
    


class HMCRule(MappingRule):
    mapping = {
        "kill homunculus":              R(Function(kill, nexus=_NEXUS), rdescript="Kill Helper Window"),
        "complete":                     R(Function(complete, nexus=_NEXUS), rdescript="Complete Input")
    }
grammar = Grammar("hmc", context=AppContext(title=settings.HOMUNCULUS_VERSION))
grammar.add_rule(HMCRule())
grammar.load()

class HMCHistoryRule(MappingRule):
    mapping = {
        # specific to macro recorder
        "check <n>":                    R(Function(hmc_checkbox, nexus=_NEXUS), rdescript="Check Checkbox"),
        "check from <n> to <n2>":       R(Function(hmc_recording_check_range, nexus=_NEXUS), rdescript="Check Range"),
        "exclude <n>":                  R(Function(hmc_recording_exclude, nexus=_NEXUS), rdescript="Uncheck Checkbox"),
        "[make] repeatable":            R(Function(hmc_recording_repeatable, nexus=_NEXUS), rdescript="Make Macro Repeatable")
    }   
    extras = [
              IntegerRefST("n", 1, 25),
              IntegerRefST("n2", 1, 25),
             ]
Example #42
0
from dragonfly import (Grammar, AppContext, MappingRule, Dictation,
                       Key, Text, FocusWindow, IntegerRef, Choice,
                       Pause)
from dragonglue import LinuxAppContext

#---------------------------------------------------------------------------
# Create this module's grammar and the context under which it'll be active.

context = LinuxAppContext(executable='pycharm')
grammar = Grammar("pycharm", context=context)


#---------------------------------------------------------------------------
# Create a mapping rule which maps things you can say to actions.
#
# Note the relationship between the *mapping* and *extras* keyword
#  arguments.  The extras is a list of Dragonfly elements which are
#  available to be used in the specs of the mapping.  In this example
#  the Dictation("text")* extra makes it possible to use "<text>"
#  within a mapping spec and "%(text)s" within the associated action.

example_rule = MappingRule(
    name="pycharm",    # The name of the rule.
    mapping={
        'command': Key('cs-a'),
        'command <text>': Key('cs-a/5') + Text('%(text)s'),

        # file and editor navigation
        '[Activate] editor': Key('escape:2'),
        'close [file]': Key('c-f4'),
        'close tab': Key('c-f4'),
        # Protocols.
        "protocol H T T P": Text("http://"),
        "protocol H T T P S": Text("https://"),
        "protocol (git|G I T)": Text("git://"),
        "protocol F T P": Text("ftp://"),
        "protocol S S H": Text("ssh://"),
        },
    extras=[
        IntegerRef("n", 1, 100),
        Dictation("text"),
        ],
    defaults={
        "n": 1
    }
)

context = None
if config.get("aenea.enabled", False) == True:
    context = aenea.global_context
grammar = Grammar("Programming help", context=context)
grammar.add_rule(series_rule)
grammar.load()


# Unload function which will be called at unload time.
def unload():
    global grammar
    if grammar:
        grammar.unload()
    grammar = None
Example #44
0
class LockRule(CompoundRule):

    spec = config.lang.lock_screen

    def _process_recognition(self, node, extras):
        self._log.debug("%s: locking screen." % self)

        # Put the microphone to sleep.
        natlink.setMicState("sleeping")

        # Lock screen.
        success = ctypes.windll.user32.LockWorkStation()
        if not success:
            self._log.error("%s: failed to lock screen." % self)


# ---------------------------------------------------------------------------
# Create and manage this module's grammar.

grammar = Grammar("lock screen")
grammar.add_rule(LockRule())
grammar.load()


def unload():
    global grammar
    if grammar:
        grammar.unload()
    grammar = None
Example #45
0
        "refresh":                          R(Function(navigation.mouse_alternates, mode="legion"), rdescript="Legion: Refresh"),
        "exit | escape | cancel":           R(Function(kill), rdescript="Exit Legion"),


        }
    extras = [
              Choice("action", {
                              "kick": 0,
                              "psychic": 1,
                              "light": 2,
                             }
                    ),
              IntegerRef("n", 0, 1000),
              
             ]
    defaults = {
            "action":-1,
            }

#---------------------------------------------------------------------------

context = AppContext(title="legiongrid")
grammar = Grammar("legiongrid", context=context)
grammar.add_rule(GridControlRule())
grammar.load()

def unload():
    global grammar
    if grammar: grammar.unload()
    grammar = None
Example #46
0
from dragonfly import (Grammar, Dictation, Context, MappingRule, Pause)
from proxy_nicknames import Key, Text, Events, AppRegexContext

from raul import SelfChoice, processDictation, NUMBERS as numbers

from _mac import *

import aenea

adium_context = aenea.global_context & AppRegexContext(name="Adium")
grammar = Grammar("adium", context=adium_context)

class AlfredCommand(MappingRule):
    mapping = {
        "search": Events("key->code=44&modifier=command"),
        "[<text>]": Events("text->%(text)s")
    }
    extras = [Dictation("text")]
    defaults = {"text":"", "n":1}

grammar.add_rule(AlfredCommand())

grammar.load()

def unload():
  global grammar
  if grammar: grammar.unload()
  grammar = None

Example #47
0
from dragonfly import (Grammar, AppContext, MappingRule, Dictation, IntegerRef,
                       Key, Text)

grammar = Grammar("common")

general_rule = MappingRule(
	name = "general",
	mapping = {
		#"kay": Key("enter"),
		#"slap": Key("enter"),
		#"left": Key("left"),
		#"right": Key("right"),

		#"say <text>": Text("%(text)s"),

		},
	extras = [
		Dictation("text"),
		],
)

common_name_rule = MappingRule(
	name = "common names",
	mapping = {
		"T. M. P.": Text("tmp"),
		},
	extras = [
		],
)

#grammar.add_rule(general_rule)
    def test_sequence_timeout(self):
        """
        Test that when speaking subsequent parts of rules involving Dictation
        elements, the recognition times out if there is a timeout period set.
        """
        # Set a timeout period for this test of 100 ms, which is unreasonable for
        # humans, but fine for mimic().
        timeout = 0.1
        self.engine.config.NEXT_PART_TIMEOUT = timeout

        # Set up a test grammar with some rules
        test1 = self.get_test_function()
        test2 = self.get_test_function()

        class TestRule(MappingRule):
            mapping = {
                "say <dictation>": Function(test1),
                "hello world": Function(test2),
            }
            extras = [Dictation("dictation")]

        grammar = Grammar("test")
        grammar.add_rule(TestRule())
        grammar.load()
        self.assertTrue(grammar.loaded)

        # Test that mapping 1 can be recognised fully
        self.assert_mimic_success("say", "hello")
        self.assert_test_function_called(test1, 1)

        # Start recognising mapping 1 again, then sleep for a bit
        self.assert_mimic_success("say")
        time.sleep(timeout)

        # The rest of mapping 1 should not match
        self.assert_mimic_failure("testing")
        self.assert_test_function_called(test1, 1)  # still only called 1 time

        # Should be able to match mapping 2 now
        self.assert_mimic_success("hello world")
        self.assert_test_function_called(test2, 1)

        # Test that it works with a break shorter than the timeout value
        self.assert_mimic_success("say")
        time.sleep(timeout / 2)
        self.assert_mimic_success("hello")
        self.assert_test_function_called(test1, 2)

        # Test with a timeout of 0 (no timeout)
        timeout = 0
        self.engine.config.NEXT_PART_TIMEOUT = timeout
        self.assert_mimic_success("say")
        time.sleep(0.1)  # sleep for 100ms
        self.assert_test_function_called(test1, 2)  # no change
        self.assert_mimic_success("testing")
        self.assert_test_function_called(test1, 3)

        # Test without a sleep between rule parts
        self.assert_mimic_success("say", "testing")
        self.assert_test_function_called(test1, 4)

        # Test that mapping 2 still has no issue matching
        self.assert_mimic_success("hello world")
        self.assert_test_function_called(test2, 2)
Example #49
0
        'com off':                      R(Playback([(["command", "mode", "off"], 0.0)]), rdescript="Dragon: Command Mode (Off)"),
        'scratch':                      R(Playback([(["scratch", "that"], 0.0)]), rdescript="Dragon: 'Scratch That'"),
        "reboot dragon":                R(Function(utilities.reboot), rdescript="Reboot Dragon Naturallyspeaking"),
        "fix dragon double":            R(Function(fix_dragon_double), rdescript="Fix Dragon Double Letter"),
        "add word to vocabulary":       R(Function(vocabulary_processing.add_vocab), rdescript="Vocabulary Management: Add"),
        "delete word from vocabulary":  R(Function(vocabulary_processing.del_vocab), rdescript="Vocabulary Management: Delete"),
        "left point":                   R(Playback([(["MouseGrid"], 0.1), (["four", "four"], 0.1), (["click"], 0.0)]), rdescript="Mouse: Left Point"),
        "right point":                  R(Playback([(["MouseGrid"], 0.1), (["six", "six"], 0.1), (["click"], 0.0)]), rdescript="Mouse: Right Point"),
        "center point":                 R(Playback([(["MouseGrid"], 0.1), (["click"], 0.0)]), rdescript="Mouse: Center Point"),
        }
    extras = [
              Dictation("text"),
              Dictation("mim"),
              IntegerRef("n", 1, 1000),
              
             ]
    defaults = {"n": 1, "mim":""}

#---------------------------------------------------------------------------

grammar = None

if not settings.WSR:
    grammar = Grammar("Dragon Naturallyspeaking")
    grammar.add_rule(CommandRule())
    grammar.load()

def unload():
    global grammar
    if grammar: grammar.unload()
    grammar = None
Example #50
0
        R(Key("f9"), rdescript="MSVC: Breakpoint"),
        "format code":
        R(Key("cs-f"), rdescript="MSVC: Format Code"),
        "(do imports | import all)":
        R(Key("cs-o"), rdescript="MSVC: Do Imports"),
        "comment line":
        R(Key("c-slash"), rdescript="MSVC: Comment Line"),
        "go to line":
        R(Key("c-g"), rdescript="MSVC: Go To Line"),
    }
    extras = [
        Dictation("text"),
        IntegerRefST("n", 1, 1000),
    ]
    defaults = {"n": 1}


#---------------------------------------------------------------------------

context = AppContext(executable="WDExpress")
grammar = Grammar("WDExpress", context=context)

if settings.SETTINGS["apps"]["msvc"]:
    if settings.SETTINGS["miscellaneous"]["rdp_mode"]:
        control.nexus().merger.add_global_rule(MSVCRule())
    else:
        rule = MSVCRule(name="M S V C")
        gfilter.run_on(rule)
        grammar.add_rule(rule)
        grammar.load()
Example #51
0
from dragonfly import CompoundRule, Grammar, Key, MappingRule, Integer

from raul import SelfChoice

import config

if config.PLATFORM == "proxy":
  import aenea
  grammar = Grammar("stopgap", context=aenea.global_context)
  from proxy_contexts import *
  from proxy_nicknames import *
else:
  grammar = Grammar("stopgap")

class MouseClick(MappingRule):
  mapping = {
      "click [left]":Mouse("left"),
      "click middle":Mouse("middle"),
      "click right":Mouse("right"),
    }

class QuadCommand(CompoundRule):
  spec = "zip <xcoord> <ycoord>"
  extras = [Integer("xcoord", min=0, max=11), Integer("ycoord", min=0, max=11)]

  def _process_recognition(self, node, extras):
    x = extras["xcoord"]
    y = extras["ycoord"]
    xres, yres = config.SCREEN_RESOLUTION
    x = xres * int(x) / 10
    y = yres * int(y) / 10
Example #52
0
            "down": "down"
        }),
        generate_ccr_choices.__func__(_NEXUS),
        generate_sm_ccr_choices.__func__(_NEXUS),
        IntegerRefST("monitor", 1, 10)
    ]
    defaults = {
        "n": 1,
        "nnv": 1,
        "text": "",
        "volume_mode": "setsysvolume",
        "enable": -1
    }


grammar = Grammar('general')
main_rule = MainRule()
gfilter.run_on(main_rule)
grammar.add_rule(main_rule)

if settings.SETTINGS["feature_rules"]["again"]:
    again_rule = Again(_NEXUS)
    gfilter.run_on(again_rule)
    grammar.add_rule(again_rule)

if settings.SETTINGS["feature_rules"]["alias"]:
    alias_rule = Alias(name="alias")
    gfilter.run_on(alias_rule)
    grammar.add_rule(alias_rule)

grammar.load()
Example #53
0
    This module demonstrates the use of Dragonfly's CompoundRule class.

    It shows how to use Dragonfly's Grammar, AppContext, and CompoundRule
    classes.  This module can be activated in the same way as other
    Natlink macros by placing it in the My Documents\Natlink folder.

"""

from dragonfly import (Grammar, AppContext, CompoundRule, Choice, Dictation)


#---------------------------------------------------------------------------
# Create this module's grammar and the context under which it'll be active.

grammar_context = AppContext(executable="notepad")
grammar = Grammar("notepad_example", context=grammar_context)


#---------------------------------------------------------------------------
# Create a compound rule which demonstrates CompoundRule and Choice types.

class FoodGroupRule(CompoundRule):

    spec   = "(I ate <food> <time> | <time> I ate <food>) [and thought it was <opinion>]"
    time   = {
              "(two days ago | day before yesterday)":  2,
              "yesterday":                              1,
              "today":                                  0,
             }
    food   = {
              "(a Granny Smith | an) apple":  "fruit",
Example #54
0
        'load wheel':
        Function(gitPause) + Text('loadWheel.sh') + Key('enter'),
        'build wheel':
        Function(gitPause) + Text('buildWheel.sh') + Key('enter'),

        # 'test': bar("text"),
    }
    extras = [
        Integer('number', 1, 9999),
        Integer('numberdot', 1, 9999),
        Integer('n', 1, 9999),
        Dictation("text"),
        Dictation("nocaps", default="").lower(),
        Dictation("camel_text", default="").camel(),
        Dictation("snaketext", default="").lower().replace(" ", "_"),
        Dictation("nospace", default="").lower().replace(" ", ""),
        Dictation("lowtext", default="").lower(),
    ]


context = AppContext(executable='pycharm64')
grammar = Grammar('Pycharm', context=context)
grammar.add_rule(CodeMappings())
grammar.load()


def unload():
    global grammar
    if grammar: grammar.unload()
    grammar = None
Example #55
0
        IntegerRef("numeric", 1, 10000),
        Dictation("text"),
        Choice("prop", cssProperties),
        Choice("hex1", hexValue),
        Choice("hex2", hexValue),
        Choice("hex3", hexValue),
        Choice("hex4", hexValue),
        Choice("hex5", hexValue),
        Choice("hex6", hexValue),
    ],
    defaults={
        "n": 0
    }
)

grammar = Grammar("Css grammar", context=GlobalDynamicContext())
grammar.add_rule(rules)
grammar.load()
grammar.disable()


def dynamic_enable():
    global grammar
    if grammar.enabled:
        return False
    else:
        grammar.enable()
        return True


def dynamic_disable():
Example #56
0
def build_grammar(context):
    grammar = Grammar("default", context=(context))
    #grammar.add_rule(keyword_rule)
    custom = RuleRef(rule=keyword_rule, name='custom')
    grammar.add_rule(build_rule())
    return grammar
Example #57
0
import natlink, os, time

from proxy_nicknames import Key, Text, AppRegexContext, Events

from dictionary_grammars import DIGITS, SYMBOLS, ALPHABET, CASE_ALPHABET

from _mac import FormatRule

import aenea

escape = Key("Escape")

vim_context = aenea.global_context & AppRegexContext(name="iTerm")


grammar = Grammar("vim", context=vim_context)
escape = Events("key->key=escape")
save = escape + Events("text->:w\n")
quit = escape + Events("key->code=41&modifier=shift;key->key=q;key->key=return")
_zip = Events("key->key=z&times=2")
jump = Events("number->%(text)s&modifiers=text;key->key=g&times=2") + _zip
finish = Events("key->key=4&modifier=shift")
match = Events("key->key=5&modifier=shift")
submit = Events("key->code=36")



def strip_number(words):
    '''remove trailing number that dictation sometimes adds'''
    return map((lambda word: word.split('\\')[0]), words)
Example #58
0

def hmc_settings_complete(nexus):
    nexus.comm.get_com("hmc").complete()


class HMCRule(MergeRule):
    mapping = {
        "kill homunculus":
        R(Function(kill, nexus=_NEXUS), rdescript="Kill Helper Window"),
        "complete":
        R(Function(complete, nexus=_NEXUS), rdescript="Complete Input")
    }


grammar = Grammar("hmc", context=AppContext(title=settings.HOMUNCULUS_VERSION))
r1 = HMCRule()
gfilter.run_on(r1)
grammar.add_rule(r1)
if settings.SETTINGS["feature_rules"]["hmc"]:
    grammar.load()


class HMCHistoryRule(MergeRule):
    mapping = {
        # specific to macro recorder
        "check <n>":
        R(Function(hmc_checkbox, nexus=_NEXUS), rdescript="Check Checkbox"),
        "check from <n> to <n2>":
        R(Function(hmc_recording_check_range, nexus=_NEXUS),
          rdescript="Check Range"),
#In order to make this work, you need Windows Speech Recognition and dragonfly.
#Dragonfly has a bunch of pre-requisites as well that you can find on their Github page. 

from dragonfly import Grammar, MappingRule, Text, Dictation
import pythoncom
import time

test_com = MappingRule(\
name="test",\
mapping = {"write <text>": Text("%(text)s")},\
extras=[Dictation("text"),],)

grammar = Grammar("test grammar")
grammar.add_rule(test_com)
grammar.load()

#Keeps the program running to execute the commands
while True:
    pythoncom.PumpWaitingMessages()
    time.sleep(0.1)
    
Example #60
0
from dragonfly import (Grammar, AppContext, MappingRule, Dictation, IntegerRef,
                       Key, Text)

git_context = AppContext(title="git Bash")
git_context2 = AppContext(title="MINGW32:")
# set the window title to bash in putty for this context to work
putty_context = AppContext(title="bash")
grammar = Grammar("bash", context=(putty_context | git_context | git_context2))

general_rule = MappingRule(
    name="general",
    mapping={
        "cancel": Key("c-c"),
        "kay": Key("enter"),
        "left": Key("left"),
        "right": Key("right"),
        "say <text>": Text("%(text)s"),
    },
    extras=[
        Dictation("text"),
    ],
)

file_extensions_rule = MappingRule(
    name="file extensions",
    mapping={
        "dot text": Text(".txt"),
        "dot pie": Text(".py"),
    },
    extras=[],
)