From 1248a2636102e70c30d5d9a35c423b3c870749c3 Mon Sep 17 00:00:00 2001 From: karim hamidou Date: Sat, 7 Dec 2013 18:28:22 +0100 Subject: [PATCH] refactored threads_index structure --- src/back/kite/filterdaemon.py | 16 +++++++++----- src/back/kite/jsonfile.py | 33 +++++++++++++++++++++++------ src/back/kite/threads.py | 3 +++ src/back/tests/test_filterdaemon.py | 8 +++++-- src/back/tests/test_jsonfile.py | 11 ++++++++++ 5 files changed, 58 insertions(+), 13 deletions(-) diff --git a/src/back/kite/filterdaemon.py b/src/back/kite/filterdaemon.py index 775b212..e77d2f1 100755 --- a/src/back/kite/filterdaemon.py +++ b/src/back/kite/filterdaemon.py @@ -6,7 +6,6 @@ # ======================= # # 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 ? # ================ @@ -21,6 +20,7 @@ import sys import os import pyinotify import threading +import datetime import time import email.parser @@ -71,6 +71,8 @@ class DumperThread(threading.Thread): while True: time.sleep(DUMPER_SLEEP_DURATION) print "Dumping threads index" + + # sort list by date self.threads_index.save() def process_new_email(path, threads_index): @@ -81,11 +83,15 @@ def process_new_email(path, threads_index): if subject != None: subject = headers.cleanup_subject(subject) - if subject in threads_index.data: - threads_index.data[subject].append(path) - else: + thread = next((thread for thread in threads_index.data if thread.subject == subject), None) + if not thread: # create a new thread - threads_index.data[subject] = [path] + thread = threads.create_thread_structure() + thread["subject"] = subject + threads_index.data.append(thread) + + thread["messages"].append(path) + thread["date"] = datetime.datetime.utcnow() class ProcessorThread(threading.Thread): diff --git a/src/back/kite/jsonfile.py b/src/back/kite/jsonfile.py index 22d0c3c..a6cba42 100644 --- a/src/back/kite/jsonfile.py +++ b/src/back/kite/jsonfile.py @@ -2,8 +2,25 @@ # reading and writing is locking import os import json +import datetime +import dateutil.parser from lockfile import FileLock +class DatetimeEncoder(json.JSONEncoder): + def default(self, obj): + if isinstance(obj, datetime.datetime): + return obj.strftime("%y-%m-%dT%H:%M:%S+00:00") + + return json.JSONEncoder.default(self, obj) + +def deserialize_datetime(obj): + try: + obj['date'] = dateutil.parser.parse(obj['date']) + except KeyError: + pass + return obj + + class JsonFile(object): def __init__(self, path=None): self.path = path @@ -15,19 +32,23 @@ class JsonFile(object): try: with FileLock(self.path): fd = open(self.path, "r") - self.data = json.load(fd) + self.data = json.load(fd, object_hook=deserialize_datetime) fd.close() except IOError: pass - def save(self): + def save(self, path=None): + if path == None: + path = self.path + def save_proper(): - with open(self.path, "w+") as fd: - json.dump(self.data, fd) + with open(path, "w+") as fd: + json.dump(self.data, fd, cls=DatetimeEncoder) # Lock the file only if it already exists - if os.path.exists(self.path): - with FileLock(self.path): + # FIXME: this is maybe a race condition + if os.path.exists(path): + with FileLock(path): save_proper() else: save_proper() diff --git a/src/back/kite/threads.py b/src/back/kite/threads.py index da24656..b20b179 100644 --- a/src/back/kite/threads.py +++ b/src/back/kite/threads.py @@ -1,5 +1,6 @@ # threads management functions import jsonfile +import datetime import uuid import os @@ -16,6 +17,8 @@ def create_thread(parent_dir): def load_thread(parent_dir, uuid): path = os.path.join(parent_dir, name) +def create_thread_structure(): + return {"date": datetime.datetime.utcnow(), "messages": [], "subject": ""} diff --git a/src/back/tests/test_filterdaemon.py b/src/back/tests/test_filterdaemon.py index 1867cc6..2e8ba1d 100644 --- a/src/back/tests/test_filterdaemon.py +++ b/src/back/tests/test_filterdaemon.py @@ -4,6 +4,10 @@ import kite.filterdaemon as filterdaemon class TestEmailProcessor(unittest.TestCase): def test_process_email(self): - threads_index = {"data": {}} + class MockThreadsIndex(object): + def __init__(self): + self.data = [] + + threads_index = MockThreadsIndex() filterdaemon.process_new_email("mocks/test_process_email.txt", threads_index) - self.assertTrue("Hey, How are you ?" in threads_index["data"]) + self.assertTrue("Hey, How are you ?" == threads_index.data[0]["subject"]) diff --git a/src/back/tests/test_jsonfile.py b/src/back/tests/test_jsonfile.py index 7ea1237..5d95f14 100644 --- a/src/back/tests/test_jsonfile.py +++ b/src/back/tests/test_jsonfile.py @@ -2,6 +2,7 @@ import unittest import tempfile import fcntl import os +import datetime import kite.jsonfile import tmpdir from kite.jsonfile import JsonFile @@ -19,3 +20,13 @@ class TestJsonfile(tmpdir.TestCaseWithTempFile): fd.close() json_file.refresh() self.assertEqual(json_file.data, [], msg="Value is not refreshed correctly") + + def test_datetime_serialization(self): + d = datetime.datetime.now() + json_file = JsonFile() + json_file.data = {"date": d} + json_file.save(self.tmpfile) + + json_file2 = JsonFile(self.tmpfile) + self.assertTrue(isinstance(json_file2.data["date"], datetime.datetime), msg="Datetime value is not deserialized correctly") + -- 2.43.0