]> khamidou — GitHub mirrors - kite.git/commitdiff
in the middle of a refactor
authorkarim hamidou <khamidou@gmail.com>
Thu, 28 Nov 2013 18:57:14 +0000 (19:57 +0100)
committerkarim hamidou <khamidou@gmail.com>
Thu, 28 Nov 2013 18:57:14 +0000 (19:57 +0100)
src/back/kite/filterdaemon.py
src/back/mocks/test_process_email.txt [new file with mode: 0644]
src/back/tests/test_filterdaemon.py [new file with mode: 0644]

index 700dca1c87688f4c30fe7a70d1fa61de2717cbb4..7e037aba590998273fd035667e28e3cc248f4aac 100644 (file)
@@ -72,6 +72,22 @@ class DumperThread(threading.Thread):
             print "Dumping threads index"
             threads_index.save()
             
+def process_new_email(path):
+    global threads_index
+
+    with open(path, "r") as fd:
+        parser = email.parser.HeaderParser()
+        email_headers = parser.parse(fd)
+        subject = email_headers.get("Subject")
+
+        if subject != None:
+            subject = headers.cleanup_subject(subject)
+            if subject in threads_index.data:
+                threads_index.data[subject].append(path)
+            else:
+                # create a new thread
+                threads_index.data[subject] = [path]
+
 
 class ProcessorThread(threading.Thread):
     def __init__(self, path):
@@ -89,21 +105,9 @@ class ProcessorThread(threading.Thread):
                 event = events_queue.pop(0)
                 if event["type"] == "create":
                     try:
-                        print event["path"]
-                        with open(event["path"], "r") as fd:
-                            parser = email.parser.HeaderParser()
-                            email_headers = parser.parse(fd)
-                            subject = email_headers.get("Subject")
-                            print "Subject: %s" % subject
-
-                            if subject != None:
-                                subject = headers.cleanup_subject(subject)
-                                if subject in threads_index.data:
-                                    threads_index.data[subject].append(event["path"])
-                                else:
-                                    threads_index.data[subject] = [event["path"]]
+                        self.process_new_email(event["path"])
                     except IOError:
-                        # Postfix/Dovecot creates temporary files. Ignore them
+                        # This may be a Postfix/Dovecot temporary file. Ignore it.
                         pass
                     
             time.sleep(EVENTS_QUEUE_PROCESSING_DELAY)
diff --git a/src/back/mocks/test_process_email.txt b/src/back/mocks/test_process_email.txt
new file mode 100644 (file)
index 0000000..c488050
--- /dev/null
@@ -0,0 +1 @@
+Subject: Hey, How are you ?
diff --git a/src/back/tests/test_filterdaemon.py b/src/back/tests/test_filterdaemon.py
new file mode 100644 (file)
index 0000000..9ecfb5d
--- /dev/null
@@ -0,0 +1,10 @@
+import unittest
+import kite.filterdaemon as filterdaemon
+
+threads_index = {"data": {}}
+
+class TestEmailProcessor(unittest.TestCase):
+    def test_process_email(self):
+        global threads_index
+        filterdaemon.process_new_email("mocks/test_process_email.txt")
+        self.assertTrue("Hey, How are you ?" in threads_index["data"])