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:
parent
08f4e17158
commit
65e926c181
2 changed files with 26 additions and 20 deletions
|
@ -636,12 +636,22 @@ class Logger:
|
||||||
start_of_day = int(time.mktime(local_time))
|
start_of_day = int(time.mktime(local_time))
|
||||||
return start_of_day
|
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):
|
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
|
The conversation contains all messages that were exchanged between
|
||||||
found nothing to meet our demands
|
`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:
|
try:
|
||||||
self.get_jid_id(jid)
|
self.get_jid_id(jid)
|
||||||
|
@ -663,14 +673,11 @@ class Logger:
|
||||||
ORDER BY time
|
ORDER BY time
|
||||||
''' % (where_sql, start_of_day, last_second_of_day), jid_tuple)
|
''' % (where_sql, start_of_day, last_second_of_day), jid_tuple)
|
||||||
|
|
||||||
results = self.cur.fetchall()
|
results = [self.Message(*row) for row in self.cur.fetchall()]
|
||||||
messages = []
|
for message in results:
|
||||||
for entry in results:
|
message._replace(additional_data=json.loads(message.additional_data))
|
||||||
additional_data = json.loads(entry[6])
|
|
||||||
parsed_entry = entry[:6] + (additional_data, ) + entry[7:]
|
|
||||||
messages.append(parsed_entry)
|
|
||||||
|
|
||||||
return messages
|
return results
|
||||||
|
|
||||||
SearchResult = namedtuple('SearchResult',
|
SearchResult = namedtuple('SearchResult',
|
||||||
['contact_name', 'time', 'kind', 'show', 'message', 'subject',
|
['contact_name', 'time', 'kind', 'show', 'message', 'subject',
|
||||||
|
|
|
@ -391,20 +391,19 @@ class HistoryWindow:
|
||||||
"""
|
"""
|
||||||
Add all the lines for given date in textbuffer
|
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
|
self.last_time_printout = 0
|
||||||
show_status = self.show_status_checkbutton.get_active()
|
show_status = self.show_status_checkbutton.get_active()
|
||||||
|
|
||||||
lines = gajim.logger.get_conversation_for_date(self.jid, year, month, day, self.account)
|
conversation = gajim.logger.get_conversation_for_date(
|
||||||
for line in lines:
|
self.jid, year, month, day, self.account)
|
||||||
# line[0] is contact_name, line[1] is time of message
|
for message in conversation:
|
||||||
# line[2] is kind, line[3] is show, line[4] is message, line[5] is subject
|
if not show_status and message.kind in (KindConstant.GCSTATUS,
|
||||||
# line[6] is additional_data, line[7] is log_line_id
|
|
||||||
if not show_status and line[2] in (KindConstant.GCSTATUS,
|
|
||||||
KindConstant.STATUS):
|
KindConstant.STATUS):
|
||||||
continue
|
continue
|
||||||
self._add_new_line(line[0], line[1], line[2], line[3], line[4],
|
self._add_new_line(message.contact_name, message.time, message.kind,
|
||||||
line[5], line[6], line[7])
|
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,
|
def _add_new_line(self, contact_name, tim, kind, show, message, subject,
|
||||||
additional_data, log_line_id):
|
additional_data, log_line_id):
|
||||||
|
|
Loading…
Add table
Reference in a new issue