[mhonarc] more consistent variable names



commit 945333e25e03708f79e60e8af99906c28af3a6c2
Author: Olav Vitters <olav vitters nl>
Date:   Tue Feb 12 22:16:46 2013 +0100

    more consistent variable names

 archive.py |   71 +++++++++++++++++++++++++++++++++---------------------------
 1 files changed, 39 insertions(+), 32 deletions(-)
---
diff --git a/archive.py b/archive.py
index 597b992..f55842a 100755
--- a/archive.py
+++ b/archive.py
@@ -10,6 +10,7 @@ import dateutil.parser
 import re
 import datetime
 import email.utils
+import tempfile
 try:
         from cStringIO import StringIO
 except ImportError:
@@ -48,17 +49,13 @@ class Archiver:
         self.start_time = None
         self.end_time = None
 
-        self.tmpfile = None
-
         self.start_time = None
         self.end_time = None
 
-    def output(self, archivepath):
+    def output(self, newmsgs, archivepath):
         if archivepath is None:
             return False
 
-        tmpname = self.tmpname
-
         path = os.path.join(self.PRIVATE_ARCHIVE_DIR, self.listname, archivepath)
 
         if not os.path.exists(path):
@@ -66,20 +63,25 @@ class Archiver:
 
         rcfile = self.PRIVATE_RCFILE if self.private else self.PUBLIC_RCFILE
 
-        # Call mhonarc for all messages in 'tmpfile'
+        # Call mhonarc for all messages in 'newmsgs'
         with open(self.ERRORLOG, "a") as error_fd:
-            cmd = ['mhonarc', '-umask', '022', '-rcfile', rcfile, '-add', '-output', path, tmpname,
+            cmd = ['mhonarc', '-umask', '022', '-rcfile', rcfile, '-add', '-output', path, newmsgs.name,
                               '-definevar', 'ARCHDATE=%s LISTNAME=%s' % (archivepath, self.listname)]
 
             #subprocess.call(cmd, stdout=error_fd, stderr=subprocess.STDOUT)
 
         # Append message(s) to mbox (possibly gzipped)
         if os.path.exists("%s.txt" % path):
-            fp = open("%s.txt" % path, 'ab')
+            archivedmsgs = open("%s.txt" % path, 'ab')
         else:
-            fp = gzip.GzipFile("%s.txt.gz" % path, 'ab')
-        fp.write(open(tmpname, 'rb').read())
-        fp.close()
+            archivedmsgs = gzip.GzipFile("%s.txt.gz" % path, 'ab')
+
+        newmsgs.seek(0)
+        shutil.copyfileobj(newmsgs, archivedmsgs)
+        archivedmsgs.close()
+
+        newmsgs.seek(0)
+        newmsgs.truncate(0)
 
     def determine_received_time(self, fd):
         """Determines the received time of a message"""
@@ -121,14 +123,18 @@ class Archiver:
         # Archive emails per month
         return received_time.strftime("%Y-%B")
 
-    def handle_message(self, fd, archivepath):
+    def handle_message(self, fd, newmsgs, archivepath):
         path = self.determine_received_time(fd)
 
         if path == False:
             return archivepath
 
         if archivepath is not None and path is not None and archivepath != path:
-            self.output(archivepath)
+            self.output(newmsgs, archivepath)
+
+        # Add message to list of messages
+        fd.seek(0)
+        shutil.copyfileobj(fd, newmsgs)
 
         return path
 
@@ -143,33 +149,34 @@ class Archiver:
         re_from = re.compile(_fromlinepattern)
 
         blank = True
-        msg = StringIO()
         archivepath = None
-        while 1:
-            line = fd.readline()
-            if line == "":
-                break
+        msg = StringIO()
+        with tempfile.NamedTemporaryFile() as newmsgs:
+            while 1:
+                line = fd.readline()
+                if line == "":
+                    break
 
-            if line == "\n":
-                msg.write(line)
-                blank = True
-                continue
+                if line == "\n":
+                    msg.write(line)
+                    blank = True
+                    continue
 
-            if blank and re_from.match(line):
-                sys.stdout.write(line)
+                if blank and re_from.match(line):
+                    sys.stdout.write(line)
 
-                if msg.tell():
-                    msg.seek(0)
+                    if msg.tell():
+                        msg.seek(0)
 
-                    archivepath = self.handle_message(msg, archivepath)
+                        archivepath = self.handle_message(msg, newmsgs, archivepath)
 
-                    msg.seek(0)
-                    msg.truncate(0)
+                        msg.seek(0)
+                        msg.truncate(0)
 
-            msg.write(line)
+                msg.write(line)
 
-        if msg.tell():
-            archivepath = self.handle_message(msg, archivepath)
+            if msg.tell():
+                archivepath = self.handle_message(msg, newmsgs, archivepath)
 
         if archivepath:
             Archiver.make_index(self.listname, self.private)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]