Files
deb-python-openstacksdk/openstack/format.py
Steve Lewis 049a2a09d5 Handle booleans that are strings in APIs
Coerces string values to a boolean only when the prop is given a
type=BoolStr. BoolStr only accepts string words, no shortened values,
synonyms or integer values. When no acceptable value is found a
TypeError or ValueError is raised. Setters and Constructor calls
validate the attribute value as a fail-fast behavior.

Change-Id: I382426325522227a83ec5234c5116da97e71cfa2
Closes-Bug: #1365724
2014-10-13 10:35:32 -07:00

41 lines
1.3 KiB
Python

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
class BoolStr(object):
"""A custom boolean/string hybrid type for resource.props.
Translates a given value to the desired type.
"""
def __init__(self, given):
"""A boolean parser.
Interprets the given value as a boolean, ignoring whitespace and case.
A TypeError is raised when interpreted as neither True nor False.
"""
expr = str(given).lower()
if 'true' == expr:
self.parsed = True
elif 'false' == expr:
self.parsed = False
else:
msg = 'Invalid as boolean: %s' % given
raise ValueError(msg)
def __bool__(self):
return self.parsed
__nonzero__ = __bool__
def __str__(self):
return str(self.parsed)