diff options
author | Anton Khirnov <anton@khirnov.net> | 2021-05-13 11:59:08 +0200 |
---|---|---|
committer | Anton Khirnov <anton@khirnov.net> | 2021-05-13 11:59:08 +0200 |
commit | ef6f051d36e32720f583699bc40e073f706db0ed (patch) | |
tree | d5ee235a0508f32025ab77623966a76737c1842b /alot | |
parent | 6af5671b7dab355fec3c3c8c6f43c7ea6d18f798 (diff) |
db/message: turn _email and body into cached properties
Does not really change much for now, since the email is still loaded in
the constructor, but will allow optimization later.
Diffstat (limited to 'alot')
-rw-r--r-- | alot/db/message.py | 34 |
1 files changed, 19 insertions, 15 deletions
diff --git a/alot/db/message.py b/alot/db/message.py index 401230ba..f2a298d7 100644 --- a/alot/db/message.py +++ b/alot/db/message.py @@ -6,6 +6,7 @@ import codecs import email import email.charset as charset import email.policy +from functools import cached_property import logging import os import subprocess @@ -383,15 +384,7 @@ class Message: """ parent = None - """ - The object providing access to the email's headers. - """ - headers = None - - """ - A MimeTree object containing the body of the message. - """ - body = None + _session_keys = None def __init__(self, dbman, thread, msg, depth): """ @@ -421,11 +414,7 @@ class Message: session_keys = [] for name, value in msg.properties.getall('session-key', exact = True): session_keys.append(value) - - self._email = self._load_email(session_keys) - - self.body = _MimeTree(self._email, session_keys) - self.headers = self.body.headers + self._session_keys = session_keys self._tags = set(msg.tags) @@ -457,11 +446,26 @@ class Message: return self.id == other.id return NotImplemented + @cached_property + def body(self): + """ + A MimeTree object containing the body of the message. + """ + return _MimeTree(self._email, self._session_keys) + + @property + def headers(self): + """ + The object providing access to the email's headers. + """ + return self.body.headers + @property def filename(self): return self.filenames[0] - def _load_email(self, session_keys): + @cached_property + def _email(self): try: with open(self.filename, 'rb') as f: mail = email.message_from_bytes(f.read(), policy = email.policy.SMTP) |