forked from dictation-toolbox/Caster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_caster.py
307 lines (252 loc) · 9.67 KB
/
_caster.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#! python2.7
'''
main Caster module
Created on Jun 29, 2014
'''
import os
import logging
logging.basicConfig()
def version_minimum():
try:
import pkg_resources
version = "0.15.0" # Version needs to be manually updated Caster requires a certain version of Dragonfly
pkg_resources.require("dragonfly2 >= %s" % (version))
except Exception: # pylint: disable=broad-except
print("\nCaster: Requires at least dragonfly2 version %s\n" % (version))
version_minimum()
import time, socket, os
from dragonfly import (Function, Grammar, Playback, Dictation, Choice, Pause, RunCommand)
from castervoice.lib.ccr.standard import SymbolSpecs
def _wait_for_wsr_activation():
count = 1
while True:
try:
from castervoice.apps.browser import firefox
break
except:
print(
"(%d) Attempting to load Caster -- WSR not loaded and listening yet..." %
count)
count += 1
time.sleep(1)
_NEXUS = None
from castervoice.lib import settings # requires nothing
if settings.SYSTEM_INFORMATION["platform"] != "win32":
raise SystemError("Your platform is not currently supported by Caster.")
settings.WSR = __name__ == "__main__"
from castervoice.lib import utilities # requires settings
if settings.WSR:
_wait_for_wsr_activation()
SymbolSpecs.set_cancel_word("escape")
from castervoice.lib import control
_NEXUS = control.nexus()
from castervoice.apps import __init__
from castervoice.asynch import *
from castervoice.lib import context
from castervoice.lib.actions import Key
from castervoice.lib.terminal import TerminalCommand
import castervoice.lib.dev.dev
from castervoice.asynch.sikuli import sikuli
from castervoice.lib import navigation
navigation.initialize_clipboard(_NEXUS)
from castervoice.lib.dfplus.state.short import R
from castervoice.lib.dfplus.additions import IntegerRefST
from castervoice.lib.dfplus.merge.mergepair import MergeInf
from castervoice.lib.ccr import *
from castervoice.lib.ccr.recording.again import Again
from castervoice.lib.ccr.recording.bringme import bring_rule
from castervoice.lib.ccr.recording.alias import Alias
from castervoice.lib.ccr.recording import history
from castervoice.lib.dev import dev
from castervoice.lib.dfplus.hint.nodes import css
from castervoice.lib.dfplus.merge.mergerule import MergeRule
from castervoice.lib.dfplus.merge import gfilter
if not globals().has_key('profile_switch_occurred'):
# Load user rules
_NEXUS.process_user_content()
_NEXUS.merger.update_config()
_NEXUS.merger.merge(MergeInf.BOOT)
# Checks if install is classic or PIP of caster
def installtype():
directory = os.path.join(os.getcwd(), "castervoice")
if os.path.isdir(directory):
return
def internetcheck(host="1.1.1.1", port=53, timeout=3):
"""
Checks for network connection via DNS resolution.
:param host: CloudFire DNS
:param port: 53/tcp
:param timeout: An integer
:return: True or False
"""
try:
socket.setdefaulttimeout(timeout)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
return True
except Exception as error:
print(error.message)
return False
# Find the pip.exe script for Python 2.7. Fallback on pip.exe.
PIP_PATH = "pip.exe"
python_scripts = os.path.join("Python27", "Scripts")
for path in os.environ["PATH"].split(";"):
pip = os.path.join(path, "pip.exe")
if path.endswith(python_scripts) and os.path.isfile(pip):
PIP_PATH = pip
break
class DependencyCheck(TerminalCommand):
trusted = True # Command will execute silently without ConfirmAction
synchronous = True
# pylint: disable=method-hidden
def process_command(self, proc):
update = False
for line in iter(proc.stdout.readline, b''):
if "INSTALLED" and "latest" in line:
update = True
return update
class CasterCheck(DependencyCheck):
command = [PIP_PATH, "search", "castervoice"]
# pylint: disable=method-hidden
def process_command(self, proc):
if DependencyCheck.process_command(self, proc):
print("Caster: Caster is up-to-date")
else:
print("Caster: Say 'Update Caster' to update.")
class DragonflyCheck(DependencyCheck):
command = [PIP_PATH, "search", "dragonfly2"]
# pylint: disable=method-hidden
def process_command(self, proc):
if DependencyCheck.process_command(self, proc):
print("Caster: Dragonfly is up-to-date")
else:
print("Caster: Say 'Update Dragonfly' to update.")
class DependencyUpdate(RunCommand):
synchronous = True
# pylint: disable=method-hidden
def process_command(self, proc):
# Process the output from the command.
RunCommand.process_command(self, proc)
# Only reboot dragon if the command was successful and there is an
# internet connection; 'pip install ...' may exit successfully even
# if there were connection errors.
if proc.wait() == 0 and internetcheck():
Playback([(["reboot", "dragon"], 0.0)]).execute()
if settings.SETTINGS["miscellaneous"]["online_mode"]:
if internetcheck():
if installtype() is False:
CasterCheck().execute()
DragonflyCheck().execute()
else:
print("\nCaster: Network off-line check network connection\n")
else:
print("\nCaster: Off-line mode is enabled\n")
def change_monitor():
if settings.SETTINGS["sikuli"]["enabled"]:
Playback([(["monitor", "select"], 0.0)]).execute()
else:
print("This command requires SikuliX to be enabled in the settings file")
class MainRule(MergeRule):
@staticmethod
def generate_ccr_choices(nexus):
choices = {}
for ccr_choice in nexus.merger.global_rule_names():
choices[ccr_choice] = ccr_choice
return Choice("name", choices)
@staticmethod
def generate_sm_ccr_choices(nexus):
choices = {}
for ccr_choice in nexus.merger.selfmod_rule_names():
choices[ccr_choice] = ccr_choice
return Choice("name2", choices)
mapping = {
# update management
"update caster":
R(DependencyUpdate([PIP_PATH, "install", "--upgrade", "castervoice"]),
rdescript="Core: Update and restart Caster"),
"update dragonfly":
R(DependencyUpdate([PIP_PATH, "install", "--upgrade", "dragonfly2"]),
rdescript="Core: Update dragonfly2 and restart Caster"),
# hardware management
"volume <volume_mode> [<n>]":
R(Function(navigation.volume_control, extra={'n', 'volume_mode'}),
rdescript="Volume Control"),
"change monitor":
R(Key("w-p") + Pause("100") + Function(change_monitor),
rdescript="Change Monitor"),
# window management
'minimize':
R(Playback([(["minimize", "window"], 0.0)]), rdescript="Minimize Window"),
'maximize':
R(Playback([(["maximize", "window"], 0.0)]), rdescript="Maximize Window"),
"remax":
R(Key("a-space/10,r/10,a-space/10,x"), rdescript="Force Maximize Window"),
# passwords
# mouse alternatives
"legion [<monitor>]":
R(Function(navigation.mouse_alternates, mode="legion", nexus=_NEXUS),
rdescript="Activate Legion"),
"rainbow [<monitor>]":
R(Function(navigation.mouse_alternates, mode="rainbow", nexus=_NEXUS),
rdescript="Activate Rainbow Grid"),
"douglas [<monitor>]":
R(Function(navigation.mouse_alternates, mode="douglas", nexus=_NEXUS),
rdescript="Activate Douglas Grid"),
# ccr de/activation
"<enable> <name>":
R(Function(_NEXUS.merger.global_rule_changer(), save=True),
rdescript="Toggle CCR Module"),
"<enable> <name2>":
R(Function(_NEXUS.merger.selfmod_rule_changer(), save=True),
rdescript="Toggle sm-CCR Module"),
"enable caster":
R(Function(_NEXUS.merger.merge, time=MergeInf.RUN, name="numbers"),
rdescript="Enable CCR rules"),
"disable caster":
R(Function(_NEXUS.merger.ccr_off), rdescript="Disable CCR rules"),
}
extras = [
IntegerRefST("n", 1, 50),
Dictation("text"),
Dictation("text2"),
Dictation("text3"),
Choice("enable", {
"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')
main_rule = MainRule()
gfilter.run_on(main_rule)
grammar.add_rule(main_rule)
gfilter.run_on(bring_rule)
grammar.add_rule(bring_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(_NEXUS)
gfilter.run_on(alias_rule)
grammar.add_rule(alias_rule)
grammar.load()
if globals().has_key('profile_switch_occurred'):
reload(sikuli)
else:
profile_switch_occurred = None
print("\n*- Starting " + settings.SOFTWARE_NAME + " -*")
if settings.WSR:
import pythoncom
print("Windows Speech Recognition is garbage; it is " \
+"recommended that you not run Caster this way. ")
while True:
pythoncom.PumpWaitingMessages() # @UndefinedVariable
time.sleep(.1)