Commit db91c385 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 b15c9009
Pipeline #5399 failed with stages
......@@ -179,7 +179,7 @@ class Bookmarks(BaseModule):
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')
......@@ -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,16 @@ 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
return from_xs_boolean(value)
except ValueError:
raise WrongFieldValue
def value(self, value):
self.setTagData('value', value and '1' or '0')
self.setTagData('value', '1' if value else '0')
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)
......@@ -103,7 +103,7 @@ class Smacks:
self._log.error('Received "enabled", but SM is already enabled')
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'):
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