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 | ''' | |
17 | ||
18 | import pickle | |
19 | ||
20 | class CachingData: | |
21 | """This class is responsible for the caching of data.""" | |
22 | ||
23 | def __init__(self): | |
24 | try: | |
25 | cachingfile = open('caching_data.pkl', 'rb') | |
26 | self.data = pickle.load(cachingfile) | |
27 | #print(len(self.data), "users found.") | |
28 | self.save_flag = {} | |
29 | for user in self.data: | |
30 | self.save_flag[user] = {} | |
31 | for uid_key in self.data[user]: | |
32 | self.save_flag[user][uid_key] = False | |
33 | except IOError: | |
34 | self.data = {} | |
35 | self.save_flag = {} | |
36 | with open('caching_data.pkl', 'wb') as cachingfile: | |
37 | pickle.dump(self.data, cachingfile) | |
38 | ||
39 | def __del__(self): | |
40 | with open('caching_data.pkl', 'wb') as cachingfile: | |
41 | # prepare data based on a save flag | |
42 | for user in self.save_flag: | |
43 | for uid_key in self.save_flag[user]: | |
44 | if(not self.save_flag[user][uid_key]): | |
45 | del self.data[user][uid_key] | |
46 | #print(uidvalidity, "deleted from cache.") | |
47 | if(len(self.data[user])==0): | |
48 | del self.data[user] | |
49 | #print(user, "deleted from cache.") | |
50 | ||
51 | # serialize in file | |
52 | pickle.dump(self.data, cachingfile) | |
53 | ||
54 | #print(len(self.data), "users stored.") | |
55 | ||
56 | def _cache_new_mailbox(self, username, uid_key): | |
57 | """Store the mailbox as integer uidvalidity""" | |
58 | if(username not in self.data): | |
59 | self.data[username] = {} | |
60 | self.save_flag[username] = {} | |
61 | #print(username, "created.") | |
62 | if(uid_key not in self.data[username]): | |
63 | self.data[username][uid_key] = [] | |
64 | self.save_flag[username][uid_key] = False | |
65 | #print(uid_key, "created.") | |
66 | return | |
67 | ||
68 | def sync_cached_mailbox(self, username, uid_key, list_ids): | |
69 | """Adds new messages to the cache and returns a list of them. | |
70 | Confirm the changes to a mailbox to finally save it.""" | |
71 | new_ids = [] | |
72 | ||
73 | if(username not in self.data or \ | |
74 | uid_key not in self.data[username]): | |
75 | self._cache_new_mailbox(username, uid_key) | |
76 | new_ids = list_ids | |
77 | else: | |
78 | for uid in list_ids: | |
79 | try: | |
80 | self.data[username][uid_key].index(uid) | |
81 | #print("found", uid, uid_key) | |
82 | except ValueError: | |
83 | #print("new", uid, uid_key) | |
84 | new_ids.append(uid) | |
85 | ||
86 | # update cached_mailbox | |
87 | self.data[username][uid_key] = list_ids | |
88 | ||
89 | return new_ids | |
90 | ||
91 | def commit_cached_mailbox(self, username, uid_key): | |
92 | """Confirm the chages to the cached mailbox.""" | |
93 | self.save_flag[username][uid_key] = True | |
94 | #print(username, uid_key, "committed.") | |
95 | return |