[Patchew-devel] [PATCH 3/3] Text to json converter for mbox

Shubham Jain shubhamjain7495 at gmail.com
Tue May 8 11:50:51 UTC 2018


- Mbox would now return a dictionary object which would help REST API deserialize easily into model object.
- Added test for text to json converter.

For recepients and sender couldn't use AddressSerializer as api.rest calls models which call back mbox. Possible soulution to replace to_representation in AddressSerializer with add_tuple_to_dic to remove code redundancy?
---
 mbox.py            |  18 ++++++++++
 tests/test_mbox.py | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 120 insertions(+)

diff --git a/mbox.py b/mbox.py
index fe108f3..1296078 100644
--- a/mbox.py
+++ b/mbox.py
@@ -13,6 +13,7 @@ import email.utils
 import email.header
 import datetime
 import re
+from rest_framework.fields import DateTimeField
 
 def _parse_header(header):
     r = ''
@@ -34,6 +35,11 @@ def _addr_fmt_text(name, addr):
     else:
         return addr
 
+def addr_tuple_to_dic(obj):
+        if obj[0] != obj[1]:
+            return {"name": obj[0], "address": obj[1]}
+        else:
+            return {"address": obj[1]}
 
 class MboxMessage(object):
     """ Helper class to process mbox """
@@ -269,3 +275,15 @@ class MboxMessage(object):
         if c == 0:
             return True
         return False
+
+    def get_json(self):
+        """Return the JSON format of the mbox """
+        msg = {}
+        msg['message_id'] = self.get_message_id()
+        msg['in_reply_to'] = self.get_in_reply_to() or ""
+        msg['date'] = DateTimeField().to_representation(self.get_date())
+        msg['subject'] = self.get_subject()
+        msg['sender'] = addr_tuple_to_dic(self.get_from())
+        msg['recipients'] = [addr_tuple_to_dic(x) for x in (self.get_to() + self.get_cc())]
+        msg['mbox'] = self.get_mbox()
+        return msg
\ No newline at end of file
diff --git a/tests/test_mbox.py b/tests/test_mbox.py
index 8493df7..7a21d4a 100755
--- a/tests/test_mbox.py
+++ b/tests/test_mbox.py
@@ -52,5 +52,107 @@ Virtualization:  qemu.org | libvirt.org
             msg = mbox.MboxMessage(f.read())
         self.assertTrue(msg.is_patch())
 
+    def test_get_json(self):
+        expected = {'message_id': '20160628014747.20971-1-famz at redhat.com',
+                    'in_reply_to': '',
+                    'date': '2016-06-28T01:47:47',
+                    'subject': '[Qemu-devel] [PATCH] quorum: Only compile when supported',
+                    'sender': {'name': 'Fam Zheng', 'address': 'famz at redhat.com'},
+                    'recipients': [{'address': 'qemu-devel at nongnu.org'},
+                                   {'name': 'Kevin Wolf', 'address': 'kwolf at redhat.com'},
+                                   {'name': 'Alberto Garcia', 'address': 'berto at igalia.com'},
+                                   {'address': 'qemu-block at nongnu.org'},
+                                   {'name': 'Max Reitz', 'address': 'mreitz at redhat.com'}],
+                    'mbox': 'Delivered-To: importer at patchew.org\nReceived-SPF: '
+                            'Pass (zoho.com: domain of qemu-devel-bounces at nongnu.org '
+                            'designates 208.118.235.17 as permitted sender )  client-ip: '
+                            '208.118.235.17\nReceived-SPF: pass (zoho.com: domain of '
+                            'gnu.org designates 208.118.235.17 as permitted sender) '
+                            'client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+'
+                            'importer=patchew.org at nongnu.org; helo=lists.gnu.org;\n'
+                            'Return-Path: <qemu-devel-bounces+importer=patchew.org at nongnu.org>'
+                            '\nReceived: from lists.gnu.org (lists.gnu.org [208.118.235.17]) '
+                            'by mx.zohomail.com\n\twith SMTPS id 1467078971424862.8927889595075;'
+                            ' Mon, 27 Jun 2016 18:56:11 -0700 (PDT)\nReceived: from localhost '
+                            '([::1]:33689 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp '
+                            '(Exim 4.71)\n\t(envelope-from <qemu-devel-bounces+importer='
+                            'patchew.org at nongnu.org>)\n\tid 1bHi94-0006LP-Ok\n\tfor '
+                            'importer at patchew.org; Mon, 27 Jun 2016 21:48:58 -0400\nReceived: '
+                            'from eggs.gnu.org ([2001:4830:134:3::10]:53270)\n\tby lists.gnu.org'
+                            ' with esmtp (Exim 4.71)\n\t(envelope-from <famz at redhat.com>) id '
+                            '1bHi8E-0002Lm-KR\n\tfor qemu-devel at nongnu.org; Mon, 27 Jun 2016 '
+                            '21:48:07 -0400\nReceived: from Debian-exim by eggs.gnu.org with '
+                            'spam-scanned (Exim 4.71)\n\t(envelope-from <famz at redhat.com>) '
+                            'id 1bHi8D-0008T4-N7\n\tfor qemu-devel at nongnu.org; Mon, 27 Jun '
+                            '2016 21:48:06 -0400\nReceived: from mx1.redhat.com '
+                            '([209.132.183.28]:47972)\n\tby eggs.gnu.org with esmtp '
+                            '(Exim 4.71)\n\t(envelope-from <famz at redhat.com>)\n\tid '
+                            '1bHi86-0008SN-IZ; Mon, 27 Jun 2016 21:47:58 -0400\nReceived: '
+                            'from int-mx10.intmail.prod.int.phx2.redhat.com\n\t'
+                            '(int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23])\n\t'
+                            '(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 '
+                            '(256/256 bits))\n\t(No client certificate requested)\n\tby '
+                            'mx1.redhat.com (Postfix) with ESMTPS id BDB007F088;\n\tTue, '
+                            '28 Jun 2016 01:47:57 +0000 (UTC)\nReceived: '
+                            'from ad.usersys.redhat.com (dhcp-15-133.nay.redhat.com\n\t'
+                            '[10.66.15.133])\n\tby int-mx10.intmail.prod.int.phx2.redhat.com'
+                            ' (8.14.4/8.14.4) with ESMTP\n\tid u5S1lssT024908; Mon, 27 Jun '
+                            '2016 21:47:55 -0400\nFrom: Fam Zheng <famz at redhat.com>\nTo: '
+                            'qemu-devel at nongnu.org\nDate: Tue, 28 Jun 2016 09:47:47 '
+                            '+0800\nMessage-Id: <20160628014747.20971-1-famz at redhat.com>\n'
+                            'X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23\nX-Greylist: '
+                            'Sender IP whitelisted, not delayed by milter-greylist-4.5.16\n\t'
+                            '(mx1.redhat.com [10.5.110.26]);\n\tTue, 28 Jun 2016 01:47:57 '
+                            '+0000 (UTC)\nX-detected-operating-system: by eggs.gnu.org: '
+                            'GNU/Linux 2.2.x-3.x [generic]\nX-Received-From: 209.132.183.28\n'
+                            'Subject: [Qemu-devel] [PATCH] quorum: Only compile when supported\n'
+                            'X-BeenThere: qemu-devel at nongnu.org\nX-Mailman-Version: 2.1.21\n'
+                            'Precedence: list\nList-Id: <qemu-devel.nongnu.org>\nList-Unsubscribe:'
+                            ' <https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t'
+                            '<mailto:qemu-devel-request at nongnu.org?subject=unsubscribe>\n'
+                            'List-Archive: <http://lists.nongnu.org/archive/html/qemu-devel/>\n'
+                            'List-Post: <mailto:qemu-devel at nongnu.org>\nList-Help: '
+                            '<mailto:qemu-devel-request at nongnu.org?subject=help>\nList-Subscribe:'
+                            ' <https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t'
+                            '<mailto:qemu-devel-request at nongnu.org?subject=subscribe>\nCc:'
+                            ' Kevin Wolf <kwolf at redhat.com>, Alberto Garcia <berto at igalia.com>,'
+                            '\n\tqemu-block at nongnu.org, Max Reitz <mreitz at redhat.com>\nErrors-To:'
+                            ' qemu-devel-bounces+importer=patchew.org at nongnu.org\nSender: '
+                            '"Qemu-devel" <qemu-devel-bounces+importer=patchew.org at nongnu.org>\n'
+                            'X-ZohoMail-Owner: <20160628014747.20971-1-famz at redhat.com>+zmo_0_'
+                            '<qemu-devel-bounces+importer=patchew.org at nongnu.org>\n'
+                            'X-ZohoMail-Sender: 209.132.183.28\nX-ZohoMail: RSF_0  Z_629925259 '
+                            'SPT_1 Z_629926901 SPT_1  SS_1 SFPD SFPP UW2468 UB2468 ZFF-EB_1'
+                            ' COSF  ODL   SGR3_1_2_0_27046_53\nX-Zoho-Virus-Status: 2\n\n'
+                            'This was the only exceptional module init function that does '
+                            'something\nelse than a simple list of bdrv_register() calls, '
+                            'in all the block\ndrivers.\n\nThe qcrypto_hash_supports is actually'
+                            ' a static check, determined at\ncompile time.  Follow the '
+                            'block-job-$(CONFIG_FOO) convention for\nconsistency.\n\n'
+                            'Signed-off-by: Fam Zheng <famz at redhat.com>\n---\n block/'
+                            'Makefile.objs | 2 +-\n block/quorum.c      | 4 ----\n 2 files'
+                            ' changed, 1 insertion(+), 5 deletions(-)\n\ndiff --git a/block/'
+                            'Makefile.objs b/block/Makefile.objs\nindex 44a5416..c87d605 '
+                            '100644\n--- a/block/Makefile.objs\n+++ b/block/Makefile.objs\n@@'
+                            ' -3,7 +3,7 @@ block-obj-y += qcow2.o qcow2-refcount.o qcow2-cluster.o'
+                            ' qcow2-snapshot.o qcow2-c\n block-obj-y += qed.o qed-gencb.o '
+                            'qed-l2-cache.o qed-table.o qed-cluster.o\n block-obj-y += '
+                            'qed-check.o\n block-obj-$(CONFIG_VHDX) += vhdx.o vhdx-endian.o'
+                            ' vhdx-log.o\n-block-obj-y += quorum.o\n+block-obj-$(CONFIG_GNUTLS_HASH)'
+                            ' += quorum.o\n block-obj-y += parallels.o blkdebug.o blkverify.o '
+                            'blkreplay.o\n block-obj-y += block-backend.o snapshot.o qapi.o\n '
+                            'block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o\ndiff --git '
+                            'a/block/quorum.c b/block/quorum.c\nindex 331b726..18fbed8 100644\n'
+                            '--- a/block/quorum.c\n+++ b/block/quorum.c\n@@ -1113,10 +1113,6 @@'
+                            ' static BlockDriver bdrv_quorum = {\n \n static void bdrv_quorum_init'
+                            '(void)\n {\n-    if (!qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA256))'
+                            ' {\n-        /* SHA256 hash support is required for quorum device */\n-'
+                            '        return;\n-    }\n     bdrv_register(&bdrv_quorum);\n }\n \n-- \n'
+                            '2.9.0\n\n\n'}
+        dp = self.get_data_path("0001-simple-patch.mbox.gz")
+        with open(dp, "r") as f:
+            msg = mbox.MboxMessage(f.read()).get_json()
+        self.assertEqual(msg, expected)
+
 if __name__ == '__main__':
     main()
-- 
2.14.3 (Apple Git-98)




More information about the Patchew-devel mailing list