You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ponymail.apache.org by se...@apache.org on 2020/08/16 12:46:55 UTC

[incubator-ponymail-foal] branch master updated: Simplify -- move format flowed code own method

This is an automated email from the ASF dual-hosted git repository.

sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-ponymail-foal.git


The following commit(s) were added to refs/heads/master by this push:
     new a679ae0  Simplify -- move format flowed code own method
a679ae0 is described below

commit a679ae0f907b44942172f0014f915826a50b5a77
Author: Sebb <se...@apache.org>
AuthorDate: Sun Aug 16 13:46:41 2020 +0100

    Simplify -- move format flowed code own method
    
    N.B. This does not affect the unit tests
---
 tools/archiver.py | 48 ++++++++++++++++++++++++++----------------------
 1 file changed, 26 insertions(+), 22 deletions(-)

diff --git a/tools/archiver.py b/tools/archiver.py
index 7d477b0..19702c8 100755
--- a/tools/archiver.py
+++ b/tools/archiver.py
@@ -328,6 +328,31 @@ class Archiver(object):  # N.B. Also used by import-mbox.py
 
         return body
 
+    def format_flowed(self, body, msg_metadata):
+        try:
+            if (
+                msg_metadata.get("content-type")
+                and msg_metadata.get("content-type", "").find("format=flowed") != -1
+            ):
+                body = formatflowed.convertToWrapped(
+                    bytes(body, "utf-8"), character_set="utf-8"
+                )
+            if isinstance(body, str):
+                body = body.encode("utf-8")
+        except UnicodeEncodeError:
+            try:
+                body = body.decode(chardet.detect(body)["encoding"])
+            except UnicodeDecodeError:
+                try:
+                    body = body.decode("latin-1")
+                except UnicodeDecodeError:
+                    try:
+                        if isinstance(body, str):
+                            body = body.encode("utf-8")
+                    except UnicodeEncodeError:
+                        body = None
+        return body
+
     # N.B. this is also called by import-mbox.py
     def compute_updates(
         self,
@@ -406,28 +431,7 @@ class Archiver(object):  # N.B. Also used by import-mbox.py
         # message_date calculations are all done, prepare the index entry
         date_as_string = time.strftime("%Y/%m/%d %H:%M:%S", time.gmtime(epoch))
         body = self.message_body(msg, verbose=args.verbose, ignore_body=args.ibody)
-        try:
-            if (
-                msg_metadata.get("content-type")
-                and msg_metadata.get("content-type", "").find("format=flowed") != -1
-            ):
-                body = formatflowed.convertToWrapped(
-                    bytes(body, "utf-8"), character_set="utf-8"
-                )
-            if isinstance(body, str):
-                body = body.encode("utf-8")
-        except UnicodeEncodeError:
-            try:
-                body = body.decode(chardet.detect(body)["encoding"])
-            except UnicodeDecodeError:
-                try:
-                    body = body.decode("latin-1")
-                except UnicodeDecodeError:
-                    try:
-                        if isinstance(body, str):
-                            body = body.encode("utf-8")
-                    except UnicodeEncodeError:
-                        body = None
+        body = self.format_flowed(body, msg_metadata)
 
         attachments, contents = message_attachments(msg)
         irt = ""