Philipp Hörist pushed to branch master at gajim / python-nbxmpp
Commits: 0c33075e by Philipp Hörist at 2019-08-15T17:43:16Z DiscoInfo: Add stanza attribute
Make stanza object available in DiscoInfo and derive some propertys from it
- - - - - 18c9fd5a by Philipp Hörist at 2019-08-16T15:39:56Z DiscoInfo: Add timestamp attribute
- - - - - 2f4a79e0 by Philipp Hörist at 2019-08-16T15:40:04Z DiscoInfo: Add serialize() method
- - - - - d56945cc by Philipp Hörist at 2019-08-17T10:46:28Z Use error_factory() everywhere
- - - - -
6 changed files:
- nbxmpp/const.py - nbxmpp/modules/discovery.py - nbxmpp/modules/iq.py - nbxmpp/modules/message.py - nbxmpp/modules/presence.py - nbxmpp/structs.py
Changes:
===================================== nbxmpp/const.py ===================================== @@ -255,36 +255,6 @@ class Role(Enum): return self == Role.NONE
-class Error(Enum): - BAD_REQUEST = 'bad-request' - CONFLICT = 'conflict' - FEATURE_NOT_IMPLEMENTED = 'feature-not-implemented' - FORBIDDEN = 'forbidden' - GONE = 'gone' - INTERNAL_SERVER_ERROR = 'internal-server-error' - ITEM_NOT_FOUND = 'item-not-found' - JID_MALFORMED = 'jid-malformed' - NOT_ACCEPTABLE = 'not-acceptable' - NOT_ALLOWED = 'not-allowed' - NOT_AUTHORIZED = 'not-authorized' - PAYMENT_REQUIRED = 'payment-required' - RECIPIENT_UNAVAILABLE = 'recipient-unavailable' - REDIRECT = 'redirect' - REGISTRATION_REQUIRED = 'registration-required' - REMOTE_SERVER_NOT_FOUND = 'remote-server-not-found' - REMOTE_SERVER_TIMEOUT = 'remote-server-timeout' - RESOURCE_CONSTRAINT = 'resource-constraint' - SERVICE_UNAVAILABLE = 'service-unavailable' - SUBSCRIPTION_REQUIRED = 'subscription-required' - UNDEFINED_CONDITION = 'undefined-condition' - UNEXPECTED_REQUEST = 'unexpected-request' - POLICY_VIOLATION = 'policy-violation' - UNKNOWN_ERROR = 'unknown-error' - - def __str__(self): - return self.value - - class BookmarkStoreType(Enum): PUBSUB = 'pubsub' PRIVATE = 'private'
===================================== nbxmpp/modules/discovery.py ===================================== @@ -15,6 +15,7 @@ # You should have received a copy of the GNU General Public License # along with this program; If not, see http://www.gnu.org/licenses/.
+import time import logging
from nbxmpp.protocol import Iq @@ -63,11 +64,14 @@ class Discovery: return parse_disco_items(stanza)
-def parse_disco_info(stanza): +def parse_disco_info(stanza, timestamp=None): idenities = [] features = [] dataforms = []
+ if timestamp is None: + timestamp = time.time() + query = stanza.getQuery() for node in query.getTags('identity'): attrs = node.getAttrs() @@ -89,11 +93,11 @@ def parse_disco_info(stanza): for node in query.getTags('x', namespace=NS_DATA): dataforms.append(extend_form(node))
- return DiscoInfo(jid=stanza.getFrom(), - node=query.getAttr('node'), + return DiscoInfo(stanza=stanza, identities=idenities, features=features, - dataforms=dataforms) + dataforms=dataforms, + timestamp=timestamp)
def parse_disco_items(stanza):
===================================== nbxmpp/modules/iq.py ===================================== @@ -21,7 +21,7 @@ from nbxmpp.protocol import Error as ErrorStanza from nbxmpp.protocol import ERR_BAD_REQUEST from nbxmpp.protocol import NodeProcessed from nbxmpp.structs import StanzaHandler -from nbxmpp.structs import ErrorProperties +from nbxmpp.util import error_factory from nbxmpp.const import IqType
log = logging.getLogger('nbxmpp.m.iq') @@ -57,4 +57,4 @@ class BaseIq: properties.query = stanza.getQuery()
if properties.type.is_error: - properties.error = ErrorProperties(stanza) + properties.error = error_factory(stanza)
===================================== nbxmpp/modules/message.py ===================================== @@ -19,8 +19,8 @@ import logging
from nbxmpp.protocol import NodeProcessed from nbxmpp.structs import StanzaHandler -from nbxmpp.structs import ErrorProperties from nbxmpp.structs import StanzaIDData +from nbxmpp.util import error_factory from nbxmpp.const import MessageType
log = logging.getLogger('nbxmpp.m.message') @@ -50,7 +50,7 @@ class BaseMessage: properties.stanza_id = StanzaIDData(id=id_, by=by)
if properties.type.is_error: - properties.error = ErrorProperties(stanza) + properties.error = error_factory(stanza)
@staticmethod def _process_message_after_base(_con, stanza, properties):
===================================== nbxmpp/modules/presence.py ===================================== @@ -21,7 +21,7 @@ from nbxmpp.protocol import Error as ErrorStanza from nbxmpp.protocol import ERR_BAD_REQUEST from nbxmpp.protocol import NodeProcessed from nbxmpp.structs import StanzaHandler -from nbxmpp.structs import ErrorProperties +from nbxmpp.util import error_factory from nbxmpp.const import PresenceType from nbxmpp.const import PresenceShow
@@ -46,7 +46,7 @@ class BasePresence: properties.status = stanza.getStatus()
if properties.type.is_error: - properties.error = ErrorProperties(stanza) + properties.error = error_factory(stanza)
own_jid = self._client.get_bound_jid() properties.self_presence = own_jid == properties.jid
===================================== nbxmpp/structs.py ===================================== @@ -29,7 +29,6 @@ from nbxmpp.const import MessageType from nbxmpp.const import AvatarState from nbxmpp.const import StatusCode from nbxmpp.const import PresenceType -from nbxmpp.const import Error from nbxmpp.const import LOCATION_DATA from nbxmpp.const import AdHocStatus
@@ -119,13 +118,13 @@ DiscoItem = namedtuple('DiscoItem', 'jid name node') DiscoItem.__new__.__defaults__ = (None, None)
-class DiscoInfo(namedtuple('DiscoInfo', 'jid node identities features dataforms')): +class DiscoInfo(namedtuple('DiscoInfo', 'stanza identities features dataforms timestamp')):
__slots__ = []
- def __new__(cls, jid, node, identities, features, dataforms): - return super(DiscoInfo, cls).__new__(cls, jid, node, identities, - features, dataforms) + def __new__(cls, stanza, identities, features, dataforms, timestamp=None): + return super(DiscoInfo, cls).__new__(cls, stanza, identities, + features, dataforms, timestamp)
def get_caps_hash(self): try: @@ -150,6 +149,29 @@ class DiscoInfo(namedtuple('DiscoInfo', 'jid node identities features dataforms' def supports(self, feature): return feature in self.features
+ def serialize(self): + if self.stanza is None: + raise ValueError('Unable to serialize DiscoInfo, no stanza found') + return str(self.stanza) + + @property + def node(self): + try: + query = self.stanza.getQuery() + except Exception: + return None + + if query is not None: + return query.getAttr('node') + return None + + @property + def jid(self): + try: + return self.stanza.getFrom() + except Exception: + return None + @property def mam_namespace(self): if NS_MAM_2 in self.features: @@ -185,7 +207,7 @@ class DiscoInfo(namedtuple('DiscoInfo', 'jid node identities features dataforms'
@property def muc_room_name(self): - return self._get_form_value(NS_MUC_INFO, 'muc#roomconfig_roomname') + return self._get_form_value(NS_MUC_INFO, 'muc#roomconfig_roomname')
@property def muc_description(self): @@ -625,20 +647,3 @@ class PresenceProperties: return self.muc_user.role except Exception: return None - - -class ErrorProperties: - def __init__(self, stanza): - for child in stanza.getTag('error').getChildren(): - if child.getNamespace() == NS_STANZAS: - try: - self.type = Error(child.name) - except ValueError: - self.type = Error('unknown-error') - break - self.legacy_code = stanza.getErrorCode() - self.legacy_type = stanza.getErrorType() - self.message = stanza.getErrorMsg() - - def __str__(self): - return '%s %s' % (self.type, self.message)
View it on GitLab: https://dev.gajim.org/gajim/python-nbxmpp/compare/ecfed11e600c6e9d9be64ffd7b...