-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_cmdMemory.py
88 lines (74 loc) · 3.71 KB
/
_cmdMemory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# inspired/taken from https://github.com/t4ngo/dragonfly-modules/blob/master/command-modules/_cmdmemory.py
from dragonfly import PlaybackHistory, MappingRule, DictList, DictListRef, Config, Section, Item, Function, IntegerRef, Dictation, Grammar, Key
from utility.cmdMemory import get_backspaces_for_commands
config = Config("command memory")
config.lang = Section("Language section")
config.lang.playback_last = Item("(playback | repeat) [last] [<n>] (commands | command | recognitions) [<count> times]")
config.lang.recall = Item("(recall everything | show history)")
config.lang.clear_command_history = Item("clear command history")
config.lang.backspace_commands = Item("undo [<n>] command")
config.load()
# Dictionary for storing memories.
memories = DictList("memories")
memories_ref = DictListRef("memory", memories)
playback_history = PlaybackHistory()
try:
playback_history.register()
except Exception, e:
print "Warning, failed to register playback_history: %s" % e
def playback_last(n, count):
# Retrieve playback-action from recognition observer.
if playback_history and playback_history.complete:
playback_history.pop() # Remove playback recognition itself.
action = playback_history[-n:] # Retrieve last *n* recognitions.
action.speed = 10 # Playback at 10x original speed.
# Playback action.
import time; time.sleep(1)
playback_history.unregister() # Don't record playback.
try:
for index in range(count): # Playback *count* times.
action.execute() # Perform playback.
finally:
playback_history.register() # Continue recording after playback.
def print_history():
o = playback_history
print ("(%s) %s: %s\n" % (id(o), "playback_history", "recall") + "\n".join((" - %r" % (item,)) for item in o))
def clear_command_history():
while playback_history:
playback_history.pop()
def backspaces_for_commands(n):
for n in range(get_backspaces_for_commands(list(x for (x,_) in playback_history), n)):
Key("backspace").execute()
class PlaybackRule(MappingRule):
mapping = { # Spoken form -> -> -> action
config.lang.playback_last: Function(playback_last),
config.lang.recall: Function(print_history),
config.lang.clear_command_history: Function(clear_command_history),
config.lang.backspace_commands: Function(backspaces_for_commands),
}
extras = [
IntegerRef("n", 1, 100), # *n* designates the number
# of recent recognitions.
IntegerRef("count", 1, 100), # *count* designates how
# many times to repeat
# playback.
Dictation("name"), # *name* is used when
# Naming new memories.
memories_ref, # This is the list of
# already-remembered
# memories; its name is
# *memory*.
]
defaults = {
"n": 1,
"count": 1,
}
grammar = Grammar("command memory") # Create this module's grammar.
grammar.add_rule(PlaybackRule()) # Add the top-level rule.
grammar.load() # Load the grammar.
# Unload function which will be called at unload time.
def unload():
playback_history.unregister()
global grammar
if grammar: grammar.unload()
grammar = None