def SaveChanges(self): self.pending_changes = False # Set of dictionaries (paths) that needs saving. needs_saving = set() # Creates for item in self.added_items: item.dictionary[normalize_steno( item.stroke)] = unescape_translation(item.translation) needs_saving.add(item.dictionary.get_path()) # Updates for item_id in self.modified_items: item = self.all_keys[item_id] item.dictionary[normalize_steno( item.stroke)] = unescape_translation(item.translation) needs_saving.add(item.dictionary.get_path()) # Deletes for item in self.deleted_items: del item.dictionary[normalize_steno(item.stroke)] needs_saving.add(item.dictionary.get_path()) self.engine.get_dictionary().save(needs_saving)
def test_escape_unescape_translation(self): for raw, escaped in ( # No change. ('foobar', 'foobar'), (r'\\', r'\\'), ('\\\\\\', '\\\\\\'), # -> \\\ # Basic support: \n, \r, \t. ('\n', r'\n'), ('\r', r'\r'), ('\t', r'\t'), # Allow a literal \n, \r, or \t by doubling the \. (r'\n', r'\\n'), (r'\r', r'\\r'), (r'\t', r'\\t'), (r'\\n', r'\\\n'), (r'\\r', r'\\\r'), (r'\\t', r'\\\t'), # A little more complex. ('\tfoo\nbar\r', r'\tfoo\nbar\r'), ('\\tfoo\\nbar\\r', r'\\tfoo\\nbar\\r'), ): result = unescape_translation(escaped) self.assertEqual(result, raw, msg='unescape_translation(%r)=%r != %r' % (escaped, result, raw)) result = escape_translation(raw) self.assertEqual(result, escaped, msg='escape_translation(%r)=%r != %r' % (raw, result, escaped))
def on_apply_filter(self): strokes_filter = '/'.join( normalize_steno(self.strokes_filter.text().strip())) translation_filter = unescape_translation( self.translation_filter.text().strip()) self._model.filter(strokes_filter=strokes_filter, translation_filter=translation_filter)
def on_add_translation(self, event=None): d = self.engine.get_dictionary() strokes = self._normalized_strokes() translation = self.translation_text.GetValue().strip() if strokes and translation: d.set(strokes, unescape_translation(translation)) d.save(path_list=(d.dicts[0].get_path(),)) self.Close()
def on_add_translation(self, event=None): d = self.engine.get_dictionary() strokes = self._normalized_strokes() translation = self.translation_text.GetValue().strip() if strokes and translation: d.set(strokes, unescape_translation(translation)) d.save(path_list=(d.dicts[0].get_path(), )) self.Close()
def on_lookup(self, pattern): # Wherever a character is typed or a checkbox is changed, refresh the lookup results. # TODO: preserve the state of search mode checkboxes? translation = unescape_translation(pattern.strip()) suggestion_list = self._engine.get_suggestions( translation, count=self.search_limit, partial=self.partialCheck.isChecked(), regex=self.regexCheck.isChecked()) self._update_suggestions(suggestion_list)
def handle(self, words): if not words: return False text = " ".join(words) lookup = unescape_translation(text) suggestions = format_suggestions(self.engine.get_suggestions(lookup)) if suggestions: self.output(suggestions) else: self.output(f"'{lookup}' not found") return True
def SaveChanges(self): self.pending_changes = False # Set of dictionaries (paths) that needs saving. needs_saving = set() # Creates for item in self.added_items: item.dictionary[normalize_steno(item.stroke)] = unescape_translation(item.translation) needs_saving.add(item.dictionary.get_path()) # Updates for item_id in self.modified_items: item = self.all_keys[item_id] item.dictionary[normalize_steno(item.stroke)] = unescape_translation(item.translation) needs_saving.add(item.dictionary.get_path()) # Deletes for item in self.deleted_items: del item.dictionary[normalize_steno(item.stroke)] needs_saving.add(item.dictionary.get_path()) self.engine.get_dictionary().save(needs_saving)
def on_translation_change(self, event): # TODO: normalize dict entries to make reverse lookup more reliable with # whitespace. translation = event.GetString().strip() self.listbox.Clear() if translation: d = self.engine.get_dictionary() strokes_list = d.reverse_lookup(unescape_translation(translation)) if strokes_list: entries = ('/'.join(x) for x in strokes_list) for str in entries: self.listbox.Append(str) else: self.listbox.Append('No entries') self.GetSizer().Layout()
def on_translation_change(self, event): # TODO: normalize dict entries to make reverse lookup more reliable # with whitespace. translation = event.GetString().strip() if translation: d = self.engine.get_dictionary() strokes_list = d.reverse_lookup(unescape_translation(translation)) if strokes_list: strokes = ', '.join('/'.join(x) for x in strokes_list) label = '%s is mapped from %s' % (translation, strokes) else: label = '%s is not in the dictionary' % translation label = util.shorten_unicode(label) else: label = '' self.translation_mapping_text.SetLabel(label) self.GetSizer().Layout()
def on_translation_change(self, event): translation = event.GetString() self.listbox.Clear() if translation: suggestions = self.engine.get_suggestions( unescape_translation(translation) ) if suggestions: for suggestion, strokes in suggestions: self.listbox.Append(escape_translation(suggestion)) entries = ('/'.join(x) for x in strokes) for entry in entries: self.listbox.Append(' %s' % (entry)) self.listbox.EnsureVisible(0) else: self.listbox.Append('No entries') self.GetSizer().Layout()
def setData(self, index, value, role=Qt.EditRole, record=True): assert role == Qt.EditRole row = index.row() column = index.column() old_item = self._entries[row] strokes = old_item.strokes steno, translation, dictionary = old_item if column == _COL_STENO: strokes = normalize_steno(value.strip()) steno = '/'.join(strokes) if not steno or steno == old_item.steno: return False elif column == _COL_TRANS: translation = unescape_translation(value.strip()) if translation == old_item.translation: return False elif column == _COL_DICT: path = expand_path(value) for dictionary in self._dictionary_list: if dictionary.path == path: break if dictionary == old_item.dictionary: return False try: del old_item.dictionary[old_item.strokes] except KeyError: pass if not old_item.strokes and not old_item.translation: # Merge operations when editing a newly added row. if self._operations and self._operations[-1] == [(None, old_item)]: self._operations.pop() old_item = None new_item = DictionaryItem(steno, translation, dictionary) self._entries[row] = new_item dictionary[strokes] = translation if record: self._operations.append((old_item, new_item)) self.dataChanged.emit(index, index) return True
def setData(self, index, value, role=Qt.EditRole, record=True): assert role == Qt.EditRole row = index.row() column = index.column() old_item = self._entries[row] strokes, translation, dictionary = old_item if column == _COL_STENO: strokes = normalize_steno(value.strip()) if not strokes or strokes == old_item.strokes: return False elif column == _COL_TRANS: translation = unescape_translation(value.strip()) if translation == old_item.translation: return False elif column == _COL_DICT: path = expand_path(value) for dictionary in self._dictionary_list: if dictionary.get_path() == path: break if dictionary == old_item.dictionary: return False try: del old_item.dictionary[old_item.strokes] except KeyError: pass if not old_item.strokes and not old_item.translation: # Merge operations when editing a newly added row. if self._operations and self._operations[-1] == [(None, old_item)]: self._operations.pop() old_item = None new_item = DictionaryItem(strokes, translation, dictionary) self._entries[row] = new_item dictionary[strokes] = translation if record: self._operations.append((old_item, new_item)) self.dataChanged.emit(index, index) return True
def on_apply_filter(self): strokes_filter = '/'.join(normalize_steno(self.strokes_filter.text().strip())) translation_filter = unescape_translation(self.translation_filter.text().strip()) self._model.filter(strokes_filter=strokes_filter, translation_filter=translation_filter)
def _translation(self): translation = self.translation.text().strip() return unescape_translation(translation)
def test_escape_unescape_translation(raw, escaped): assert unescape_translation(escaped) == raw assert escape_translation(raw) == escaped
def on_lookup(self, pattern): translation = unescape_translation(pattern.strip()) suggestion_list = self._engine.get_suggestions(translation) self._update_suggestions(suggestion_list)