Commit 355cfbac authored by Emmanuel Gil Peyrot's avatar Emmanuel Gil Peyrot
Browse files

Parse booleans according to the xs:boolean spec

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

Also use this helper in more locations.
parent b70acbc1
Pipeline #5439 failed with stages
in 10 seconds
......@@ -134,7 +134,7 @@ class Bookmarks(BaseModule):
autojoin = False
else:
try:
autojoin = from_xs_boolean(autojoin)
autojoin = from_xs_boolean(autojoin, default=False)
except ValueError as error:
self._log.warning(error)
self._log.warning(storage)
......@@ -176,7 +176,7 @@ class Bookmarks(BaseModule):
self._log.warning(item)
return None
autojoin = conference.getAttr('autojoin') in ('True', 'true', '1')
autojoin = from_xs_boolean(conference.getAttr('autojoin'), default=False)
name = conference.getAttr('name')
nick = conference.getTagData('nick')
......
......@@ -22,6 +22,7 @@
from nbxmpp.namespaces import Namespace
from nbxmpp.protocol import JID
from nbxmpp.simplexml import Node
from nbxmpp.util import from_xs_boolean
# exceptions used in this module
......@@ -309,17 +310,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, default=False)
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):
......
......@@ -44,6 +44,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
......@@ -250,7 +251,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'), default=False)
data['thread'] = direct.getAttr('thread')
data['type'] = InviteType.DIRECT
properties.muc_invite = InviteData(**data)
......
......@@ -22,6 +22,7 @@ from nbxmpp.namespaces import Namespace
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')
......@@ -102,7 +103,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, default=False):
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
......@@ -124,17 +125,20 @@ def callback(func):
return func_wrapper
def from_xs_boolean(value):
if value in ('1', 'true', 'True'):
def from_xs_boolean(value: Optional[str], *, default: Optional[bool] = None) -> bool:
if value in ('1', 'true'):
return True
if value in ('0', 'false', 'False', ''):
if value in ('0', 'false'):
return False
if value is None and default is not None:
return default
raise ValueError('Cant convert %s to python boolean' % value)
def to_xs_boolean(value):
def to_xs_boolean(value: Optional[bool]) -> str:
# 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