Better wording
[imap-fix-internaldate] / caching_data.py
CommitLineData
8301e589
PD
1'''
2caching_data.py - The module contains the CachingData class.
3
4Copyright (c) 2012 Intra2net AG
5Author: Plamen Dimitrov
6
7This program is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16'''
7a1d4c35 17import os, tempfile
8301e589 18import pickle
8fe4e3ff 19import logging
7a1d4c35
PD
20from mailbox_state import MailboxState
21
8fe4e3ff 22CACHE_FILENAME = "message_cache.dat"
8301e589
PD
23
24class CachingData:
25 """This class is responsible for the caching of data."""
7a1d4c35
PD
26
27 # class attributes
28 # integer for version of the cache
29 version = None
30 # dictionary of usernames as keys and dictionaries as values
31 # the second dictionaries have unique mailbox keys and mailboxes as values
32 data = None
8301e589
PD
33
34 def __init__(self):
8fe4e3ff 35 # open data file or create one and initialize date if not found
8301e589 36 try:
8fe4e3ff
PD
37 cachefile = open(CACHE_FILENAME, 'rb')
38 self.version, self.data = pickle.load(cachefile)
39 logging.info("Cache version %s", self.version)
40 logging.debug("%s users found.", len(self.data))
8301e589 41 except IOError:
7a1d4c35 42 self.version = 0
8301e589 43 self.data = {}
8fe4e3ff
PD
44 with open(CACHE_FILENAME, 'wb') as cachefile:
45 pickle.dump((0, self.data), cachefile)
8301e589
PD
46
47 def __del__(self):
7a1d4c35 48 # create temporary file first
8fe4e3ff 49 location = os.path.dirname(CACHE_FILENAME)
7a1d4c35 50 file_descriptor, tmpname = tempfile.mkstemp(dir=location)
8fe4e3ff
PD
51 try:
52 cachefile = os.fdopen(file_descriptor, 'wb')
53
54 # prepare data based on a save flag
55 saved_data = {}
56 for user in self.data:
57 saved_data[user] = {}
58 for box_key in self.data[user]:
59 if(self.data[user][box_key].needs_save):
60 saved_data[user][box_key] = self.data[user][box_key]
61 logging.debug("The mailbox %s will be saved.", saved_data[user][box_key].name)
62 if(len(saved_data[user])==0):
63 del saved_data[user]
64 logging.debug("The user %s will not be saved.", user)
65 self.data = saved_data
66
67 # avoid test mode or cases where nothing needs saving
68 if(len(saved_data)==0):
69 os.unlink(tmpname)
70 return
71
72 # serialize in file
73 self.version += 1
74 pickle.dump((self.version, self.data), cachefile)
75 logging.debug("%s users stored.", len(self.data))
76 cachefile.close()
77 os.rename(tmpname, CACHE_FILENAME)
78 except:
79 os.unlink(tmpname)
8301e589 80
7a1d4c35
PD
81 def retrieve_cached_mailbox(self, name, uidvalidity, user):
82 """Retrieve a cached mailbox or create it."""
83 box_key = name.strip('"') + uidvalidity
84 if(user not in self.data):
85 self.data[user] = {}
8fe4e3ff 86 logging.debug("New user %s cached.", user)
7a1d4c35
PD
87 if(box_key not in self.data[user]):
88 self.data[user][box_key] = MailboxState(name, uidvalidity, user)
8fe4e3ff 89 logging.debug("New mailbox %s cached.", box_key)
7a1d4c35
PD
90 return self.data[user][box_key]
91
92 def report_date_conflicts(self):
93 """Write a date conflicts report in a file."""
94 with open("conflict_stats.txt", 'w') as statsfile:
95 owner_total_conflicts = {}
96 owner_total_missing = {}
97 for user in self.data:
98 owner_total_conflicts[user] = 0
99 owner_total_missing[user] = 0
100 for box_key in self.data[user]:
101 owner_total_conflicts[user] += self.data[user][box_key].date_conflicts
102 owner_total_missing[user] += self.data[user][box_key].no_received_field
103 statsfile.write("Total date conflicts to be corrected in a mailbox {0} are {1}.\n"\
104 .format(self.data[user][box_key].name, self.data[user][box_key].date_conflicts))
474b47d4 105 statsfile.write("Total messages without received headers in a mailbox {0} are {1}.\n"\
7a1d4c35
PD
106 .format(self.data[user][box_key].name, self.data[user][box_key].no_received_field))
107 statsfile.write("Total date conflicts to be corrected for user {0} are {1}.\n\n"\
108 .format(user, owner_total_missing[user]))
8301e589 109 return