diff options
author | Lars Wirzenius <liw@liw.fi> | 2016-02-21 11:45:17 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2016-02-21 12:16:16 +0200 |
commit | 1a68a7db5f00341667cc3fd18acac0bbaf9c13d6 (patch) | |
tree | 233c5b88fd4f878045dce6fafbed270cacff4178 | |
parent | 30a32b318b1f84eb5888bfbcaf6fdf6c3744e832 (diff) | |
download | distix-1a68a7db5f00341667cc3fd18acac0bbaf9c13d6.tar.gz |
Add MessageThread class
-rw-r--r-- | distixlib/__init__.py | 1 | ||||
-rw-r--r-- | distixlib/message_thread.py | 39 | ||||
-rw-r--r-- | distixlib/message_thread_tests.py | 55 | ||||
-rw-r--r-- | setup.py | 19 |
4 files changed, 106 insertions, 8 deletions
diff --git a/distixlib/__init__.py b/distixlib/__init__.py index 37e06b8..d22ba60 100644 --- a/distixlib/__init__.py +++ b/distixlib/__init__.py @@ -36,6 +36,7 @@ from .ticket_store import ( TicketHasNoId, TicketAlreadyInStoreError, TicketNotInStoreError) +from .message_thread import MessageThread from .repo import Repository from .git import Git from .util import get_ticket_ids diff --git a/distixlib/message_thread.py b/distixlib/message_thread.py new file mode 100644 index 0000000..715bf71 --- /dev/null +++ b/distixlib/message_thread.py @@ -0,0 +1,39 @@ +# Copyright 2016 Lars Wirzenius +# +# 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/>. +# +# =*= License: GPL-3+ =*= + + +import email +import time + + +class MessageThread(object): + + def __init__(self): + self._messages = [] + + def add_message(self, msg): + self._messages.append(msg) + + def get_messages_in_date_order(self): + return sorted(self._messages, key=self._get_timestamp) + + def _get_timestamp(self, msg): + date_value = msg['Date'] + tm = email.utils.parsedate(date_value) + if tm is None: # pragma: no cover + return 0 + return time.mktime(tm) diff --git a/distixlib/message_thread_tests.py b/distixlib/message_thread_tests.py new file mode 100644 index 0000000..d64b45e --- /dev/null +++ b/distixlib/message_thread_tests.py @@ -0,0 +1,55 @@ +# Copyright 2016 Lars Wirzenius +# +# 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/>. +# +# =*= License: GPL-3+ =*= + + +import email +import unittest + +import distixlib + + +class MessageThreadTests(unittest.TestCase): + + def make_message(self, date): + return email.message_from_string( + 'Date: {}\nFrom: user@example.com\n\nbody\n'.format(date)) + + def test_has_no_messages_initially(self): + thread = distixlib.MessageThread() + self.assertEqual(thread.get_messages_in_date_order(), []) + + def test_sorts_single_message_correctly(self): + msg = self.make_message('Sun, 21 Feb 2016 11:56:07 +0200') + thread = distixlib.MessageThread() + thread.add_message(msg) + self.assertEqual(thread.get_messages_in_date_order(), [msg]) + + def test_sorts_two_messages_added_in_date_order_correctly(self): + older = self.make_message('Sun, 21 Feb 2016 11:56:07 +0200') + newer = self.make_message('Sun, 21 Feb 2016 11:56:16 +0200') + thread = distixlib.MessageThread() + thread.add_message(older) + thread.add_message(newer) + self.assertEqual(thread.get_messages_in_date_order(), [older, newer]) + + def test_sorts_two_messages_added_in_reverse_order_correctly(self): + older = self.make_message('Sun, 21 Feb 2016 11:56:07 +0200') + newer = self.make_message('Sun, 21 Feb 2016 11:56:16 +0200') + thread = distixlib.MessageThread() + thread.add_message(newer) + thread.add_message(older) + self.assertEqual(thread.get_messages_in_date_order(), [older, newer]) @@ -55,17 +55,20 @@ class Check(Command): self.set_all_options(True) def run(self): - if self.unit_tests: - self.run_unit_tests() + try: + if self.unit_tests: + self.run_unit_tests() - if self.yarns: - self.run_yarns() + if self.yarns: + self.run_yarns() - if self.pep8 and self.command_is_available('pep8'): - self.run_pep8() + if self.pep8 and self.command_is_available('pep8'): + self.run_pep8() - if self.pylint and self.command_is_available('pylint'): - self.run_pylint() + if self.pylint and self.command_is_available('pylint'): + self.run_pylint() + except cliapp.AppException: + raise SystemExit(1) def run_unit_tests(self): cliapp.runcmd( |