From c83da858d191c663894bef6d192d9bb489b082f5 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sun, 31 Jan 2021 09:51:36 +0100 Subject: db: use symbolic constants for sort orders --- alot/buffers/search.py | 5 +++-- alot/commands/globals.py | 6 +++--- alot/commands/search.py | 7 ++++--- alot/db/manager.py | 19 +++++-------------- alot/db/sort.py | 14 ++++++++++++++ 5 files changed, 29 insertions(+), 22 deletions(-) create mode 100644 alot/db/sort.py diff --git a/alot/buffers/search.py b/alot/buffers/search.py index c1650db4..c7b92920 100644 --- a/alot/buffers/search.py +++ b/alot/buffers/search.py @@ -8,6 +8,7 @@ import urwid from notmuch2 import NotmuchError from .buffer import Buffer +from ..db.sort import NAME as SORT_NAME from ..settings.const import settings from ..widgets.search import ThreadlineWidget @@ -103,8 +104,8 @@ class SearchBuffer(Buffer): self.dbman = ui.dbman self.ui = ui self.querystring = initialquery - default_order = settings.get('search_threads_sort_order') - self.sort_order = sort_order or default_order + default_order = SORT_NAME[settings.get('search_threads_sort_order')] + self.sort_order = sort_order or default_order self.rebuild() super().__init__() diff --git a/alot/commands/globals.py b/alot/commands/globals.py index 6c73e6c9..5304b32d 100644 --- a/alot/commands/globals.py +++ b/alot/commands/globals.py @@ -28,6 +28,7 @@ from ..completion.commandline import CommandLineCompleter from ..completion.contacts import ContactsCompleter from ..completion.accounts import AccountCompleter from ..completion.tags import TagsCompleter +from ..db.sort import NAME as SORT_NAME from ..widgets.utils import DialogBox from ..mail.envelope import Envelope from ..settings.const import settings @@ -77,8 +78,7 @@ class ExitCommand(Command): ui.exit() @registerCommand(MODE, 'search', usage='search query', arguments=[ - (['--sort'], {'help': 'sort order', 'choices': [ - 'oldest_first', 'newest_first', 'message_id', 'unsorted']}), + (['--sort'], {'help': 'sort order', 'choices': list(SORT_NAME.keys())}), (['query'], {'nargs': argparse.REMAINDER, 'help': 'search string'})]) class SearchCommand(Command): @@ -96,7 +96,7 @@ class SearchCommand(Command): :type sort: str """ self.query = ' '.join(query) - self.order = sort + self.order = SORT_NAME[sort] if sort else None super().__init__(**kwargs) def apply(self, ui): diff --git a/alot/commands/search.py b/alot/commands/search.py index 14f267cd..82a3cf39 100644 --- a/alot/commands/search.py +++ b/alot/commands/search.py @@ -13,6 +13,7 @@ from .. import commands from .. import buffers from ..db.errors import DatabaseROError +from ..db.sort import NAME as SORT_NAME MODE = 'search' @@ -35,8 +36,8 @@ class OpenThreadCommand(Command): @registerCommand(MODE, 'refine', help='refine query', arguments=[ - (['--sort'], {'help': 'sort order', 'choices': [ - 'oldest_first', 'newest_first', 'message_id', 'unsorted']}), + (['--sort'], {'help': 'sort order', + 'choices': list(SORT_NAME.keys())}), (['query'], {'nargs': argparse.REMAINDER, 'help': 'search string'})]) @registerCommand(MODE, 'sort', help='set sort order', arguments=[ (['sort'], {'help': 'sort order', 'choices': [ @@ -55,7 +56,7 @@ class RefineCommand(Command): self.querystring = None else: self.querystring = ' '.join(query) - self.sort_order = sort + self.sort_order = SORT_NAME[sort] super().__init__(**kwargs) def apply(self, ui): diff --git a/alot/db/manager.py b/alot/db/manager.py index c4ec02d1..e57af181 100644 --- a/alot/db/manager.py +++ b/alot/db/manager.py @@ -14,6 +14,7 @@ from notmuch2 import Database, NotmuchError from .errors import DatabaseError from .errors import DatabaseROError from .errors import NonexistantObjectError +from .sort import ORDER from .thread import Thread from ..settings.const import settings @@ -116,12 +117,6 @@ class DBManager: lets you look up threads and messages directly to the persistent wrapper classes. """ - _sort_orders = { - 'oldest_first': Database.SORT.OLDEST_FIRST, - 'newest_first': Database.SORT.NEWEST_FIRST, - 'unsorted': Database.SORT.UNSORTED, - 'message_id': Database.SORT.MESSAGE_ID, - } """constants representing sort orders""" _loop = None @@ -202,25 +197,21 @@ class DBManager: queries = filter(lambda k: k.startswith(q_prefix), db.config) return { q[len(q_prefix):] : db.config[q] for q in queries } - def get_threads(self, querystring, sort='newest_first', exclude_tags = frozenset()): + def get_threads(self, querystring, sort = ORDER.NEWEST_FIRST, + exclude_tags = frozenset()): """ asynchronously look up thread ids matching `querystring`. :param querystring: The query string to use for the lookup :type querystring: str. - :param sort: Sort order. one of ['oldest_first', 'newest_first', - 'message_id', 'unsorted'] - :type query: str + :param sort: Sort order. + :type query: alot.db.sort.ORDER :param exclude_tags: Tags to exclude by default unless included in the search :type exclude_tags: set of str :returns: iterator over thread ids """ - # TODO: use a symbolic constant for this - assert sort in self._sort_orders - with self._db_ro() as db: - sort = self._sort_orders[sort] exclude_tags = self._exclude_tags | exclude_tags for t in db.threads(querystring, sort = sort, exclude_tags = exclude_tags): diff --git a/alot/db/sort.py b/alot/db/sort.py new file mode 100644 index 00000000..b93c814d --- /dev/null +++ b/alot/db/sort.py @@ -0,0 +1,14 @@ +# Copyright (C) 2012 Anton Khirnov +# This file is released under the GNU GPL, version 3 or a later revision. +# For further details see the COPYING file + +import notmuch2 + +ORDER = notmuch2.Database.SORT + +NAME = { + 'oldest_first' : ORDER.OLDEST_FIRST, + 'newest_first' : ORDER.NEWEST_FIRST, + 'message_id' : ORDER.MESSAGE_ID, + 'unsorted' : ORDER.UNSORTED, +} -- cgit v1.2.3