diff options
author | Lars Wirzenius <liw@liw.fi> | 2016-12-31 23:54:14 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2016-12-31 23:54:14 +0200 |
commit | 2f50a506b1d2a375c43a6904b54c213c11890f0b (patch) | |
tree | 008f857d68bf7a23646007f30ddce806d72e2daf | |
parent | 81497b71b4ef951679907dce34d3c7883f5d0762 (diff) | |
download | distix-2f50a506b1d2a375c43a6904b54c213c11890f0b.tar.gz |
Allos PGP words as ticket ids
-rw-r--r-- | distixlib/plugins/set_plugin.py | 2 | ||||
-rw-r--r-- | distixlib/templates/list.j2 | 2 | ||||
-rw-r--r-- | distixlib/ticket.py | 11 | ||||
-rw-r--r-- | distixlib/ticket_tests.py | 11 | ||||
-rw-r--r-- | distixlib/util.py | 25 |
5 files changed, 48 insertions, 3 deletions
diff --git a/distixlib/plugins/set_plugin.py b/distixlib/plugins/set_plugin.py index 1fd99f2..7a82e26 100644 --- a/distixlib/plugins/set_plugin.py +++ b/distixlib/plugins/set_plugin.py @@ -36,7 +36,7 @@ class SetPlugin(cliapp.Plugin): repo = distixlib.Repository('.') repo.require_clean_working_tree() - ticket_id = distixlib.get_ticket_ids(self.app.settings, args)[0] + ticket_id = distixlib.get_ticket_ids(self.app.settings, [args[0]])[0] ticket_store = repo.open_ticket_store(distixlib.tickets_dir_name) ticket = ticket_store.get_ticket(ticket_id) for pair in args[1:]: diff --git a/distixlib/templates/list.j2 b/distixlib/templates/list.j2 index 3bdb486..d3453ea 100644 --- a/distixlib/templates/list.j2 +++ b/distixlib/templates/list.j2 @@ -1,3 +1,3 @@ {% for ticket in tickets %} -{{ ticket.get_ticket_id() }} {{ ticket.get_newest_message_timestamp()|date }} {{ ticket.get_title() }} +{{ ticket.get_ticket_id() }} ({{ ticket.get_ticket_id_as_words().upper() }}) {{ ticket.get_newest_message_timestamp()|date }} {{ ticket.get_title() }} {% endfor %} diff --git a/distixlib/ticket.py b/distixlib/ticket.py index 907b1d0..9ac6c83 100644 --- a/distixlib/ticket.py +++ b/distixlib/ticket.py @@ -18,9 +18,14 @@ import email +import pgpwordlist + import distixlib +NYBBLES_FOR_WORDS = 6 + + class TicketAlreadyHasId(distixlib.StructuredError): msg = 'Ticket {ticket_id} already has an id' @@ -58,6 +63,12 @@ class Ticket(object): def get_ticket_id(self): return self._metadata.get_first_value('ticket-id') + def get_ticket_id_as_words(self): + ticket_id = self.get_ticket_id() + if ticket_id is not None: + return pgpwordlist.hex_to_words(ticket_id[:NYBBLES_FOR_WORDS]) + return None + def set_ticket_id(self, ticket_id): if 'ticket-id' in self._metadata: raise TicketAlreadyHasId(ticket_id=self.get_ticket_id()) diff --git a/distixlib/ticket_tests.py b/distixlib/ticket_tests.py index 839f947..99c0d64 100644 --- a/distixlib/ticket_tests.py +++ b/distixlib/ticket_tests.py @@ -50,6 +50,17 @@ class TicketTests(unittest.TestCase): ticket.set_ticket_id('this-is-my-id') self.assertEqual(ticket.get_ticket_id(), 'this-is-my-id') + def test_returns_ticket_id_None_instead_of_words_if_unset(self): + ticket = distixlib.Ticket() + self.assertEqual(ticket.get_ticket_id_as_words(), None) + + def test_returns_ticket_id_as_words(self): + ticket = distixlib.Ticket() + ticket.set_ticket_id('010203') + self.assertEqual( + ticket.get_ticket_id_as_words(), + 'absurd aftermath acme') + def test_raises_error_if_setting_id_of_ticket_with_id(self): ticket = distixlib.Ticket() ticket.set_ticket_id('this-is-my-id') diff --git a/distixlib/util.py b/distixlib/util.py index 9c0ae12..5db5e66 100644 --- a/distixlib/util.py +++ b/distixlib/util.py @@ -16,14 +16,37 @@ # =*= License: GPL-3+ =*= +import re + +import pgpwordlist + + def get_ticket_ids(settings, args): filename = settings['ticket-id-from'] if filename: return _read_ticket_ids_from_file(filename) else: - return args + return _ticket_ids_from_words(args) def _read_ticket_ids_from_file(filename): with open(filename) as f: return [line.strip() for line in f.readlines()] + + +def _ticket_ids_from_words(args): + ticket_ids = [] + for arg in args: + if _is_hex(arg): + ticket_ids.append(arg) + else: + assert arg is not None, 'xxx: arg=%r' % arg + ticket_ids.append(pgpwordlist.words_to_hex(arg)) + return ticket_ids + + +_hex_pat = re.compile(r'^[0-9a-fA-F]+$') + + +def _is_hex(arg): + return _hex_pat.match(arg) is not None |