summaryrefslogtreecommitdiff
path: root/slixmpp/plugins/xep_0030/stanza/info.py
diff options
context:
space:
mode:
Diffstat (limited to 'slixmpp/plugins/xep_0030/stanza/info.py')
-rw-r--r--slixmpp/plugins/xep_0030/stanza/info.py74
1 files changed, 44 insertions, 30 deletions
diff --git a/slixmpp/plugins/xep_0030/stanza/info.py b/slixmpp/plugins/xep_0030/stanza/info.py
index a77917e9..367afdad 100644
--- a/slixmpp/plugins/xep_0030/stanza/info.py
+++ b/slixmpp/plugins/xep_0030/stanza/info.py
@@ -1,10 +1,19 @@
-
# Slixmpp: The Slick XMPP Library
# Copyright (C) 2010 Nathanael C. Fritz, Lance J.T. Stout
# This file is part of Slixmpp.
# See the file LICENSE for copying permission.
+from typing import (
+ Iterable,
+ List,
+ Optional,
+ Set,
+ Tuple,
+ Union,
+)
from slixmpp.xmlstream import ElementBase, ET
+IdentityType = Tuple[str, str, Optional[str], Optional[str]]
+
class DiscoInfo(ElementBase):
@@ -18,21 +27,23 @@ class DiscoInfo(ElementBase):
category with a type of 'pc' to indicate the agent is a human operated
client with a GUI, or a category of 'gateway' with a type of 'aim' to
identify the agent as a gateway for the legacy AIM protocol. See
- <http://xmpp.org/registrar/disco-categories.html> for a full list of
+ `XMPP Registrar Disco Categories`_ for a full list of
accepted category and type combinations.
+ .. _XMPP Registrar Disco Categories: <http://xmpp.org/registrar/disco-categories.html>
+
Features are simply a set of the namespaces that identify the supported
features. For example, a client that supports service discovery will
- include the feature 'http://jabber.org/protocol/disco#info'.
+ include the feature ``http://jabber.org/protocol/disco#info``.
Since clients and components may operate in several roles at once, identity
and feature information may be grouped into "nodes". If one were to write
all of the identities and features used by a client, then node names would
be like section headings.
- Example disco#info stanzas:
+ Example disco#info stanza:
- ::
+ .. code-block:: xml
<iq type="get">
<query xmlns="http://jabber.org/protocol/disco#info" />
@@ -46,30 +57,26 @@ class DiscoInfo(ElementBase):
<feature var="urn:xmpp:ping" />
</query>
</iq>
-
- Stanza Interface:
- ::
-
- node -- The name of the node to either
- query or return info from.
- identities -- A set of 4-tuples, where each tuple contains
- the category, type, xml:lang, and name
- of an identity.
- features -- A set of namespaces for features.
-
"""
name = 'query'
namespace = 'http://jabber.org/protocol/disco#info'
plugin_attrib = 'disco_info'
+ #: Stanza interfaces:
+ #:
+ #: - ``node``: The name of the node to either query or return the info from
+ #: - ``identities``: A set of 4-tuples, where each tuple contains the
+ #: category, type, xml:lang and name of an identity
+ #: - ``features``: A set of namespaces for features
+ #:
interfaces = {'node', 'features', 'identities'}
lang_interfaces = {'identities'}
# Cache identities and features
- _identities = set()
- _features = set()
+ _identities: Set[Tuple[str, str, Optional[str]]]
+ _features: Set[str]
- def setup(self, xml=None):
+ def setup(self, xml: Optional[ET.ElementTree] = None):
"""
Populate the stanza object using an optional XML object.
@@ -84,7 +91,9 @@ class DiscoInfo(ElementBase):
self._identities = {id[0:3] for id in self['identities']}
self._features = self['features']
- def add_identity(self, category, itype, name=None, lang=None):
+ def add_identity(self, category: str, itype: str,
+ name: Optional[str] = None, lang: Optional[str] = None
+ ) -> bool:
"""
Add a new identity element. Each identity must be unique
in terms of all four identity components.
@@ -113,7 +122,8 @@ class DiscoInfo(ElementBase):
return True
return False
- def del_identity(self, category, itype, name=None, lang=None):
+ def del_identity(self, category: str, itype: str, name=None,
+ lang: Optional[str] = None) -> bool:
"""
Remove a given identity.
@@ -134,7 +144,8 @@ class DiscoInfo(ElementBase):
return True
return False
- def get_identities(self, lang=None, dedupe=True):
+ def get_identities(self, lang: Optional[str] = None, dedupe: bool = True
+ ) -> Iterable[IdentityType]:
"""
Return a set of all identities in tuple form as so:
@@ -147,6 +158,7 @@ class DiscoInfo(ElementBase):
:param dedupe: If True, de-duplicate identities, otherwise
return a list of all identities.
"""
+ identities: Union[List[IdentityType], Set[IdentityType]]
if dedupe:
identities = set()
else:
@@ -158,13 +170,14 @@ class DiscoInfo(ElementBase):
id_xml.attrib['type'],
id_xml.attrib.get('{%s}lang' % self.xml_ns, None),
id_xml.attrib.get('name', None))
- if dedupe:
+ if isinstance(identities, set):
identities.add(id)
else:
identities.append(id)
return identities
- def set_identities(self, identities, lang=None):
+ def set_identities(self, identities: Iterable[IdentityType],
+ lang: Optional[str] = None):
"""
Add or replace all identities. The identities must be a in set
where each identity is a tuple of the form:
@@ -187,7 +200,7 @@ class DiscoInfo(ElementBase):
category, itype, lang, name = identity
self.add_identity(category, itype, name, lang)
- def del_identities(self, lang=None):
+ def del_identities(self, lang: Optional[str] = None):
"""
Remove all identities. If a language was specified, only
remove identities using that language.
@@ -204,7 +217,7 @@ class DiscoInfo(ElementBase):
id_xml.attrib.get('{%s}lang' % self.xml_ns, None)))
self.xml.remove(id_xml)
- def add_feature(self, feature):
+ def add_feature(self, feature: str) -> bool:
"""
Add a single, new feature.
@@ -218,7 +231,7 @@ class DiscoInfo(ElementBase):
return True
return False
- def del_feature(self, feature):
+ def del_feature(self, feature: str) -> bool:
"""
Remove a single feature.
@@ -232,20 +245,21 @@ class DiscoInfo(ElementBase):
return True
return False
- def get_features(self, dedupe=True):
+ def get_features(self, dedupe: bool = True) -> Iterable[str]:
"""Return the set of all supported features."""
+ features: Union[List[str], Set[str]]
if dedupe:
features = set()
else:
features = []
for feature_xml in self.xml.findall('{%s}feature' % self.namespace):
- if dedupe:
+ if isinstance(features, set):
features.add(feature_xml.attrib['var'])
else:
features.append(feature_xml.attrib['var'])
return features
- def set_features(self, features):
+ def set_features(self, features: Iterable[str]):
"""
Add or replace the set of supported features.