-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Dragonfly's new timers and update Sikuli code (#577)
* Use Dragonfly timers * Make Sikuli work with the new timer Made the whole thing a class in order to avoid globals * Remove no longer needed TimerForWSR * Update pip requirements * Do not display loading socket error again
- Loading branch information
Showing
7 changed files
with
112 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,108 @@ | ||
from castervoice.lib.imports import * | ||
import xmlrpclib | ||
from subprocess import Popen | ||
import traceback | ||
import socket | ||
|
||
grammar = None | ||
custom_rule = None | ||
server_proxy = None | ||
|
||
def launch_IDE(): | ||
ide_path = settings.SETTINGS["paths"]["SIKULI_IDE"] | ||
if ide_path == "": | ||
print("No 'SIKULI_IDE' path is available. Did you configure it in " + settings.get_filename()) | ||
else: | ||
Popen(["java", "-jar", ide_path]) | ||
class SikuliController(): | ||
grammar = Grammar("sikuli") | ||
custom_rule = None | ||
server_proxy = None | ||
timer = None | ||
|
||
def launch_server(): | ||
runner_path = settings.SETTINGS["paths"]["SIKULI_RUNNER"] | ||
if runner_path == "": | ||
print("No 'SIKULI_RUNNER' path is available. Did you configure it in " + settings.get_filename()) | ||
else: | ||
command = [] if settings.SETTINGS["sikuli"]["version"] == "1.1.3" else ["java", "-jar"] | ||
command.extend([ | ||
settings.SETTINGS["paths"]["SIKULI_RUNNER"], | ||
"-r", settings.SETTINGS["paths"]["SIKULI_SERVER_PATH"], | ||
"--args", settings.SETTINGS["paths"]["SIKULI_SCRIPTS_PATH"] | ||
]) | ||
Popen(command) | ||
# | ||
# Popen([ | ||
# settings.SETTINGS["paths"]["SIKULI_COMPATIBLE_JAVA_EXE_PATH"], "-jar", | ||
# settings.SETTINGS["paths"]["SIKULI_SCRIPTS_JAR_PATH"], "-r", | ||
# settings.SETTINGS["paths"]["SIKULI_SERVER_PATH"] | ||
# ]) | ||
def launch_IDE(self): | ||
ide_path = settings.SETTINGS["paths"]["SIKULI_IDE"] | ||
if ide_path == "": | ||
print("No 'SIKULI_IDE' path is available. Did you configure it in " + settings.get_filename()) | ||
else: | ||
Popen(["java", "-jar", ide_path]) | ||
|
||
def execute(fname): | ||
try: | ||
global server_proxy | ||
fn = getattr(server_proxy, fname) | ||
fn() | ||
except Exception: | ||
utilities.simple_log() | ||
def launch_server(self): | ||
runner_path = settings.SETTINGS["paths"]["SIKULI_RUNNER"] | ||
if runner_path == "": | ||
print("No 'SIKULI_RUNNER' path is available. Did you configure it in " + settings.get_filename()) | ||
else: | ||
command = [] if settings.SETTINGS["sikuli"]["version"] == "1.1.3" else ["java", "-jar"] | ||
command.extend([ | ||
settings.SETTINGS["paths"]["SIKULI_RUNNER"], | ||
"-r", settings.SETTINGS["paths"]["SIKULI_SERVER_PATH"], | ||
"--args", settings.SETTINGS["paths"]["SIKULI_SCRIPTS_PATH"] | ||
]) | ||
Popen(command) | ||
|
||
def terminate_sick_command(): | ||
global server_proxy | ||
global grammar | ||
global custom_rule | ||
grammar.unload() | ||
grammar.remove_rule(custom_rule) | ||
grammar.load() | ||
control.nexus().comm.coms.pop('sikuli') | ||
server_proxy.terminate() | ||
def execute(self, fname): | ||
try: | ||
fn = getattr(self.server_proxy, fname) | ||
fn() | ||
except Exception: | ||
utilities.simple_log() | ||
|
||
def start_server_proxy(): | ||
global server_proxy | ||
global grammar | ||
server_proxy = control.nexus().comm.get_com("sikuli") | ||
fns = server_proxy.list_functions() | ||
# Even though bootstrap_start_server_proxy() didn't load grammar before, | ||
# you have to unload() here because terminate_sick_command() might have been | ||
# called before and loaded the grammar. | ||
grammar.unload() | ||
populate_grammar(fns) | ||
grammar.load() | ||
print("Caster-Sikuli server started successfully.") | ||
def terminate_sick_command(self): | ||
if self.custom_rule: | ||
self.grammar.unload() | ||
self.grammar.remove_rule(self.custom_rule) | ||
self.grammar.load() | ||
control.nexus().comm.coms.pop('sikuli') | ||
self.server_proxy.terminate() | ||
|
||
def populate_grammar(fns): | ||
global grammar | ||
global custom_rule | ||
if len(fns) > 0: | ||
mapping_custom_commands = generate_custom_commands(fns) | ||
custom_rule = MappingRule(mapping=mapping_custom_commands, name="sikuli custom") | ||
grammar.add_rule(custom_rule) | ||
def start_server_proxy(self): | ||
self.server_proxy = control.nexus().comm.get_com("sikuli") | ||
fns = self.server_proxy.list_functions() | ||
# Even though bootstrap_start_server_proxy() didn't load grammar before, | ||
# you have to unload() here because terminate_sick_command() might have been | ||
# called before and loaded the grammar. | ||
self.grammar.unload() | ||
self.populate_grammar(fns) | ||
self.grammar.load() | ||
print("Caster-Sikuli server started successfully.") | ||
|
||
def generate_custom_commands(list_of_functions): | ||
mapping = {} | ||
for fname in list_of_functions: | ||
spec = " ".join(fname.split("_")) | ||
mapping[spec] = Function(execute, fname=fname) | ||
return mapping | ||
def populate_grammar(self, fns): | ||
if len(fns) > 0: | ||
mapping_custom_commands = self.generate_custom_commands(fns) | ||
self.custom_rule = MappingRule(mapping=mapping_custom_commands, name="sikuli custom") | ||
self.grammar.add_rule(self.custom_rule) | ||
|
||
def server_proxy_timer_fn(): | ||
print("Attempting Caster-Sikuli connection [...]") | ||
try: | ||
start_server_proxy() | ||
control.nexus().timer.remove_callback(server_proxy_timer_fn) | ||
except Exception: | ||
pass | ||
def generate_custom_commands(self, list_of_functions): | ||
mapping = {} | ||
for fname in list_of_functions: | ||
spec = " ".join(fname.split("_")) | ||
mapping[spec] = Function(self.execute, fname=fname) | ||
return mapping | ||
|
||
def bootstrap_start_server_proxy(): | ||
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 server_proxy_timer_fn(self): | ||
print("Attempting Caster-Sikuli connection [...]") | ||
try: | ||
self.start_server_proxy() | ||
if self.timer: | ||
self.timer.stop() | ||
self.timer = None | ||
except socket.error: | ||
pass | ||
except Exception: | ||
traceback.print_exc() | ||
|
||
def bootstrap_start_server_proxy(self): | ||
try: | ||
# if the server is already running, this should go off without a hitch | ||
self.start_server_proxy() | ||
except Exception: | ||
self.launch_server() | ||
seconds5 = 5 | ||
self.timer = get_engine().create_timer(self.server_proxy_timer_fn, seconds5) | ||
|
||
sikuli = SikuliController() | ||
|
||
class SikuliControlCommandsRule(MergeRule): | ||
pronunciation = "sikuli" | ||
|
||
mapping = { | ||
"launch sick IDE": R(Function(launch_IDE)), | ||
"launch sick server": R(Function(bootstrap_start_server_proxy)), | ||
"terminate sick server": R(Function(terminate_sick_command)), | ||
"launch sick IDE": R(Function(sikuli.launch_IDE)), | ||
"launch sick server": R(Function(sikuli.bootstrap_start_server_proxy)), | ||
"terminate sick server": R(Function(sikuli.terminate_sick_command)), | ||
} | ||
|
||
if settings.SETTINGS["sikuli"]["enabled"]: | ||
control.non_ccr_app_rule(SikuliControlCommandsRule(), context=None, rdp=False) | ||
bootstrap_start_server_proxy() | ||
rule = SikuliControlCommandsRule() | ||
sikuli.grammar.add_rule(rule) | ||
control.non_ccr_app_rule(rule, context=None, rdp=False) | ||
sikuli.bootstrap_start_server_proxy() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
dragonfly2>=0.14.0 | ||
wxpython>=4.0.3 | ||
pillow>=5.3.0 | ||
toml>=0.10.0 | ||
future | ||
mock | ||
pylint | ||
dragonfly2==0.15.0 | ||
wxpython==4.0.4 | ||
pillow==5.3.0 | ||
toml==0.10.0 | ||
future==0.17.1 | ||
mock==3.0.5 | ||
pylint==1.9.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
dragonfly2>=0.14.0 | ||
wxpython>=4.0.3 | ||
pillow>=5.3.0 | ||
toml>=0.10.0 | ||
future | ||
dragonfly2==0.15.0 | ||
wxpython==4.0.4 | ||
pillow==5.3.0 | ||
toml==0.10.0 | ||
future==0.17.1 | ||
mock==3.0.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters