示例#1
0
    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)
示例#2
0
 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))
示例#3
0
 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)
示例#4
0
 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()
示例#5
0
 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()
示例#6
0
 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)
示例#7
0
 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
示例#8
0
    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)
示例#9
0
文件: lookup.py 项目: Potam-IL/plover
 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()
示例#10
0
文件: lookup.py 项目: Achim63/plover
 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()
示例#11
0
 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()
示例#12
0
 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()
示例#13
0
 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()
示例#14
0
 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
示例#15
0
 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
示例#16
0
 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)
示例#17
0
 def _translation(self):
     translation = self.translation.text().strip()
     return unescape_translation(translation)
示例#18
0
def test_escape_unescape_translation(raw, escaped):
    assert unescape_translation(escaped) == raw
    assert escape_translation(raw) == escaped
示例#19
0
 def _translation(self):
     translation = self.translation.text().strip()
     return unescape_translation(translation)
示例#20
0
def test_escape_unescape_translation(raw, escaped):
    assert unescape_translation(escaped) == raw
    assert escape_translation(raw) == escaped
示例#21
0
 def on_lookup(self, pattern):
     translation = unescape_translation(pattern.strip())
     suggestion_list = self._engine.get_suggestions(translation)
     self._update_suggestions(suggestion_list)
示例#22
0
 def on_lookup(self, pattern):
     translation = unescape_translation(pattern.strip())
     suggestion_list = self._engine.get_suggestions(translation)
     self._update_suggestions(suggestion_list)