Skip to content

Commit

Permalink
factor out string-to-boolean operations
Browse files Browse the repository at this point in the history
  • Loading branch information
djmitche committed May 27, 2013
1 parent be22213 commit 2da7502
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
12 changes: 2 additions & 10 deletions master/buildbot/data/types.py
Expand Up @@ -16,6 +16,7 @@
# See "Type Validation" in master/docs/developer/tests.rst

import re
from buildbot import util
from buildbot.util import json
from buildbot.data import base

Expand Down Expand Up @@ -100,16 +101,7 @@ class Boolean(Instance):
types = (bool,)

def valueFromString(self, arg):
return {
'on': True,
'true': True,
'yes': True,
'1': True,
'off': False,
'false': False,
'no': False,
'0': False,
}[arg.lower()]
return util.string2boolean(arg)


class Link(Instance):
Expand Down
33 changes: 33 additions & 0 deletions master/buildbot/test/unit/test_util.py
Expand Up @@ -208,3 +208,36 @@ def test_nonascii(self):

def test_None(self):
self.assertEqual(util.ascii2unicode(None), None)

class StringToBoolean(unittest.TestCase):

def test_it(self):
stringValues = [
('on', True),
('true', True),
('yes', True),
('1', True),
('off', False),
('false', False),
('no', False),
('0', False),
('ON', True),
('TRUE', True),
('YES', True),
('OFF', False),
('FALSE', False),
('NO', False),
]
for s, b in stringValues:
self.assertEqual(util.string2boolean(s), b, repr(s))

def test_ascii(self):
rv = util.ascii2unicode('abcd')
self.assertEqual((rv, type(rv)), (u'abcd', unicode))

def test_nonascii(self):
self.assertRaises(UnicodeDecodeError, lambda :
util.ascii2unicode('a\x85'))

def test_None(self):
self.assertEqual(util.ascii2unicode(None), None)
14 changes: 13 additions & 1 deletion master/buildbot/util/__init__.py
Expand Up @@ -207,8 +207,20 @@ def do_stop(r):
wrap._orig = f # for tests
return wrap

def string2boolean(str):
return {
'on': True,
'true': True,
'yes': True,
'1': True,
'off': False,
'false': False,
'no': False,
'0': False,
}[str.lower()]

__all__ = [
'naturalSort', 'now', 'formatInterval', 'ComparableMixin', 'json',
'safeTranslate', 'LRUCache', 'none_or_str',
'NotABranch', 'deferredLocked', 'SerializedInvocation', 'UTC',
'diffLists', 'makeList', 'in_reactor' ]
'diffLists', 'makeList', 'in_reactor', 'string2boolean' ]
10 changes: 10 additions & 0 deletions master/docs/developer/utils.rst
Expand Up @@ -144,6 +144,16 @@ package.
If given a bytestring, it returns the string decoded as ASCII (and will thus fail for any bytes 0x80 or higher).
If given a unicode string, it returns it directly.

.. py:function:: string2boolean(str):
:param str: string
:raises KeyError:
:returns: boolean

This function converts a string to a boolean.
It is intended to be liberal in what it accepts: case-insensitive, "true", "on", "yes", "1", etc.
It raises :py:exc:`KeyError` if the value is not recognized.

.. py:data:: NotABranch
This is a sentinel value used to indicate that no branch is specified. It
Expand Down

0 comments on commit 2da7502

Please sign in to comment.