Style improvements and cache from settings validation added
[imap-fix-internaldate] / src / caching_data.py
index ff32212..4ee3ac0 100644 (file)
@@ -20,7 +20,7 @@ import logging
 from mailbox_state import MailboxState
 
 CACHE_FILENAME = "message_cache.dat"
-CACHE_VERSION = 1
+CACHE_VERSION = "1"
 
 class CachingData:
     """This class is responsible for the caching of data."""
@@ -28,26 +28,35 @@ class CachingData:
     # class attributes
     # integer for version of the cache
     version = None
+    # boolean flag which indicates fallback mode of the cache
+    fallback_to_date_header = None
     # dictionary of usernames as keys and dictionaries as values
     # the second dictionaries have unique mailbox keys and mailboxes as values
     data = None
 
-    def __init__(self):
+    def __init__(self, fallback_mode):
         # open data file or create one and initialize date if not found
         try:
             cachefile = open(CACHE_FILENAME, 'rb')
-            self.version, self.data = pickle.load(cachefile)
+            cache_info, self.data = pickle.load(cachefile)
+            cache_info = cache_info.split(' ')
+            self.version = cache_info[0]
             if(self.version != CACHE_VERSION):
                 logging.warning("Cache file has version %s and the script version is %s. Deleting cache.",
                                 self.version, CACHE_VERSION)
                 raise IOError
+            self.fallback_to_date_header = cache_info[1]
+            if(self.fallback_to_date_header != str(fallback_mode)):
+                logging.warning("Cache file date fallback mode setting is different than current settings. Deleting cache.")
+                raise IOError
             logging.info("Cache file %s loaded", CACHE_FILENAME)
             logging.debug("%s users found.", len(self.data))
         except IOError:
             self.version = CACHE_VERSION
+            stored_cache_info = self.version + ' ' + str(fallback_mode)
             self.data = {}
             with open(CACHE_FILENAME, 'wb') as cachefile:
-                pickle.dump((self.version, self.data), cachefile)
+                pickle.dump((stored_cache_info, self.data), cachefile)
 
     def __del__(self):
         # create temporary file first
@@ -75,7 +84,8 @@ class CachingData:
                 return
 
             # serialize in file
-            pickle.dump((self.version, self.data), cachefile)
+            stored_cache_info = self.version + ' ' + self.fallback_to_date_header
+            pickle.dump((stored_cache_info, self.data), cachefile)
             logging.debug("%s users stored.", len(self.data))
 
             # handle windows non-atomic rename