コード例 #1
0
def retro_everything(translator: Translator, stroke: Stroke, cmdline: str):
    print("\n\n\nRetro everything invoked with: " + str(stroke) + ", " +
          cmdline)
    args = cmdline.split(",")
    left_char = args[0]
    right_char = args[1]

    all_translations = translator.get_state().translations

    affected_translation_cnt = len(
        list(
            itertools.takewhile(lambda x: x.strokes[-1] == stroke,
                                reversed(all_translations))))

    # translations that _will_ be affected
    affected_translations = all_translations[-(affected_translation_cnt + 1):]

    affected_strokes = flatten([x.strokes for x in affected_translations])
    affected_string = " ".join(
        flatten([
            recursively_get_old_english(stroke, t)
            for t in affected_translations
        ]))

    resulting_translation = left_char + affected_string + right_char
    my_trans = Translation(affected_strokes + [stroke], resulting_translation)
    my_trans.replaced = affected_translations

    translator.translate_translation(my_trans)
コード例 #2
0
def repeat_last_translation(translator: Translator, stroke: Stroke, macro_args: str) -> None:
    '''
    Macro to repeat the last translation(s) in Plover.

    :param translator: The active Plover translator that is executing the macro.
    :type translator: plover.translation.Translator

    :param stroke: The current stroke (what invoked this macro).
    :type stroke: plover.translation.Stroke

    :param macro_args: The optional arguments specified to the macro as a comma-delimited string.
                       Piece 1: The number of previous translations to repeat. Default is 1.
    :type macro_args: str
    '''

    # Get the current state
    translations = translator.get_state().translations
    if not translations:
        return

    # Process input
    try:
        num_to_repeat = int(macro_args.split(DELIM_ARGS)[0])
    except:
        num_to_repeat = 1

    # Output the new translations
    for translation in translations[-num_to_repeat:]:
        repeated_translation = Translation(translation.strokes, translation.english)
        translator.translate_translation(repeated_translation)
コード例 #3
0
def repeat_last_fragment(translator: Translator, stroke: Stroke, macro_args: str) -> None:
    '''
    Macro to repeat the last fragments(s) in Plover.

    :param translator: The active Plover translator that is executing the macro.
    :type translator: plover.translation.Translator

    :param stroke: The current stroke (what invoked this macro).
    :type stroke: plover.translation.Stroke

    :param macro_args: The optional arguments specified to the macro as a comma-delimited string.
                       Piece 1: The number of previous fragments to repeat. Default is 1.
    :type macro_args: str
    '''

    # Get the current state
    translations = translator.get_state().translations
    if not translations:
        return

    # Process input
    try:
        num_to_repeat = int(macro_args.split(DELIM_ARGS)[0])
    except:
        num_to_repeat = 1

    # Output the new translations
    formatter = RetroFormatter(translations)
    last_fragments = formatter.last_fragments(num_to_repeat)

    for fragment in last_fragments:
        new_translation = Translation([stroke], fragment)
        translator.translate_translation(new_translation)
コード例 #4
0
ファイル: retrospective.py プロジェクト: ohAitch/plover
def insert_space(translator, stroke, cmdline):
    # Retrospective insert space
    translations = translator.get_state().translations
    if not translations:
        return
    replaced = translations[-1]
    if replaced.is_retrospective_command:
        return
    lookup_stroke = replaced.strokes[-1]
    english = [t.english or '/'.join(t.rtfcre)
               for t in replaced.replaced]
    if english:
        english.append(translator.lookup([lookup_stroke]) or lookup_stroke.rtfcre)
        t = Translation([stroke], ' '.join(english))
        t.replaced = [replaced]
        t.is_retrospective_command = True
        translator.translate_translation(t)
コード例 #5
0
def insert_space(translator, stroke, cmdline):
    # Retrospective insert space
    translations = translator.get_state().translations
    if not translations:
        return
    replaced = translations[-1]
    if replaced.is_retrospective_command:
        return
    lookup_stroke = replaced.strokes[-1]
    english = [t.english or '/'.join(t.rtfcre) for t in replaced.replaced]
    if english:
        english.append(
            translator.lookup([lookup_stroke]) or lookup_stroke.rtfcre)
        t = Translation([stroke], ' '.join(english))
        t.replaced = [replaced]
        t.is_retrospective_command = True
        translator.translate_translation(t)
コード例 #6
0
ファイル: undo.py プロジェクト: tux21b/plover
def undo(translator, stroke, cmdline):
    for t in reversed(translator.get_state().translations):
        translator.untranslate_translation(t)
        if t.has_undo():
            return
    # There is no more buffer to delete from -- remove undo and add a
    # stroke that removes last word on the user's OS, but don't add it
    # to the state history.
    translator.flush([Translation([stroke], BACK_STRING)])
コード例 #7
0
ファイル: retrospective.py プロジェクト: tux21b/plover
def delete_space(translator, stroke, cmdline):
    # Retrospective delete space
    translations = translator.get_state().translations
    if len(translations) < 2:
        return
    replaced = translations[-2:]
    if replaced[1].is_retrospective_command:
        return
    english = []
    for t in replaced:
        if t.english is not None:
            english.append(t.english)
        elif len(t.rtfcre) == 1 and t.rtfcre[0].isdigit():
            english.append('{&%s}' % t.rtfcre[0])
    if len(english) > 1:
        t = Translation([stroke], '{^~|^}'.join(english))
        t.replaced = replaced
        t.is_retrospective_command = True
        translator.translate_translation(t)
コード例 #8
0
ファイル: retrospective.py プロジェクト: ohAitch/plover
def delete_space(translator, stroke, cmdline):
    # Retrospective delete space
    translations = translator.get_state().translations
    if len(translations) < 2:
        return
    replaced = translations[-2:]
    if replaced[1].is_retrospective_command:
        return
    english = []
    for t in replaced:
        if t.english is not None:
            english.append(t.english)
        elif len(t.rtfcre) == 1 and t.rtfcre[0].isdigit():
            english.append('{&%s}' % t.rtfcre[0])
    if len(english) > 1:
        t = Translation([stroke], '{^~|^}'.join(english))
        t.replaced = replaced
        t.is_retrospective_command = True
        translator.translate_translation(t)
コード例 #9
0
ファイル: test_translation.py プロジェクト: tlevine/plover
    def test_listeners(self):
        output1 = []

        def listener1(undo, do, prev):
            output1.append((undo, do, prev))

        output2 = []

        def listener2(undo, do, prev):
            output2.append((undo, do, prev))

        t = Translator()
        s = stroke('S')
        tr = Translation([s], None)
        expected_output = [([], [tr], [tr])]

        t.translate(s)

        t.add_listener(listener1)
        t.translate(s)
        self.assertEqual(output1, expected_output)

        del output1[:]
        t.add_listener(listener2)
        t.translate(s)
        self.assertEqual(output1, expected_output)
        self.assertEqual(output2, expected_output)

        del output1[:]
        del output2[:]
        t.add_listener(listener2)
        t.translate(s)
        self.assertEqual(output1, expected_output)
        self.assertEqual(output2, expected_output)

        del output1[:]
        del output2[:]
        t.remove_listener(listener1)
        t.translate(s)
        self.assertEqual(output1, [])
        self.assertEqual(output2, expected_output)

        del output1[:]
        del output2[:]
        t.remove_listener(listener2)
        t.translate(s)
        self.assertEqual(output1, [])
        self.assertEqual(output2, [])
コード例 #10
0
    def _on_message(self, data: dict):
        if data.get("secretkey", "") != self._config.secretkey:
            return
        with self._engine:
            forced_on = False
            if data.get('forced') and not self._engine._is_running:
                forced_on = True
                self._engine._is_running = True

            if data.get('zero_last_stroke_length'):
                self._engine._machine._last_stroke_key_down_count = 0
                self._engine._machine._stroke_key_down_count = 0

            import traceback

            if 'stroke' in data:
                steno_keys = data['stroke']
                if isinstance(steno_keys, list):
                    try:
                        self._engine._machine_stroke_callback(steno_keys)
                    except:
                        traceback.print_exc()

            if 'translation' in data:
                mapping = data['translation']
                if isinstance(mapping, str):
                    try:
                        from plover.steno import Stroke
                        from plover.translation import _mapping_to_macro, Translation
                        stroke = Stroke([]) # required, because otherwise Plover will try to merge the outlines together
						# and the outline [] (instead of [Stroke([])]) can be merged to anything
                        macro = _mapping_to_macro(mapping, stroke)
                        if macro is not None:
                            self._engine._translator.translate_macro(macro)
                            return
                        t = (
                            #self._engine._translator._find_translation_helper(stroke) or
                            #self._engine._translator._find_translation_helper(stroke, system.SUFFIX_KEYS) or
                            Translation([stroke], mapping)
                        )
                        self._engine._translator.translate_translation(t)
                        self._engine._translator.flush()
                        #self._engine._trigger_hook('stroked', stroke)
                    except:
                        traceback.print_exc()

            if forced_on:
                self._engine._is_running = False
コード例 #11
0
def test_listeners():
    output1 = []

    def listener1(undo, do, prev):
        output1.append((undo, do, prev))

    output2 = []

    def listener2(undo, do, prev):
        output2.append((undo, do, prev))

    t = Translator()
    s = stroke('S')
    tr = Translation([s], None)
    expected_output = [([], [tr], [tr])]

    t.translate(s)

    t.add_listener(listener1)
    t.translate(s)
    assert output1 == expected_output

    del output1[:]
    t.add_listener(listener2)
    t.translate(s)
    assert output1 == expected_output
    assert output2 == expected_output

    del output1[:]
    del output2[:]
    t.add_listener(listener2)
    t.translate(s)
    assert output1 == expected_output
    assert output2 == expected_output

    del output1[:]
    del output2[:]
    t.remove_listener(listener1)
    t.translate(s)
    assert output1 == []
    assert output2 == expected_output

    del output1[:]
    del output2[:]
    t.remove_listener(listener2)
    t.translate(s)
    assert output1 == []
    assert output2 == []
コード例 #12
0
ファイル: test_translation.py プロジェクト: Quetzal2/plover
def test_changing_state():
    output = []
    def listener(undo, do, prev):
        prev = list(prev) if prev else None
        output.append((undo, do, prev))

    d = StenoDictionary()
    d['S/P'] = 'hi'
    dc = StenoDictionaryCollection([d])
    t = Translator()
    t.set_dictionary(dc)
    t.translate(stroke('T'))
    t.translate(stroke('S'))
    s = copy.deepcopy(t.get_state())

    t.add_listener(listener)

    expected = [([Translation([stroke('S')], None)],
                 [Translation([stroke('S'), stroke('P')], 'hi')],
                 [Translation([stroke('T')], None)])]
    t.translate(stroke('P'))
    assert output == expected

    del output[:]
    t.set_state(s)
    t.translate(stroke('P'))
    assert output == expected

    del output[:]
    t.clear_state()
    t.translate(stroke('P'))
    assert output == [([], [Translation([stroke('P')], None)], None)]

    del output[:]
    t.set_state(s)
    t.translate(stroke('K'))
    assert output == [([],
                       [Translation([stroke('K')], None)],
                       [Translation([stroke('T')], None), Translation([stroke('S'), stroke('P')], 'hi')])]
コード例 #13
0
ファイル: test_translation.py プロジェクト: benreynwar/plover
    def test_changing_state(self):
        output = []

        def listener(undo, do, prev):
            output.append((undo, do, prev))

        d = StenoDictionary()
        d[('S', 'P')] = 'hi'
        dc = StenoDictionaryCollection()
        dc.set_dicts([d])
        t = Translator()
        t.set_dictionary(dc)
        t.translate(stroke('T'))
        t.translate(stroke('S'))
        s = copy.deepcopy(t.get_state())

        t.add_listener(listener)

        expected = [([Translation([stroke('S')], None)],
                     [Translation([stroke('S'), stroke('P')],
                                  'hi')], Translation([stroke('T')], None))]
        t.translate(stroke('P'))
        self.assertEqual(output, expected)

        del output[:]
        t.set_state(s)
        t.translate(stroke('P'))
        self.assertEqual(output, expected)

        del output[:]
        t.clear_state()
        t.translate(stroke('P'))
        self.assertEqual(output,
                         [([], [Translation([stroke('P')], None)], None)])

        del output[:]
        t.set_state(s)
        t.translate(stroke('P'))
        self.assertEqual(output,
                         [([], [Translation([stroke('P')], None)],
                           Translation([stroke('S'), stroke('P')], 'hi'))])
コード例 #14
0
 def test_undo_tail(self):
     self.s.tail = self.t('T/A/I/L')
     self.translate(stroke('*'))
     self.assertTranslations([])
     self.assertOutput([], [Translation([Stroke('*')], _back_string())], self.lt('T/A/I/L'))
コード例 #15
0
 def test_undo_tail(self):
     self.s.tail = self.t('T/A/EU/L')
     self.translate('*')
     self._check_translations([])
     self._check_output([], [Translation([Stroke('*')], BACK_STRING)],
                        [self.s.tail])
コード例 #16
0
 def test_empty_undo(self):
     self.translate('*')
     self._check_translations([])
     self._check_output([], [Translation([Stroke('*')], BACK_STRING)], None)
コード例 #17
0
 def t(self, strokes):
     """A quick way to make a translation."""
     strokes = [stroke(x) for x in strokes.split('/')]
     key = tuple(s.rtfcre for s in strokes)
     translation = self.dc.lookup(key)
     return Translation(strokes, translation)
コード例 #18
0
 def setup_method(self):
     self.a = Translation([stroke('S')], None)
     self.b = Translation([stroke('T'), stroke('-D')], None)
     self.c = Translation([stroke('-Z'), stroke('P'), stroke('T*')], None)
コード例 #19
0
def test_translation():
    t = Translation([stroke('S'), stroke('T')], 'translation')
    assert t.strokes == [stroke('S'), stroke('T')]
    assert t.rtfcre == ('S', 'T')
    assert t.english == 'translation'
コード例 #20
0
def test_no_translation():
    t = Translation([stroke('S'), stroke('T')], None)
    assert t.strokes == [stroke('S'), stroke('T')]
    assert t.rtfcre == ('S', 'T')
    assert t.english is None
コード例 #21
0
ファイル: test_translation.py プロジェクト: benreynwar/plover
 def t(self, strokes):
     """A quick way to make a translation."""
     strokes = [stroke(x) for x in strokes.split('/')]
     return Translation(strokes, _lookup(strokes, self.dc, []))
コード例 #22
0
ファイル: test_translation.py プロジェクト: benreynwar/plover
 def test_empty_undo(self):
     self.translate(stroke('*'))
     self.assertEquals(self.s.translations[0].english, _back_string())
     self.assertOutput([], [Translation([Stroke('*')], _back_string())],
                       None)
コード例 #23
0
ファイル: test_translation.py プロジェクト: benreynwar/plover
 def test_undo_tail(self):
     self.s.tail = self.t('T/A/I/L')
     self.translate(stroke('*'))
     self.assertEquals(self.s.translations[0].english, _back_string())
     self.assertOutput([], [Translation([Stroke('*')], _back_string())],
                       self.t('T/A/I/L'))
コード例 #24
0
 def test_no_translation(self):
     t = Translation([stroke('S'), stroke('T')], None)
     self.assertEqual(t.strokes, [stroke('S'), stroke('T')])
     self.assertEqual(t.rtfcre, ('S', 'T'))
     self.assertIsNone(t.english)
コード例 #25
0
 def test_empty_undo(self):
     self.translate(stroke('*'))
     self.assertTranslations([])
     self.assertOutput([], [Translation([Stroke('*')], _back_string())], None)
コード例 #26
0
 def test_translation(self):
     t = Translation([stroke('S'), stroke('T')], 'translation')
     self.assertEqual(t.strokes, [stroke('S'), stroke('T')])
     self.assertEqual(t.rtfcre, ('S', 'T'))
     self.assertEqual(t.english, 'translation')