Skip to content

Commit

Permalink
lazy loading of message body
Browse files Browse the repository at this point in the history
Currently `Message.mime_part` is set in `Message.__init__`. As a result,
when `Thread.get_authors` calls `Thread.get_messages`, the  message
contents are read from disk. Here, I defined `Message.get_mime_part` and
`Message.set_mime_part`, and the mime part is no longer set during init.
As a result, the mime part is only loaded on the first call of
`Message.get_mime_part`. As `Message.get_author` doesn't need the mime
part, this fixes the slowness issue.
  • Loading branch information
paretje authored and pazz committed Jun 23, 2020
1 parent 4ce506b commit 424669b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion alot/buffers/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def get_info(self):
info['thread_tags'] = self.translated_tags_str()
info['intersection_tags'] = self.translated_tags_str(intersection=True)
info['mimetype'] = (
self.get_selected_message().mime_part.get_content_type())
self.get_selected_message().get_mime_part().get_content_type())
return info

def get_selected_thread(self):
Expand Down
2 changes: 1 addition & 1 deletion alot/commands/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ def matches(msgt):
if self.mimepart == 'toggle':
message = mt.get_message()
mimetype = {'plain': 'html', 'html': 'plain'}[
message.mime_part.get_content_subtype()]
message.get_mime_part().get_content_subtype()]
mimepart = get_body_part(message.get_email(), mimetype)
elif self.mimepart is True:
mimepart = ui.get_deep_focus().mimepart
Expand Down
13 changes: 10 additions & 3 deletions alot/db/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def __init__(self, dbman, msg, thread=None):
self._filename = msg.get_filename()
self._email = None # will be read upon first use
self._attachments = None # will be read upon first use
self._mime_part = None # will be read upon first use
self._mime_tree = None # will be read upon first use
self._tags = set(msg.get_tags())

Expand All @@ -68,8 +69,6 @@ def __init__(self, dbman, msg, thread=None):
else:
self._from = '"Unknown" <>'

self.mime_part = get_body_part(self.get_email())

def __str__(self):
"""prettyprint the message"""
aname, aaddress = self.get_author()
Expand Down Expand Up @@ -275,9 +274,17 @@ def _is_attachment(part, ct_override=None):

return False

def get_mime_part(self):
if not self._mime_part:
self._mime_part = get_body_part(self.get_email())
return self._mime_part

def set_mime_part(self, mime_part):
self._mime_part = mime_part

def get_body_text(self):
""" returns bodystring extracted from this mail """
return extract_body_part(self.mime_part)
return extract_body_part(self.get_mime_part())

def matches(self, querystring):
"""tests if this messages is in the resultset for `querystring`"""
Expand Down
2 changes: 1 addition & 1 deletion alot/widgets/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def _text_tree_to_widget_tree(self, tree):

def set_mimepart(self, mimepart):
""" Set message widget mime part and invalidate body tree."""
self.get_message().mime_part = mimepart
self.get_message().set_mime_part(mimepart)
self._bodytree = None


Expand Down

0 comments on commit 424669b

Please sign in to comment.