Convert get_search_results_for_query to return named tuples

The get_search_results_for_query method in the logger module returns a
list of bare tuples. Knowledge of how to pack and unpack the many
indidual components of the tuples is scattered across both the callers
and the callee, making the method hard to maintain and ugly to use.
Therefore, convert the method to return named tuples instead.
This commit is contained in:
Markus Böhme 2017-03-23 22:56:54 +01:00
parent 167cf02afd
commit d612e80a47
3 changed files with 29 additions and 24 deletions

View file

@ -100,23 +100,22 @@ class StandardCommonCommands(CommandContainer):
raise CommandError(_("Limit must be an integer")) raise CommandError(_("Limit must be an integer"))
for row in results: for row in results:
contact, time, kind, show, message, subject, log_line_id = row contact = row.contact_name
if not contact: if not contact:
if kind == KindConstant.CHAT_MSG_SENT: if row.kind == KindConstant.CHAT_MSG_SENT:
contact = gajim.nicks[self.account] contact = gajim.nicks[self.account]
else: else:
contact = self.contact.name contact = self.contact.name
time_obj = localtime(time) time_obj = localtime(row.time)
date_obj = date.fromtimestamp(time) date_obj = date.fromtimestamp(row.time)
date_ = strftime('%Y-%m-%d', time_obj) date_ = strftime('%Y-%m-%d', time_obj)
time_ = strftime('%H:%M:%S', time_obj) time_ = strftime('%H:%M:%S', time_obj)
if date_obj == date.today(): if date_obj == date.today():
formatted = "[%s] %s: %s" % (time_, contact, message) formatted = "[%s] %s: %s" % (time_, contact, row.message)
else: else:
formatted = "[%s, %s] %s: %s" % (date_, time_, contact, message) formatted = "[%s, %s] %s: %s" % (date_, time_, contact, row.message)
self.echo(formatted) self.echo(formatted)

View file

@ -33,6 +33,7 @@ import sys
import time import time
import datetime import datetime
import json import json
from collections import namedtuple
from gzip import GzipFile from gzip import GzipFile
from io import BytesIO from io import BytesIO
from gi.repository import GLib from gi.repository import GLib
@ -671,13 +672,22 @@ class Logger:
return messages return messages
SearchResult = namedtuple('SearchResult',
['contact_name', 'time', 'kind', 'show', 'message', 'subject',
'log_line_id'])
def get_search_results_for_query(self, jid, query, account, year=False, def get_search_results_for_query(self, jid, query, account, year=False,
month=False, day=False): month=False, day=False):
""" """
Returns contact_name, time, kind, show, message, subject, log_line_id Search the conversation log for messages containing the `query` string.
For each row in a list of tuples, returns list with empty tuple if we The search can either span the complete log for the given `account` and
found nothing to meet our demands `jid` or be restriced to a single day by specifying `year`, `month` and
`day`, where `month` and `day` are 1-based.
All messages matching the specified criteria will be returned in a list
containing tuples of type `Logger.SearchResult`. If no messages match
the criteria, an empty list will be returned.
""" """
try: try:
self.get_jid_id(jid) self.get_jid_id(jid)
@ -705,8 +715,7 @@ class Logger:
ORDER BY time ORDER BY time
''' % (where_sql, like_sql), jid_tuple) ''' % (where_sql, like_sql), jid_tuple)
results = self.cur.fetchall() return [Logger.SearchResult(*row) for row in self.cur.fetchall()]
return results
def get_days_with_logs(self, jid, year, month, max_day, account): def get_days_with_logs(self, jid, year, month, max_day, account):
""" """

View file

@ -549,25 +549,22 @@ class HistoryWindow:
# add "subject: | message: " in message column if kind is single # add "subject: | message: " in message column if kind is single
# also do we need show at all? (we do not search on subject) # also do we need show at all? (we do not search on subject)
for row in results: for row in results:
if not show_status and row[2] in (KindConstant.GCSTATUS, if not show_status and row.kind in (KindConstant.GCSTATUS,
KindConstant.STATUS): KindConstant.STATUS):
continue continue
contact_name = row[0]
contact_name = row.contact_name
if not contact_name: if not contact_name:
kind = row[2] if row.kind == KindConstant.CHAT_MSG_SENT: # it's us! :)
if kind == KindConstant.CHAT_MSG_SENT: # it's us! :)
contact_name = gajim.nicks[account] contact_name = gajim.nicks[account]
else: else:
contact_name = self.completion_dict[jid][InfoColumn.NAME] contact_name = self.completion_dict[jid][InfoColumn.NAME]
tim = row[1]
message = row[4] local_time = time.localtime(row.time)
log_line_id = row[6]
local_time = time.localtime(tim)
date = time.strftime('%Y-%m-%d', local_time) date = time.strftime('%Y-%m-%d', local_time)
# jid (to which log is assigned to), name, date, message, model.append((jid, contact_name, date, row.message,
# time (full unix time) str(row.time), row.log_line_id))
model.append((jid, contact_name, date, message, str(tim), log_line_id))
def on_results_treeview_row_activated(self, widget, path, column): def on_results_treeview_row_activated(self, widget, path, column):
""" """