Skip to content

Commit

Permalink
Sikuli Fix #463,#468 (#472)
Browse files Browse the repository at this point in the history
* Fix #463,#468

* Add documentation and fix layout

* Aded rdescripts

added rdescript to Sikulix commands
  • Loading branch information
seekM authored and LexiconCode committed May 2, 2019
1 parent b019caf commit 875fc37
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 52 deletions.
5 changes: 5 additions & 0 deletions _caster.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ def generate_sm_ccr_choices(nexus):

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:
Expand Down
94 changes: 42 additions & 52 deletions castervoice/asynch/sikuli/sikuli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@

from castervoice.lib import control
from castervoice.lib import settings, utilities
from castervoice.lib.actions import Key
from castervoice.lib.dfplus.merge import gfilter
from castervoice.lib.dfplus.merge.mergerule import MergeRule
from castervoice.lib.dfplus.state.short import R

grammar = None
custom_rule = None
server_proxy = None
_NEXUS = control.nexus()


def launch_IDE():
ide_path = settings.SETTINGS["paths"]["SIKULI_IDE"]
Expand All @@ -21,7 +20,6 @@ def launch_IDE():
else:
Popen(["java", "-jar", ide_path])


def launch_server():
runner_path = settings.SETTINGS["paths"]["SIKULI_RUNNER"]
if runner_path == "":
Expand All @@ -41,10 +39,6 @@ def launch_server():
# settings.SETTINGS["paths"]["SIKULI_SERVER_PATH"]
# ])


#


def execute(fname):
try:
global server_proxy
Expand All @@ -53,28 +47,43 @@ def execute(fname):
except Exception:
utilities.simple_log()


def generate_commands(list_of_functions):
def terminate_sick_command():
global server_proxy
global grammar
mapping = {}
for fname in list_of_functions:
spec = " ".join(fname.split("_"))
mapping[spec] = Function(execute, fname=fname)
global custom_rule
grammar.unload()
grammar = Grammar("sikuli")
grammar.add_rule(MappingRule(mapping=mapping, name="sikuli server"))
grammar.remove_rule(custom_rule)
grammar.load()

control.nexus().comm.coms.pop('sikuli')
server_proxy.terminate()

def start_server_proxy():
global server_proxy
global grammar
server_proxy = control.nexus().comm.get_com("sikuli")
fns = server_proxy.list_functions()
if len(fns) > 0:
generate_commands(fns)
# 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 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 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 server_proxy_timer_fn():
print("Attempting Caster-Sikuli connection [...]")
Expand All @@ -84,38 +93,7 @@ def server_proxy_timer_fn():
except Exception:
pass


# utilities.simple_log(False)


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


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
def bootstrap_start_server_proxy():
try:
# if the server is already running, this should go off without a hitch
start_server_proxy()
Expand All @@ -124,6 +102,18 @@ def refresh_sick_command():
seconds5 = 5
control.nexus().timer.add_callback(server_proxy_timer_fn, seconds5)

class SikuliControlCommandsRule(MergeRule):
pronunciation = "sikuli"

mapping = {
"launch sick IDE": R(Function(launch_IDE), rdescript="Sikulix: Launch Sikulix IDE"),
"launch sick server": R(Function(bootstrap_start_server_proxy), rdescript="Sikulix: Launch Sikulix Server"),
"terminate sick server": R(Function(terminate_sick_command), rdescript="Sikulix: Terminate Sikulix server"),
}

if settings.SETTINGS["sikuli"]["enabled"]:
refresh(_NEXUS)
grammar = Grammar("sikuli")
rule = SikuliControlCommandsRule(name="sikuli control commands")
gfilter.run_on(rule)
grammar.add_rule(rule)
bootstrap_start_server_proxy()

0 comments on commit 875fc37

Please sign in to comment.