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.
19 from mailbox_state import MailboxState
21 CACHING_FILENAME = "caching_data.dat"
24 """This class is responsible for the caching of data."""
27 # integer for version of the cache
29 # dictionary of usernames as keys and dictionaries as values
30 # the second dictionaries have unique mailbox keys and mailboxes as values
35 cachingfile = open(CACHING_FILENAME, 'rb')
36 self.version, self.data = pickle.load(cachingfile)
37 print("Cache version", self.version)
38 print(len(self.data), "users found.")
42 with open(CACHING_FILENAME, 'wb') as cachingfile:
43 pickle.dump((0, self.data), cachingfile)
46 # create temporary file first
47 location = os.path.dirname(CACHING_FILENAME)
48 file_descriptor, tmpname = tempfile.mkstemp(dir=location)
49 cachingfile = os.fdopen(file_descriptor, 'wb')
51 # prepare data based on a save flag
53 for user in self.data:
55 for box_key in self.data[user]:
56 if(self.data[user][box_key].needs_save):
57 saved_data[user][box_key] = self.data[user][box_key]
58 print(saved_data[user][box_key].name, "will be saved.")
59 if(len(saved_data[user])==0):
61 print(user, "will not be saved.")
62 self.data = saved_data
64 # avoid test mode or cases where nothing needs saving
65 if(len(saved_data)==0):
70 pickle.dump((self.version, self.data), cachingfile)
71 print(len(self.data), "users stored.")
73 os.rename(tmpname, CACHING_FILENAME)
75 def retrieve_cached_mailbox(self, name, uidvalidity, user):
76 """Retrieve a cached mailbox or create it."""
77 box_key = name.strip('"') + uidvalidity
78 if(user not in self.data):
80 #print(user, "created.")
81 if(box_key not in self.data[user]):
82 self.data[user][box_key] = MailboxState(name, uidvalidity, user)
83 #print(box_key, "created.")
84 return self.data[user][box_key]
86 def report_date_conflicts(self):
87 """Write a date conflicts report in a file."""
88 with open("conflict_stats.txt", 'w') as statsfile:
89 owner_total_conflicts = {}
90 owner_total_missing = {}
91 for user in self.data:
92 owner_total_conflicts[user] = 0
93 owner_total_missing[user] = 0
94 for box_key in self.data[user]:
95 owner_total_conflicts[user] += self.data[user][box_key].date_conflicts
96 owner_total_missing[user] += self.data[user][box_key].no_received_field
97 statsfile.write("Total date conflicts to be corrected in a mailbox {0} are {1}.\n"\
98 .format(self.data[user][box_key].name, self.data[user][box_key].date_conflicts))
99 statsfile.write("Total missing received headers in a mailbox {0} are {1}.\n"\
100 .format(self.data[user][box_key].name, self.data[user][box_key].no_received_field))
101 statsfile.write("Total date conflicts to be corrected for user {0} are {1}.\n\n"\
102 .format(user, owner_total_missing[user]))