| 1 |
#------------------------------------------------------------------------- |
| 2 |
# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 |
# Licensed under the MIT License. See License.txt in the project root for |
| 4 |
# license information. |
| 5 |
#-------------------------------------------------------------------------- |
| 6 |
|
| 7 |
# Python imports |
| 8 |
from enum import Enum |
| 9 |
import logging |
| 10 |
import uuid |
| 11 |
import copy |
| 12 |
|
| 13 |
|
| 14 |
# C improts |
| 15 |
from libc cimport stdint |
| 16 |
from libc.stdlib cimport malloc, realloc, free |
| 17 |
from libc.string cimport memcpy |
| 18 |
|
| 19 |
cimport cython |
| 20 |
cimport c_amqpvalue |
| 21 |
cimport c_amqp_definitions |
| 22 |
|
| 23 |
|
| 24 |
_logger = logging.getLogger(__name__) |
| 25 |
|
| 26 |
|
| 27 |
cdef int encode_bytes_callback(void* context, const unsigned char* encoded_bytes, size_t length): |
| 28 |
context_obj = <object>context |
| 29 |
context_obj.append(encoded_bytes[:length]) |
| 30 |
return 0 |
| 31 |
|
| 32 |
|
| 33 |
cdef get_amqp_value_type(c_amqpvalue.AMQP_VALUE value): |
| 34 |
type_val = c_amqpvalue.amqpvalue_get_type(value) |
| 35 |
try: |
| 36 |
return AMQPType(type_val) |
| 37 |
except ValueError: |
| 38 |
_logger.info("Received unrecognized type value: %r", type_val) |
| 39 |
return AMQPType.ErrorType |
| 40 |
|
| 41 |
|
| 42 |
cpdef enocde_batch_value(AMQPValue value, message_body): |
| 43 |
if c_amqpvalue.amqpvalue_encode(<c_amqpvalue.AMQP_VALUE>value._c_value, |
| 44 |
<c_amqpvalue.AMQPVALUE_ENCODER_OUTPUT>encode_bytes_callback, |
| 45 |
<void*>message_body) != 0: |
| 46 |
raise ValueError("Failed to encode batched message data.") |
| 47 |
|
| 48 |
|
| 49 |
class AMQPType(Enum): |
| 50 |
NullValue = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_NULL |
| 51 |
BoolValue = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_BOOL |
| 52 |
UByteValue = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_UBYTE |
| 53 |
UShortValue = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_USHORT |
| 54 |
UIntValue = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_UINT |
| 55 |
ULongValue = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_ULONG |
| 56 |
ByteValue = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_BYTE |
| 57 |
ShortValue = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_SHORT |
| 58 |
IntValue = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_INT |
| 59 |
LongValue = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_LONG |
| 60 |
FloatValue = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_FLOAT |
| 61 |
DoubleValue = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_DOUBLE |
| 62 |
CharValue = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_CHAR |
| 63 |
TimestampValue = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_TIMESTAMP |
| 64 |
UUIDValue = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_UUID |
| 65 |
BinaryValue = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_BINARY |
| 66 |
StringValue = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_STRING |
| 67 |
SymbolValue = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_SYMBOL |
| 68 |
ListValue = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_LIST |
| 69 |
DictValue = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_MAP |
| 70 |
ArrayValue = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_ARRAY |
| 71 |
DescribedType = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_DESCRIBED |
| 72 |
CompositeType = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_COMPOSITE |
| 73 |
UnknownType = c_amqpvalue.AMQP_TYPE_TAG.AMQP_TYPE_UNKNOWN |
| 74 |
ErrorType = 999 |
| 75 |
|
| 76 |
|
| 77 |
cdef value_factory(c_amqpvalue.AMQP_VALUE value): |
| 78 |
type_val = get_amqp_value_type(value) |
| 79 |
_logger.debug("Wrapping value type: %r", type_val) |
| 80 |
if type_val == AMQPType.NullValue: |
| 81 |
new_obj = AMQPValue() |
| 82 |
elif type_val == AMQPType.BoolValue: |
| 83 |
new_obj = BoolValue() |
| 84 |
elif type_val == AMQPType.UByteValue: |
| 85 |
new_obj = UByteValue() |
| 86 |
elif type_val == AMQPType.UShortValue: |
| 87 |
new_obj = UShortValue() |
| 88 |
elif type_val == AMQPType.UIntValue: |
| 89 |
new_obj = UIntValue() |
| 90 |
elif type_val == AMQPType.ULongValue: |
| 91 |
new_obj = ULongValue() |
| 92 |
elif type_val == AMQPType.ByteValue: |
| 93 |
new_obj = ByteValue() |
| 94 |
elif type_val == AMQPType.ShortValue: |
| 95 |
new_obj = ShortValue() |
| 96 |
elif type_val == AMQPType.IntValue: |
| 97 |
new_obj = IntValue() |
| 98 |
elif type_val == AMQPType.LongValue: |
| 99 |
new_obj = LongValue() |
| 100 |
elif type_val == AMQPType.FloatValue: |
| 101 |
new_obj = FloatValue() |
| 102 |
elif type_val == AMQPType.DoubleValue: |
| 103 |
new_obj = DoubleValue() |
| 104 |
elif type_val == AMQPType.CharValue: |
| 105 |
new_obj = CharValue() |
| 106 |
elif type_val == AMQPType.TimestampValue: |
| 107 |
new_obj = TimestampValue() |
| 108 |
elif type_val == AMQPType.UUIDValue: |
| 109 |
new_obj = UUIDValue() |
| 110 |
elif type_val == AMQPType.BinaryValue: |
| 111 |
new_obj = BinaryValue() |
| 112 |
elif type_val == AMQPType.StringValue: |
| 113 |
new_obj = StringValue() |
| 114 |
elif type_val == AMQPType.SymbolValue: |
| 115 |
new_obj = SymbolValue() |
| 116 |
elif type_val == AMQPType.ListValue: |
| 117 |
new_obj = ListValue() |
| 118 |
elif type_val == AMQPType.DictValue: |
| 119 |
new_obj = DictValue() |
| 120 |
elif type_val == AMQPType.ArrayValue: # Do checking |
| 121 |
new_obj = ArrayValue() |
| 122 |
elif type_val == AMQPType.CompositeType: |
| 123 |
new_obj = CompositeValue() |
| 124 |
elif type_val == AMQPType.DescribedType: |
| 125 |
new_obj = DescribedValue() |
| 126 |
else: |
| 127 |
error = "Unrecognized AMQPType: {}".format(type_val) |
| 128 |
_logger.info(error) |
| 129 |
raise TypeError(error) |
| 130 |
new_obj.wrap(value) |
| 131 |
return new_obj |
| 132 |
|
| 133 |
|
| 134 |
cpdef null_value(): |
| 135 |
new_obj = AMQPValue() |
| 136 |
new_obj.create() |
| 137 |
return new_obj |
| 138 |
|
| 139 |
|
| 140 |
cpdef bool_value(bint value): |
| 141 |
value = 1 if value else 0 |
| 142 |
new_obj = BoolValue() |
| 143 |
new_obj.create(value) |
| 144 |
return new_obj |
| 145 |
|
| 146 |
|
| 147 |
cpdef ubyte_value(unsigned char value): |
| 148 |
new_obj = UByteValue() |
| 149 |
new_obj.create(value) |
| 150 |
return new_obj |
| 151 |
|
| 152 |
|
| 153 |
cpdef ushort_value(stdint.uint16_t value): |
| 154 |
new_obj = UShortValue() |
| 155 |
new_obj.create(value) |
| 156 |
return new_obj |
| 157 |
|
| 158 |
|
| 159 |
cpdef uint_value(stdint.uint32_t value): |
| 160 |
new_obj = UIntValue() |
| 161 |
new_obj.create(value) |
| 162 |
return new_obj |
| 163 |
|
| 164 |
|
| 165 |
cpdef ulong_value(stdint.uint64_t value): |
| 166 |
new_obj = ULongValue() |
| 167 |
new_obj.create(value) |
| 168 |
return new_obj |
| 169 |
|
| 170 |
|
| 171 |
cpdef byte_value(char value): |
| 172 |
new_obj = ByteValue() |
| 173 |
new_obj.create(value) |
| 174 |
return new_obj |
| 175 |
|
| 176 |
|
| 177 |
cpdef short_value(stdint.int16_t value): |
| 178 |
new_obj = ShortValue() |
| 179 |
new_obj.create(value) |
| 180 |
return new_obj |
| 181 |
|
| 182 |
|
| 183 |
cpdef int_value(stdint.int32_t value): |
| 184 |
new_obj = IntValue() |
| 185 |
new_obj.create(value) |
| 186 |
return new_obj |
| 187 |
|
| 188 |
|
| 189 |
cpdef long_value(stdint.int64_t value): |
| 190 |
new_obj = LongValue() |
| 191 |
new_obj.create(value) |
| 192 |
return new_obj |
| 193 |
|
| 194 |
|
| 195 |
cpdef float_value(float value): |
| 196 |
new_obj = FloatValue() |
| 197 |
new_obj.create(value) |
| 198 |
return new_obj |
| 199 |
|
| 200 |
|
| 201 |
cpdef double_value(double value): |
| 202 |
new_obj = DoubleValue() |
| 203 |
new_obj.create(value) |
| 204 |
return new_obj |
| 205 |
|
| 206 |
|
| 207 |
cpdef char_value(stdint.uint32_t value): |
| 208 |
new_obj = CharValue() |
| 209 |
new_obj.create(value) |
| 210 |
return new_obj |
| 211 |
|
| 212 |
|
| 213 |
cpdef timestamp_value(stdint.int64_t value): |
| 214 |
new_obj = TimestampValue() |
| 215 |
new_obj.create(value) |
| 216 |
return new_obj |
| 217 |
|
| 218 |
|
| 219 |
cpdef uuid_value(value): |
| 220 |
if not isinstance(value, uuid.UUID): |
| 221 |
raise TypeError("Input value must be type UUID.") |
| 222 |
new_obj = UUIDValue() |
| 223 |
new_obj.create(<bytes>value.bytes) |
| 224 |
return new_obj |
| 225 |
|
| 226 |
|
| 227 |
cpdef binary_value(value): |
| 228 |
bytes_value = bytes(value) |
| 229 |
new_obj = BinaryValue() |
| 230 |
new_obj.create(<bytes>bytes_value) |
| 231 |
return new_obj |
| 232 |
|
| 233 |
|
| 234 |
cpdef string_value(char* value): |
| 235 |
new_obj = StringValue() |
| 236 |
new_obj.create(value) |
| 237 |
return new_obj |
| 238 |
|
| 239 |
|
| 240 |
cpdef symbol_value(char* value): |
| 241 |
new_obj = SymbolValue() |
| 242 |
new_obj.create(value) |
| 243 |
return new_obj |
| 244 |
|
| 245 |
|
| 246 |
cpdef list_value(): |
| 247 |
new_obj = ListValue() |
| 248 |
new_obj.create() |
| 249 |
return new_obj |
| 250 |
|
| 251 |
|
| 252 |
cpdef dict_value(): |
| 253 |
new_obj = DictValue() |
| 254 |
new_obj.create() |
| 255 |
return new_obj |
| 256 |
|
| 257 |
|
| 258 |
cpdef array_value(): |
| 259 |
new_obj = ArrayValue() |
| 260 |
new_obj.create() |
| 261 |
return new_obj |
| 262 |
|
| 263 |
|
| 264 |
cpdef described_value(AMQPValue descriptor, AMQPValue value): |
| 265 |
new_obj = DescribedValue() |
| 266 |
new_obj.create(descriptor, value) |
| 267 |
return new_obj |
| 268 |
|
| 269 |
|
| 270 |
cdef class AMQPValue(StructBase): |
| 271 |
|
| 272 |
_type = AMQPType.NullValue |
| 273 |
cdef c_amqpvalue.AMQP_VALUE _c_value |
| 274 |
|
| 275 |
def __cinit__(self): |
| 276 |
pass |
| 277 |
|
| 278 |
def __dealloc__(self): |
| 279 |
if _logger: |
| 280 |
_logger.debug("Deallocating %r", self.__class__.__name__) |
| 281 |
self.destroy() |
| 282 |
|
| 283 |
def __eq__(self, AMQPValue other): |
| 284 |
return c_amqpvalue.amqpvalue_are_equal(self._c_value, other._c_value) |
| 285 |
|
| 286 |
def __ne__(self, AMQPValue other): |
| 287 |
return not c_amqpvalue.amqpvalue_are_equal(self._c_value, other._c_value) |
| 288 |
|
| 289 |
def __bytes__(self): |
| 290 |
return self._as_string() |
| 291 |
|
| 292 |
def __str__(self): |
| 293 |
as_bytes = self._as_string() |
| 294 |
return str(as_bytes, encoding="UTF-8", errors="ignore" ) |
| 295 |
|
| 296 |
def __unicode__(self): |
| 297 |
as_bytes = self._as_string() |
| 298 |
try: |
| 299 |
return str(as_bytes.decode('UTF-8')) |
| 300 |
except UnicodeDecodeError: |
| 301 |
return str(as_bytes) |
| 302 |
|
| 303 |
cpdef _as_string(self): |
| 304 |
cdef c_amqpvalue.AMQP_VALUE value |
| 305 |
cdef const char* as_string |
| 306 |
value = c_amqpvalue.amqpvalue_clone(self._c_value) |
| 307 |
if <void*>value == NULL: |
| 308 |
self._value_error() |
| 309 |
as_string = c_amqpvalue.amqpvalue_to_string(value) |
| 310 |
py_string = <bytes> as_string |
| 311 |
free(as_string) |
| 312 |
c_amqpvalue.amqpvalue_destroy(self._c_value) |
| 313 |
return py_string |
| 314 |
|
| 315 |
cdef _validate(self): |
| 316 |
if <void*>self._c_value is NULL: |
| 317 |
self._memory_error() |
| 318 |
|
| 319 |
cpdef destroy(self): |
| 320 |
try: |
| 321 |
if <void*>self._c_value is not NULL: |
| 322 |
if _logger: |
| 323 |
_logger.debug("Destroying %r", self.__class__.__name__) |
| 324 |
c_amqpvalue.amqpvalue_destroy(self._c_value) |
| 325 |
except KeyboardInterrupt: |
| 326 |
pass |
| 327 |
finally: |
| 328 |
self._c_value = <c_amqpvalue.AMQP_VALUE>NULL |
| 329 |
|
| 330 |
cdef wrap(self, c_amqpvalue.AMQP_VALUE value): |
| 331 |
self.destroy() |
| 332 |
self._c_value = value |
| 333 |
self._validate() |
| 334 |
|
| 335 |
def create(self): |
| 336 |
new_value = c_amqpvalue.amqpvalue_create_null() |
| 337 |
self.wrap(new_value) |
| 338 |
|
| 339 |
@property |
| 340 |
def type(self): |
| 341 |
if <void*>self._c_value is NULL: |
| 342 |
self._null_error() |
| 343 |
obj_type = get_amqp_value_type(self._c_value) |
| 344 |
return obj_type |
| 345 |
|
| 346 |
@property |
| 347 |
def value(self): |
| 348 |
return None |
| 349 |
|
| 350 |
cpdef get_encoded_size(self): |
| 351 |
cdef size_t length |
| 352 |
if c_amqpvalue.amqpvalue_get_encoded_size(self._c_value, &length) != 0: |
| 353 |
self._value_error("Failed to get encoded size.") |
| 354 |
return length |
| 355 |
|
| 356 |
cpdef clone(self): |
| 357 |
cdef c_amqpvalue.AMQP_VALUE value |
| 358 |
value = c_amqpvalue.amqpvalue_clone(self._c_value) |
| 359 |
if <void*>value == NULL: |
| 360 |
self._value_error() |
| 361 |
return value_factory(value) |
| 362 |
|
| 363 |
|
| 364 |
cdef class BoolValue(AMQPValue): |
| 365 |
|
| 366 |
_type = AMQPType.BoolValue |
| 367 |
|
| 368 |
cpdef _bool_value(self): |
| 369 |
cdef bint _value |
| 370 |
if c_amqpvalue.amqpvalue_get_boolean(self._c_value, &_value) == 0: |
| 371 |
return _value |
| 372 |
else: |
| 373 |
self._value_error() |
| 374 |
|
| 375 |
def create(self, bint value): |
| 376 |
new_value = c_amqpvalue.amqpvalue_create_boolean(value) |
| 377 |
self.wrap(new_value) |
| 378 |
|
| 379 |
@property |
| 380 |
def value(self): |
| 381 |
assert self.type |
| 382 |
str_value = str(self) |
| 383 |
if str_value in ["false"]: |
| 384 |
return False |
| 385 |
elif str_value in ["true"]: |
| 386 |
return True |
| 387 |
else: |
| 388 |
self._value_error() |
| 389 |
|
| 390 |
|
| 391 |
cdef class UByteValue(AMQPValue): |
| 392 |
|
| 393 |
_type = AMQPType.UByteValue |
| 394 |
|
| 395 |
def create(self, unsigned char value): |
| 396 |
new_value = c_amqpvalue.amqpvalue_create_ubyte(value) |
| 397 |
self.wrap(new_value) |
| 398 |
|
| 399 |
@property |
| 400 |
def value(self): |
| 401 |
assert self.type |
| 402 |
cdef unsigned char _value |
| 403 |
if c_amqpvalue.amqpvalue_get_ubyte(self._c_value, &_value) == 0: |
| 404 |
return _value |
| 405 |
else: |
| 406 |
self._value_error() |
| 407 |
|
| 408 |
|
| 409 |
cdef class UShortValue(AMQPValue): |
| 410 |
|
| 411 |
_type = AMQPType.UShortValue |
| 412 |
|
| 413 |
def create(self, stdint.uint16_t value): |
| 414 |
new_value = c_amqpvalue.amqpvalue_create_ushort(value) |
| 415 |
self.wrap(new_value) |
| 416 |
|
| 417 |
@property |
| 418 |
def value(self): |
| 419 |
assert self.type |
| 420 |
cdef stdint.uint16_t _value |
| 421 |
if c_amqpvalue.amqpvalue_get_ushort(self._c_value, &_value) == 0: |
| 422 |
return _value |
| 423 |
else: |
| 424 |
self._value_error() |
| 425 |
|
| 426 |
|
| 427 |
cdef class UIntValue(AMQPValue): |
| 428 |
|
| 429 |
_type = AMQPType.UIntValue |
| 430 |
|
| 431 |
def create(self, stdint.uint32_t value): |
| 432 |
new_value = c_amqpvalue.amqpvalue_create_uint(value) |
| 433 |
self.wrap(new_value) |
| 434 |
|
| 435 |
@property |
| 436 |
def value(self): |
| 437 |
assert self.type |
| 438 |
cdef stdint.uint32_t _value |
| 439 |
if c_amqpvalue.amqpvalue_get_uint(self._c_value, &_value) == 0: |
| 440 |
return _value |
| 441 |
else: |
| 442 |
self._value_error() |
| 443 |
|
| 444 |
|
| 445 |
cdef class ULongValue(AMQPValue): |
| 446 |
|
| 447 |
_type = AMQPType.ULongValue |
| 448 |
|
| 449 |
def create(self, stdint.uint64_t value): |
| 450 |
new_value = c_amqpvalue.amqpvalue_create_ulong(value) |
| 451 |
self.wrap(new_value) |
| 452 |
|
| 453 |
@property |
| 454 |
def value(self): |
| 455 |
assert self.type |
| 456 |
cdef stdint.uint64_t _value |
| 457 |
if c_amqpvalue.amqpvalue_get_ulong(self._c_value, &_value) == 0: |
| 458 |
return _value |
| 459 |
else: |
| 460 |
self._value_error() |
| 461 |
|
| 462 |
|
| 463 |
cdef class ByteValue(AMQPValue): |
| 464 |
|
| 465 |
_type = AMQPType.ByteValue |
| 466 |
|
| 467 |
def create(self, char value): |
| 468 |
new_value = c_amqpvalue.amqpvalue_create_byte(value) |
| 469 |
self.wrap(new_value) |
| 470 |
|
| 471 |
@property |
| 472 |
def value(self): |
| 473 |
assert self.type |
| 474 |
cdef char _value |
| 475 |
if c_amqpvalue.amqpvalue_get_byte(self._c_value, &_value) == 0: |
| 476 |
return _value |
| 477 |
else: |
| 478 |
self._value_error() |
| 479 |
|
| 480 |
|
| 481 |
cdef class ShortValue(AMQPValue): |
| 482 |
|
| 483 |
_type = AMQPType.ShortValue |
| 484 |
|
| 485 |
def create(self, stdint.int16_t value): |
| 486 |
new_value = c_amqpvalue.amqpvalue_create_short(value) |
| 487 |
self.wrap(new_value) |
| 488 |
|
| 489 |
@property |
| 490 |
def value(self): |
| 491 |
assert self.type |
| 492 |
cdef stdint.int16_t _value |
| 493 |
if c_amqpvalue.amqpvalue_get_short(self._c_value, &_value) == 0: |
| 494 |
return _value |
| 495 |
else: |
| 496 |
self._value_error() |
| 497 |
|
| 498 |
|
| 499 |
cdef class IntValue(AMQPValue): |
| 500 |
|
| 501 |
_type = AMQPType.IntValue |
| 502 |
|
| 503 |
def create(self, stdint.int32_t value): |
| 504 |
new_value = c_amqpvalue.amqpvalue_create_int(value) |
| 505 |
self.wrap(new_value) |
| 506 |
|
| 507 |
@property |
| 508 |
def value(self): |
| 509 |
assert self.type |
| 510 |
cdef stdint.int32_t _value |
| 511 |
if c_amqpvalue.amqpvalue_get_int(self._c_value, &_value) == 0: |
| 512 |
return _value |
| 513 |
else: |
| 514 |
self._value_error() |
| 515 |
|
| 516 |
|
| 517 |
cdef class LongValue(AMQPValue): |
| 518 |
|
| 519 |
_type = AMQPType.LongValue |
| 520 |
|
| 521 |
def create(self, stdint.int64_t value): |
| 522 |
new_value = c_amqpvalue.amqpvalue_create_long(value) |
| 523 |
self.wrap(new_value) |
| 524 |
|
| 525 |
@property |
| 526 |
def value(self): |
| 527 |
assert self.type |
| 528 |
cdef stdint.int64_t _value |
| 529 |
if c_amqpvalue.amqpvalue_get_long(self._c_value, &_value) == 0: |
| 530 |
return _value |
| 531 |
else: |
| 532 |
self._value_error() |
| 533 |
|
| 534 |
|
| 535 |
cdef class FloatValue(AMQPValue): |
| 536 |
|
| 537 |
_type = AMQPType.FloatValue |
| 538 |
|
| 539 |
def create(self, float value): |
| 540 |
new_value = c_amqpvalue.amqpvalue_create_float(value) |
| 541 |
self.wrap(new_value) |
| 542 |
|
| 543 |
@property |
| 544 |
def value(self): |
| 545 |
assert self.type |
| 546 |
cdef float _value |
| 547 |
if c_amqpvalue.amqpvalue_get_float(self._c_value, &_value) == 0: |
| 548 |
return _value |
| 549 |
else: |
| 550 |
self._value_error() |
| 551 |
|
| 552 |
|
| 553 |
cdef class DoubleValue(AMQPValue): |
| 554 |
|
| 555 |
_type = AMQPType.DoubleValue |
| 556 |
|
| 557 |
def create(self, double value): |
| 558 |
new_value = c_amqpvalue.amqpvalue_create_double(value) |
| 559 |
self.wrap(new_value) |
| 560 |
|
| 561 |
@property |
| 562 |
def value(self): |
| 563 |
assert self.type |
| 564 |
cdef double _value |
| 565 |
if c_amqpvalue.amqpvalue_get_double(self._c_value, &_value) == 0: |
| 566 |
return _value |
| 567 |
else: |
| 568 |
self._value_error() |
| 569 |
|
| 570 |
|
| 571 |
cdef class CharValue(AMQPValue): |
| 572 |
|
| 573 |
_type = AMQPType.CharValue |
| 574 |
|
| 575 |
def create(self, stdint.uint32_t value): |
| 576 |
new_value = c_amqpvalue.amqpvalue_create_char(value) |
| 577 |
self.wrap(new_value) |
| 578 |
|
| 579 |
@property |
| 580 |
def value(self): |
| 581 |
assert self.type |
| 582 |
cdef stdint.uint32_t _value |
| 583 |
if c_amqpvalue.amqpvalue_get_char(self._c_value, &_value) == 0: |
| 584 |
return chr(_value) |
| 585 |
else: |
| 586 |
self._value_error() |
| 587 |
|
| 588 |
|
| 589 |
cdef class TimestampValue(AMQPValue): |
| 590 |
|
| 591 |
_type = AMQPType.TimestampValue |
| 592 |
|
| 593 |
def create(self, stdint.int64_t value): |
| 594 |
new_value = c_amqpvalue.amqpvalue_create_timestamp(value) |
| 595 |
self.wrap(new_value) |
| 596 |
|
| 597 |
@property |
| 598 |
def value(self): |
| 599 |
assert self.type |
| 600 |
cdef stdint.int64_t _value |
| 601 |
if c_amqpvalue.amqpvalue_get_timestamp(self._c_value, &_value) == 0: |
| 602 |
return _value |
| 603 |
else: |
| 604 |
self._value_error() |
| 605 |
|
| 606 |
|
| 607 |
cdef class UUIDValue(AMQPValue): |
| 608 |
|
| 609 |
_type = AMQPType.UUIDValue |
| 610 |
|
| 611 |
def create(self, bytes value): |
| 612 |
new_value = c_amqpvalue.amqpvalue_create_uuid(value) |
| 613 |
self.wrap(new_value) |
| 614 |
|
| 615 |
@property |
| 616 |
def value(self): |
| 617 |
assert self.type |
| 618 |
str_val = str(self) |
| 619 |
return uuid.UUID(str_val) # TODO: Get proper value |
| 620 |
|
| 621 |
|
| 622 |
cdef class BinaryValue(AMQPValue): |
| 623 |
|
| 624 |
_type = AMQPType.BinaryValue |
| 625 |
|
| 626 |
def create(self, bytes value): |
| 627 |
cdef c_amqpvalue.amqp_binary _binary |
| 628 |
length = len(list(value)) |
| 629 |
_binary.length = length |
| 630 |
_binary.bytes = <char*>value |
| 631 |
new_value = c_amqpvalue.amqpvalue_create_binary(_binary) |
| 632 |
self.wrap(new_value) |
| 633 |
|
| 634 |
def __len__(self): |
| 635 |
assert self.type |
| 636 |
cdef c_amqpvalue.amqp_binary _value |
| 637 |
if c_amqpvalue.amqpvalue_get_binary(self._c_value, &_value) == 0: |
| 638 |
return _value.length |
| 639 |
else: |
| 640 |
self._value_error() |
| 641 |
|
| 642 |
@property |
| 643 |
def value(self): |
| 644 |
assert self.type |
| 645 |
cdef c_amqpvalue.amqp_binary _value |
| 646 |
cdef char* bytes_value |
| 647 |
if c_amqpvalue.amqpvalue_get_binary(self._c_value, &_value) == 0: |
| 648 |
bytes_value = <char*>_value.bytes |
| 649 |
return bytes_value[:_value.length] |
| 650 |
else: |
| 651 |
self._value_error() |
| 652 |
|
| 653 |
|
| 654 |
cdef class StringValue(AMQPValue): |
| 655 |
|
| 656 |
_type = AMQPType.StringValue |
| 657 |
|
| 658 |
def create(self, char* value): |
| 659 |
new_value = c_amqpvalue.amqpvalue_create_string(value) |
| 660 |
self.wrap(new_value) |
| 661 |
|
| 662 |
@property |
| 663 |
def value(self): |
| 664 |
assert self.type |
| 665 |
cdef const char* _value |
| 666 |
if c_amqpvalue.amqpvalue_get_string(self._c_value, &_value) == 0: |
| 667 |
return copy.deepcopy(_value) |
| 668 |
else: |
| 669 |
self._value_error() |
| 670 |
|
| 671 |
|
| 672 |
cdef class SymbolValue(AMQPValue): |
| 673 |
|
| 674 |
_type = AMQPType.SymbolValue |
| 675 |
|
| 676 |
def create(self, char* value): |
| 677 |
new_value = c_amqpvalue.amqpvalue_create_symbol(value) |
| 678 |
self.wrap(new_value) |
| 679 |
|
| 680 |
@property |
| 681 |
def value(self): |
| 682 |
assert self.type |
| 683 |
cdef const char* _value |
| 684 |
if c_amqpvalue.amqpvalue_get_symbol(self._c_value, &_value) == 0: |
| 685 |
return copy.deepcopy(_value) |
| 686 |
else: |
| 687 |
self._value_error() |
| 688 |
|
| 689 |
|
| 690 |
cdef class ListValue(AMQPValue): |
| 691 |
|
| 692 |
_type = AMQPType.ListValue |
| 693 |
|
| 694 |
def create(self): |
| 695 |
new_value = c_amqpvalue.amqpvalue_create_list() |
| 696 |
self.wrap(new_value) |
| 697 |
|
| 698 |
def __len__(self): |
| 699 |
return self.size |
| 700 |
|
| 701 |
def __getitem__(self, size_t index): |
| 702 |
if index >= self.size: |
| 703 |
raise IndexError("Index is out of range.") |
| 704 |
cdef c_amqpvalue.AMQP_VALUE value |
| 705 |
value = c_amqpvalue.amqpvalue_get_list_item(self._c_value, index) |
| 706 |
if <void*>value == NULL: |
| 707 |
self._value_error() |
| 708 |
try: |
| 709 |
return value_factory(value) |
| 710 |
except TypeError: |
| 711 |
return None |
| 712 |
|
| 713 |
def __setitem__(self, stdint.uint32_t index, AMQPValue value): |
| 714 |
assert value.type |
| 715 |
if index >= self.size: |
| 716 |
raise IndexError("Index is out of range.") |
| 717 |
if c_amqpvalue.amqpvalue_set_list_item(self._c_value, index, value._c_value) != 0: |
| 718 |
self._value_error() |
| 719 |
|
| 720 |
@property |
| 721 |
def size(self): |
| 722 |
assert self.type |
| 723 |
cdef stdint.uint32_t _value |
| 724 |
if c_amqpvalue.amqpvalue_get_list_item_count(self._c_value, &_value) == 0: |
| 725 |
return _value |
| 726 |
else: |
| 727 |
self._value_error() |
| 728 |
|
| 729 |
@size.setter |
| 730 |
def size(self, stdint.uint32_t length): |
| 731 |
assert self.type |
| 732 |
if c_amqpvalue.amqpvalue_set_list_item_count(self._c_value, length) != 0: |
| 733 |
self._value_error() |
| 734 |
|
| 735 |
@property |
| 736 |
def value(self): |
| 737 |
assert self.type |
| 738 |
value = [] |
| 739 |
for i in range(self.size): |
| 740 |
value.append(copy.deepcopy(self[i].value)) |
| 741 |
return value |
| 742 |
|
| 743 |
|
| 744 |
cdef class DictValue(AMQPValue): |
| 745 |
|
| 746 |
_type = AMQPType.DictValue |
| 747 |
|
| 748 |
def create(self): |
| 749 |
new_value = c_amqpvalue.amqpvalue_create_map() |
| 750 |
self.wrap(new_value) |
| 751 |
|
| 752 |
def __len__(self): |
| 753 |
return self.size |
| 754 |
|
| 755 |
def __getitem__(self, AMQPValue key): |
| 756 |
cdef c_amqpvalue.AMQP_VALUE value |
| 757 |
value = c_amqpvalue.amqpvalue_get_map_value(self._c_value, key._c_value) |
| 758 |
if <void*>value == NULL: |
| 759 |
raise KeyError("No value found for key: {}".format(key.value)) |
| 760 |
try: |
| 761 |
return value_factory(value) |
| 762 |
except TypeError: |
| 763 |
return None |
| 764 |
|
| 765 |
def __setitem__(self, AMQPValue key, AMQPValue value): |
| 766 |
if c_amqpvalue.amqpvalue_set_map_value(self._c_value, key._c_value, value._c_value) != 0: |
| 767 |
self._value_error() |
| 768 |
|
| 769 |
def get(self, stdint.uint32_t index): |
| 770 |
if index >= self.size: |
| 771 |
raise IndexError("Index is out of range.") |
| 772 |
cdef c_amqpvalue.AMQP_VALUE key |
| 773 |
cdef c_amqpvalue.AMQP_VALUE value |
| 774 |
if c_amqpvalue.amqpvalue_get_map_key_value_pair(self._c_value, index, &key, &value) != 0: |
| 775 |
self._value_error() |
| 776 |
return (value_factory(key), value_factory(value)) |
| 777 |
|
| 778 |
@property |
| 779 |
def size(self): |
| 780 |
assert self.type |
| 781 |
cdef stdint.uint32_t _value |
| 782 |
if c_amqpvalue.amqpvalue_get_map_pair_count(self._c_value, &_value) == 0: |
| 783 |
return _value |
| 784 |
else: |
| 785 |
self._value_error() |
| 786 |
|
| 787 |
@property |
| 788 |
def value(self): |
| 789 |
assert self.type |
| 790 |
value = {} |
| 791 |
for i in range(self.size): |
| 792 |
key, item = self.get(i) |
| 793 |
value[copy.deepcopy(key.value)] = copy.deepcopy(item.value) |
| 794 |
return value |
| 795 |
|
| 796 |
|
| 797 |
cdef class ArrayValue(AMQPValue): |
| 798 |
|
| 799 |
_type = AMQPType.ArrayValue |
| 800 |
|
| 801 |
def create(self): |
| 802 |
self._c_value = c_amqpvalue.amqpvalue_create_array() |
| 803 |
self._validate() |
| 804 |
|
| 805 |
def __len__(self): |
| 806 |
return self.size |
| 807 |
|
| 808 |
def __getitem__(self, stdint.uint32_t index): |
| 809 |
if index >= self.size: |
| 810 |
raise IndexError("Index is out of range.") |
| 811 |
cdef c_amqpvalue.AMQP_VALUE value |
| 812 |
value = c_amqpvalue.amqpvalue_get_array_item(self._c_value, index) |
| 813 |
if <void*>value == NULL: |
| 814 |
self._value_error() |
| 815 |
return value_factory(value) |
| 816 |
|
| 817 |
@property |
| 818 |
def size(self): |
| 819 |
assert self.type |
| 820 |
cdef stdint.uint32_t _value |
| 821 |
if c_amqpvalue.amqpvalue_get_array_item_count(self._c_value, &_value) == 0: |
| 822 |
return _value |
| 823 |
else: |
| 824 |
self._value_error() |
| 825 |
|
| 826 |
cpdef append(self, AMQPValue value): |
| 827 |
assert self.type |
| 828 |
cdef c_amqpvalue.AMQP_VALUE _value |
| 829 |
_value = <c_amqpvalue.AMQP_VALUE>value._c_value |
| 830 |
if c_amqpvalue.amqpvalue_add_array_item(self._c_value, <c_amqpvalue.AMQP_VALUE>value._c_value) != 0: |
| 831 |
self._value_error() |
| 832 |
|
| 833 |
@property |
| 834 |
def value(self): |
| 835 |
assert self.type |
| 836 |
value = [] |
| 837 |
for i in range(self.size): |
| 838 |
value.append(copy.deepcopy(self[i].value)) |
| 839 |
return value |
| 840 |
|
| 841 |
|
| 842 |
cdef class CompositeValue(AMQPValue): |
| 843 |
|
| 844 |
_type = AMQPType.CompositeType |
| 845 |
|
| 846 |
def create(self, AMQPValue descriptor, stdint.uint32_t list_size): |
| 847 |
new_value = c_amqpvalue.amqpvalue_create_composite(descriptor._c_value, list_size) |
| 848 |
self.wrap(new_value) |
| 849 |
|
| 850 |
def create_from_long(self, stdint.uint64_t descriptor): |
| 851 |
new_value = c_amqpvalue.amqpvalue_create_composite_with_ulong_descriptor(descriptor) |
| 852 |
self.wrap(new_value) |
| 853 |
|
| 854 |
def __len__(self): |
| 855 |
return self.size |
| 856 |
|
| 857 |
def __getitem__(self, stdint.uint32_t index): |
| 858 |
if index >= self.size: |
| 859 |
raise IndexError("Index is out of range.") |
| 860 |
cdef c_amqpvalue.AMQP_VALUE value |
| 861 |
value = c_amqpvalue.amqpvalue_get_composite_item(self._c_value, index) |
| 862 |
if <void*>value == NULL: |
| 863 |
self._value_error() |
| 864 |
try: |
| 865 |
return value_factory(value) |
| 866 |
except TypeError: |
| 867 |
raise IndexError("No item found at index {}".format(index)) |
| 868 |
|
| 869 |
def __setitem__(self, stdint.uint32_t index, AMQPValue value): |
| 870 |
assert value.type |
| 871 |
if index >= self.size: |
| 872 |
raise IndexError("Index is out of range.") |
| 873 |
if c_amqpvalue.amqpvalue_set_composite_item(self._c_value, index, value._c_value) != 0: |
| 874 |
self._value_error() |
| 875 |
|
| 876 |
@property |
| 877 |
def size(self): |
| 878 |
assert self.type |
| 879 |
cdef stdint.uint32_t _value |
| 880 |
if c_amqpvalue.amqpvalue_get_composite_item_count(self._c_value, &_value) == 0: |
| 881 |
return _value |
| 882 |
else: |
| 883 |
self._value_error() |
| 884 |
|
| 885 |
def pop(self, stdint.uint32_t index): |
| 886 |
if index >= self.size: |
| 887 |
raise IndexError("Index is out of range.") |
| 888 |
cdef c_amqpvalue.AMQP_VALUE value |
| 889 |
value = c_amqpvalue.amqpvalue_get_composite_item(self._c_value, index) |
| 890 |
if <void*>value == NULL: |
| 891 |
self._value_error() |
| 892 |
try: |
| 893 |
return value_factory(value) |
| 894 |
except TypeError: |
| 895 |
raise IndexError("No item found at index {}".format(index)) |
| 896 |
|
| 897 |
|
| 898 |
cdef class DescribedValue(AMQPValue): |
| 899 |
|
| 900 |
_type = AMQPType.DescribedType |
| 901 |
|
| 902 |
def create(self, AMQPValue descriptor, AMQPValue value): |
| 903 |
cdef c_amqpvalue.AMQP_VALUE cloned_descriptor |
| 904 |
cdef c_amqpvalue.AMQP_VALUE cloned_value |
| 905 |
cloned_descriptor = c_amqpvalue.amqpvalue_clone(<c_amqpvalue.AMQP_VALUE>descriptor._c_value) |
| 906 |
if <void*>cloned_descriptor == NULL: |
| 907 |
self._value_error() |
| 908 |
cloned_value = c_amqpvalue.amqpvalue_clone(<c_amqpvalue.AMQP_VALUE>value._c_value) |
| 909 |
if <void*>cloned_value == NULL: |
| 910 |
self._value_error() |
| 911 |
new_value = c_amqpvalue.amqpvalue_create_described(cloned_descriptor, cloned_value) |
| 912 |
self.wrap(new_value) |
| 913 |
|
| 914 |
@property |
| 915 |
def description(self): |
| 916 |
cdef c_amqpvalue.AMQP_VALUE value |
| 917 |
cdef c_amqpvalue.AMQP_VALUE cloned |
| 918 |
value = c_amqpvalue.amqpvalue_get_inplace_descriptor(self._c_value) |
| 919 |
if <void*>value == NULL: |
| 920 |
self._value_error() |
| 921 |
cloned = c_amqpvalue.amqpvalue_clone(value) |
| 922 |
if <void*>cloned == NULL: |
| 923 |
self._value_error() |
| 924 |
return value_factory(cloned) |
| 925 |
|
| 926 |
@property |
| 927 |
def data(self): |
| 928 |
cdef c_amqpvalue.AMQP_VALUE value |
| 929 |
cdef c_amqpvalue.AMQP_VALUE cloned |
| 930 |
value = c_amqpvalue.amqpvalue_get_inplace_described_value(self._c_value) |
| 931 |
if <void*>value == NULL: |
| 932 |
self._value_error() |
| 933 |
cloned = c_amqpvalue.amqpvalue_clone(value) |
| 934 |
if <void*>cloned == NULL: |
| 935 |
self._value_error() |
| 936 |
return value_factory(cloned) |
| 937 |
|
| 938 |
@property |
| 939 |
def value(self): |
| 940 |
assert self.type |
| 941 |
#descriptor = self.description |
| 942 |
described = self.data |
| 943 |
return copy.deepcopy(described.value) |