diff --git a/src/calibre/gui2/library/annotations.py b/src/calibre/gui2/library/annotations.py index 775fdf47b7..e7dfc09d90 100644 --- a/src/calibre/gui2/library/annotations.py +++ b/src/calibre/gui2/library/annotations.py @@ -5,9 +5,9 @@ import codecs import json import os import re +from enum import Enum, auto from functools import lru_cache, partial from urllib.parse import quote -from enum import Enum, auto from qt.core import ( QAbstractItemView, @@ -388,12 +388,13 @@ class Group(Enum): def field_name(self) -> str: '''The underlying annotation or book field this grouping applies to.''' return GROUP_INFO[self]['field_name'] - + @property def display_name(self) -> str: '''A localized human-readable name for this grouping.''' return GROUP_INFO[self]['display_name'] + GROUP_INFO: dict[Group, dict[str, str]] = { Group.BOOK_ID: { 'field_name': 'title', @@ -413,6 +414,7 @@ GROUP_INFO: dict[Group, dict[str, str]] = { }, } + def get_annotation_value(annotation, bid, field, db): ''' Get the value for a field from an annotation result, checking @@ -425,6 +427,7 @@ def get_annotation_value(annotation, bid, field, db): val = db.field_for(field, bid) return val + def get_group_key(result, field, db): ''' Return (sort_key, display_label) for an annotation result grouped by field. @@ -476,13 +479,13 @@ def get_group_key(result, field, db): if not label: label = _('Unknown date') return (days_past, label), label - else: # Assume it's a year + else: # Assume it's a year year = QDateTime.fromString(ts, Qt.DateFormat.ISODate).date().year() current_year = QDateTime.currentDateTime().date().year() years_past = current_year - year label = str(year) if year > 0 else _('Unknown year') return (years_past, label), label - + # Generic fallback val = get_annotation_value(result, bid, field, db) if not val: @@ -493,6 +496,7 @@ def get_group_key(result, field, db): label = str(val) return (sort_key, label), label + class ResultsList(QTreeWidget): current_result_changed = pyqtSignal(object) @@ -820,6 +824,7 @@ class Restrictions(QWidget): self.rla.setVisible(tb_is_visible or ub_is_visible) self.setVisible(True) + class GroupOptions(QWidget): grouping_changed = pyqtSignal() @@ -869,6 +874,7 @@ class GroupOptions(QWidget): gb.setCurrentIndex(row) gb.blockSignals(False) + class BrowsePanel(QWidget): current_result_changed = pyqtSignal(object) diff --git a/src/calibre/gui2/library/test_annotations.py b/src/calibre/gui2/library/test_annotations.py index b4b527b5ad..c2e96f19bd 100644 --- a/src/calibre/gui2/library/test_annotations.py +++ b/src/calibre/gui2/library/test_annotations.py @@ -37,6 +37,7 @@ def _make_mock_db(field_for_map=None, field_metadata=None): db.field_metadata = field_metadata or {} return db + class GroupKeyTest(unittest.TestCase): def test_pubdate_year_buckets_without_day_in_format(self): @@ -57,8 +58,8 @@ class GroupKeyTest(unittest.TestCase): expected_year = QDateTime.fromString(pub_iso, Qt.DateFormat.ISODate).date().year() current_year = QDateTime.currentDateTime().date().year() - self.assertEqual(label, str(expected_year), "Expected label to be the year string") - self.assertEqual(sort_key, (current_year - expected_year, label), "Expected sort key to be (years_past, label)") + self.assertEqual(label, str(expected_year), 'Expected label to be the year string') + self.assertEqual(sort_key, (current_year - expected_year, label), 'Expected sort key to be (years_past, label)') def test_pubdate_unknown_year_when_invalid_timestamp(self): db = _make_mock_db( @@ -73,7 +74,7 @@ class GroupKeyTest(unittest.TestCase): (_, label) = get_group_key(_make_result(), 'pubdate', db) # year() returns 0 for an invalid QDate; the code treats 0 as "unknown" - self.assertIn('Unknown', label, "Expected label to indicate unknown year") + self.assertIn('Unknown', label, 'Expected label to indicate unknown year') def test_arbitrary_text_field_sorts_case_insensitively(self): db = _make_mock_db( @@ -102,7 +103,7 @@ class GroupKeyTest(unittest.TestCase): ) (sort_key, _) = get_group_key(_make_result(), 'publisher', db) - self.assertEqual(sort_key, ('',), "Expected sort key to be the unknown sentinel") + self.assertEqual(sort_key, ('',), 'Expected sort key to be the unknown sentinel') def test_annotation_level_field_read_from_result_dict(self): db = _make_mock_db(field_metadata={'format': {'datatype': 'text'}}) @@ -144,7 +145,7 @@ class GroupKeyTest(unittest.TestCase): (sort_key, label) = get_group_key(_make_result(), 'authors', db) self.assertEqual(label, 'F. Scott Fitzgerald') - self.assertIsInstance(sort_key[0], str) # Don't test the implementation of authors_to_sort_string + self.assertIsInstance(sort_key[0], str) # Don't test the implementation of authors_to_sort_string def test_group_by_authors_unknown_when_empty(self): db = _make_mock_db( @@ -181,7 +182,7 @@ class GroupKeyTest(unittest.TestCase): # Parse the timestamp to verify sort key qdt = QDateTime.fromString(ts_iso, Qt.DateFormat.ISODate) - self.assertTrue(qdt.isValid(), "Test setup: timestamp should be valid") + self.assertTrue(qdt.isValid(), 'Test setup: timestamp should be valid') qdt = qdt.toLocalTime() qdate = qdt.date() today = QDateTime.currentDateTime().toLocalTime().date() diff --git a/src/pyj/srv.pyj b/src/pyj/srv.pyj index 72cd188d17..cd1c73250c 100644 --- a/src/pyj/srv.pyj +++ b/src/pyj/srv.pyj @@ -41,7 +41,8 @@ def toplevel_main(): script = undefined if window.navigator.serviceWorker: window.navigator.serviceWorker.register(absolute_path('service_worker.js')).catch( - def(err): console.error('Service worker registration failed:', err); + def(err): + console.error('Service worker registration failed:', err) ) ajax('ajax-setup', def(end_type, xhr, event):