def test_compound_element(self): """ Test the dragonfly Compound element. """ # Compound is the element that the base dragonfly rule classes use. # It uses custom parser elements, so it needs to be tested separately. self.assertEqual(Compound(spec=u"touché").gstring(), u"(touché)") self.assertEqual( Compound(spec=u"йцукенгшщзхъфывапролджэячсмитьбю").gstring(), u"(йцукенгшщзхъфывапролджэячсмитьбю)")
def __init__(self, choices, name = None, extras = None, default = None): # Argument type checking. assert isinstance(name, basestring) or name is None assert isinstance(choices, dict) for k, v in choices.iteritems(): assert isinstance(k, basestring) # Construct children from the given choice keys and values. self._choices = choices self._extras = extras children = [] for k, v in choices.iteritems(): if callable(v): child = Compound(spec = k, value_func = v, extras = extras) else: child = Compound(spec = k, value = v, extras = extras) children.append(child) # Initialize super class. Alternative.__init__(self, children = children, name = name, default = default)
def __init__(self, parameter): self.__parameter = parameter spec = None extras = [] if parameter["type"] == "dictation": spec = "set " + parameter["name"] + " <value>" extras = [Dictation("value")] if parameter["type"] == "alternative": values = parameter["values"] if isinstance(values, dict): extras = [Choice("value", values)] if isinstance(values, list): extras = [Alternative(name="value", children=[Compound(spec=word, value=word) for word in values])] spec = "set " + parameter["name"] + " <value>" if parameter["type"] == "switch": spec = "enable " + parameter["name"] CompoundRule.__init__(self, name=get_unique_rule_name(), spec=spec, extras=extras)
class AccessibilityRule(MergeRule): pronunciation = "accessibility" mapping = { # Accessibility API Mappings "go before <text_position_query>": Function(lambda text_position_query: accessibility.move_cursor( text_position_query, CursorPosition.BEFORE)), "go after <text_position_query>": Function(lambda text_position_query: accessibility.move_cursor( text_position_query, CursorPosition.AFTER)), "words <text_query>": Function(accessibility.select_text), "words <text_query> delete": Function( lambda text_query: accessibility.replace_text(text_query, "")), "replace <text_query> with <replacement>": Function(accessibility.replace_text), } extras = [ Dictation("replacement"), Compound( name="text_query", spec= ("[[([<start_phrase>] <start_relative_position> <start_relative_phrase>|<start_phrase>)] <through>] " "([<end_phrase>] <end_relative_position> <end_relative_phrase>|<end_phrase>)" ), extras=[ Dictation("start_phrase", default=""), Alternative( [Literal("before"), Literal("after")], name="start_relative_position"), Dictation("start_relative_phrase", default=""), Literal("through", "through", value=True, default=False), Dictation("end_phrase", default=""), Alternative( [Literal("before"), Literal("after")], name="end_relative_position"), Dictation("end_relative_phrase", default="") ], value_func=lambda node, extras: TextQuery( start_phrase=str(extras["start_phrase"]), start_relative_position=( CursorPosition[extras["start_relative_position"].upper()] if "start_relative_position" in extras else None), start_relative_phrase=str(extras["start_relative_phrase"]), through=extras["through"], end_phrase=str(extras["end_phrase"]), end_relative_position=( CursorPosition[extras["end_relative_position"].upper()] if "end_relative_position" in extras else None), end_relative_phrase=str(extras["end_relative_phrase"]))), Compound(name="text_position_query", spec="<phrase> [<relative_position> <relative_phrase>]", extras=[ Dictation("phrase", default=""), Alternative([Literal("before"), Literal("after")], name="relative_position"), Dictation("relative_phrase", default="") ], value_func=lambda node, extras: TextQuery( end_phrase=str(extras["phrase"]), end_relative_position=( CursorPosition[extras["relative_position"].upper()] if "relative_position" in extras else None), end_relative_phrase=str(extras["relative_phrase"]))) ]
Rule.__init__(self, name="fraction", element=Integer("_frac", 0, self.sections + 1), exported=False) def value(self, node): value = node.get_child_by_name("_frac").value() return float(value) / self.sections fraction_rule = FractionRule() # --------------------------------------------------------------------------- horz_left = Compound(config.lang.left, name="horz", value=0.0) horz_right = Compound(config.lang.right, name="horz", value=1.0) vert_top = Compound(config.lang.top, name="vert", value=0.0) vert_bottom = Compound(config.lang.bottom, name="vert", value=1.0) horz_frac = RuleRef(fraction_rule, name="horz") vert_frac = RuleRef(fraction_rule, name="vert") horz_expl = Alternative([horz_left, horz_right], name="horz_expl") horz_all = Alternative([horz_expl, horz_frac], name="horz_all") vert_expl = Alternative([vert_top, vert_bottom], name="vert_expl") vert_all = Alternative([vert_expl, vert_frac], name="vert_all") # --------------------------------------------------------------------------- position_element = Compound(
break class FocusTitleRule(CompoundRule): spec = "focus title <text>" extras = [Dictation("text")] def _process_recognition(self, node, extras): title = str(extras["text"]) action = FocusWindow(title=title) try: action.execute() except ActionError: self._log.warning("No window with that name found.") horz_left = Compound("left", name="horz", value=0.0) horz_center = Compound("center", name="horz", value=0.5) horz_right = Compound("right", name="horz", value=1.0) vert_top = Compound("top", name="vert", value=0.0) vert_center = Compound("center", name="vert", value=0.5) vert_bottom = Compound("bottom", name="vert", value=1.0) horz_all = Alternative([horz_left, horz_center, horz_right], name="horz_all") vert_all = Alternative([vert_top, vert_center, vert_bottom], name="vert_all") position_element = Compound( spec=" <horz_all>" " | <vert_all>" " | <vert_all> <horz_all>",
def __init__(self, spec, start, end): self.__start = start self.__end = end child = Compound("[empty] " + spec) Rule.__init__(self, get_unique_rule_name(), child, exported=True)
self.window_movement.target_window = notepad_windows[0] self.move_target_window(extras) #--------------------------------------------------------------------------- # Create window movement objects to be used by our window movement rules. move_window_here = MoveWindowHere() center_window_here = CenterWindowHere() # Define common extras for movement rules: an <animation> extra for # optionally animating window movement. common_extras = [ Compound( spec="animated", name="animation", # Possible window animation values: "linear", "spline". value_func=lambda _, __: "spline") ] # Create a rule for moving the foreground window to the current cursor # position. rule1 = MoveForegroundWindowRule(window_movement=move_window_here, name="rule1", spec="move [this] window here [<animation>]", extras=common_extras) # Create a rule for centering the foreground window on the current cursor # position. rule2 = MoveForegroundWindowRule( window_movement=center_window_here,
class WebStormFunctionRules(MergeRule): pronunciation = 'web storm custom' mapping = { 'function [<dictation>]': R(Function(write_function), rdescript='WebStorm: Generate Function'), 'par <dictation>': R(Function(writeParam), rdescript='WebStorm: Write Function Param'), 'arg <dictation>': R(Function(writeParam), rdescript='WebStorm: Write Function Arg'), 'type <dictation>': R(Function(writeParam), rdescript='WebStorm: Write Type'), 'params': R( IdeaAction( 'com.nathantreid.codeNavigationHelpers.Actions.MoveCaretToFunctionParameters' )), 'previous function': R( IdeaAction( 'com.nathantreid.codeNavigationHelpers.Actions.MoveCaretToPreviousFunctionName' )), 'next function': R( IdeaAction( 'com.nathantreid.codeNavigationHelpers.Actions.MoveCaretToNextFunctionName' )), 'current function': R( IdeaAction( 'com.nathantreid.codeNavigationHelpers.Actions.MoveCaretToFunctionName' )), 'previous argument': R( IdeaAction( 'com.nathantreid.codeNavigationHelpers.Actions.MoveCaretToPreviousArgument' )), 'next argument': R( IdeaAction( 'com.nathantreid.codeNavigationHelpers.Actions.MoveCaretToNextArgument' )), 'previous parameter': R( IdeaAction( 'com.nathantreid.codeNavigationHelpers.Actions.MoveCaretToPreviousParameter' )), 'next parameter': R( IdeaAction( 'com.nathantreid.codeNavigationHelpers.Actions.MoveCaretToNextParameter' )), # 'go to arrow params': R(Function(findText, text = 'params')), 'new javascript file [<dictation>]': R(Function(newJsFile)), 'call <dictation>': Function(write_function), } extras = [ Alternative( name='dictation', children=[ dictation_element, Compound(spec='<dictation>', value_func=F(text_format.format_camel), extras=[Dictation('dictation')]) ], ), ] defaults = { 'n': 1, }