From c8f3d99eab089d337e5fdcb8ff39efd6ab1aa5e9 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sun, 24 May 2020 13:14:33 +0200 Subject: Consistently use set/frozenset for tags. --- alot/buffers/taglist.py | 12 ++++++------ alot/commands/globals.py | 4 ++-- alot/commands/search.py | 10 +++++----- alot/commands/thread.py | 11 +++++------ alot/db/envelope.py | 6 +++--- alot/db/manager.py | 9 ++++----- alot/db/message.py | 12 ++++++------ alot/db/thread.py | 8 ++++---- 8 files changed, 35 insertions(+), 37 deletions(-) (limited to 'alot') diff --git a/alot/buffers/taglist.py b/alot/buffers/taglist.py index bb344c45..e8c8df33 100644 --- a/alot/buffers/taglist.py +++ b/alot/buffers/taglist.py @@ -13,8 +13,10 @@ class TagListBuffer(Buffer): modename = 'taglist' - def __init__(self, alltags=None): - self.tags = alltags or [] + _tags = None + + def __init__(self, tags): + self._tags = sorted(tags, key = str.lower) self.isinitialized = False self.rebuild() @@ -28,9 +30,7 @@ class TagListBuffer(Buffer): self.isinitialized = True lines = list() - displayedtags = sorted((t for t in self.tags if t), - key=str.lower) - for (num, b) in enumerate(displayedtags): + for (num, b) in enumerate(self._tags): if (num % 2) == 0: attr = settings.get_theming_attribute('taglist', 'line_even') else: @@ -50,7 +50,7 @@ class TagListBuffer(Buffer): self.taglist = urwid.ListBox(urwid.SimpleListWalker(lines)) self.body = self.taglist - self.taglist.set_focus(focusposition % len(displayedtags)) + self.taglist.set_focus(focusposition % len(self._tags)) def focus_first(self): """Focus the first line in the tag list.""" diff --git a/alot/commands/globals.py b/alot/commands/globals.py index b803b9fd..c2128135 100644 --- a/alot/commands/globals.py +++ b/alot/commands/globals.py @@ -563,7 +563,7 @@ class TagListCommand(Command): Command.__init__(self, **kwargs) def apply(self, ui): - tags = self.tags or ui.dbman.get_all_tags() + tags = frozenset(self.tags) if self.tags else ui.dbman.get_all_tags() blists = ui.get_buffers_of_type(buffers.TagListBuffer) if blists: buf = blists[0] @@ -977,7 +977,7 @@ class ComposeCommand(Command): if self.bcc: self.envelope.add('Bcc', ','.join(self.bcc)) if self.tags: - self.envelope.tags = [t for t in self.tags.split(',') if t] + self.envelope.tags = frozenset(filter(None, self.tags.split(','))) async def _set_subject(self, ui): if settings.get('ask_subject') and \ diff --git a/alot/commands/search.py b/alot/commands/search.py index 5d7a7cbd..8e959e65 100644 --- a/alot/commands/search.py +++ b/alot/commands/search.py @@ -196,7 +196,7 @@ class TagCommand(Command): ui.update() - tags = [x for x in self.tagsstring.split(',') if x] + tags = frozenset(filter(None, self.tagsstring.split(','))) try: if self.action == 'add': @@ -207,13 +207,13 @@ class TagCommand(Command): ui.dbman.untag(testquery, tags) elif self.action == 'toggle': if not self.allm: - to_remove = [] - to_add = [] + to_remove = set() + to_add = () for t in tags: if t in thread.get_tags(): - to_remove.append(t) + to_remove.add(t) else: - to_add.append(t) + to_add.add(t) thread.remove_tags(to_remove) thread.add_tags(to_add, afterwards=refresh) except DatabaseROError: diff --git a/alot/commands/thread.py b/alot/commands/thread.py index de948747..1da58560 100644 --- a/alot/commands/thread.py +++ b/alot/commands/thread.py @@ -483,7 +483,6 @@ class EditNewCommand(Command): tags = set(self.message.get_tags()) tags.difference_update({'inbox', 'sent', 'draft', 'killed', 'replied', 'signed', 'encrypted', 'unread', 'attachment'}) - tags = list(tags) # set body text mailcontent = self.message.get_body_text() envelope = Envelope(bodytext=mailcontent, tags=tags) @@ -1078,7 +1077,7 @@ class TagCommand(Command): else: messages = [ui.current_buffer.get_selected_message()] - tags = [t for t in self.tagsstring.split(',') if t] + tags = frozenset(filter(None, self.tagsstring.split(','))) try: for m in messages: if self.action == 'add': @@ -1088,13 +1087,13 @@ class TagCommand(Command): elif self.action == 'remove': m.remove_tags(tags) elif self.action == 'toggle': - to_remove = [] - to_add = [] + to_remove = set() + to_add = () for t in tags: if t in m.get_tags(): - to_remove.append(t) + to_remove.add(t) else: - to_add.append(t) + to_add.add(t) m.remove_tags(to_remove) m.add_tags(to_add) diff --git a/alot/db/envelope.py b/alot/db/envelope.py index 041b7124..314be03c 100644 --- a/alot/db/envelope.py +++ b/alot/db/envelope.py @@ -44,7 +44,7 @@ class Envelope: """template text for initial content""" attachments = None """list of :class:`Attachments `""" - tags = [] + tags = None """tags to add after successful sendout""" account = None """account to send from""" @@ -65,7 +65,7 @@ class Envelope: :param attachments: file attachments to include :type attachments: list of :class:`~alot.db.attachment.Attachment` :param tags: tags to add after successful sendout and saving this msg - :type tags: list of str + :type tags: set of str :param replied: message being replied to :type replied: :class:`~alot.db.message.Message` :param passed: message being passed on @@ -87,7 +87,7 @@ class Envelope: self.sign_key = sign_key self.encrypt = encrypt self.encrypt_keys = {} - self.tags = tags or [] # tags to add after successful sendout + self.tags = tags or frozenset() # tags to add after successful sendout self.replied = replied # message being replied to self.passed = passed # message being passed on self.sent_time = None diff --git a/alot/db/manager.py b/alot/db/manager.py index 8ba6bd02..6424f32f 100644 --- a/alot/db/manager.py +++ b/alot/db/manager.py @@ -202,8 +202,8 @@ class DBManager: :param querystring: notmuch search string :type querystring: str - :param tags: a list of tags to be added - :type tags: list of str + :param tags: a set of tags to be added + :type tags: set of str :param afterwards: callback that gets called after successful application of this tagging operation :type afterwards: callable @@ -269,10 +269,9 @@ class DBManager: def get_all_tags(self): """ returns all tagsstrings used in the database - :rtype: list of str + :rtype: set of str """ - # XXX should be set - return list(self._db_ro().tags) + return self._db_ro().tags def get_named_queries(self): """ diff --git a/alot/db/message.py b/alot/db/message.py index 02d5565b..61f95383 100644 --- a/alot/db/message.py +++ b/alot/db/message.py @@ -435,8 +435,8 @@ class Message: return self._email def get_tags(self): - """returns tags attached to this message as list of strings""" - return sorted(self._tags) + """returns tags attached to this message as set of strings""" + return self._tags def get_datestring(self): """ @@ -469,8 +469,8 @@ class Message: :class:`DBManager's ` write queue. You need to call :meth:`~alot.db.DBManager.flush` to write out. - :param tags: a list of tags to be added - :type tags: list of str + :param tags: a set of tags to be added + :type tags: set of str :param afterwards: callback that gets called after successful application of this tagging operation :type afterwards: callable @@ -498,8 +498,8 @@ class Message: :class:`DBManager's ` write queue. You need to call :meth:`~alot.db.DBManager.flush` to actually out. - :param tags: a list of tags to be added - :type tags: list of str + :param tags: a set of tags to be removed + :type tags: set of str :param afterwards: callback that gets called after successful application of this tagging operation :type afterwards: callable diff --git a/alot/db/thread.py b/alot/db/thread.py index db72aad6..2b580dcf 100644 --- a/alot/db/thread.py +++ b/alot/db/thread.py @@ -151,8 +151,8 @@ class Thread: You need to call :meth:`DBManager.flush ` to actually write out. - :param tags: a list of tags to be added - :type tags: list of str + :param tags: a set of tags to be added + :type tags: set of str :param afterwards: callback that gets called after successful application of this tagging operation :type afterwards: callable @@ -178,8 +178,8 @@ class Thread: You need to call :meth:`DBManager.flush ` to actually write out. - :param tags: a list of tags to be added - :type tags: list of str + :param tags: a set of tags to be added + :type tags: set of str :param afterwards: callback that gets called after successful application of this tagging operation :type afterwards: callable -- cgit v1.2.3