diff options
author | mathieui <mathieui@mathieui.net> | 2014-04-22 20:02:07 +0200 |
---|---|---|
committer | mathieui <mathieui@mathieui.net> | 2014-04-22 20:02:07 +0200 |
commit | 3415619895054b2c9a1bf1b98bdfa1153c0bf7e6 (patch) | |
tree | e8ae3eaffc57f111a89b48c6cc1be1724b9c9673 /src/text_buffer.py | |
parent | b14aceaa4f3806dd797cd3e31c1973387409784a (diff) | |
download | poezio-3415619895054b2c9a1bf1b98bdfa1153c0bf7e6.tar.gz poezio-3415619895054b2c9a1bf1b98bdfa1153c0bf7e6.tar.bz2 poezio-3415619895054b2c9a1bf1b98bdfa1153c0bf7e6.tar.xz poezio-3415619895054b2c9a1bf1b98bdfa1153c0bf7e6.zip |
80-columns wrapping and some docstrings
also bump version, and add some gettext wraps
Diffstat (limited to 'src/text_buffer.py')
-rw-r--r-- | src/text_buffer.py | 111 |
1 files changed, 85 insertions, 26 deletions
diff --git a/src/text_buffer.py b/src/text_buffer.py index 71820b2f..ed213d2d 100644 --- a/src/text_buffer.py +++ b/src/text_buffer.py @@ -1,12 +1,11 @@ -# Copyright 2010-2011 Florent Le Coz <louiz@louiz.org> -# -# This file is part of Poezio. -# -# Poezio is free software: you can redistribute it and/or modify -# it under the terms of the zlib license. See the COPYING file. - """ Define the TextBuffer class + +A text buffer contains a list of intermediate representations of messages +(not xml stanzas, but neither the Lines used in windows.py. + +Each text buffer can be linked to multiple windows, that will be rendered +independantly by their TextWins. """ import logging @@ -18,13 +17,15 @@ from datetime import datetime from config import config from theming import get_theme, dump_tuple -message_fields = 'txt nick_color time str_time nickname user identifier highlight me old_message revisions jid' +message_fields = ('txt nick_color time str_time nickname user identifier' + ' highlight me old_message revisions jid') Message = collections.namedtuple('Message', message_fields) class CorrectionError(Exception): pass def other_elems(self): + "Helper for the repr_message function" acc = ['Message('] fields = message_fields.split() fields.remove('old_message') @@ -33,6 +34,11 @@ def other_elems(self): return ', '.join(acc) + ', old_message=' def repr_message(self): + """ + repr() for the Message class, for debug purposes, since the default + repr() is recursive, so it can stack overflow given too many revisions + of a message + """ init = other_elems(self) acc = [init] next_message = self.old_message @@ -55,12 +61,17 @@ class TextBuffer(object): This class just keep trace of messages, in a list with various informations and attributes. """ - def __init__(self, messages_nb_limit=config.get('max_messages_in_memory', 2048)): + def __init__(self, messages_nb_limit=None): + + if messages_nb_limit is None: + messages_nb_limit = config.get('max_messages_in_memory', 2048) self.messages_nb_limit = messages_nb_limit - self.messages = [] # Message objects - self.windows = [] # we keep track of one or more windows + # Message objects + self.messages = [] + # we keep track of one or more windows # so we can pass the new messages to them, as they are added, so # they (the windows) can build the lines from the new message + self.windows = [] def add_window(self, win): self.windows.append(win) @@ -71,19 +82,35 @@ class TextBuffer(object): @staticmethod - def make_message(txt, time, nickname, nick_color, history, user, identifier, str_time=None, highlight=False, old_message=None, revisions=0, jid=None): + def make_message(txt, time, nickname, nick_color, history, user, + identifier, str_time=None, highlight=False, + old_message=None, revisions=0, jid=None): + """ + Create a new Message object with parameters, check for /me messages, + and delayed messages + """ time = time or datetime.now() - me = False if txt.startswith('/me '): me = True - txt = '\x19%(info_col)s}' % {'info_col': get_theme().COLOR_ME_MESSAGE[0]} + txt[4:] + txt = '\x19%s}%s' % (dump_tuple(get_theme().COLOR_ME_MESSAGE), + txt[4:]) + else: + me = False if history: - txt = txt.replace('\x19o', '\x19o\x19%s}' % dump_tuple(get_theme().COLOR_LOG_MSG)) + txt = txt.replace('\x19o', '\x19o\x19%s}' % + dump_tuple(get_theme().COLOR_LOG_MSG)) + str_time = time.strftime("%Y-%m-%d %H:%M:%S") + else: + if str_time is None: + str_time = time.strftime("%H:%M:%S") + else: + str_time = '' + msg = Message( txt='%s\x19o'%(txt.replace('\t', ' '),), nick_color=nick_color, time=time, - str_time=(time.strftime("%Y-%m-%d %H:%M:%S") if history else time.strftime("%H:%M:%S")) if str_time is None else '', + str_time=str_time, nickname=nickname, user=user, identifier=identifier, @@ -95,42 +122,74 @@ class TextBuffer(object): log.debug('Set message %s with %s.', identifier, msg) return msg - def add_message(self, txt, time=None, nickname=None, nick_color=None, history=None, user=None, highlight=False, identifier=None, str_time=None, jid=None): - msg = self.make_message(txt, time, nickname, nick_color, history, user, identifier, str_time=str_time, highlight=highlight, jid=jid) + def add_message(self, txt, time=None, nickname=None, + nick_color=None, history=None, user=None, highlight=False, + identifier=None, str_time=None, jid=None): + """ + Create a message and add it to the text buffer + """ + msg = self.make_message(txt, time, nickname, nick_color, history, + user, identifier, str_time=str_time, + highlight=highlight, jid=jid) self.messages.append(msg) + while len(self.messages) > self.messages_nb_limit: self.messages.pop(0) + ret_val = None + show_timestamps = config.get('show_timestamps', True) for window in self.windows: # make the associated windows - # build the lines from the new message - nb = window.build_new_message(msg, history=history, highlight=highlight, timestamp=config.get("show_timestamps", True)) + # build the lines from the new message + nb = window.build_new_message(msg, history=history, + highlight=highlight, + timestamp=show_timestamps) if ret_val is None: ret_val = nb if window.pos != 0: window.scroll_up(nb) + return ret_val or 1 - def modify_message(self, txt, old_id, new_id, highlight=False, time=None, user=None, jid=None): + def modify_message(self, txt, old_id, new_id, highlight=False, + time=None, user=None, jid=None): + """ + Correct a message in a text buffer. + """ + for i in range(len(self.messages) -1, -1, -1): msg = self.messages[i] + if msg.identifier == old_id: if msg.user and msg.user is not user: raise CorrectionError("Different users") elif len(msg.str_time) > 8: # ugly raise CorrectionError("Delayed message") elif not msg.user and (msg.jid is None or jid is None): - raise CorrectionError('Could not check the identity of the sender') + raise CorrectionError('Could not check the ' + 'identity of the sender') elif not msg.user and msg.jid != jid: - raise CorrectionError('Messages %s and %s have not been sent by the same fullJID' % (old_id, new_id)) - message = self.make_message(txt, time if time else msg.time, msg.nickname, msg.nick_color, None, msg.user, new_id, highlight=highlight, old_message=msg, revisions=msg.revisions + 1, jid=jid) + raise CorrectionError('Messages %s and %s have not been ' + 'sent by the same fullJID' % + (old_id, new_id)) + + if not time: + time = msg.time + message = self.make_message(txt, time, msg.nickname, + msg.nick_color, None, msg.user, + new_id, highlight=highlight, + old_message=msg, + revisions=msg.revisions + 1, + jid=jid) self.messages[i] = message log.debug('Replacing message %s with %s.', old_id, new_id) return message - log.debug('Message %s not found in text_buffer, abort replacement.', old_id) + log.debug('Message %s not found in text_buffer, abort replacement.', + old_id) raise CorrectionError("nothing to replace") def del_window(self, win): self.windows.remove(win) def __del__(self): - log.debug('** Deleting %s messages from textbuffer', len(self.messages)) + size = len(self.messages) + log.debug('** Deleting %s messages from textbuffer', size) |