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)
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)
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)
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)
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)
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)])
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)
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, [])
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
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 == []
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')])]
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'))])
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'))
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])
def test_empty_undo(self): self.translate('*') self._check_translations([]) self._check_output([], [Translation([Stroke('*')], BACK_STRING)], None)
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)
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)
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'
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
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, []))
def test_empty_undo(self): self.translate(stroke('*')) self.assertEquals(self.s.translations[0].english, _back_string()) self.assertOutput([], [Translation([Stroke('*')], _back_string())], None)
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'))
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)
def test_empty_undo(self): self.translate(stroke('*')) self.assertTranslations([]) self.assertOutput([], [Translation([Stroke('*')], _back_string())], None)
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')