python: use dataclasses to reduce boilerplate

This commit is contained in:
link2xt
2023-02-14 15:05:25 +00:00
parent 78577594d0
commit 243f28f8a5
5 changed files with 17 additions and 56 deletions

View File

@@ -3,6 +3,7 @@
## Unreleased ## Unreleased
### Changes ### Changes
- deltachat-rpc-client: use `dataclass` for `Account`, `Chat`, `Contact` and `Message` #4042
### Fixes ### Fixes
- deltachat-rpc-server: do not block stdin while processing the request. #4041 - deltachat-rpc-server: do not block stdin while processing the request. #4041

View File

@@ -1,4 +1,5 @@
from typing import TYPE_CHECKING, List, Optional, Tuple, Union from typing import TYPE_CHECKING, List, Optional, Tuple, Union
from dataclasses import dataclass
from ._utils import AttrDict from ._utils import AttrDict
from .chat import Chat from .chat import Chat
@@ -11,28 +12,17 @@ if TYPE_CHECKING:
from .deltachat import DeltaChat from .deltachat import DeltaChat
@dataclass
class Account: class Account:
"""Delta Chat account.""" """Delta Chat account."""
def __init__(self, manager: "DeltaChat", account_id: int) -> None: manager: "DeltaChat"
self.manager = manager id: int
self.id = account_id
@property @property
def _rpc(self) -> Rpc: def _rpc(self) -> Rpc:
return self.manager.rpc return self.manager.rpc
def __eq__(self, other) -> bool:
if not isinstance(other, Account):
return False
return self.id == other.id and self.manager == other.manager
def __ne__(self, other) -> bool:
return not self == other
def __repr__(self) -> str:
return f"<Account id={self.id}>"
async def wait_for_event(self) -> AttrDict: async def wait_for_event(self) -> AttrDict:
"""Wait until the next event and return it.""" """Wait until the next event and return it."""
return AttrDict(await self._rpc.wait_for_event(self.id)) return AttrDict(await self._rpc.wait_for_event(self.id))

View File

@@ -1,6 +1,7 @@
import calendar import calendar
from datetime import datetime from datetime import datetime
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union
from dataclasses import dataclass
from ._utils import AttrDict from ._utils import AttrDict
from .const import ChatVisibility from .const import ChatVisibility
@@ -12,28 +13,17 @@ if TYPE_CHECKING:
from .account import Account from .account import Account
@dataclass
class Chat: class Chat:
"""Chat object which manages members and through which you can send and retrieve messages.""" """Chat object which manages members and through which you can send and retrieve messages."""
def __init__(self, account: "Account", chat_id: int) -> None: account: "Account"
self.account = account id: int
self.id = chat_id
@property @property
def _rpc(self) -> Rpc: def _rpc(self) -> Rpc:
return self.account._rpc return self.account._rpc
def __eq__(self, other) -> bool:
if not isinstance(other, Chat):
return False
return self.id == other.id and self.account == other.account
def __ne__(self, other) -> bool:
return not self == other
def __repr__(self) -> str:
return f"<Chat id={self.id} account={self.account.id}>"
async def delete(self) -> None: async def delete(self) -> None:
"""Delete this chat and all its messages. """Delete this chat and all its messages.

View File

@@ -1,4 +1,5 @@
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from dataclasses import dataclass
from ._utils import AttrDict from ._utils import AttrDict
from .rpc import Rpc from .rpc import Rpc
@@ -8,6 +9,7 @@ if TYPE_CHECKING:
from .chat import Chat from .chat import Chat
@dataclass
class Contact: class Contact:
""" """
Contact API. Contact API.
@@ -15,20 +17,8 @@ class Contact:
Essentially a wrapper for RPC, account ID and a contact ID. Essentially a wrapper for RPC, account ID and a contact ID.
""" """
def __init__(self, account: "Account", contact_id: int) -> None: account: "Account"
self.account = account id: int
self.id = contact_id
def __eq__(self, other) -> bool:
if not isinstance(other, Contact):
return False
return self.id == other.id and self.account == other.account
def __ne__(self, other) -> bool:
return not self == other
def __repr__(self) -> str:
return f"<Contact id={self.id} account={self.account.id}>"
@property @property
def _rpc(self) -> Rpc: def _rpc(self) -> Rpc:

View File

@@ -1,5 +1,6 @@
import json import json
from typing import TYPE_CHECKING, Union from typing import TYPE_CHECKING, Union
from dataclasses import dataclass
from ._utils import AttrDict from ._utils import AttrDict
from .contact import Contact from .contact import Contact
@@ -9,23 +10,12 @@ if TYPE_CHECKING:
from .account import Account from .account import Account
@dataclass
class Message: class Message:
"""Delta Chat Message object.""" """Delta Chat Message object."""
def __init__(self, account: "Account", msg_id: int) -> None: account: "Account"
self.account = account id: int
self.id = msg_id
def __eq__(self, other) -> bool:
if not isinstance(other, Message):
return False
return self.id == other.id and self.account == other.account
def __ne__(self, other) -> bool:
return not self == other
def __repr__(self) -> str:
return f"<Message id={self.id} account={self.account.id}>"
@property @property
def _rpc(self) -> Rpc: def _rpc(self) -> Rpc: