From 77bcd2ab6cd0afafc4c9ce8ec720fa167f1baeaf Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sat, 30 Jan 2010 11:24:39 -0500 Subject: [PATCH] libbe.command.html.HTMLGen._long_to_linked_user() handles failed conversion. Before, anything matching libbe.util.id.REGEXP was convert-or-die. Now it's convert-or-no-op. Much safer ;). The new _long_to_linked_user doctest would have failed with the old implementation. --- libbe/command/html.py | 36 ++++++++++++++++++++++++++++++++++-- libbe/util/id.py | 9 +++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/libbe/command/html.py b/libbe/command/html.py index bac310f..47ee587 100644 --- a/libbe/command/html.py +++ b/libbe/command/html.py @@ -267,21 +267,53 @@ class HTMLGen (object): return '\n'.join(comment_entries) def _long_to_linked_user(self, text): + """ + >>> import libbe.bugdir + >>> bd = libbe.bugdir.SimpleBugDir(memory=False) + >>> h = HTMLGen(bd) + >>> h._long_to_linked_user('A link #abc123/a#, and a non-link #x#y#.') + 'A link abc/a, and a non-link #x#y#.' + >>> bd.cleanup() + """ replacer = libbe.util.id.IDreplacer( [self.bd], self._long_to_linked_user_replacer, wrap=False) return re.sub( libbe.util.id.REGEXP, replacer, text) def _long_to_linked_user_replacer(self, bugdirs, long_id): + """ + >>> import libbe.bugdir + >>> import libbe.util.id + >>> bd = libbe.bugdir.SimpleBugDir(memory=False) + >>> a = bd.bug_from_uuid('a') + >>> uuid_gen = libbe.util.id.uuid_gen + >>> libbe.util.id.uuid_gen = lambda : '0123' + >>> c = a.new_comment('comment for link testing') + >>> libbe.util.id.uuid_gen = uuid_gen + >>> c.uuid + '0123' + >>> h = HTMLGen(bd) + >>> h._long_to_linked_user_replacer([bd], 'abc123') + '#abc123#' + >>> h._long_to_linked_user_replacer([bd], 'abc123/a') + 'abc/a' + >>> h._long_to_linked_user_replacer([bd], 'abc123/a/0123') + 'abc/a/012' + >>> h._long_to_linked_user_replacer([bd], 'x') + '#x#' + >>> h._long_to_linked_user_replacer([bd], '') + '##' + >>> bd.cleanup() + """ try: p = libbe.util.id.parse_user(bugdirs[0], long_id) short_id = libbe.util.id.long_to_short_user(bugdirs, long_id) except (libbe.util.id.MultipleIDMatches, libbe.util.id.NoIDMatches, libbe.util.id.InvalidIDStructure), e: - return long_id + return '#%s#' % long_id # re-wrap failures if p['type'] == 'bugdir': - return long_id + return '#%s#' % long_id elif p['type'] == 'bug': return '%s' \ % (p['bug'], short_id) diff --git a/libbe/util/id.py b/libbe/util/id.py index 3c6c957..81f5396 100644 --- a/libbe/util/id.py +++ b/libbe/util/id.py @@ -69,7 +69,7 @@ HIERARCHY = ['bugdir', 'bug', 'comment'] class MultipleIDMatches (ValueError): def __init__(self, id, common, matches): msg = ('More than one id matches %s. ' - 'Please be more specific (%s/*).\n%s' % (id, common, matches)) + 'Please be more specific (%s*).\n%s' % (id, common, matches)) ValueError.__init__(self, msg) self.id = id self.common = common @@ -249,7 +249,12 @@ def child_uuids(child_storage_ids): def long_to_short_user(bugdirs, id): ids = _split(id, check_length=True) - bugdir = [bd for bd in bugdirs if bd.uuid == ids[0]][0] + matching_bugdirs = [bd for bd in bugdirs if bd.uuid == ids[0]] + if len(matching_bugdirs) == 0: + raise NoIDMatches(id, [bd.uuid for bd in bugdirs]) + elif len(matching_bugdirs) > 1: + raise MultipleIDMatches(id, '', [bd.uuid for bd in bugdirs]) + bugdir = matching_bugdirs[0] objects = [bugdir] if len(ids) >= 2: bug = bugdir.bug_from_uuid(ids[1]) -- 2.26.2