def test_find(self): ' Test searching for substrings ' self.ae((1, 1), icu.find(b'a', b'1ab')) self.ae((1, 1), icu.find('\U0001f431', 'x\U0001f431x')) self.ae((1, 1), icu.find('y', '\U0001f431y')) self.ae((0, 4), icu.primary_find('pena', 'peña')) for k, v in iteritems({'pèché': 'peche', 'flüße':'Flusse', 'Štepánek':'ŠtepaneK'}): self.ae((1, len(k)), icu.primary_find(v, ' ' + k), f'Failed to find {v} in {k}') self.assertTrue(icu.startswith(b'abc', b'ab')) self.assertTrue(icu.startswith('abc', 'abc')) self.assertFalse(icu.startswith('xyz', 'a')) self.assertTrue(icu.startswith('xxx', '')) self.assertTrue(icu.primary_startswith('pena', 'peña')) self.assertTrue(icu.contains('\U0001f431', '\U0001f431')) self.assertTrue(icu.contains('something', 'some other something else')) self.assertTrue(icu.contains('', 'a')) self.assertTrue(icu.contains('', '')) self.assertFalse(icu.contains('xxx', 'xx')) self.assertTrue(icu.primary_contains('pena', 'peña')) x = icu.primary_collator() self.ae(x.get_attribute(icu._icu.UCOL_STRENGTH), icu._icu.UCOL_PRIMARY), self.ae((0, 4), icu.primary_no_punc_find('pena"', 'peña')) self.ae((0, 13), icu.primary_no_punc_find("typographers", 'typographer’s')) self.ae((0, 7), icu.primary_no_punc_find('abcd', 'a\u00adb\u200cc\u200dd')) self.ae((0, 5), icu.primary_no_punc_find('abcd', 'ab cd')) # test find all m = [] a = lambda p,l : m.append((p, l)) icu.primary_collator_without_punctuation().find_all('a', 'a a🐱a', a) self.ae(m, [(0, 1), (2, 1), (5, 1)]) # test find whole words c = icu.primary_collator_without_punctuation() self.ae(c.find('a', 'abc a bc'), (0, 1)) self.ae(c.find('a', 'abc a bc', True), (4, 1)) self.ae(c.find('pena', 'a peñaabc peña', True), (10, 4))
def test_find(self): ' Test searching for substrings ' self.ae((1, 1), icu.find(b'a', b'1ab')) self.ae((1, 1 if sys.maxunicode >= 0x10ffff else 2), icu.find('\U0001f431', 'x\U0001f431x')) self.ae((1 if sys.maxunicode >= 0x10ffff else 2, 1), icu.find('y', '\U0001f431y')) self.ae((0, 4), icu.primary_find('pena', 'peña')) for k, v in { u'pèché': u'peche', u'flüße': u'Flusse', u'Štepánek': u'ŠtepaneK' }.iteritems(): self.ae((1, len(k)), icu.primary_find(v, ' ' + k), 'Failed to find %s in %s' % (v, k)) self.assertTrue(icu.startswith(b'abc', b'ab')) self.assertTrue(icu.startswith('abc', 'abc')) self.assertFalse(icu.startswith('xyz', 'a')) self.assertTrue(icu.startswith('xxx', '')) self.assertTrue(icu.primary_startswith('pena', 'peña')) self.assertTrue(icu.contains('\U0001f431', '\U0001f431')) self.assertTrue(icu.contains('something', 'some other something else')) self.assertTrue(icu.contains('', 'a')) self.assertTrue(icu.contains('', '')) self.assertFalse(icu.contains('xxx', 'xx')) self.assertTrue(icu.primary_contains('pena', 'peña'))
def set_completion_prefix(self, prefix): old_prefix = self.current_prefix self.current_prefix = prefix if prefix == old_prefix: return if not prefix: self.current_items = self.all_items self.reset() return subset = prefix.startswith(old_prefix) universe = self.current_items if subset else self.all_items self.current_items = tuple(x for x in universe if primary_startswith(x, prefix)) self.reset()
def test_find(self): ' Test searching for substrings ' self.ae((1, 1), icu.find(b'a', b'1ab')) self.ae((1, 1 if sys.maxunicode >= 0x10ffff else 2), icu.find('\U0001f431', 'x\U0001f431x')) self.ae((1 if sys.maxunicode >= 0x10ffff else 2, 1), icu.find('y', '\U0001f431y')) self.ae((0, 4), icu.primary_find('pena', 'peña')) for k, v in {u'pèché': u'peche', u'flüße':u'Flusse', u'Štepánek':u'ŠtepaneK'}.iteritems(): self.ae((1, len(k)), icu.primary_find(v, ' ' + k), 'Failed to find %s in %s' % (v, k)) self.assertTrue(icu.startswith(b'abc', b'ab')) self.assertTrue(icu.startswith('abc', 'abc')) self.assertFalse(icu.startswith('xyz', 'a')) self.assertTrue(icu.startswith('xxx', '')) self.assertTrue(icu.primary_startswith('pena', 'peña')) self.assertTrue(icu.contains('\U0001f431', '\U0001f431')) self.assertTrue(icu.contains('something', 'some other something else')) self.assertTrue(icu.contains('', 'a')) self.assertTrue(icu.contains('', '')) self.assertFalse(icu.contains('xxx', 'xx')) self.assertTrue(icu.primary_contains('pena', 'peña'))
def test_find(self): " Test searching for substrings " self.ae((1, 1), icu.find(b"a", b"1ab")) self.ae((1, 1 if sys.maxunicode >= 0x10FFFF else 2), icu.find("\U0001f431", "x\U0001f431x")) self.ae((1 if sys.maxunicode >= 0x10FFFF else 2, 1), icu.find("y", "\U0001f431y")) self.ae((0, 4), icu.primary_find("pena", "peña")) for k, v in {"pèché": "peche", "flüße": "Flusse", "Štepánek": "ŠtepaneK"}.iteritems(): self.ae((1, len(k)), icu.primary_find(v, " " + k), "Failed to find %s in %s" % (v, k)) self.assertTrue(icu.startswith(b"abc", b"ab")) self.assertTrue(icu.startswith("abc", "abc")) self.assertFalse(icu.startswith("xyz", "a")) self.assertTrue(icu.startswith("xxx", "")) self.assertTrue(icu.primary_startswith("pena", "peña")) self.assertTrue(icu.contains("\U0001f431", "\U0001f431")) self.assertTrue(icu.contains("something", "some other something else")) self.assertTrue(icu.contains("", "a")) self.assertTrue(icu.contains("", "")) self.assertFalse(icu.contains("xxx", "xx")) self.assertTrue(icu.primary_contains("pena", "peña"))
def test_find(self): ' Test searching for substrings ' self.ae((1, 1), icu.find(b'a', b'1ab')) self.ae((1, 1), icu.find('\U0001f431', 'x\U0001f431x')) self.ae((1, 1), icu.find('y', '\U0001f431y')) self.ae((0, 4), icu.primary_find('pena', 'peña')) for k, v in iteritems({ 'pèché': 'peche', 'flüße': 'Flusse', 'Štepánek': 'ŠtepaneK' }): self.ae((1, len(k)), icu.primary_find(v, ' ' + k), f'Failed to find {v} in {k}') self.assertTrue(icu.startswith(b'abc', b'ab')) self.assertTrue(icu.startswith('abc', 'abc')) self.assertFalse(icu.startswith('xyz', 'a')) self.assertTrue(icu.startswith('xxx', '')) self.assertTrue(icu.primary_startswith('pena', 'peña')) self.assertTrue(icu.contains('\U0001f431', '\U0001f431')) self.assertTrue(icu.contains('something', 'some other something else')) self.assertTrue(icu.contains('', 'a')) self.assertTrue(icu.contains('', '')) self.assertFalse(icu.contains('xxx', 'xx')) self.assertTrue(icu.primary_contains('pena', 'peña'))
def index_for_prefix(self, prefix): for i, item in enumerate(self.current_items): if primary_startswith(item, prefix): return self.index(i)
def fill_in_table(self, tags, tag_to_match, ttm_is_first_letter): data = self.get_book_ids(self.apply_vl_checkbox.isChecked()) self.all_tags = {} filter_text = icu_lower(unicode_type(self.filter_box.text())) for k, v, count in data: if not filter_text or self.string_contains(filter_text, icu_lower(v)): self.all_tags[v] = { 'key': k, 'count': count, 'cur_name': v, 'is_deleted': k in self.to_delete } self.original_names[k] = v if self.is_enumerated: self.edit_delegate.set_completion_data(self.enum_permitted_values) else: self.edit_delegate.set_completion_data( self.original_names.values()) self.ordered_tags = sorted(self.all_tags.keys(), key=self.sorter) if tags is None: tags = self.ordered_tags select_item = None self.table.blockSignals(True) self.table.clear() self.table.setColumnCount(3) self.name_col = QTableWidgetItem(self.category_name) self.table.setHorizontalHeaderItem(0, self.name_col) self.count_col = QTableWidgetItem(_('Count')) self.table.setHorizontalHeaderItem(1, self.count_col) self.was_col = QTableWidgetItem(_('Was')) self.table.setHorizontalHeaderItem(2, self.was_col) self.table.setRowCount(len(tags)) for row, tag in enumerate(tags): item = NameTableWidgetItem(self.sorter) item.set_is_deleted(self.all_tags[tag]['is_deleted']) _id = self.all_tags[tag]['key'] item.setData(Qt.UserRole, _id) item.set_initial_text(tag) if _id in self.to_rename: item.setText(self.to_rename[_id]) else: item.setText(tag) if self.is_enumerated and unicode_type( item.text()) not in self.enum_permitted_values: item.setBackground(QColor('#FF2400')) item.setToolTip( '<p>' + _("This is not one of this column's permitted values ({0})" ).format(', '.join(self.enum_permitted_values)) + '</p>') item.setFlags(item.flags() | Qt.ItemIsSelectable | Qt.ItemIsEditable) self.table.setItem(row, 0, item) if select_item is None: if ttm_is_first_letter: if primary_startswith(tag, tag_to_match): select_item = item elif tag == tag_to_match: select_item = item item = CountTableWidgetItem(self.all_tags[tag]['count']) # only the name column can be selected item.setFlags(item.flags() & ~(Qt.ItemIsSelectable | Qt.ItemIsEditable)) self.table.setItem(row, 1, item) item = QTableWidgetItem() item.setFlags(item.flags() & ~(Qt.ItemIsSelectable | Qt.ItemIsEditable)) if _id in self.to_rename or _id in self.to_delete: item.setData(Qt.DisplayRole, tag) self.table.setItem(row, 2, item) if self.last_sorted_by == 'name': self.table.sortByColumn(0, self.name_order) elif self.last_sorted_by == 'count': self.table.sortByColumn(1, self.count_order) else: self.table.sortByColumn(2, self.was_order) if select_item is not None: self.table.setCurrentItem(select_item) self.table.setFocus(True) self.start_find_pos = select_item.row() else: self.table.setCurrentCell(0, 0) self.search_box.setFocus() self.start_find_pos = -1 self.table.blockSignals(False)
def show_table(self, id_to_select, select_sort, select_link, is_first_letter): filter_text = icu_lower(unicode_type(self.filter_box.text())) auts_to_show = [] for t in self.find_aut_func( use_virtual_library=self.apply_vl_checkbox.isChecked()): if self.string_contains(filter_text, icu_lower(t[1])): auts_to_show.append(t[0]) self.table.blockSignals(True) self.table.clear() self.table.setColumnCount(3) self.table.setRowCount(len(auts_to_show)) row = 0 for id_, v in self.authors.items(): if id_ not in auts_to_show: continue name, sort, link = (v['name'], v['sort'], v['link']) orig = self.original_authors[id_] name = name.replace('|', ',') name_item = tableItem(name) name_item.setData(Qt.UserRole, id_) if name != orig['name']: name_item.setIcon(self.edited_icon) sort_item = tableItem(sort) if sort != orig['sort']: sort_item.setIcon(self.edited_icon) link_item = tableItem(link) if link != orig['link']: link_item.setIcon(self.edited_icon) self.table.setItem(row, 0, name_item) self.table.setItem(row, 1, sort_item) self.table.setItem(row, 2, link_item) row += 1 self.table.setItemDelegate(EditColumnDelegate(self.completion_data)) self.table.setHorizontalHeaderLabels( [_('Author'), _('Author sort'), _('Link')]) if self.last_sorted_by == 'sort': self.author_sort_order = 1 - self.author_sort_order self.do_sort_by_author_sort() elif self.last_sorted_by == 'author': self.author_order = 1 - self.author_order self.do_sort_by_author() else: self.link_order = 1 - self.link_order self.do_sort_by_link() # Position on the desired item if id_to_select: select_item = None use_as = tweaks['categories_use_field_for_author_name'] for row in range(0, len(auts_to_show)): name_item = self.table.item( row, 1) if use_as else self.table.item(row, 0) if is_first_letter: if primary_startswith(name_item.text(), id_to_select): select_item = self.table.item(row, 1) break elif id_to_select == self.table.item(row, 0).data(Qt.UserRole): if select_sort: select_item = self.table.item(row, 1) elif select_link: select_item = self.table.item(row, 2) else: select_item = name_item break if select_item: self.table.setCurrentItem(select_item) if select_sort or select_link: self.table.editItem(select_item) self.start_find_pos = select_item.row( ) * 2 + select_item.column() else: self.table.setCurrentCell(0, 0) self.start_find_pos = -1 self.table.blockSignals(False)
def show_table(self, id_to_select, select_sort, select_link, is_first_letter): auts_to_show = { t[0] for t in self.find_aut_func( use_virtual_library=self.apply_vl_checkbox.isChecked()) } filter_text = icu_lower(str(self.filter_box.text())) if filter_text: auts_to_show = { id_ for id_ in auts_to_show if self.string_contains( filter_text, icu_lower(self.authors[id_]['name'])) } self.table.blockSignals(True) self.table.clear() self.table.setColumnCount(3) self.table.setRowCount(len(auts_to_show)) row = 0 for id_, v in self.authors.items(): if id_ not in auts_to_show: continue name, sort, link = (v['name'], v['sort'], v['link']) name = name.replace('|', ',') name_item = tableItem(name) name_item.setData(Qt.ItemDataRole.UserRole, id_) sort_item = tableItem(sort) link_item = tableItem(link) self.table.setItem(row, 0, name_item) self.table.setItem(row, 1, sort_item) self.table.setItem(row, 2, link_item) self.set_icon(name_item, id_) self.set_icon(sort_item, id_) self.set_icon(link_item, id_) row += 1 self.table.setItemDelegate(EditColumnDelegate(self.completion_data)) self.table.setHorizontalHeaderLabels( [_('Author'), _('Author sort'), _('Link')]) if self.last_sorted_by == 'sort': self.author_sort_order = 1 - self.author_sort_order self.do_sort_by_author_sort() elif self.last_sorted_by == 'author': self.author_order = 1 - self.author_order self.do_sort_by_author() else: self.link_order = 1 - self.link_order self.do_sort_by_link() # Position on the desired item select_item = None if id_to_select: use_as = tweaks[ 'categories_use_field_for_author_name'] == 'author_sort' for row in range(0, len(auts_to_show)): if is_first_letter: item_txt = str( self.table.item(row, 1).text() if use_as else self. table.item(row, 0).text()) if primary_startswith(item_txt, id_to_select): select_item = self.table.item(row, 1 if use_as else 0) break elif id_to_select == self.table.item(row, 0).data( Qt.ItemDataRole.UserRole): if select_sort: select_item = self.table.item(row, 1) elif select_link: select_item = self.table.item(row, 2) else: select_item = (self.table.item(row, 1) if use_as else self.table.item(row, 0)) break if select_item: self.table.setCurrentItem(select_item) self.table.setFocus(Qt.FocusReason.OtherFocusReason) if select_sort or select_link: self.table.editItem(select_item) self.start_find_pos = select_item.row() * 2 + select_item.column() else: self.table.setCurrentCell(0, 0) self.find_box.setFocus() self.start_find_pos = -1 self.table.blockSignals(False)