# Author: Jeremy Hayes # Modified from: Brandon Lovrien version # This script includes commands used for Terminal coding from dragonfly import (Grammar, CompoundRule, Dictation, Text, Key, AppContext, MappingRule, Choice) import win32com.client speaker = win32com.client.Dispatch("SAPI.SpVoice") class TerminalEnabler(CompoundRule): spec = "Activate Terminal" # Spoken form of command. def _process_recognition(self, node, extras): # Callback when command is spoken. TerminalBootstrap.disable() TerminalGrammar.enable() s = "Terminal grammar activated" print (s) speaker.Speak(s) class TerminalDisabler(CompoundRule): spec = "switch language" # Spoken form of command. def _process_recognition(self, node, extras): # Callback when command is spoken. TerminalGrammar.disable() TerminalBootstrap.enable() s = "Terminal grammar deactivated" print (s) speaker.Speak(s) class TerminalTestRule(CompoundRule): spec = "test Terminal" # Spoken form of command. def _process_recognition(self, node, extras): # Callback when command is spoken. print ("Terminal grammar tested") class TerminalTags(MappingRule): mapping = { "copy": Text("cp"), "list": Text("ls"), "move": Text("mv"), "force remove ": Text("rm -rf %(tagname)s"), "make directory": Text("mkdir"), "nano": Text("nano" ), "remove": Text( "rm" ), "dac": Text( "-" ), # used to specify tag attributes "attribute": Text( ' attributeName=""' ) + Key( "left" ), " attribute": Text( ' %(attribute)s=""' ) + Key( "left" ), } extras = [ Choice("attribute", { "ID": "id", "class": "class", "style": "style", "title": "title", "SRC": "src", "HREF": "href", "type": "type", "value": "value", "ng": "ng-", } ), Choice("tagname", { "anchor": "a", } ) ] # Code for initial setup of the Terminal grammar TerminalBootstrap = Grammar("Terminal bootstrap") # Create a grammar to contain the command rule. TerminalBootstrap.add_rule(TerminalEnabler()) TerminalBootstrap.load() TerminalGrammar = Grammar("Terminal grammar") TerminalGrammar.add_rule(TerminalTestRule()) TerminalGrammar.add_rule(TerminalDisabler()) TerminalGrammar.add_rule(TerminalTags()) TerminalGrammar.load() TerminalGrammar.disable() # Unload function which will be called by natlink at unload time. def unload(): global TerminalGrammar if TerminalGrammar: TerminalGrammar.unload() TerminalGrammar = None