Commit | Line | Data |
---|---|---|
8301e589 PD |
1 | ''' |
2 | caching_data.py - The module contains the CachingData class. | |
3 | ||
4 | Copyright (c) 2012 Intra2net AG | |
5 | Author: Plamen Dimitrov | |
6 | ||
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. | |
11 | ||
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. | |
16 | ''' | |
7a1d4c35 | 17 | import os, tempfile |
8301e589 | 18 | import pickle |
7a1d4c35 PD |
19 | from mailbox_state import MailboxState |
20 | ||
21 | CACHING_FILENAME = "caching_data.dat" | |
8301e589 PD |
22 | |
23 | class CachingData: | |
24 | """This class is responsible for the caching of data.""" | |
7a1d4c35 PD |
25 | |
26 | # class attributes | |
27 | # integer for version of the cache | |
28 | version = None | |
29 | # dictionary of usernames as keys and dictionaries as values | |
30 | # the second dictionaries have unique mailbox keys and mailboxes as values | |
31 | data = None | |
8301e589 PD |
32 | |
33 | def __init__(self): | |
34 | try: | |
7a1d4c35 PD |
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.") | |
8301e589 | 39 | except IOError: |
7a1d4c35 | 40 | self.version = 0 |
8301e589 | 41 | self.data = {} |
7a1d4c35 PD |
42 | with open(CACHING_FILENAME, 'wb') as cachingfile: |
43 | pickle.dump((0, self.data), cachingfile) | |
8301e589 PD |
44 | |
45 | def __del__(self): | |
7a1d4c35 PD |
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') | |
8301e589 | 50 | |
7a1d4c35 PD |
51 | # prepare data based on a save flag |
52 | saved_data = {} | |
53 | for user in self.data: | |
54 | saved_data[user] = {} | |
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): | |
60 | del saved_data[user] | |
61 | print(user, "will not be saved.") | |
62 | self.data = saved_data | |
8301e589 | 63 | |
7a1d4c35 PD |
64 | # avoid test mode or cases where nothing needs saving |
65 | if(len(saved_data)==0): | |
66 | os.unlink(tmpname) | |
67 | return | |
68 | # serialize in file | |
69 | self.version += 1 | |
70 | pickle.dump((self.version, self.data), cachingfile) | |
71 | print(len(self.data), "users stored.") | |
72 | cachingfile.close() | |
73 | os.rename(tmpname, CACHING_FILENAME) | |
8301e589 | 74 | |
7a1d4c35 PD |
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): | |
79 | self.data[user] = {} | |
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] | |
85 | ||
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])) | |
8301e589 | 103 | return |