]> khamidou — GitHub mirrors - kite.git/commitdiff
thread file generation works
authorkarim hamidou <khamidou@gmail.com>
Sun, 24 Nov 2013 19:22:49 +0000 (20:22 +0100)
committerkarim hamidou <khamidou@gmail.com>
Sun, 24 Nov 2013 19:22:49 +0000 (20:22 +0100)
src/back/kite/filterdaemon.py

index 991366d20c440bc951c0387c22011708fbbd349a..700dca1c87688f4c30fe7a70d1fa61de2717cbb4 100644 (file)
@@ -1,6 +1,19 @@
 # filterdaemon.py 
-# This background process creates metadata files for threads and filters.
+# This background process creates indexes files for threads and filters.
 #
+# What is an index file ?
+# =======================
+#
+# An index file is a JSON list which holds the paths to files in a conversation.
+# The file name of an index file is of the form UUID4.json
+#
+# Why is it used ?
+# ================
+#
+# Because building a list of the emails in a thread at each server request would 
+# be madness.
+
+
 import sys
 import os
 import pyinotify
@@ -9,15 +22,24 @@ import time
 import email.parser
 
 import threads
+import headers
 from jsonfile import JsonFile
 
 # The architecture is pretty simple. This program is multithreaded.
 # One thread is the producer. It uses inotify to detect changes to the
 # fs. It writes in the events_queue.
 #
-# Another thread is the consumers. It processes the files.
+# Another thread is the consumer. It processes the files to build an index of 
+# threads (threads_index).
+#
+# Finally, there's a thread which periodically dumps the thread index.
 events_queue = []
-threads_index = {} # the email thread index - this is a dictionnary pointing to the threads filenames
+
+# the email thread index - this is a dictionary pointing to the threads filenames
+threads_index = None
+
+DUMPER_SLEEP_DURATION=20
+EVENTS_QUEUE_PROCESSING_DELAY=10
 
 class WatcherThread(threading.Thread):
     class EventHandler(pyinotify.ProcessEvent):
@@ -32,43 +54,66 @@ class WatcherThread(threading.Thread):
         self.path = path
 
     def run(self):
-        wm = pyinotify.WatchManager()  # Watch Manager
-        mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE  # watched events
+        wm = pyinotify.WatchManager()
+        mask = pyinotify.IN_DELETE | pyinotify.IN_CREATE
         handler = WatcherThread.EventHandler()
         notifier = pyinotify.Notifier(wm, handler)
         wdd = wm.add_watch(self.path, mask, rec=True)
         notifier.loop()
 
-class ProcessorThread(threading.Thread):
+class DumperThread(threading.Thread):
     def __init__(self, path):
         threading.Thread.__init__(self)
         self.path = path
 
     def run(self):
+        while True:
+            time.sleep(DUMPER_SLEEP_DURATION)
+            print "Dumping threads index"
+            threads_index.save()
+            
+
+class ProcessorThread(threading.Thread):
+    def __init__(self, path):
+        threading.Thread.__init__(self)
+        self.path = path
+
+        global threads_index
         threads_index = JsonFile(os.path.join(self.path, "threads_index.json"))
         if threads_index.data == None:
             threads_index.data = {}
 
+    def run(self):
         while True:
             while len(events_queue) != 0:
-                print "Found an event!"
                 event = events_queue.pop(0)
                 if event["type"] == "create":
                     try:
+                        print event["path"]
                         with open(event["path"], "r") as fd:
-                            headers = email.parser.Parser()
-                            headers.parse(fd, headersonly=True)
-                            for header in headers:
-                                print header
+                            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"]]
                     except IOError:
+                        # Postfix/Dovecot creates temporary files. Ignore them
                         pass
                     
-            time.sleep(10)
+            time.sleep(EVENTS_QUEUE_PROCESSING_DELAY)
 
 
 if __name__ == "__main__":
-    watcher_thread = WatcherThread(sys.argv[1])
     processor_thread = ProcessorThread(sys.argv[1])
+    watcher_thread = WatcherThread(sys.argv[1])
+    dumper_thread = DumperThread(sys.argv[1])
 
-    watcher_thread.start()
     processor_thread.start()
+    watcher_thread.start()
+    dumper_thread.start()