class _TestMappingRule(MappingRule): mapping = {"some <text>": NullAction(), "other [<choice>]": NullAction()} extras = [ Dictation("text"), Choice("choice", { "alarm": "alarm", "custom grid": "CustomGrid", "element": "e" }), ] defaults = {"text": "asdf"}
def get_css_node(): H = TreeNode css_sections = [] css_sections.append(_get_background()) css_sections.append(_get_border()) css_sections.append(_get_box()) css_sections.append(_get_font()) css_sections.append(_get_box_model()) css_sections.append(_get_text()) css_sections.append(_get_column()) css_sections.append(_get_speech()) css_sections += _get_miscellaneous() return H("css", NullAction(), css_sections)
def __init__(self, config_path, name=None): """ SelfModifyingRule is a kind of rule which gets its command set changed on-the-fly based on some kind of user interaction. Child classes must implement their own version of the _refresh and _deserialize methods. :param name: str """ self._reload_shim = None self._hooks_runner = None default_smr_mapping = {"spec which gets replaced": NullAction()} self._smr_mapping = default_smr_mapping # extras and defaults may not get replaced: self._smr_extras = [IntegerRefST("n", 1, 50), Dictation("s")] self._smr_defaults = {"n": 1, "s": ""} self._config = SelfModStateSavingConfig(config_path) self._config.load() self._deserialize() MergeRule.__init__(self, name, self._smr_mapping, self._smr_extras, self._smr_defaults)
class BaseAliasRule(BaseSelfModifyingRule): """ Alias rules allow for highlighting text on the screen, then using a GUI component to create an instant Text command. """ mapping = {"default alias command": NullAction()} def __init__(self, config_path, **kwargs): super(BaseAliasRule, self).__init__(config_path, **kwargs) def _deserialize(self): mapping = {} pronunciation = self.get_pronunciation() # recreate all saved aliases commands = self._config.get_copy() for spec in commands: text = commands[spec] mapping[spec] = R(Text(text), rdescript="{}: {}".format(pronunciation, spec)) # add command for creating new aliases mapping["{} [<s>]".format(pronunciation)] = R( Function(lambda s: self._alias(s)), rdescript="Create {}".format(pronunciation)) # add command for deleting all aliases mapping["delete {}es".format(pronunciation)] = R( Function(lambda: self._delete_all()), rdescript="Delete {}".format(pronunciation)) self._smr_mapping = mapping def _refresh(self, *args): if len(args) > 1 and args[0] != "": spec = str(args[0]) text = str(args[1]) self._config.put(spec, text) self._config.save() self.reset() def _alias(self, spec): """ Takes highighted text and makes a Text action of it and the passed spec. Uses an AsynchronousAction to wait for a GUI to get the aliased word. :param spec: str :return: """ text = BaseAliasRule._read_highlighted(10) spec = str(spec) if text is not None: if spec: self._refresh(spec, str(text)) else: h_launch.launch(settings.QTYPE_INSTRUCTIONS, data="Enter_spec_for_command|") on_complete = AsynchronousAction.hmc_complete( lambda data: self._refresh(data[0].replace("\n", ""), text)) AsynchronousAction( [L(S(["cancel"], on_complete))], time_in_seconds=0.5, repetitions=300, blocking=False).execute() def _delete_all(self): self._config.replace({}) self._refresh() @staticmethod def _read_highlighted(max_tries): for i in range(0, max_tries): result = context.read_selected_without_altering_clipboard(True) if result[0] == 0: return result[1] return None
class BaseAliasRule(BaseSelfModifyingRule): """ Alias rules allow for highlighting text on the screen, then using a GUI component to create an instant Text command. """ mapping = {"default alias command": NullAction()} def __init__(self, config_path, **kwargs): super(BaseAliasRule, self).__init__(config_path, **kwargs) def _deserialize(self): mapping = {} pronunciation = self.get_pronunciation() # recreate all saved aliases commands = self._config.get_copy() for spec in commands: text = commands[spec] mapping[spec] = R(Text(text), rdescript="{}: {}".format(pronunciation, spec)) # add command for creating new aliases mapping["{} [<s>]".format(pronunciation)] = R( Function(lambda s: self._alias(s)), rdescript="Create {}".format(pronunciation)) # add command for deleting all aliases mapping["delete {}es".format(pronunciation)] = R( Function(lambda: self._delete_all()), rdescript="Delete {}".format(pronunciation)) self._smr_mapping = mapping def _refresh(self, *args): if len(args) > 1 and args[0] != "": spec = str(args[0]) text = str(args[1]) self._config.put(spec, text) self._config.save() self.reset() def _alias(self, spec): """ Takes highighted text and makes a Text action of it and the passed spec. Uses an AsynchronousAction to wait for a GUI to get the aliased word. :param spec: str :return: """ text = BaseAliasRule._read_highlighted(10) if text is not None: if spec: spec = re.sub(r'[^A-Za-z\'\s]+', '', str(spec)).lower( ) # Sanitize free dictation for spec, words and apostrophes only. self._refresh(spec, str(text)) def _delete_all(self): self._config.replace({}) self._refresh() @staticmethod def _read_highlighted(max_tries): for i in range(0, max_tries): result = context.read_selected_without_altering_clipboard(True) if result[0] == 0: return result[1] return None
class FakeRuleOne(MergeRule): mapping = { "a": NullAction(), "b": NullAction(), "one exclusive": NullAction() }
class FakeRuleThree(MergeRule): mapping = {"c": NullAction()}
class FakeRuleTwo(MergeRule): mapping = { "a": NullAction(), "b": NullAction(), "two exclusive": NullAction() }
class HistoryRule(BaseSelfModifyingRule): pronunciation = "history" mapping = {"default sequence": NullAction()} def __init__(self): super(HistoryRule, self).__init__(settings.settings(["paths", "SM_HISTORY_PATH"])) self._history = recognition_history.get_and_register_history(20) self._preserved = None def _record_from_history(self): """ Inspects the recognition history, formats it for the GUI component which lets the user choose which of their prior utterances will become the Playbacks for the new command. """ # save the list as it was when the command was spoken self._preserved = self._history[:] # format for display formatted = "" for t in self._preserved: for w in t: formatted += w.split("\\")[0] + "[w]" formatted += "[s]" formatted = formatted.encode("unicode_escape") # use a response window to get a spec and word sequences for the new macro h_launch.launch(settings.QTYPE_RECORDING, data=formatted) on_complete = AsynchronousAction.hmc_complete( lambda data: self._add_recorded_macro(data)) AsynchronousAction([L(S(["cancel"], on_complete))], time_in_seconds=0.5, repetitions=300, blocking=False).execute() def _add_recorded_macro(self, data): """ Receives data asynchronously from a GUI component. Transforms said data into a new command. :param data: a dict containing both the spec and the indices of the selected utterances (which are stored locally) -- these are used to build the Playback actions. Also contains a boolean "repeatable" which indicates that the new command should get a repeat multiplier option added to it. :return: """ spec = data["word"] word_sequences = [] # word_sequences is a list of lists of strings for i in data["selected_indices"]: # Convert from a tuple to a list because we may need to modify it. single_sequence = list(self._preserved[i]) # clean the results for k in range(0, len(single_sequence)): if "\\" in single_sequence[k]: single_sequence[k] = single_sequence[k].split("\\")[0] word_sequences.append(single_sequence) # clear the dictation cache self._preserved = None if spec != "" and len(word_sequences) > 0: if data["repeatable"]: spec += " [times <n>]" self._refresh(spec, word_sequences) def _refresh(self, *args): """ :param args: [0] = spec, [1] = list of lists of strings which becomes a series of Playback actions :return: """ if len(args) > 0: spec = str(args[0]) sequences = args[1] self._config.put(spec, sequences) self._config.save() else: self._config.replace({}) self.reset() def _delete_recorded_macros(self): """ Deletes all macros. """ self._refresh() def _deserialize(self): mapping = {} recorded_macros = self._config.get_copy() for spec in recorded_macros: sequences = recorded_macros[spec] delay = settings.settings( ["miscellaneous", "history_playback_delay_secs"]) # The associative string (ascii_str) must be ascii, but the sequences within Playback must be Unicode. mapping[spec] = R( Playback([(sequence, delay) for sequence in sequences]), rdescript="Recorded Macro: " + spec) * Repeat(extra="n") mapping["record from history"] = R( Function(lambda: self._record_from_history()), rdescript="Record From History") mapping["delete recorded macros"] = R( Function(lambda: self._delete_recorded_macros()), rdescript="Delete Recorded Macros") self._smr_mapping = mapping
class _TestRuleA(MergeRule): mapping = {"hello": NullAction(), "a": NullAction()}
def _get_mapping(self): mapping = sikuli_controller.get_instance().generate_commands() if len(mapping) == 0: mapping["this was a test instantiation"] = NullAction() return mapping
class _TestRuleA(MergeRule): mapping = {"spec one": NullAction()}
class _TestRuleC(MergeRule): mapping = {"spec one": NullAction(), "spec two": NullAction()}
class _TestRuleB(MergeRule): mapping = {"spec two": NullAction()}
def _get_speech(): _volume = [ H("uri", Text("uri")), H("silent", Text("silent")), H("extra soft", Text("x-soft")), H("soft", Text("soft")), H("medium", Text("medium")), H("loud", Text("loud")), H("extra loud", Text("x-loud")), H("none", Text("none")), H("inherit", Text("inherit")) ] _pause_rest = [ H("none", Text("none")), H("extra weak", Text("x-weak")), H("weak", Text("weak")), H("medium", Text("medium")), H("strong", Text("strong")), H("extra strong", Text("x-strong")), H("inherit", Text("inherit")) ] _pitch = [ H("extra low", Text("x-low")), H("low", Text("low")), H("medium", Text("medium")), H("high", Text("high")), H("extra high", Text("x-high")), H("inherit", Text("inherit")) ] return H("speech", NullAction(), [ H("cue", Text("cue"), [ H("before", Text("-before: "), _volume), H("after", Text("-after: "), _volume) ]), H("mark", Text("mark"), [H("before", Text("-before: ")), H("after", Text("-after"))]), H("pause", Text("pause"), [ H("before", Text("-before: "), _pause_rest), H("after", Text("-after: "), _pause_rest) ]), H("rest", Text("rest"), [ H("before", Text("-before: "), _pause_rest), H("after", Text("-after: "), _pause_rest) ]), H("speak", Text("speak: "), [ H("none", Text("none")), H("normal", Text("normal")), H("spell out", Text("spell-out")), H("digits", Text("digits")), H("literal punctuation", Text("literal-punctuation")), H("no punctuation", Text("no-punctuation")), H("inherit", Text("inherit")) ]), H("voice", Text("voice"), [ H("balance", Text("-balance: "), [ H("left", Text("left")), H("center", Text("center")), H("right", Text("right")), H("leftwards", Text("leftwards")), H("rightwards", Text("rightwards")), H("inherit", Text("inherit")) ]), H("duration", Text("-duration: ")), H("family", Text("-family: "), [H("inherit", Text("inherit"))]), H("rate", Text("-rate: "), [ H("extra low", Text("x-low")), H("low", Text("low")), H("medium", Text("medium")), H("fast", Text("fast")), H("next for fast", Text("x-fast")), H("inherit", Text("inherit")), ]), H("pitch", Text("-pitch: "), _pitch), H("pitch range", Text("-pitch-range: "), _pitch), H("stress", Text("-stress: "), [ H("strong", Text("strong")), H("moderate", Text("moderate")), H("none", Text("none")), H("reduced", Text("reduced")), H("inherit", Text("inherit")) ]), H("volume", Text("-volume: "), _volume) ]), ])
def _get_box_model( ): # display can be optimized by doing more nesting, this whole section is to be moved somewhere else H = TreeNode _auto = [H("auto", Text("auto"))] _height = H("height", Text("height: "), _auto) _width = H("width", Text("width: "), _auto) _sides = [ H("top", Text("-top: "), _auto), H("bottom", Text("-bottom: "), _auto), H("left", Text("-left: "), _auto), H("right", Text("-right: "), _auto) ] _overflow = [ H("visible", Text("visible")), H("hidden", Text("hidden")), H("scroll", Text("scroll")), H("auto", Text("auto")), H("no display", Text("no-display")), H("no content", Text("no-content")), ] return H("box model", NullAction(), [ H("clear", Text("clear: "), [ H("left", Text("left")), H("right", Text("right")), H("both", Text("both")), H("none", Text("none")) ]), H("display", Text("display: "), [ H("none", Text("none")), H("block", Text("block")), H("compact", Text("compact")), H("table", Text("table")), H("inline", Text("inline"), [H("block", Text("-block")), H("table", Text("-table"))]), H("run in", Text("run-in")), H("list item", Text("list-item")), H("table", Text("table"), [ H("row", Text("-row"), [H("group", Text("-group"))]), H("footer", Text("-footer"), [H("group", Text("-group"))]), H("column", Text("-column"), [H("group", Text("-group"))]), H("cell", Text("-cell")), H("caption", Text("-caption")) ]), H("ruby", Text("ruby"), [ H("base", Text("-base"), [H("group", Text("-group"))]), H("text", Text("-text"), [H("group", Text("-group"))]) ]) ]), H("float", Text("float: "), [ H("left", Text("left")), H("right", Text("right")), H("none", Text("none")) ]), _height, _width, H("max", Text("max-"), [_height, _width]), H("min", Text("min-"), [_height, _width]), H("margin", Text("margin"), _sides), H("padding", Text("padding"), _sides), H("marquee", Text("marquee"), [ H("direction", Text("-direction: "), [H("forward", Text("forward")), H("reverse", Text("reverse"))]), H("loop", Text("-loop: "), [H("infinite", Text("infinite"))]), H("play count", Text("-play-count: "), [H("infinite", Text("infinite"))]), H("speed", Text("-speed: "), [ H("slow", Text("slow")), H("normal", Text("normal")), H("fast", Text("fast")) ]), H("style", Text("-style: "), [ H("scroll", Text("scroll")), H("slide", Text("slide")), H("alternate", Text("alternate")) ]) ]), H("overflow", Text("overflow: "), _overflow), H("overflow X", Text("overflow-x: "), _overflow), H("overflow Y", Text("overflow-y: "), _overflow), H("overflow style", Text("overflow-style: "), [ H("auto", Text("auto")), H("marquee line", Text("marquee-line")), H("marquee block", Text("marquee-block")) ]), H("rotation", Text("rotation: "), [H("angle", Text("ANGLE"))]), H("rotation point", Text("rotation-point: ")), H("visibility", Text("visibility: "), [ H("visible", Text("visible")), H("hidden", Text("hidden")), H("collapse", Text("collapse")) ]) ])