Daniel Brötzmann pushed to branch master at gajim / gajim
Commits:
-
20b33a7e
by wurstsalat at 2022-05-28T02:04:03+02:00
-
1987c330
by wurstsalat at 2022-05-28T02:05:41+02:00
-
b7ab4045
by wurstsalat at 2022-05-28T02:05:58+02:00
-
16dc125b
by wurstsalat at 2022-05-28T02:06:11+02:00
4 changed files:
- gajim/common/modules/gateway.py
- gajim/common/modules/iq.py
- gajim/common/modules/user_avatar.py
- gajim/common/modules/util.py
Changes:
| ... | ... | @@ -14,20 +14,26 @@ |
| 14 | 14 | |
| 15 | 15 | # XEP-0100: Gateway Interaction
|
| 16 | 16 | |
| 17 | +from __future__ import annotations
|
|
| 18 | + |
|
| 19 | +from typing import Optional
|
|
| 20 | + |
|
| 17 | 21 | import nbxmpp
|
| 22 | +from nbxmpp.protocol import Iq
|
|
| 18 | 23 | from nbxmpp.namespaces import Namespace
|
| 19 | 24 | |
| 20 | 25 | from gajim.common import app
|
| 26 | +from gajim.common import types
|
|
| 21 | 27 | from gajim.common.events import AgentRemoved
|
| 22 | 28 | from gajim.common.events import GatewayPromptReceived
|
| 23 | 29 | from gajim.common.modules.base import BaseModule
|
| 24 | 30 | |
| 25 | 31 | |
| 26 | 32 | class Gateway(BaseModule):
|
| 27 | - def __init__(self, con):
|
|
| 33 | + def __init__(self, con: types.Client) -> None:
|
|
| 28 | 34 | BaseModule.__init__(self, con)
|
| 29 | 35 | |
| 30 | - def unsubscribe(self, agent):
|
|
| 36 | + def unsubscribe(self, agent: str) -> None:
|
|
| 31 | 37 | if not app.account_is_available(self._account):
|
| 32 | 38 | return
|
| 33 | 39 | iq = nbxmpp.Iq('set', Namespace.REGISTER, to=agent)
|
| ... | ... | @@ -37,7 +43,10 @@ def unsubscribe(self, agent): |
| 37 | 43 | iq, self._on_unsubscribe_result)
|
| 38 | 44 | self._con.get_module('Roster').delete_item(agent)
|
| 39 | 45 | |
| 40 | - def _on_unsubscribe_result(self, _nbxmpp_client, stanza):
|
|
| 46 | + def _on_unsubscribe_result(self,
|
|
| 47 | + _nbxmpp_client: types.xmppClient,
|
|
| 48 | + stanza: nbxmpp.Node
|
|
| 49 | + ) -> None:
|
|
| 41 | 50 | if not nbxmpp.isResultNode(stanza):
|
| 42 | 51 | self._log.info('Error: %s', stanza.getError())
|
| 43 | 52 | return
|
| ... | ... | @@ -63,7 +72,10 @@ def _on_unsubscribe_result(self, _nbxmpp_client, stanza): |
| 63 | 72 | agent=agent,
|
| 64 | 73 | jid_list=jid_list))
|
| 65 | 74 | |
| 66 | - def request_gateway_prompt(self, jid, prompt=None):
|
|
| 75 | + def request_gateway_prompt(self,
|
|
| 76 | + jid: str,
|
|
| 77 | + prompt: Optional[str] = None
|
|
| 78 | + ) -> None:
|
|
| 67 | 79 | typ_ = 'get'
|
| 68 | 80 | if prompt:
|
| 69 | 81 | typ_ = 'set'
|
| ... | ... | @@ -73,7 +85,10 @@ def request_gateway_prompt(self, jid, prompt=None): |
| 73 | 85 | query.setTagData('prompt', prompt)
|
| 74 | 86 | self._con.connection.SendAndCallForResponse(iq, self._on_prompt_result)
|
| 75 | 87 | |
| 76 | - def _on_prompt_result(self, _nbxmpp_client, stanza):
|
|
| 88 | + def _on_prompt_result(self,
|
|
| 89 | + _nbxmpp_client: types.xmppClient,
|
|
| 90 | + stanza: Iq
|
|
| 91 | + ) -> None:
|
|
| 77 | 92 | jid = str(stanza.getFrom())
|
| 78 | 93 | fjid = stanza.getFrom().bare
|
| 79 | 94 | resource = stanza.getFrom().resource
|
| ... | ... | @@ -14,10 +14,15 @@ |
| 14 | 14 | |
| 15 | 15 | # Iq handler
|
| 16 | 16 | |
| 17 | +from __future__ import annotations
|
|
| 18 | + |
|
| 17 | 19 | import nbxmpp
|
| 20 | +from nbxmpp.protocol import Iq
|
|
| 21 | +from nbxmpp.structs import IqProperties
|
|
| 18 | 22 | from nbxmpp.structs import StanzaHandler
|
| 19 | 23 | |
| 20 | 24 | from gajim.common import app
|
| 25 | +from gajim.common import types
|
|
| 21 | 26 | from gajim.common.events import FileRequestError
|
| 22 | 27 | from gajim.common.events import FileSendError
|
| 23 | 28 | from gajim.common.helpers import to_user_string
|
| ... | ... | @@ -26,7 +31,7 @@ |
| 26 | 31 | |
| 27 | 32 | |
| 28 | 33 | class Iq(BaseModule):
|
| 29 | - def __init__(self, con):
|
|
| 34 | + def __init__(self, con: types.Client) -> None:
|
|
| 30 | 35 | BaseModule.__init__(self, con)
|
| 31 | 36 | |
| 32 | 37 | self.handlers = [
|
| ... | ... | @@ -36,7 +41,11 @@ def __init__(self, con): |
| 36 | 41 | priority=51),
|
| 37 | 42 | ]
|
| 38 | 43 | |
| 39 | - def _iq_error_received(self, _con, _stanza, properties):
|
|
| 44 | + def _iq_error_received(self,
|
|
| 45 | + _con: types.xmppClient,
|
|
| 46 | + _stanza: Iq,
|
|
| 47 | + properties: IqProperties
|
|
| 48 | + ) -> None:
|
|
| 40 | 49 | self._log.info('Error: %s', properties.error)
|
| 41 | 50 | if properties.error.condition in ('jid-malformed',
|
| 42 | 51 | 'forbidden',
|
| ... | ... | @@ -78,7 +87,7 @@ def _iq_error_received(self, _con, _stanza, properties): |
| 78 | 87 | raise nbxmpp.NodeProcessed
|
| 79 | 88 | |
| 80 | 89 | @staticmethod
|
| 81 | - def _get_sid(id_):
|
|
| 90 | + def _get_sid(id_: str) -> str:
|
|
| 82 | 91 | sid = id_
|
| 83 | 92 | if len(id_) > 3 and id_[2] == '_':
|
| 84 | 93 | sid = id_[3:]
|
| ... | ... | @@ -14,10 +14,19 @@ |
| 14 | 14 | |
| 15 | 15 | # XEP-0084: User Avatar
|
| 16 | 16 | |
| 17 | +from __future__ import annotations
|
|
| 18 | + |
|
| 19 | +from typing import Generator
|
|
| 20 | +from typing import Optional
|
|
| 21 | + |
|
| 17 | 22 | from nbxmpp.namespaces import Namespace
|
| 23 | +from nbxmpp.modules.user_avatar import AvatarData
|
|
| 18 | 24 | from nbxmpp.modules.util import is_error
|
| 25 | +from nbxmpp.protocol import Message
|
|
| 26 | +from nbxmpp.structs import MessageProperties
|
|
| 19 | 27 | |
| 20 | 28 | from gajim.common import app
|
| 29 | +from gajim.common import types
|
|
| 21 | 30 | from gajim.common.modules.base import BaseModule
|
| 22 | 31 | from gajim.common.modules.util import event_node
|
| 23 | 32 | from gajim.common.modules.util import as_task
|
| ... | ... | @@ -32,12 +41,16 @@ class UserAvatar(BaseModule): |
| 32 | 41 | 'set_access_model'
|
| 33 | 42 | ]
|
| 34 | 43 | |
| 35 | - def __init__(self, con):
|
|
| 44 | + def __init__(self, con: types.Client) -> None:
|
|
| 36 | 45 | BaseModule.__init__(self, con)
|
| 37 | 46 | self._register_pubsub_handler(self._avatar_metadata_received)
|
| 38 | 47 | |
| 39 | 48 | @event_node(Namespace.AVATAR_METADATA)
|
| 40 | - def _avatar_metadata_received(self, _con, _stanza, properties):
|
|
| 49 | + def _avatar_metadata_received(self,
|
|
| 50 | + _con: types.xmppClient,
|
|
| 51 | + _stanza: Message,
|
|
| 52 | + properties: MessageProperties
|
|
| 53 | + ) -> None:
|
|
| 41 | 54 | if properties.pubsub_event.retracted:
|
| 42 | 55 | return
|
| 43 | 56 | |
| ... | ... | @@ -65,7 +78,10 @@ def _avatar_metadata_received(self, _con, _stanza, properties): |
| 65 | 78 | self._request_avatar_data(contact, metadata.default)
|
| 66 | 79 | |
| 67 | 80 | @as_task
|
| 68 | - def _request_avatar_data(self, contact, sha):
|
|
| 81 | + def _request_avatar_data(self,
|
|
| 82 | + contact: types.ChatContactT,
|
|
| 83 | + sha: str
|
|
| 84 | + ) -> Generator[Optional[AvatarData], None, None]:
|
|
| 69 | 85 | _task = yield
|
| 70 | 86 | |
| 71 | 87 | avatar = yield self._nbxmpp('UserAvatar').request_avatar_data(
|
| ... | ... | @@ -14,15 +14,23 @@ |
| 14 | 14 | |
| 15 | 15 | # Util module
|
| 16 | 16 | |
| 17 | +from __future__ import annotations
|
|
| 18 | + |
|
| 19 | +from typing import Any
|
|
| 17 | 20 | from typing import Union
|
| 18 | 21 | |
| 19 | 22 | from logging import LoggerAdapter
|
| 20 | 23 | from functools import wraps
|
| 21 | 24 | from functools import partial
|
| 22 | 25 | |
| 26 | +import nbxmpp
|
|
| 27 | +from nbxmpp.protocol import Message
|
|
| 28 | +from nbxmpp.structs import EMEData
|
|
| 29 | +from nbxmpp.structs import MessageProperties
|
|
| 23 | 30 | from nbxmpp.task import Task
|
| 24 | 31 | |
| 25 | 32 | from gajim.common import app
|
| 33 | +from gajim.common import types
|
|
| 26 | 34 | from gajim.common.const import EME_MESSAGES
|
| 27 | 35 | |
| 28 | 36 | |
| ... | ... | @@ -36,7 +44,7 @@ def from_xs_boolean(value: Union[str, bool]) -> bool: |
| 36 | 44 | if value in ('0', 'false', 'False', ''):
|
| 37 | 45 | return False
|
| 38 | 46 | |
| 39 | - raise ValueError('Cant convert %s to python boolean' % value)
|
|
| 47 | + raise ValueError(f'Cant convert {value} to python boolean')
|
|
| 40 | 48 | |
| 41 | 49 | |
| 42 | 50 | def to_xs_boolean(value: Union[bool, None]) -> str:
|
| ... | ... | @@ -51,14 +59,17 @@ def to_xs_boolean(value: Union[bool, None]) -> str: |
| 51 | 59 | if value is None:
|
| 52 | 60 | return 'false'
|
| 53 | 61 | |
| 54 | - raise ValueError(
|
|
| 55 | - 'Cant convert %s to xs:boolean' % value)
|
|
| 62 | + raise ValueError(f'Cant convert {value} to xs:boolean')
|
|
| 56 | 63 | |
| 57 | 64 | |
| 58 | -def event_node(node):
|
|
| 59 | - def event_node_decorator(func):
|
|
| 65 | +def event_node(node: nbxmpp.Node) -> Any:
|
|
| 66 | + def event_node_decorator(func: Any):
|
|
| 60 | 67 | @wraps(func)
|
| 61 | - def func_wrapper(self, _con, _stanza, properties):
|
|
| 68 | + def func_wrapper(self: Any,
|
|
| 69 | + _con: types.xmppClient,
|
|
| 70 | + _stanza: Message,
|
|
| 71 | + properties: MessageProperties
|
|
| 72 | + ) -> Any:
|
|
| 62 | 73 | if not properties.is_pubsub_event:
|
| 63 | 74 | return
|
| 64 | 75 | if properties.pubsub_event.node != node:
|
| ... | ... | @@ -69,9 +80,9 @@ def func_wrapper(self, _con, _stanza, properties): |
| 69 | 80 | return event_node_decorator
|
| 70 | 81 | |
| 71 | 82 | |
| 72 | -def store_publish(func):
|
|
| 83 | +def store_publish(func: Any):
|
|
| 73 | 84 | @wraps(func)
|
| 74 | - def func_wrapper(self, *args, **kwargs):
|
|
| 85 | + def func_wrapper(self: Any, *args: Any, **kwargs: Any):
|
|
| 75 | 86 | # pylint: disable=protected-access
|
| 76 | 87 | if not app.account_is_connected(self._account):
|
| 77 | 88 | self._stored_publish = partial(func, self, *args, **kwargs)
|
| ... | ... | @@ -80,7 +91,7 @@ def func_wrapper(self, *args, **kwargs): |
| 80 | 91 | return func_wrapper
|
| 81 | 92 | |
| 82 | 93 | |
| 83 | -def get_eme_message(eme_data):
|
|
| 94 | +def get_eme_message(eme_data: EMEData) -> str:
|
|
| 84 | 95 | try:
|
| 85 | 96 | return EME_MESSAGES[eme_data.namespace]
|
| 86 | 97 | except KeyError:
|
| ... | ... | @@ -88,8 +99,8 @@ def get_eme_message(eme_data): |
| 88 | 99 | |
| 89 | 100 | |
| 90 | 101 | class LogAdapter(LoggerAdapter):
|
| 91 | - def process(self, msg, kwargs):
|
|
| 92 | - return '(%s) %s' % (self.extra['account'], msg), kwargs
|
|
| 102 | + def process(self, msg: str, kwargs: Any) -> tuple[str, Any]:
|
|
| 103 | + return f'({self.extra["account"]}) {msg}', kwargs
|
|
| 93 | 104 | |
| 94 | 105 | |
| 95 | 106 | def as_task(func):
|