1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# SPDX-License-Identifier: GPL-2.0+
import asyncio
import functools
import inspect
from abc import abstractmethod
from typing import TYPE_CHECKING, Dict
if TYPE_CHECKING:
from . import config
from messages import MessageDB
from messages import CHMsgDict_Type
from messages import CHMsgMappingDict_Type
def update_on_failure(func):
"""Decorator for mailbox class methods that cause the mailbox to need a full
update if the method throws an exception."""
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
try:
return func(self, *args, **kwargs)
except:
self.need_update = True
Mailbox.changed_event.set()
raise
@functools.wraps(func)
async def async_wrapper(self, *args, **kwargs):
try:
return await func(self, *args, **kwargs)
except:
self.need_update = True
Mailbox.changed_event.set()
raise
if inspect.iscoroutinefunction(func):
return async_wrapper
return wrapper
class Mailbox(object):
messages: "CHMsgDict_Type" = {}
changed_event = asyncio.Event()
need_update = True
@abstractmethod
async def setup_mbox(self, cfg: "config.Config") -> None:
pass
@abstractmethod
def force_content(self, msgdb: "MessageDB",
msgs: "CHMsgDict_Type") -> None:
pass
@abstractmethod
async def merge_content(self, msgs: "CHMsgMappingDict_Type") -> None:
pass
def same_messages(self,
mdict: "CHMsgMappingDict_Type",
tuple_form=False) -> bool:
"""Return true if mdict is the same as the local messages"""
if len(self.messages) != len(mdict):
return False
for ch, mmsg in self.messages.items():
omsg = mdict.get(ch)
if omsg is None:
return False
# update_cloud_from_local use a different dict format
if tuple_form:
omsg = omsg[0] # Check the local mbox
if omsg is None:
return False
if (mmsg.content_hash != omsg.content_hash
or mmsg.flags != omsg.flags):
return False
return True
|