From 65e926c18189feadc886358c1ba27df4a5859474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20B=C3=B6hme?= Date: Thu, 30 Mar 2017 21:24:31 +0200 Subject: [PATCH] Convert get_conversation_for_date to return named tuples The get_conversation_for_date method in the logger module returns a list of bare tuples. Knowledge of how to pack and unpack the many individual components of the tuples is split between the caller and the callee, making the method hard to maintain and ugly to use. Therefore, convert the method to return named tuple instead. --- src/common/logger.py | 27 +++++++++++++++++---------- src/history_window.py | 19 +++++++++---------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/common/logger.py b/src/common/logger.py index bb68149e4..07407e6c2 100644 --- a/src/common/logger.py +++ b/src/common/logger.py @@ -636,12 +636,22 @@ class Logger: start_of_day = int(time.mktime(local_time)) return start_of_day + Message = namedtuple('Message', + ['contact_name', 'time', 'kind', 'show', 'message', 'subject', + 'additional_data', 'log_line_id']) + def get_conversation_for_date(self, jid, year, month, day, account): """ - Return contact_name, time, kind, show, message, subject, additional_data, log_line_id + Load the complete conversation with a given jid on a specific date - For each row in a list of tuples, returns list with empty tuple if we - found nothing to meet our demands + The conversation contains all messages that were exchanged between + `account` and `jid` on the day specified by `year`, `month` and `day`, + where `month` and `day` are 1-based. + + The conversation will be returned as a list of single messages of type + `Logger.Message`. Messages in the list are sorted chronologically. An + empty list will be returned if there are no messages in the log database + for the requested combination of `jid` and `account` on the given date. """ try: self.get_jid_id(jid) @@ -663,14 +673,11 @@ class Logger: ORDER BY time ''' % (where_sql, start_of_day, last_second_of_day), jid_tuple) - results = self.cur.fetchall() - messages = [] - for entry in results: - additional_data = json.loads(entry[6]) - parsed_entry = entry[:6] + (additional_data, ) + entry[7:] - messages.append(parsed_entry) + results = [self.Message(*row) for row in self.cur.fetchall()] + for message in results: + message._replace(additional_data=json.loads(message.additional_data)) - return messages + return results SearchResult = namedtuple('SearchResult', ['contact_name', 'time', 'kind', 'show', 'message', 'subject', diff --git a/src/history_window.py b/src/history_window.py index 1635cdb8a..e92e68576 100644 --- a/src/history_window.py +++ b/src/history_window.py @@ -391,20 +391,19 @@ class HistoryWindow: """ Add all the lines for given date in textbuffer """ - self.history_buffer.set_text('') # clear the buffer first + self.history_buffer.set_text('') self.last_time_printout = 0 show_status = self.show_status_checkbutton.get_active() - lines = gajim.logger.get_conversation_for_date(self.jid, year, month, day, self.account) - for line in lines: - # line[0] is contact_name, line[1] is time of message - # line[2] is kind, line[3] is show, line[4] is message, line[5] is subject - # line[6] is additional_data, line[7] is log_line_id - if not show_status and line[2] in (KindConstant.GCSTATUS, - KindConstant.STATUS): + conversation = gajim.logger.get_conversation_for_date( + self.jid, year, month, day, self.account) + for message in conversation: + if not show_status and message.kind in (KindConstant.GCSTATUS, + KindConstant.STATUS): continue - self._add_new_line(line[0], line[1], line[2], line[3], line[4], - line[5], line[6], line[7]) + self._add_new_line(message.contact_name, message.time, message.kind, + message.show, message.message, message.subject, + message.additional_data, message.log_line_id) def _add_new_line(self, contact_name, tim, kind, show, message, subject, additional_data, log_line_id):