stanza: add typing

This commit is contained in:
mathieui 2021-07-03 11:09:41 +02:00
parent 95b034797f
commit 7d4172808f
3 changed files with 18 additions and 17 deletions

View file

@ -1,8 +1,9 @@
# Slixmpp: The Slick XMPP Library # Slixmpp: The Slick XMPP Library
# Copyright (C) 2010 Nathanael C. Fritz # Copyright (C) 2010 Nathanael C. Fritz
# This file is part of Slixmpp. # This file is part of Slixmpp.
# See the file LICENSE for copying permission. # See the file LICENSE for copying permission.
from __future__ import annotations
from typing import Optional
from slixmpp.xmlstream import ElementBase, ET from slixmpp.xmlstream import ElementBase, ET
@ -62,10 +63,10 @@ class Error(ElementBase):
'remote-server-timeout', 'resource-constraint', 'remote-server-timeout', 'resource-constraint',
'service-unavailable', 'subscription-required', 'service-unavailable', 'subscription-required',
'undefined-condition', 'unexpected-request'} 'undefined-condition', 'unexpected-request'}
condition_ns = 'urn:ietf:params:xml:ns:xmpp-stanzas' condition_ns: str = 'urn:ietf:params:xml:ns:xmpp-stanzas'
types = {'cancel', 'continue', 'modify', 'auth', 'wait'} types = {'cancel', 'continue', 'modify', 'auth', 'wait'}
def setup(self, xml=None): def setup(self, xml: Optional[ET.Element]=None):
""" """
Populate the stanza object using an optional XML object. Populate the stanza object using an optional XML object.
@ -84,7 +85,7 @@ class Error(ElementBase):
if self.parent is not None: if self.parent is not None:
self.parent()['type'] = 'error' self.parent()['type'] = 'error'
def get_condition(self): def get_condition(self) -> str:
"""Return the condition element's name.""" """Return the condition element's name."""
for child in self.xml: for child in self.xml:
if "{%s}" % self.condition_ns in child.tag: if "{%s}" % self.condition_ns in child.tag:
@ -93,7 +94,7 @@ class Error(ElementBase):
return cond return cond
return '' return ''
def set_condition(self, value): def set_condition(self, value: str) -> Error:
""" """
Set the tag name of the condition element. Set the tag name of the condition element.
@ -105,7 +106,7 @@ class Error(ElementBase):
self.xml.append(ET.Element("{%s}%s" % (self.condition_ns, value))) self.xml.append(ET.Element("{%s}%s" % (self.condition_ns, value)))
return self return self
def del_condition(self): def del_condition(self) -> None:
"""Remove the condition element.""" """Remove the condition element."""
for child in self.xml: for child in self.xml:
if "{%s}" % self.condition_ns in child.tag: if "{%s}" % self.condition_ns in child.tag:
@ -139,14 +140,14 @@ class Error(ElementBase):
def get_redirect(self): def get_redirect(self):
return self._get_sub_text('{%s}redirect' % self.condition_ns, '') return self._get_sub_text('{%s}redirect' % self.condition_ns, '')
def set_gone(self, value): def set_gone(self, value: str):
if value: if value:
del self['condition'] del self['condition']
return self._set_sub_text('{%s}gone' % self.condition_ns, value) return self._set_sub_text('{%s}gone' % self.condition_ns, value)
elif self['condition'] == 'gone': elif self['condition'] == 'gone':
del self['condition'] del self['condition']
def set_redirect(self, value): def set_redirect(self, value: str):
if value: if value:
del self['condition'] del self['condition']
ns = self.condition_ns ns = self.condition_ns

View file

@ -61,7 +61,7 @@ class Presence(RootStanza):
'subscribed', 'unsubscribe', 'unsubscribed'} 'subscribed', 'unsubscribe', 'unsubscribed'}
showtypes = {'dnd', 'chat', 'xa', 'away'} showtypes = {'dnd', 'chat', 'xa', 'away'}
def __init__(self, *args, recv=False, **kwargs): def __init__(self, *args, recv: bool = False, **kwargs):
""" """
Initialize a new <presence /> stanza with an optional 'id' value. Initialize a new <presence /> stanza with an optional 'id' value.
@ -72,7 +72,7 @@ class Presence(RootStanza):
if self.stream is not None and self.stream.use_presence_ids: if self.stream is not None and self.stream.use_presence_ids:
self['id'] = self.stream.new_id() self['id'] = self.stream.new_id()
def set_show(self, show): def set_show(self, show: str):
""" """
Set the value of the <show> element. Set the value of the <show> element.
@ -84,7 +84,7 @@ class Presence(RootStanza):
self._set_sub_text('show', text=show) self._set_sub_text('show', text=show)
return self return self
def get_type(self): def get_type(self) -> str:
""" """
Return the value of the <presence> stanza's type attribute, or Return the value of the <presence> stanza's type attribute, or
the value of the <show> element if valid. the value of the <show> element if valid.
@ -96,7 +96,7 @@ class Presence(RootStanza):
out = 'available' out = 'available'
return out return out
def set_type(self, value): def set_type(self, value: str):
""" """
Set the type attribute's value, and the <show> element Set the type attribute's value, and the <show> element
if applicable. if applicable.
@ -119,7 +119,7 @@ class Presence(RootStanza):
self._del_attr('type') self._del_attr('type')
self._del_sub('show') self._del_sub('show')
def set_priority(self, value): def set_priority(self, value: int):
""" """
Set the entity's priority value. Some server use priority to Set the entity's priority value. Some server use priority to
determine message routing behavior. determine message routing behavior.

View file

@ -62,13 +62,13 @@ class StreamError(Error, StanzaBase):
'system-shutdown', 'undefined-condition', 'unsupported-encoding', 'system-shutdown', 'undefined-condition', 'unsupported-encoding',
'unsupported-feature', 'unsupported-stanza-type', 'unsupported-feature', 'unsupported-stanza-type',
'unsupported-version'} 'unsupported-version'}
condition_ns = 'urn:ietf:params:xml:ns:xmpp-streams' condition_ns: str = 'urn:ietf:params:xml:ns:xmpp-streams'
def get_see_other_host(self): def get_see_other_host(self) -> str:
ns = self.condition_ns ns = self.condition_ns
return self._get_sub_text('{%s}see-other-host' % ns, '') return self._get_sub_text('{%s}see-other-host' % ns, '')
def set_see_other_host(self, value): def set_see_other_host(self, value: str) -> None:
if value: if value:
del self['condition'] del self['condition']
ns = self.condition_ns ns = self.condition_ns
@ -76,5 +76,5 @@ class StreamError(Error, StanzaBase):
elif self['condition'] == 'see-other-host': elif self['condition'] == 'see-other-host':
del self['condition'] del self['condition']
def del_see_other_host(self): def del_see_other_host(self) -> None:
self._del_sub('{%s}see-other-host' % self.condition_ns) self._del_sub('{%s}see-other-host' % self.condition_ns)