[Patchew-devel] [PATCH v2 3/4] mbox: extract decode_payload and simplify get_body
Paolo Bonzini
pbonzini at redhat.com
Fri Sep 28 14:58:44 UTC 2018
We will have to extract the body of the message and process it, so we need to
do the same processing as get_body(). Extract it into a separate function.
Signed-off-by: Paolo Bonzini <pbonzini at redhat.com>
---
mbox.py | 24 +++++++++++++-----------
1 file changed, 13 insertions(+), 11 deletions(-)
diff --git a/mbox.py b/mbox.py
index 35b2270..47a770f 100644
--- a/mbox.py
+++ b/mbox.py
@@ -41,6 +41,18 @@ def addr_db_to_rest(obj):
else:
return {"address": obj[1]}
+def decode_payload(m):
+ payload = m.get_payload(decode=True)
+ charset = m.get_content_charset()
+ try:
+ return payload.decode(charset or 'utf-8', errors='replace')
+ except:
+ if charset != 'utf-8':
+ # Still fall back from non-utf-8 to utf-8
+ return payload.decode('utf-8')
+ else:
+ raise
+
class MboxMessage(object):
""" Helper class to process mbox """
def __init__(self, m):
@@ -161,21 +173,11 @@ class MboxMessage(object):
return s.intersection(self.get_prefixes(upper=True))
def get_body(self):
- def decode_payload(payload, charset):
- try:
- return payload.decode(charset or 'utf-8', errors='replace')
- except:
- if charset != 'utf-8':
- # Still fall back from non-utf-8 to utf-8
- return payload.decode('utf-8')
- else:
- raise
def _get_message_text(m):
payload = m.get_payload(decode=not self._m.is_multipart())
body = ''
if m.get_content_type() == "text/plain":
- body = decode_payload(m.get_payload(decode=True),
- self._m.get_content_charset())
+ body = decode_payload(m)
elif isinstance(payload, list):
for p in payload:
body += _get_message_text(p)
--
2.17.1
More information about the Patchew-devel
mailing list