summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Totzke <patricktotzke@gmail.com>2017-01-30 22:05:34 +0000
committerGitHub <noreply@github.com>2017-01-30 22:05:34 +0000
commitb29da9cdcda59b262e3df2a1fb739a33311b2165 (patch)
treecc2192bafa66fdd7e4d8f1b2d944142edefef120
parent53f8800854e32f86f7224c599dab854a1094b010 (diff)
parent514795d2ec9773b8a66cfdfaede3af0d49ad92b7 (diff)
Merge pull request #1009 from dcbaker/pr/remove_cmp
Remove remaining uses of cmp
-rw-r--r--alot/helper.py11
-rw-r--r--alot/widgets/globals.py29
-rw-r--r--alot/widgets/search.py7
-rw-r--r--alot/widgets/thread.py10
-rw-r--r--tests/widgets/__init__.py0
-rw-r--r--tests/widgets/globals_test.py39
6 files changed, 74 insertions, 22 deletions
diff --git a/alot/helper.py b/alot/helper.py
index bd1e3621..2e523c88 100644
--- a/alot/helper.py
+++ b/alot/helper.py
@@ -499,17 +499,6 @@ def shell_quote(text):
return "'%s'" % text.replace("'", """'"'"'""")
-def tag_cmp(a, b):
- r'''
- Sorting tags using this function puts all tags of length 1 at the
- beginning. This groups all tags mapped to unicode characters.
- '''
- if min(len(a), len(b)) == 1 and max(len(a), len(b)) > 1:
- return cmp(len(a), len(b))
- else:
- return cmp(a.lower(), b.lower())
-
-
def humanize_size(size):
"""Create a nice human readable representation of the given number
(understood as bytes) using the "KiB" and "MiB" suffixes to indicate
diff --git a/alot/widgets/globals.py b/alot/widgets/globals.py
index 822d0ae9..60397104 100644
--- a/alot/widgets/globals.py
+++ b/alot/widgets/globals.py
@@ -7,6 +7,7 @@ This contains alot-specific :class:`urwid.Widget` used in more than one mode.
"""
from __future__ import absolute_import
+import functools
import re
import operator
import urwid
@@ -271,6 +272,7 @@ class HeadersList(urwid.WidgetWrap):
return headerlines
+@functools.total_ordering
class TagWidget(urwid.AttrMap):
"""
text widget that renders a tagstring.
@@ -313,3 +315,30 @@ class TagWidget(urwid.AttrMap):
def set_unfocussed(self):
self.set_attr_map(self.attmaps['normal'])
+
+ def __lt__(self, other):
+ """Groups tags of 1 character first, then alphabetically.
+
+ This groups tags unicode characters at the begnining.
+ """
+ if not isinstance(other, TagWidget):
+ return NotImplemented
+
+ self_len = len(self.translated)
+ oth_len = len(other.translated)
+
+ if (self_len == 1) is not (oth_len == 1):
+ return self_len < oth_len
+ return self.translated.lower() < other.translated.lower()
+
+ def __eq__(self, other):
+ if not isinstance(other, TagWidget):
+ return NotImplemented
+ if len(self.translated) != len(other.translated):
+ return False
+ return self.translated.lower() == other.translated.lower()
+
+ def __ne__(self, other):
+ if not isinstance(other, TagWidget):
+ return NotImplemented
+ return self.translated.lower() != other.translated.lower()
diff --git a/alot/widgets/search.py b/alot/widgets/search.py
index a0f23b5e..32a078c7 100644
--- a/alot/widgets/search.py
+++ b/alot/widgets/search.py
@@ -10,7 +10,6 @@ import urwid
from ..settings import settings
from ..helper import shorten_author_string
-from ..helper import tag_cmp
from .utils import AttrFlipWidget
from .globals import TagWidget
@@ -116,10 +115,8 @@ class ThreadlineWidget(urwid.AttrMap):
if self.thread:
fallback_normal = struct[name]['normal']
fallback_focus = struct[name]['focus']
- tag_widgets = [TagWidget(t, fallback_normal, fallback_focus)
- for t in self.thread.get_tags()]
- tag_widgets.sort(tag_cmp,
- lambda tag_widget: tag_widget.translated)
+ tag_widgets = sorted(TagWidget(t, fallback_normal, fallback_focus)
+ for t in self.thread.get_tags())
else:
tag_widgets = []
cols = []
diff --git a/alot/widgets/thread.py b/alot/widgets/thread.py
index 6084ebd5..70e7c840 100644
--- a/alot/widgets/thread.py
+++ b/alot/widgets/thread.py
@@ -15,7 +15,6 @@ from .globals import AttachmentWidget
from ..settings import settings
from ..db.utils import decode_header, X_SIGNATURE_MESSAGE_HEADER
from ..db.utils import extract_body
-from ..helper import tag_cmp
class MessageSummaryWidget(urwid.WidgetWrap):
@@ -47,12 +46,11 @@ class MessageSummaryWidget(urwid.WidgetWrap):
if settings.get('msg_summary_hides_threadwide_tags'):
thread_tags = message.get_thread().get_tags(intersection=True)
outstanding_tags = set(message.get_tags()).difference(thread_tags)
- tag_widgets = [TagWidget(t, attr, focus_att)
- for t in outstanding_tags]
+ tag_widgets = sorted(TagWidget(t, attr, focus_att)
+ for t in outstanding_tags)
else:
- tag_widgets = [TagWidget(t, attr, focus_att)
- for t in message.get_tags()]
- tag_widgets.sort(tag_cmp, lambda tag_widget: tag_widget.translated)
+ tag_widgets = sorted(TagWidget(t, attr, focus_att)
+ for t in message.get_tags())
for tag_widget in tag_widgets:
if not tag_widget.hidden:
cols.append(('fixed', tag_widget.width(), tag_widget))
diff --git a/tests/widgets/__init__.py b/tests/widgets/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/tests/widgets/__init__.py
diff --git a/tests/widgets/globals_test.py b/tests/widgets/globals_test.py
new file mode 100644
index 00000000..7d7d6547
--- /dev/null
+++ b/tests/widgets/globals_test.py
@@ -0,0 +1,39 @@
+# encoding=utf-8
+# Copyright © 2017 Dylan Baker
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+"""Tests for the alot.widgets.globals module."""
+
+from __future__ import absolute_import
+
+import unittest
+
+import mock
+
+from alot.widgets import globals as globals_
+
+
+class TestTagWidget(unittest.TestCase):
+
+ def test_sort(self):
+ """Test sorting."""
+ with mock.patch(
+ 'alot.widgets.globals.settings.get_tagstring_representation',
+ lambda t, _, __: {'translated': t, 'normal': None,
+ 'focussed': None}):
+ expected = ['a', 'z', 'aa', 'bar', 'foo']
+ actual = [g.translated for g in
+ sorted(globals_.TagWidget(x) for x in expected)]
+ self.assertListEqual(actual, expected)