incubator-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From br...@apache.org
Subject svn commit: r1437938 - /incubator/public/trunk/incuvoter/mboxparser.py
Date Thu, 24 Jan 2013 10:41:30 GMT
Author: brane
Date: Thu Jan 24 10:41:29 2013
New Revision: 1437938

URL: http://svn.apache.org/viewvc?rev=1437938&view=rev
Log:
Added mboxparser: replacement for feedparser used by Incuvoter.

Added:
    incubator/public/trunk/incuvoter/mboxparser.py

Added: incubator/public/trunk/incuvoter/mboxparser.py
URL: http://svn.apache.org/viewvc/incubator/public/trunk/incuvoter/mboxparser.py?rev=1437938&view=auto
==============================================================================
--- incubator/public/trunk/incuvoter/mboxparser.py (added)
+++ incubator/public/trunk/incuvoter/mboxparser.py Thu Jan 24 10:41:29 2013
@@ -0,0 +1,73 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+'''
+Purpose: Parse an mbox file into a format readable by Incuvoter's feed parser.
+
+Status: Pre-Alpha, under construction.
+'''
+
+from __future__ import absolute_import
+
+import os
+import datetime
+import email
+import email.utils
+
+
+class MBoxFeed(object):
+    def __init__(self, updated):
+        self.updated_parsed = updated
+        self.updated = updated.isoformat() + 'Z'
+
+    def get(self, name, default=None):
+        if hasattr(self, name):
+            return getattr(self, name)
+        return default
+
+
+def append_message(text, entries):
+    if not text:
+        return
+
+    message = email.message_from_string(text)
+    date = message['Date']
+    subject = message['Subject']
+    if not subject or not date:
+        return
+
+    timestamp = email.utils.mktime_tz(email.utils.parsedate_tz(date))
+    emsg = MBoxFeed(datetime.datetime.utcfromtimestamp(timestamp))
+    emsg.title = email.utils.collapse_rfc2231_value(email.utils.decode_rfc2231(subject))
+    entries.append(emsg)
+
+def parse_mbox(path):
+    if not os.path.isfile(path):
+        return None
+
+    with open(path, 'rt') as mbox:
+        timestamp = os.fstat(mbox.fileno()).st_mtime
+        feed = MBoxFeed(datetime.datetime.utcfromtimestamp(timestamp))
+        entries = []
+        text = None
+        for line in mbox:
+            if line.startswith('From '):
+                append_message(text, entries)
+                text = ''
+            text += line
+        append_message(text, entries)
+
+    feed.entries = entries
+    return feed



---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@incubator.apache.org
For additional commands, e-mail: cvs-help@incubator.apache.org


Mime
View raw message