From b7b3c334a24e3c1d4c29efb3fe456783eff087fd Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Tue, 14 Apr 2009 17:57:17 +1000 Subject: [PATCH] "thread" is now available to all interfaces; Zork now can show threads --- mitterlib/ui/helpers/console_utils.py | 44 +++++++++++++++++++++++++++ mitterlib/ui/ui_cmd.py | 35 ++------------------- mitterlib/ui/ui_zork.py | 7 +++++ 3 files changed, 53 insertions(+), 33 deletions(-) diff --git a/mitterlib/ui/helpers/console_utils.py b/mitterlib/ui/helpers/console_utils.py index 5e00227..bb58768 100644 --- a/mitterlib/ui/helpers/console_utils.py +++ b/mitterlib/ui/helpers/console_utils.py @@ -20,11 +20,14 @@ import textwrap import locale import getpass +import logging from mitterlib.network.networkbase import NetworkData from timesince import timesince +_log = logging.getLogger('mitterlib.ui.helpers.console_utils') + def encode_print(text): """Try to print the text; if we get any UnicodeEncodeError, we print it @@ -143,3 +146,44 @@ def authorization(options, config): if value: config[namespace][name] = value print + + +def _thread(thread_list, connection, message_id, network): + """Build a conversation thread.""" + _log.debug('Requesting message %s.%s' % (message_id, network)) + try: + message = connection.message(message_id, network) + except NetworkError, exc: + _log.debug('Network error:') + _log.debug(exc) + thread_list.insert(0, 'Network error') + return thread_list + # TODO: Catch a permission denied exception and add a proper message + # for it. + + thread_list.insert(0, message) + if message.parent: + _log.debug('Following parent %s', message.parent) + _thread(thread_list, connection, message.parent, network) + return thread_list + + +def fetch_thread(initial_message, connection): + """Retrieves a conversation thread, when possible. Returns a list of + :obj:`NetworkData` objects, with the initial_message as the last element + (so parents are in the top of the list.)""" + + thread = [initial_message] + return _thread(thread, connection, initial_message.parent, + initial_message.network) + + +def print_thread(thread_list, connection): + """Given a thread created by :func:`fetch_thread`, display the data using + the default console functions.""" + pos = 0 + _log.debug('%d messages in thread', len(thread_list)) + for message in thread_list: + print_messages(message, connection, show_numbers=False, indent=pos) + pos += 1 + return diff --git a/mitterlib/ui/ui_cmd.py b/mitterlib/ui/ui_cmd.py index 1ca905c..d9b5347 100644 --- a/mitterlib/ui/ui_cmd.py +++ b/mitterlib/ui/ui_cmd.py @@ -145,8 +145,8 @@ class Interface(cmd.Cmd): return message = self._messages[message_id - 1] - thread = [message] - self._thread(thread, message.parent, message.network) + thread = console_utils.fetch_thread(message, self._connection) + console_utils.print_thread(thread, self._connection) return def emptyline(self): @@ -202,37 +202,6 @@ class Interface(cmd.Cmd): self._update_prompt() return - def _thread(self, thread_list, message_id, network): - """Build a conversation thread.""" - _log.debug('Requesting message %s.%s' % (message_id, network)) - try: - message = self._connection.message(message_id, network) - except NetworkError, exc: - _log.debug('Network error:') - _log.debug(exc) - thread_list.insert(0, 'Network error') - self._print_thread(thread_list) - return - # TODO: Catch a permission denied exception and add a proper message - # for it. - - thread_list.insert(0, message) - if message.parent: - self._thread(thread_list, message.parent, network) - else: - self._print_thread(thread_list) - return - - def _print_thread(self, thread_list): - """Print the conversation thread.""" - pos = 0 - _log.debug('%d messages in thread', len(thread_list)) - for message in thread_list: - console_utils.print_messages(message, self._connection, - show_numbers=False, indent=pos) - pos += 1 - return - def _update(self, status, reply_to=None): """Send the update to the server.""" try: diff --git a/mitterlib/ui/ui_zork.py b/mitterlib/ui/ui_zork.py index c2bd56d..184dee2 100644 --- a/mitterlib/ui/ui_zork.py +++ b/mitterlib/ui/ui_zork.py @@ -134,6 +134,13 @@ class Interface(cmd.Cmd): # So we don't send the same message if the user pressed enter # again + def do_thread(self, line): + """Display the conversation thread with the message pointed by the + cursor.""" + message = self._messages[self._cursor] + thread = console_utils.fetch_thread(message, self._connection) + console_utils.print_thread(thread, self._connection) + def do_exit(self, line): """Quit the application.""" _log.debug('Exiting application')