mirror of
https://github.com/openkmip/pykmip
synced 2025-12-16 00:04:26 +00:00
Finishing Boolean primitive implementation
This change finishes the Boolean primitive implementation, including a complete test suite for the Boolean class.
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import logging
|
||||
import six
|
||||
import sys
|
||||
|
||||
@@ -428,60 +429,125 @@ class Enumeration(Integer):
|
||||
|
||||
|
||||
class Boolean(Base):
|
||||
"""
|
||||
An encodeable object representing a boolean value.
|
||||
|
||||
def __init__(self, value=None, tag=Tags.DEFAULT):
|
||||
A Boolean is one of the KMIP primitive object types. It is encoded as an
|
||||
unsigned, big-endian, 8-byte value, capable of taking the values True (1)
|
||||
or False (0). For more information, see Section 9.1 of the KMIP 1.1
|
||||
specification.
|
||||
"""
|
||||
LENGTH = 8
|
||||
|
||||
def __init__(self, value=True, tag=Tags.DEFAULT):
|
||||
"""
|
||||
Create a Boolean object.
|
||||
|
||||
Args:
|
||||
value (bool): The value of the Boolean. Optional, defaults to True.
|
||||
tag (Tags): An enumeration defining the tag of the Boolean object.
|
||||
Optional, defaults to Tags.DEFAULT.
|
||||
"""
|
||||
super(Boolean, self).__init__(tag, type=Types.BOOLEAN)
|
||||
self.logger = logging.getLogger(__name__)
|
||||
self.value = value
|
||||
self.length = 8
|
||||
self.length = self.LENGTH
|
||||
|
||||
self.validate()
|
||||
|
||||
def read_value(self, istream):
|
||||
value = unpack('!Q', str(istream[0:self.length]))[0]
|
||||
"""
|
||||
Read the value of the Boolean object from the input stream.
|
||||
|
||||
Args:
|
||||
istream (Stream): A buffer containing the encoded bytes of the
|
||||
value of a Boolean object. Usually a BytearrayStream object.
|
||||
Required.
|
||||
|
||||
Raises:
|
||||
ValueError: if the read boolean value is not a 0 or 1.
|
||||
"""
|
||||
try:
|
||||
value = unpack('!Q', istream.read(self.LENGTH))[0]
|
||||
except:
|
||||
self.logger.error("Error reading boolean value from buffer")
|
||||
raise
|
||||
|
||||
if value == 1:
|
||||
self.value = True
|
||||
elif value == 0:
|
||||
self.value = False
|
||||
else:
|
||||
raise errors.ReadValueError(Boolean.__name__, 'value',
|
||||
value)
|
||||
raise ValueError("expected: 0 or 1, observed: {0}".format(value))
|
||||
|
||||
for _ in range(self.length):
|
||||
istream.pop(0)
|
||||
self.validate()
|
||||
|
||||
def read(self, istream):
|
||||
"""
|
||||
Read the encoding of the Boolean object from the input stream.
|
||||
|
||||
Args:
|
||||
istream (Stream): A buffer containing the encoded bytes of a
|
||||
Boolean object. Usually a BytearrayStream object. Required.
|
||||
"""
|
||||
super(Boolean, self).read(istream)
|
||||
self.read_value(istream)
|
||||
|
||||
def write_value(self, ostream):
|
||||
if self.value is None:
|
||||
raise errors.WriteValueError(Boolean.__name__, 'value',
|
||||
self.value)
|
||||
"""
|
||||
Write the value of the Boolean object to the output stream.
|
||||
|
||||
data_buffer = bytearray()
|
||||
|
||||
if isinstance(self.value, type(True)):
|
||||
if self.value:
|
||||
data_buffer.extend(pack('!Q', 1))
|
||||
else:
|
||||
data_buffer.extend(pack('!Q', 0))
|
||||
else:
|
||||
raise errors.WriteTypeError(Boolean.__name__, 'value',
|
||||
type(self.value))
|
||||
|
||||
ostream.extend(data_buffer)
|
||||
Args:
|
||||
ostream (Stream): A buffer to contain the encoded bytes of the
|
||||
value of a Boolean object. Usually a BytearrayStream object.
|
||||
Required.
|
||||
"""
|
||||
try:
|
||||
ostream.write(pack('!Q', self.value))
|
||||
except:
|
||||
self.logger.error("Error writing boolean value to buffer")
|
||||
raise
|
||||
|
||||
def write(self, ostream):
|
||||
"""
|
||||
Write the encoding of the Boolean object to the output stream.
|
||||
|
||||
Args:
|
||||
ostream (Stream): A buffer to contain the encoded bytes of a
|
||||
Boolean object. Usually a BytearrayStream object. Required.
|
||||
"""
|
||||
super(Boolean, self).write(ostream)
|
||||
self.write_value(ostream)
|
||||
|
||||
def validate(self):
|
||||
self.__validate()
|
||||
"""
|
||||
Verify that the value of the Boolean object is valid.
|
||||
|
||||
def __validate(self):
|
||||
pass
|
||||
Raises:
|
||||
TypeError: if the value is not of type bool.
|
||||
"""
|
||||
if self.value:
|
||||
if not isinstance(self.value, bool):
|
||||
raise TypeError("expected: {0}, observed: {1}".format(
|
||||
bool, type(self.value)))
|
||||
|
||||
def __repr__(self):
|
||||
return '<Boolean, %s>' % (self.value)
|
||||
return "{0}(value={1})".format(type(self).__name__, repr(self.value))
|
||||
|
||||
def __str__(self):
|
||||
return "{0}".format(repr(self.value))
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, Boolean):
|
||||
return self.value == other.value
|
||||
else:
|
||||
return NotImplemented
|
||||
|
||||
def __ne__(self, other):
|
||||
if isinstance(other, Boolean):
|
||||
return not self.__eq__(other)
|
||||
else:
|
||||
return NotImplemented
|
||||
|
||||
|
||||
class TextString(Base):
|
||||
|
||||
Reference in New Issue
Block a user