2 caching_data.py - The module contains the CachingData class.
4 Copyright (c) 2012 Intra2net AG
5 Author: Plamen Dimitrov
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 import os, platform, tempfile
20 from mailbox_state import MailboxState
22 CACHE_FILENAME = "message_cache.dat"
26 """This class is responsible for the caching of data."""
29 # integer for version of the cache
31 # dictionary of usernames as keys and dictionaries as values
32 # the second dictionaries have unique mailbox keys and mailboxes as values
36 # open data file or create one and initialize date if not found
38 cachefile = open(CACHE_FILENAME, 'rb')
39 self.version, self.data = pickle.load(cachefile)
40 if(self.version != CACHE_VERSION):
41 logging.warning("Cache file has version %s and the script version is %s.",
42 self.version, CACHE_VERSION)
44 logging.info("Cache version %s", self.version)
45 logging.debug("%s users found.", len(self.data))
47 self.version = CACHE_VERSION
49 with open(CACHE_FILENAME, 'wb') as cachefile:
50 pickle.dump((self.version, self.data), cachefile)
53 # create temporary file first
54 location = os.path.dirname(CACHE_FILENAME)
55 file_descriptor, tmpname = tempfile.mkstemp(dir=location)
57 cachefile = os.fdopen(file_descriptor, 'wb')
59 # prepare data based on a save flag
61 for user in self.data:
63 for box_key in self.data[user]:
64 if(self.data[user][box_key].needs_save):
65 saved_data[user][box_key] = self.data[user][box_key]
66 logging.debug("The mailbox %s will be saved.", saved_data[user][box_key].name)
67 if(len(saved_data[user])==0):
69 logging.debug("The user %s will not be saved.", user)
70 self.data = saved_data
71 # avoid test mode or cases where nothing needs saving
72 if(len(saved_data)==0):
78 pickle.dump((self.version, self.data), cachefile)
79 logging.debug("%s users stored.", len(self.data))
81 # handle windows non-atomic rename
82 if(platform.system()=='Windows'):
83 if(os.path.exists(CACHE_FILENAME)):
85 os.unlink(CACHE_FILENAME)
87 os.rename(tmpname, CACHE_FILENAME)
91 def retrieve_cached_mailbox(self, name, uidvalidity, user):
92 """Retrieve a cached mailbox or create it."""
93 box_key = name.strip('"') + uidvalidity
94 if(user not in self.data):
96 logging.debug("New user %s cached.", user)
97 if(box_key not in self.data[user]):
98 self.data[user][box_key] = MailboxState(name, uidvalidity, user)
99 logging.debug("New mailbox %s cached.", box_key)
100 return self.data[user][box_key]
102 def report_conflicts(self):
103 """Write a date conflicts report in a file."""
104 with open("conflict_stats.txt", 'w') as statsfile:
105 statsfile.write("Total date conflicts to be corrected (total1) and total messages without received headers (total2):\n\n")
106 owner_total_conflicts = {}
107 owner_total_missing = {}
108 for user in self.data:
109 statsfile.write("total1 \t\t total2 \t mailbox \n")
110 owner_total_conflicts[user] = 0
111 owner_total_missing[user] = 0
112 for box_key in self.data[user]:
113 owner_total_conflicts[user] += self.data[user][box_key].date_conflicts
114 owner_total_missing[user] += self.data[user][box_key].no_received_field
115 statsfile.write("{0} \t\t {1} \t\t {2}\n"\
116 .format(self.data[user][box_key].date_conflicts,
117 self.data[user][box_key].no_received_field,
118 self.data[user][box_key].name))
119 statsfile.write("\n{0} \t\t {1} \t\t {2}\n\n"\
120 .format(owner_total_conflicts[user], owner_total_missing[user], user))