mypy: Make multiuserchat.py pass --strict

This commit is contained in:
mathieui 2020-05-28 18:49:22 +02:00
parent 6425daae1e
commit 927f3843ad

View file

@ -11,18 +11,39 @@ slix plugin
""" """
from xml.etree import ElementTree as ET from xml.etree import ElementTree as ET
from typing import (
Callable,
Optional,
TYPE_CHECKING,
)
from poezio.common import safeJID from poezio.common import safeJID
from slixmpp import JID from slixmpp import (
from slixmpp.exceptions import IqError, IqTimeout JID,
ClientXMPP,
Iq,
)
import logging import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
if TYPE_CHECKING:
from poezio.core import Core
from poezio.tabs import Tab
from slixmpp.plugins.xep_0004 import Form
NS_MUC_ADMIN = 'http://jabber.org/protocol/muc#admin' NS_MUC_ADMIN = 'http://jabber.org/protocol/muc#admin'
NS_MUC_OWNER = 'http://jabber.org/protocol/muc#owner' NS_MUC_OWNER = 'http://jabber.org/protocol/muc#owner'
def destroy_room(xmpp, room, reason='', altroom=''): def destroy_room(
xmpp: ClientXMPP,
room: str,
reason: str = '',
altroom: str = ''
) -> bool:
""" """
destroy a room destroy a room
""" """
@ -42,7 +63,7 @@ def destroy_room(xmpp, room, reason='', altroom=''):
query.append(destroy) query.append(destroy)
iq.append(query) iq.append(query)
def callback(iq): def callback(iq: Iq) -> None:
if not iq or iq['type'] == 'error': if not iq or iq['type'] == 'error':
xmpp.core.information('Unable to destroy room %s' % room, 'Info') xmpp.core.information('Unable to destroy room %s' % room, 'Info')
else: else:
@ -52,23 +73,13 @@ def destroy_room(xmpp, room, reason='', altroom=''):
return True return True
def send_private_message(xmpp, jid, line): def change_show(
""" xmpp: ClientXMPP,
Send a private message jid: JID,
""" own_nick: str,
jid = safeJID(jid) show: str,
xmpp.send_message(mto=jid, mbody=line, mtype='chat') status: Optional[str]
) -> None:
def send_groupchat_message(xmpp, jid, line):
"""
Send a message to the groupchat
"""
jid = safeJID(jid)
xmpp.send_message(mto=jid, mbody=line, mtype='groupchat')
def change_show(xmpp, jid: JID, own_nick: str, show, status):
""" """
Change our 'Show' Change our 'Show'
""" """
@ -81,7 +92,7 @@ def change_show(xmpp, jid: JID, own_nick: str, show, status):
pres.send() pres.send()
def change_subject(xmpp, jid, subject): def change_subject(xmpp: ClientXMPP, jid: JID, subject: str) -> None:
""" """
Change the room subject Change the room subject
""" """
@ -92,7 +103,13 @@ def change_subject(xmpp, jid, subject):
msg.send() msg.send()
def change_nick(core, jid, nick, status=None, show=None): def change_nick(
core: 'Core',
jid: JID,
nick: str,
status: Optional[str] = None,
show: Optional[str] = None
) -> None:
""" """
Change our own nick in a room Change our own nick in a room
""" """
@ -103,14 +120,16 @@ def change_nick(core, jid, nick, status=None, show=None):
presence.send() presence.send()
def join_groupchat(core, def join_groupchat(
jid, core: 'Core',
nick, jid: JID,
passwd='', nick: str,
status=None, passwd: str = '',
show=None, status: Optional[str] = None,
seconds=None, show: Optional[str] = None,
tab=None): seconds: Optional[int] = None,
tab: Optional['Tab'] = None
) -> None:
xmpp = core.xmpp xmpp = core.xmpp
stanza = xmpp.make_presence( stanza = xmpp.make_presence(
pto='%s/%s' % (jid, nick), pstatus=status, pshow=show) pto='%s/%s' % (jid, nick), pstatus=status, pshow=show)
@ -119,8 +138,10 @@ def join_groupchat(core,
passelement = ET.Element('password') passelement = ET.Element('password')
passelement.text = passwd passelement.text = passwd
x.append(passelement) x.append(passelement)
def on_disco(iq):
if 'urn:xmpp:mam:2' in iq['disco_info'].get_features() or (tab and tab._text_buffer.last_message): def on_disco(iq: Iq) -> None:
if ('urn:xmpp:mam:2' in iq['disco_info'].get_features()
or (tab and tab._text_buffer.last_message)):
history = ET.Element('{http://jabber.org/protocol/muc}history') history = ET.Element('{http://jabber.org/protocol/muc}history')
history.attrib['seconds'] = str(0) history.attrib['seconds'] = str(0)
x.append(history) x.append(history)
@ -136,13 +157,15 @@ def join_groupchat(core,
xmpp.plugin['xep_0045'].rooms[jid] = {} xmpp.plugin['xep_0045'].rooms[jid] = {}
xmpp.plugin['xep_0045'].our_nicks[jid] = to.resource xmpp.plugin['xep_0045'].our_nicks[jid] = to.resource
try: xmpp.plugin['xep_0030'].get_info(jid=jid, callback=on_disco)
xmpp.plugin['xep_0030'].get_info(jid=jid, callback=on_disco)
except (IqError, IqTimeout):
return core.information('Failed to retrieve messages', 'Error')
def leave_groupchat(xmpp, jid, own_nick, msg): def leave_groupchat(
xmpp: ClientXMPP,
jid: JID,
own_nick: str,
msg: str
) -> None:
""" """
Leave the groupchat Leave the groupchat
""" """
@ -156,7 +179,14 @@ def leave_groupchat(xmpp, jid, own_nick, msg):
exc_info=True) exc_info=True)
def set_user_role(xmpp, jid, nick, reason, role, callback=None): def set_user_role(
xmpp: ClientXMPP,
jid: JID,
nick: str,
reason: str,
role: str,
callback: Callable[[Iq], None]
) -> None:
""" """
(try to) Set the role of a MUC user (try to) Set the role of a MUC user
(role = 'none': eject user) (role = 'none': eject user)
@ -172,21 +202,18 @@ def set_user_role(xmpp, jid, nick, reason, role, callback=None):
query.append(item) query.append(item)
iq.append(query) iq.append(query)
iq['to'] = jid iq['to'] = jid
if callback: iq.send(callback=callback)
return iq.send(callback=callback)
try:
return iq.send()
except (IqError, IqTimeout) as e:
return e.iq
def set_user_affiliation(xmpp, def set_user_affiliation(
muc_jid, xmpp: ClientXMPP,
affiliation, muc_jid: JID,
nick=None, affiliation: str,
jid=None, callback: Callable[[Iq], None],
reason=None, nick: Optional[str] = None,
callback=None): jid: Optional[JID] = None,
reason: Optional[str] = None
) -> None:
""" """
(try to) Set the affiliation of a MUC user (try to) Set the affiliation of a MUC user
""" """
@ -212,18 +239,10 @@ def set_user_affiliation(xmpp,
query.append(item) query.append(item)
iq = xmpp.make_iq_set(query) iq = xmpp.make_iq_set(query)
iq['to'] = muc_jid iq['to'] = muc_jid
if callback: iq.send(callback=callback)
return iq.send(callback=callback)
try:
return xmpp.plugin['xep_0045'].set_affiliation(
str(muc_jid),
str(jid) if jid else None, nick, affiliation)
except:
log.debug('Error setting the affiliation: %s', exc_info=True)
return False
def cancel_config(xmpp, room): def cancel_config(xmpp: ClientXMPP, room: str) -> None:
query = ET.Element('{http://jabber.org/protocol/muc#owner}query') query = ET.Element('{http://jabber.org/protocol/muc#owner}query')
x = ET.Element('{jabber:x:data}x', type='cancel') x = ET.Element('{jabber:x:data}x', type='cancel')
query.append(x) query.append(x)
@ -232,7 +251,7 @@ def cancel_config(xmpp, room):
iq.send() iq.send()
def configure_room(xmpp, room, form): def configure_room(xmpp: ClientXMPP, room: str, form: 'Form') -> None:
if form is None: if form is None:
return return
iq = xmpp.make_iq_set() iq = xmpp.make_iq_set()