Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
start workign on a message-based tracker
- Loading branch information
Ian Good
committed
May 22, 2015
1 parent
861b4c2
commit 6220937
Showing
2 changed files
with
76 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Copyright (c) 2014 Ian C. Good | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in | ||
# all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
# THE SOFTWARE. | ||
# | ||
|
||
import asyncio | ||
|
||
__all__ = ['MessageTracker'] | ||
|
||
|
||
class MessageTracker(object): | ||
|
||
def __init__(self): | ||
super().__init__() | ||
self.messages = {} | ||
self.exists = 0 | ||
self.recent = 0 | ||
|
||
@asyncio.coroutine | ||
def update(self, messages): | ||
"""Given a list of messages, return a new tracker and a dictionary of | ||
external updates that need to be returned in untagged FETCH responses. | ||
:param list messages: :class:`~pymap.interfaces.MessageInterface | ||
objects fetched from the backend. | ||
:returns: Two-tuple of the :class:`MessageTracker` object and details | ||
about what has changed since the last :meth:`.update`. | ||
""" | ||
ret = {} | ||
if self.exists != len(messages): | ||
ret['exists'] = self.exists = len(messages) | ||
uid_flags = {msg.uid: (msg.seq, (yield from msg.fetch_flags())) | ||
for msg in messages} | ||
recent = len([True for _, flags in uid_flags.values() | ||
if br'\Recent' in flags]) | ||
if self.recent != recent: | ||
ret['recent'] = self.recent = recent | ||
before_uids = frozenset(self.messages.keys()) | ||
after_uids = frozenset(uid_flags.keys()) | ||
ret['expunge'] = [self.messages[uid][0] | ||
for uid in (before_uids - after_uids)] | ||
ret['flags'] = [] | ||
for uid in before_uids & after_uids: | ||
before_flags = frozenset(self.messages[uid][1]) | ||
after_flags = frozenset(uid_flags[uid][1]) | ||
if before_flags != after_flags: | ||
seq = uid_flags[uid][0] | ||
ret['flags'].append((seq, after_flags)) | ||
return ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters