Commit dab88eea authored by Emmanuel Gil Peyrot's avatar Emmanuel Gil Peyrot

Parse booleans according to the xs:boolean spec

'True', 'False' or '' aren’t valid values.

Also use this helper in more locations.
parent b15c9009
Pipeline #5403 failed with stages
......@@ -179,14 +179,14 @@ class Bookmarks(BaseModule):
self._log.warning(item)
return None
autojoin = conference.getAttr('autojoin') in ('True', 'true', '1')
autojoin = from_xs_boolean(conference.getAttr('autojoin'))
name = conference.getAttr('name')
nick = conference.getTagData('nick')
return BookmarkData(jid=jid,
name=name or None,
name=name,
autojoin=autojoin,
nick=nick or None)
nick=nick)
@staticmethod
def get_private_request():
......
......@@ -23,6 +23,7 @@ from nbxmpp.protocol import NS_DATA
from nbxmpp.protocol import NS_DATA_MEDIA
from nbxmpp.protocol import JID
from nbxmpp.simplexml import Node
from nbxmpp.util import from_xs_boolean
# exceptions used in this module
......@@ -310,17 +311,14 @@ class BooleanField(DataField):
Value of field. May contain True, False or None
"""
value = self.getTagData('value')
if value in ('0', 'false'):
return False
if value in ('1', 'true'):
return True
if value is None:
return False # default value is False
raise WrongFieldValue
try:
return from_xs_boolean(value)
except ValueError:
raise WrongFieldValue
@value.setter
def value(self, value):
self.setTagData('value', value and '1' or '0')
self.setTagData('value', '1' if value else '0')
@value.deleter
def value(self):
......
......@@ -52,6 +52,7 @@ from nbxmpp.structs import MucDestroyed
from nbxmpp.util import call_on_response
from nbxmpp.util import callback
from nbxmpp.util import raise_error
from nbxmpp.util import from_xs_boolean
from nbxmpp.modules.dataforms import extend_form
from nbxmpp.modules.base import BaseModule
......@@ -258,7 +259,7 @@ class MUC(BaseModule):
data['from_'] = properties.jid
data['reason'] = direct.getAttr('reason')
data['password'] = direct.getAttr('password')
data['continued'] = direct.getAttr('continue') == 'true'
data['continued'] = from_xs_boolean(direct.getAttr('continue'))
data['thread'] = direct.getAttr('thread')
data['type'] = InviteType.DIRECT
properties.muc_invite = InviteData(**data)
......
......@@ -23,6 +23,7 @@ from nbxmpp.protocol import NS_DELAY2
from nbxmpp.simplexml import Node
from nbxmpp.const import StreamState
from nbxmpp.util import LogAdapter
from nbxmpp.util import from_xs_boolean
log = logging.getLogger('nbxmpp.smacks')
......@@ -103,7 +104,7 @@ class Smacks:
self._log.error('Received "enabled", but SM is already enabled')
return
resume = stanza.getAttr('resume')
if resume in ('true', 'True', '1'):
if from_xs_boolean(resume):
self.resume_supported = True
self._session_id = stanza.getAttr('id')
......
......@@ -25,6 +25,7 @@ import re
import logging
from logging import LoggerAdapter
from collections import defaultdict
from typing import Optional
from functools import wraps
from functools import lru_cache
......@@ -128,17 +129,17 @@ def callback(func):
return func_wrapper
def from_xs_boolean(value):
if value in ('1', 'true', 'True'):
def from_xs_boolean(value: Optional[str]):
if value in ('1', 'true'):
return True
if value in ('0', 'false', 'False', ''):
if value in ('0', 'false', None):
return False
raise ValueError('Cant convert %s to python boolean' % value)
def to_xs_boolean(value):
def to_xs_boolean(value: Optional[bool]):
# Convert to xs:boolean ('true', 'false')
# from a python boolean (True, False) or None
if value is True:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment