1 | """Ported to Python 3. |
---|
2 | """ |
---|
3 | |
---|
4 | import weakref |
---|
5 | |
---|
6 | class History(object): |
---|
7 | """Keep track of recent operations, for a status display.""" |
---|
8 | |
---|
9 | name = "history" |
---|
10 | MAX_DOWNLOAD_STATUSES = 10 |
---|
11 | MAX_UPLOAD_STATUSES = 10 |
---|
12 | MAX_MAPUPDATE_STATUSES = 20 |
---|
13 | MAX_PUBLISH_STATUSES = 20 |
---|
14 | MAX_RETRIEVE_STATUSES = 40 |
---|
15 | |
---|
16 | def __init__(self, stats_provider=None): |
---|
17 | self.stats_provider = stats_provider |
---|
18 | |
---|
19 | self.all_downloads_statuses = weakref.WeakKeyDictionary() |
---|
20 | self.recent_download_statuses = [] |
---|
21 | self.all_upload_statuses = weakref.WeakKeyDictionary() |
---|
22 | self.recent_upload_statuses = [] |
---|
23 | |
---|
24 | self.all_mapupdate_status = weakref.WeakKeyDictionary() |
---|
25 | self.recent_mapupdate_status = [] |
---|
26 | self.all_publish_status = weakref.WeakKeyDictionary() |
---|
27 | self.recent_publish_status = [] |
---|
28 | self.all_retrieve_status = weakref.WeakKeyDictionary() |
---|
29 | self.recent_retrieve_status = [] |
---|
30 | |
---|
31 | self.all_helper_upload_statuses = weakref.WeakKeyDictionary() |
---|
32 | self.recent_helper_upload_statuses = [] |
---|
33 | |
---|
34 | |
---|
35 | def add_download(self, download_status): |
---|
36 | self.all_downloads_statuses[download_status] = None |
---|
37 | self.recent_download_statuses.append(download_status) |
---|
38 | while len(self.recent_download_statuses) > self.MAX_DOWNLOAD_STATUSES: |
---|
39 | self.recent_download_statuses.pop(0) |
---|
40 | |
---|
41 | def list_all_download_statuses(self): |
---|
42 | for ds in self.all_downloads_statuses: |
---|
43 | yield ds |
---|
44 | |
---|
45 | def add_upload(self, upload_status): |
---|
46 | self.all_upload_statuses[upload_status] = None |
---|
47 | self.recent_upload_statuses.append(upload_status) |
---|
48 | while len(self.recent_upload_statuses) > self.MAX_UPLOAD_STATUSES: |
---|
49 | self.recent_upload_statuses.pop(0) |
---|
50 | |
---|
51 | def list_all_upload_statuses(self): |
---|
52 | for us in self.all_upload_statuses: |
---|
53 | yield us |
---|
54 | |
---|
55 | |
---|
56 | |
---|
57 | def notify_mapupdate(self, p): |
---|
58 | self.all_mapupdate_status[p] = None |
---|
59 | self.recent_mapupdate_status.append(p) |
---|
60 | while len(self.recent_mapupdate_status) > self.MAX_MAPUPDATE_STATUSES: |
---|
61 | self.recent_mapupdate_status.pop(0) |
---|
62 | |
---|
63 | def notify_publish(self, p, size): |
---|
64 | self.all_publish_status[p] = None |
---|
65 | self.recent_publish_status.append(p) |
---|
66 | if self.stats_provider: |
---|
67 | self.stats_provider.count('mutable.files_published', 1) |
---|
68 | # We must be told bytes_published as an argument, since the |
---|
69 | # publish_status does not yet know how much data it will be asked |
---|
70 | # to send. When we move to MDMF we'll need to find a better way |
---|
71 | # to handle this. |
---|
72 | self.stats_provider.count('mutable.bytes_published', size) |
---|
73 | while len(self.recent_publish_status) > self.MAX_PUBLISH_STATUSES: |
---|
74 | self.recent_publish_status.pop(0) |
---|
75 | |
---|
76 | def notify_retrieve(self, r): |
---|
77 | self.all_retrieve_status[r] = None |
---|
78 | self.recent_retrieve_status.append(r) |
---|
79 | if self.stats_provider: |
---|
80 | self.stats_provider.count('mutable.files_retrieved', 1) |
---|
81 | self.stats_provider.count('mutable.bytes_retrieved', r.get_size()) |
---|
82 | while len(self.recent_retrieve_status) > self.MAX_RETRIEVE_STATUSES: |
---|
83 | self.recent_retrieve_status.pop(0) |
---|
84 | |
---|
85 | |
---|
86 | def list_all_mapupdate_statuses(self): |
---|
87 | for s in self.all_mapupdate_status: |
---|
88 | yield s |
---|
89 | def list_all_publish_statuses(self): |
---|
90 | for s in self.all_publish_status: |
---|
91 | yield s |
---|
92 | def list_all_retrieve_statuses(self): |
---|
93 | for s in self.all_retrieve_status: |
---|
94 | yield s |
---|
95 | |
---|
96 | def notify_helper_upload(self, s): |
---|
97 | self.all_helper_upload_statuses[s] = None |
---|
98 | self.recent_helper_upload_statuses.append(s) |
---|
99 | while len(self.recent_helper_upload_statuses) > self.MAX_UPLOAD_STATUSES: |
---|
100 | self.recent_helper_upload_statuses.pop(0) |
---|
101 | |
---|
102 | def list_all_helper_statuses(self): |
---|
103 | for s in self.all_helper_upload_statuses: |
---|
104 | yield s |
---|
105 | |
---|