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:
parent
167cf02afd
commit
d612e80a47
|
@ -100,23 +100,22 @@ class StandardCommonCommands(CommandContainer):
|
|||
raise CommandError(_("Limit must be an integer"))
|
||||
|
||||
for row in results:
|
||||
contact, time, kind, show, message, subject, log_line_id = row
|
||||
|
||||
contact = row.contact_name
|
||||
if not contact:
|
||||
if kind == KindConstant.CHAT_MSG_SENT:
|
||||
if row.kind == KindConstant.CHAT_MSG_SENT:
|
||||
contact = gajim.nicks[self.account]
|
||||
else:
|
||||
contact = self.contact.name
|
||||
|
||||
time_obj = localtime(time)
|
||||
date_obj = date.fromtimestamp(time)
|
||||
time_obj = localtime(row.time)
|
||||
date_obj = date.fromtimestamp(row.time)
|
||||
date_ = strftime('%Y-%m-%d', time_obj)
|
||||
time_ = strftime('%H:%M:%S', time_obj)
|
||||
|
||||
if date_obj == date.today():
|
||||
formatted = "[%s] %s: %s" % (time_, contact, message)
|
||||
formatted = "[%s] %s: %s" % (time_, contact, row.message)
|
||||
else:
|
||||
formatted = "[%s, %s] %s: %s" % (date_, time_, contact, message)
|
||||
formatted = "[%s, %s] %s: %s" % (date_, time_, contact, row.message)
|
||||
|
||||
self.echo(formatted)
|
||||
|
||||
|
|
|
@ -33,6 +33,7 @@ import sys
|
|||
import time
|
||||
import datetime
|
||||
import json
|
||||
from collections import namedtuple
|
||||
from gzip import GzipFile
|
||||
from io import BytesIO
|
||||
from gi.repository import GLib
|
||||
|
@ -671,13 +672,22 @@ class Logger:
|
|||
|
||||
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,
|
||||
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
|
||||
found nothing to meet our demands
|
||||
The search can either span the complete log for the given `account` and
|
||||
`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:
|
||||
self.get_jid_id(jid)
|
||||
|
@ -705,8 +715,7 @@ class Logger:
|
|||
ORDER BY time
|
||||
''' % (where_sql, like_sql), jid_tuple)
|
||||
|
||||
results = self.cur.fetchall()
|
||||
return results
|
||||
return [Logger.SearchResult(*row) for row in self.cur.fetchall()]
|
||||
|
||||
def get_days_with_logs(self, jid, year, month, max_day, account):
|
||||
"""
|
||||
|
|
|
@ -549,25 +549,22 @@ class HistoryWindow:
|
|||
# add "subject: | message: " in message column if kind is single
|
||||
# also do we need show at all? (we do not search on subject)
|
||||
for row in results:
|
||||
if not show_status and row[2] in (KindConstant.GCSTATUS,
|
||||
KindConstant.STATUS):
|
||||
if not show_status and row.kind in (KindConstant.GCSTATUS,
|
||||
KindConstant.STATUS):
|
||||
continue
|
||||
contact_name = row[0]
|
||||
|
||||
contact_name = row.contact_name
|
||||
if not contact_name:
|
||||
kind = row[2]
|
||||
if kind == KindConstant.CHAT_MSG_SENT: # it's us! :)
|
||||
if row.kind == KindConstant.CHAT_MSG_SENT: # it's us! :)
|
||||
contact_name = gajim.nicks[account]
|
||||
else:
|
||||
contact_name = self.completion_dict[jid][InfoColumn.NAME]
|
||||
tim = row[1]
|
||||
message = row[4]
|
||||
log_line_id = row[6]
|
||||
local_time = time.localtime(tim)
|
||||
|
||||
local_time = time.localtime(row.time)
|
||||
date = time.strftime('%Y-%m-%d', local_time)
|
||||
|
||||
# jid (to which log is assigned to), name, date, message,
|
||||
# time (full unix time)
|
||||
model.append((jid, contact_name, date, message, str(tim), log_line_id))
|
||||
model.append((jid, contact_name, date, row.message,
|
||||
str(row.time), row.log_line_id))
|
||||
|
||||
def on_results_treeview_row_activated(self, widget, path, column):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue