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.
This commit is contained in:
Markus Böhme 2017-03-30 21:24:31 +02:00
parent 08f4e17158
commit 65e926c181
2 changed files with 26 additions and 20 deletions

View File

@ -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',

View File

@ -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):