Daniel Brötzmann pushed to branch master at gajim / gajim
Commits: 20b33a7e by wurstsalat at 2022-05-28T02:04:03+02:00 chore: Gateway: Add type annotations
- - - - - 1987c330 by wurstsalat at 2022-05-28T02:05:41+02:00 chore: Iq: Add type annotations
- - - - - b7ab4045 by wurstsalat at 2022-05-28T02:05:58+02:00 chore: UserAvatar: Add type annotations
- - - - - 16dc125b by wurstsalat at 2022-05-28T02:06:11+02:00 chore: Util: Add type annotations
- - - - -
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:
===================================== gajim/common/modules/gateway.py ===================================== @@ -14,20 +14,26 @@
# XEP-0100: Gateway Interaction
+from __future__ import annotations + +from typing import Optional + import nbxmpp +from nbxmpp.protocol import Iq from nbxmpp.namespaces import Namespace
from gajim.common import app +from gajim.common import types from gajim.common.events import AgentRemoved from gajim.common.events import GatewayPromptReceived from gajim.common.modules.base import BaseModule
class Gateway(BaseModule): - def __init__(self, con): + def __init__(self, con: types.Client) -> None: BaseModule.__init__(self, con)
- def unsubscribe(self, agent): + def unsubscribe(self, agent: str) -> None: if not app.account_is_available(self._account): return iq = nbxmpp.Iq('set', Namespace.REGISTER, to=agent) @@ -37,7 +43,10 @@ def unsubscribe(self, agent): iq, self._on_unsubscribe_result) self._con.get_module('Roster').delete_item(agent)
- def _on_unsubscribe_result(self, _nbxmpp_client, stanza): + def _on_unsubscribe_result(self, + _nbxmpp_client: types.xmppClient, + stanza: nbxmpp.Node + ) -> None: if not nbxmpp.isResultNode(stanza): self._log.info('Error: %s', stanza.getError()) return @@ -63,7 +72,10 @@ def _on_unsubscribe_result(self, _nbxmpp_client, stanza): agent=agent, jid_list=jid_list))
- def request_gateway_prompt(self, jid, prompt=None): + def request_gateway_prompt(self, + jid: str, + prompt: Optional[str] = None + ) -> None: typ_ = 'get' if prompt: typ_ = 'set' @@ -73,7 +85,10 @@ def request_gateway_prompt(self, jid, prompt=None): query.setTagData('prompt', prompt) self._con.connection.SendAndCallForResponse(iq, self._on_prompt_result)
- def _on_prompt_result(self, _nbxmpp_client, stanza): + def _on_prompt_result(self, + _nbxmpp_client: types.xmppClient, + stanza: Iq + ) -> None: jid = str(stanza.getFrom()) fjid = stanza.getFrom().bare resource = stanza.getFrom().resource
===================================== gajim/common/modules/iq.py ===================================== @@ -14,10 +14,15 @@
# Iq handler
+from __future__ import annotations + import nbxmpp +from nbxmpp.protocol import Iq +from nbxmpp.structs import IqProperties from nbxmpp.structs import StanzaHandler
from gajim.common import app +from gajim.common import types from gajim.common.events import FileRequestError from gajim.common.events import FileSendError from gajim.common.helpers import to_user_string @@ -26,7 +31,7 @@
class Iq(BaseModule): - def __init__(self, con): + def __init__(self, con: types.Client) -> None: BaseModule.__init__(self, con)
self.handlers = [ @@ -36,7 +41,11 @@ def __init__(self, con): priority=51), ]
- def _iq_error_received(self, _con, _stanza, properties): + def _iq_error_received(self, + _con: types.xmppClient, + _stanza: Iq, + properties: IqProperties + ) -> None: self._log.info('Error: %s', properties.error) if properties.error.condition in ('jid-malformed', 'forbidden', @@ -78,7 +87,7 @@ def _iq_error_received(self, _con, _stanza, properties): raise nbxmpp.NodeProcessed
@staticmethod - def _get_sid(id_): + def _get_sid(id_: str) -> str: sid = id_ if len(id_) > 3 and id_[2] == '_': sid = id_[3:]
===================================== gajim/common/modules/user_avatar.py ===================================== @@ -14,10 +14,19 @@
# XEP-0084: User Avatar
+from __future__ import annotations + +from typing import Generator +from typing import Optional + from nbxmpp.namespaces import Namespace +from nbxmpp.modules.user_avatar import AvatarData from nbxmpp.modules.util import is_error +from nbxmpp.protocol import Message +from nbxmpp.structs import MessageProperties
from gajim.common import app +from gajim.common import types from gajim.common.modules.base import BaseModule from gajim.common.modules.util import event_node from gajim.common.modules.util import as_task @@ -32,12 +41,16 @@ class UserAvatar(BaseModule): 'set_access_model' ]
- def __init__(self, con): + def __init__(self, con: types.Client) -> None: BaseModule.__init__(self, con) self._register_pubsub_handler(self._avatar_metadata_received)
@event_node(Namespace.AVATAR_METADATA) - def _avatar_metadata_received(self, _con, _stanza, properties): + def _avatar_metadata_received(self, + _con: types.xmppClient, + _stanza: Message, + properties: MessageProperties + ) -> None: if properties.pubsub_event.retracted: return
@@ -65,7 +78,10 @@ def _avatar_metadata_received(self, _con, _stanza, properties): self._request_avatar_data(contact, metadata.default)
@as_task - def _request_avatar_data(self, contact, sha): + def _request_avatar_data(self, + contact: types.ChatContactT, + sha: str + ) -> Generator[Optional[AvatarData], None, None]: _task = yield
avatar = yield self._nbxmpp('UserAvatar').request_avatar_data(
===================================== gajim/common/modules/util.py ===================================== @@ -14,15 +14,23 @@
# Util module
+from __future__ import annotations + +from typing import Any from typing import Union
from logging import LoggerAdapter from functools import wraps from functools import partial
+import nbxmpp +from nbxmpp.protocol import Message +from nbxmpp.structs import EMEData +from nbxmpp.structs import MessageProperties from nbxmpp.task import Task
from gajim.common import app +from gajim.common import types from gajim.common.const import EME_MESSAGES
@@ -36,7 +44,7 @@ def from_xs_boolean(value: Union[str, bool]) -> bool: if value in ('0', 'false', 'False', ''): return False
- raise ValueError('Cant convert %s to python boolean' % value) + raise ValueError(f'Cant convert {value} to python boolean')
def to_xs_boolean(value: Union[bool, None]) -> str: @@ -51,14 +59,17 @@ def to_xs_boolean(value: Union[bool, None]) -> str: if value is None: return 'false'
- raise ValueError( - 'Cant convert %s to xs:boolean' % value) + raise ValueError(f'Cant convert {value} to xs:boolean')
-def event_node(node): - def event_node_decorator(func): +def event_node(node: nbxmpp.Node) -> Any: + def event_node_decorator(func: Any): @wraps(func) - def func_wrapper(self, _con, _stanza, properties): + def func_wrapper(self: Any, + _con: types.xmppClient, + _stanza: Message, + properties: MessageProperties + ) -> Any: if not properties.is_pubsub_event: return if properties.pubsub_event.node != node: @@ -69,9 +80,9 @@ def func_wrapper(self, _con, _stanza, properties): return event_node_decorator
-def store_publish(func): +def store_publish(func: Any): @wraps(func) - def func_wrapper(self, *args, **kwargs): + def func_wrapper(self: Any, *args: Any, **kwargs: Any): # pylint: disable=protected-access if not app.account_is_connected(self._account): self._stored_publish = partial(func, self, *args, **kwargs) @@ -80,7 +91,7 @@ def func_wrapper(self, *args, **kwargs): return func_wrapper
-def get_eme_message(eme_data): +def get_eme_message(eme_data: EMEData) -> str: try: return EME_MESSAGES[eme_data.namespace] except KeyError: @@ -88,8 +99,8 @@ def get_eme_message(eme_data):
class LogAdapter(LoggerAdapter): - def process(self, msg, kwargs): - return '(%s) %s' % (self.extra['account'], msg), kwargs + def process(self, msg: str, kwargs: Any) -> tuple[str, Any]: + return f'({self.extra["account"]}) {msg}', kwargs
def as_task(func):
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/compare/f79f7c7f2347f280ad8f8e6a50c7bde6...