| 1 |
# |
| 2 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
# or more contributor license agreements. See the NOTICE file |
| 4 |
# distributed with this work for additional information |
| 5 |
# regarding copyright ownership. The ASF licenses this file |
| 6 |
# to you under the Apache License, Version 2.0 (the |
| 7 |
# "License"); you may not use this file except in compliance |
| 8 |
# with the License. You may obtain a copy of the License at |
| 9 |
# |
| 10 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
# |
| 12 |
# Unless required by applicable law or agreed to in writing, |
| 13 |
# software distributed under the License is distributed on an |
| 14 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
# KIND, either express or implied. See the License for the |
| 16 |
# specific language governing permissions and limitations |
| 17 |
# under the License. |
| 18 |
# |
| 19 |
|
| 20 |
import uuid |
| 21 |
from typing import Callable, List, Tuple, Union, Optional, Any, Dict, Iterable, TypeVar |
| 22 |
try: |
| 23 |
from typing import Literal |
| 24 |
except ImportError: |
| 25 |
# https://www.python.org/dev/peps/pep-0560/#class-getitem |
| 26 |
class GenericMeta(type): |
| 27 |
def __getitem__(self, item): |
| 28 |
pass |
| 29 |
|
| 30 |
class Literal(metaclass=GenericMeta): # type: ignore[no-redef] |
| 31 |
pass |
| 32 |
|
| 33 |
from cproton import PN_ARRAY, PN_BINARY, PN_BOOL, PN_BYTE, PN_CHAR, PN_DECIMAL128, PN_DECIMAL32, PN_DECIMAL64, \ |
| 34 |
PN_DESCRIBED, PN_DOUBLE, PN_FLOAT, PN_INT, PN_LIST, PN_LONG, PN_MAP, PN_NULL, PN_OVERFLOW, PN_SHORT, PN_STRING, \ |
| 35 |
PN_SYMBOL, PN_TIMESTAMP, PN_UBYTE, PN_UINT, PN_ULONG, PN_USHORT, PN_UUID, pn_data, pn_data_clear, pn_data_copy, \ |
| 36 |
pn_data_decode, pn_data_dump, pn_data_encode, pn_data_encoded_size, pn_data_enter, pn_data_error, pn_data_exit, \ |
| 37 |
pn_data_format, pn_data_free, pn_data_get_array, pn_data_get_array_type, pn_data_get_binary, pn_data_get_bool, \ |
| 38 |
pn_data_get_byte, pn_data_get_char, pn_data_get_decimal128, pn_data_get_decimal32, pn_data_get_decimal64, \ |
| 39 |
pn_data_get_double, pn_data_get_float, pn_data_get_int, pn_data_get_list, pn_data_get_long, pn_data_get_map, \ |
| 40 |
pn_data_get_short, pn_data_get_string, pn_data_get_symbol, pn_data_get_timestamp, pn_data_get_ubyte, \ |
| 41 |
pn_data_get_uint, pn_data_get_ulong, pn_data_get_ushort, pn_data_get_uuid, pn_data_is_array_described, \ |
| 42 |
pn_data_is_described, pn_data_is_null, pn_data_lookup, pn_data_narrow, pn_data_next, pn_data_prev, \ |
| 43 |
pn_data_put_array, pn_data_put_binary, pn_data_put_bool, pn_data_put_byte, pn_data_put_char, pn_data_put_decimal128, \ |
| 44 |
pn_data_put_decimal32, pn_data_put_decimal64, pn_data_put_described, pn_data_put_double, pn_data_put_float, \ |
| 45 |
pn_data_put_int, pn_data_put_list, pn_data_put_long, pn_data_put_map, pn_data_put_null, pn_data_put_short, \ |
| 46 |
pn_data_put_string, pn_data_put_symbol, pn_data_put_timestamp, pn_data_put_ubyte, pn_data_put_uint, \ |
| 47 |
pn_data_put_ulong, pn_data_put_ushort, pn_data_put_uuid, pn_data_rewind, pn_data_type, pn_data_widen, pn_error_text |
| 48 |
|
| 49 |
from ._common import Constant |
| 50 |
from ._exceptions import DataException, EXCEPTIONS |
| 51 |
|
| 52 |
long = int |
| 53 |
unicode = str |
| 54 |
|
| 55 |
_T = TypeVar('_T') |
| 56 |
|
| 57 |
PythonAMQPData = Union[ |
| 58 |
Dict['PythonAMQPData', 'PythonAMQPData'], |
| 59 |
List['PythonAMQPData'], |
| 60 |
'Described', 'Array', int, str, 'symbol', bytes, float, None] |
| 61 |
"""This type annotation represents Python data structures that can be encoded as AMQP Data""" |
| 62 |
|
| 63 |
|
| 64 |
class UnmappedType: |
| 65 |
|
| 66 |
def __init__(self, msg: str) -> None: |
| 67 |
self.msg = msg |
| 68 |
|
| 69 |
def __repr__(self) -> str: |
| 70 |
return "UnmappedType(%s)" % self.msg |
| 71 |
|
| 72 |
|
| 73 |
class ulong(long): |
| 74 |
""" |
| 75 |
The ulong AMQP type. |
| 76 |
|
| 77 |
An unsigned 64 bit integer in the range :math:`0` to :math:`2^{64} - 1` inclusive. |
| 78 |
""" |
| 79 |
|
| 80 |
def __init__(self, l: int) -> None: |
| 81 |
if l < 0: |
| 82 |
raise AssertionError("initializing ulong with negative value") |
| 83 |
super(ulong, self).__new__(ulong, l) |
| 84 |
|
| 85 |
def __repr__(self) -> str: |
| 86 |
return "ulong(%s)" % long.__repr__(self) |
| 87 |
|
| 88 |
|
| 89 |
class timestamp(long): |
| 90 |
""" |
| 91 |
The timestamp AMQP type. |
| 92 |
|
| 93 |
An absolute point in time, represented by a signed 64 bit value measuring |
| 94 |
milliseconds since the epoch. This value is encoded using the Unix ``time_t`` |
| 95 |
[IEEE1003] encoding of UTC, but with a precision of milliseconds. For |
| 96 |
example, ``1311704463521`` represents the moment ``2011-07-26T18:21:03.521Z``. |
| 97 |
""" |
| 98 |
|
| 99 |
def __repr__(self) -> str: |
| 100 |
return "timestamp(%s)" % long.__repr__(self) |
| 101 |
|
| 102 |
|
| 103 |
class symbol(unicode): |
| 104 |
""" |
| 105 |
The symbol AMQP type. |
| 106 |
|
| 107 |
Symbolic values from a constrained domain, represented by a sequence of ASCII characters. |
| 108 |
""" |
| 109 |
|
| 110 |
def __repr__(self) -> str: |
| 111 |
return "symbol(%s)" % unicode.__repr__(self) |
| 112 |
|
| 113 |
|
| 114 |
class char(unicode): |
| 115 |
""" |
| 116 |
The char AMQP type. |
| 117 |
|
| 118 |
A 32 bit UTF-32BE encoded Unicode character. |
| 119 |
""" |
| 120 |
|
| 121 |
def __repr__(self) -> str: |
| 122 |
return "char(%s)" % unicode.__repr__(self) |
| 123 |
|
| 124 |
|
| 125 |
class byte(int): |
| 126 |
""" |
| 127 |
The byte AMQP type. |
| 128 |
|
| 129 |
An 8 bit signed integer in the range :math:`-(2^7)` to :math:`2^7 - 1` inclusive. |
| 130 |
""" |
| 131 |
|
| 132 |
def __repr__(self) -> str: |
| 133 |
return "byte(%s)" % int.__repr__(self) |
| 134 |
|
| 135 |
|
| 136 |
class short(int): |
| 137 |
""" |
| 138 |
The short AMQP type. |
| 139 |
|
| 140 |
A 16 bit signed integer in the range :math:`-(2^{15})` to :math:`2^{15} - 1` inclusive. |
| 141 |
""" |
| 142 |
|
| 143 |
def __repr__(self) -> str: |
| 144 |
return "short(%s)" % int.__repr__(self) |
| 145 |
|
| 146 |
|
| 147 |
class int32(int): |
| 148 |
""" |
| 149 |
The signed int AMQP type. |
| 150 |
|
| 151 |
A 32 bit signed integer in the range :math:`-(2^{31})` to :math:`2^{31} - 1` inclusive. |
| 152 |
""" |
| 153 |
|
| 154 |
def __repr__(self) -> str: |
| 155 |
return "int32(%s)" % int.__repr__(self) |
| 156 |
|
| 157 |
|
| 158 |
class ubyte(int): |
| 159 |
""" |
| 160 |
The unsigned byte AMQP type. |
| 161 |
|
| 162 |
An 8 bit unsigned integer in the range :math:`0` to :math:`2^8 - 1` inclusive. |
| 163 |
""" |
| 164 |
|
| 165 |
def __init__(self, i: int) -> None: |
| 166 |
if i < 0: |
| 167 |
raise AssertionError("initializing ubyte with negative value") |
| 168 |
super(ubyte, self).__new__(ubyte, i) |
| 169 |
|
| 170 |
def __repr__(self) -> str: |
| 171 |
return "ubyte(%s)" % int.__repr__(self) |
| 172 |
|
| 173 |
|
| 174 |
class ushort(int): |
| 175 |
""" |
| 176 |
The unsigned short AMQP type. |
| 177 |
|
| 178 |
A 16 bit unsigned integer in the range :math:`0` to :math:`2^{16} - 1` inclusive. |
| 179 |
""" |
| 180 |
|
| 181 |
def __init__(self, i: int) -> None: |
| 182 |
if i < 0: |
| 183 |
raise AssertionError("initializing ushort with negative value") |
| 184 |
super(ushort, self).__new__(ushort, i) |
| 185 |
|
| 186 |
def __repr__(self) -> str: |
| 187 |
return "ushort(%s)" % int.__repr__(self) |
| 188 |
|
| 189 |
|
| 190 |
class uint(long): |
| 191 |
""" |
| 192 |
The unsigned int AMQP type. |
| 193 |
|
| 194 |
A 32 bit unsigned integer in the range :math:`0` to :math:`2^{32} - 1` inclusive. |
| 195 |
""" |
| 196 |
|
| 197 |
def __init__(self, l: int) -> None: |
| 198 |
if l < 0: |
| 199 |
raise AssertionError("initializing uint with negative value") |
| 200 |
super(uint, self).__new__(uint, l) |
| 201 |
|
| 202 |
def __repr__(self) -> str: |
| 203 |
return "uint(%s)" % long.__repr__(self) |
| 204 |
|
| 205 |
|
| 206 |
class float32(float): |
| 207 |
""" |
| 208 |
The float AMQP type. |
| 209 |
|
| 210 |
A 32 bit floating point number (IEEE 754-2008 binary32). |
| 211 |
""" |
| 212 |
|
| 213 |
def __repr__(self) -> str: |
| 214 |
return "float32(%s)" % float.__repr__(self) |
| 215 |
|
| 216 |
|
| 217 |
class decimal32(int): |
| 218 |
""" |
| 219 |
The decimal32 AMQP type. |
| 220 |
|
| 221 |
A 32 bit decimal floating point number (IEEE 754-2008 decimal32). |
| 222 |
""" |
| 223 |
|
| 224 |
def __repr__(self) -> str: |
| 225 |
return "decimal32(%s)" % int.__repr__(self) |
| 226 |
|
| 227 |
|
| 228 |
class decimal64(long): |
| 229 |
""" |
| 230 |
The decimal64 AMQP type. |
| 231 |
|
| 232 |
A 64 bit decimal floating point number (IEEE 754-2008 decimal64). |
| 233 |
""" |
| 234 |
|
| 235 |
def __repr__(self) -> str: |
| 236 |
return "decimal64(%s)" % long.__repr__(self) |
| 237 |
|
| 238 |
|
| 239 |
class decimal128(bytes): |
| 240 |
""" |
| 241 |
The decimal128 AMQP type. |
| 242 |
|
| 243 |
A 128-bit decimal floating-point number (IEEE 754-2008 decimal128). |
| 244 |
""" |
| 245 |
|
| 246 |
def __repr__(self) -> str: |
| 247 |
return "decimal128(%s)" % bytes.__repr__(self) |
| 248 |
|
| 249 |
|
| 250 |
class Described(object): |
| 251 |
""" |
| 252 |
A described AMQP type. |
| 253 |
|
| 254 |
:ivar descriptor: Any AMQP value can be a descriptor |
| 255 |
:ivar value: The described value |
| 256 |
""" |
| 257 |
|
| 258 |
def __init__( |
| 259 |
self, |
| 260 |
descriptor: PythonAMQPData, |
| 261 |
value: PythonAMQPData, |
| 262 |
) -> None: |
| 263 |
self.descriptor = descriptor |
| 264 |
self.value = value |
| 265 |
|
| 266 |
def __repr__(self) -> str: |
| 267 |
return "Described(%r, %r)" % (self.descriptor, self.value) |
| 268 |
|
| 269 |
def __eq__(self, o: Any) -> bool: |
| 270 |
if isinstance(o, Described): |
| 271 |
return self.descriptor == o.descriptor and self.value == o.value |
| 272 |
else: |
| 273 |
return False |
| 274 |
|
| 275 |
|
| 276 |
UNDESCRIBED = Constant("UNDESCRIBED") |
| 277 |
|
| 278 |
|
| 279 |
class Array(object): |
| 280 |
""" |
| 281 |
An AMQP array, a sequence of AMQP values of a single type. |
| 282 |
|
| 283 |
This class provides a convenient way to handle AMQP arrays when used with |
| 284 |
convenience methods :func:`Data.get_py_array` and :func:`Data.put_py_array`. |
| 285 |
|
| 286 |
:ivar descriptor: Optional descriptor if the array is to be described, otherwise ``None`` |
| 287 |
:ivar type: Array element type, as an integer. The :class:`Data` class has constants defined |
| 288 |
for all the valid AMQP types. For example, for an array of double values, use |
| 289 |
:const:`Data.DOUBLE`, which has integer value 14. |
| 290 |
:ivar elements: A Python list of elements of the appropriate type. |
| 291 |
""" |
| 292 |
|
| 293 |
def __init__( |
| 294 |
self, |
| 295 |
descriptor: PythonAMQPData, |
| 296 |
type: int, |
| 297 |
*elements |
| 298 |
) -> None: |
| 299 |
self.descriptor = descriptor |
| 300 |
self.type = type |
| 301 |
self.elements = elements |
| 302 |
|
| 303 |
def __iter__(self): |
| 304 |
return iter(self.elements) |
| 305 |
|
| 306 |
def __repr__(self) -> str: |
| 307 |
if self.elements: |
| 308 |
els = ", %s" % (", ".join(map(repr, self.elements))) |
| 309 |
else: |
| 310 |
els = "" |
| 311 |
return "Array(%r, %r%s)" % (self.descriptor, self.type, els) |
| 312 |
|
| 313 |
def __eq__(self, o: Any) -> bool: |
| 314 |
if isinstance(o, Array): |
| 315 |
return self.descriptor == o.descriptor and \ |
| 316 |
self.type == o.type and self.elements == o.elements |
| 317 |
else: |
| 318 |
return False |
| 319 |
|
| 320 |
|
| 321 |
def _check_type( |
| 322 |
s: _T, |
| 323 |
allow_ulong: bool = False, |
| 324 |
raise_on_error: bool = True |
| 325 |
) -> Union[symbol, ulong, _T]: |
| 326 |
if isinstance(s, symbol): |
| 327 |
return s |
| 328 |
if allow_ulong and isinstance(s, ulong): |
| 329 |
return s |
| 330 |
if isinstance(s, str): |
| 331 |
return symbol(s) |
| 332 |
if raise_on_error: |
| 333 |
raise TypeError('Non-symbol type %s: %s' % (type(s), s)) |
| 334 |
return s |
| 335 |
|
| 336 |
|
| 337 |
def _check_is_symbol(s: _T, raise_on_error: bool = True) -> Union[symbol, ulong, _T]: |
| 338 |
return _check_type(s, allow_ulong=False, raise_on_error=raise_on_error) |
| 339 |
|
| 340 |
|
| 341 |
def _check_is_symbol_or_ulong(s: _T, raise_on_error: bool = True) -> Union[symbol, ulong, _T]: |
| 342 |
return _check_type(s, allow_ulong=True, raise_on_error=raise_on_error) |
| 343 |
|
| 344 |
|
| 345 |
class RestrictedKeyDict(dict): |
| 346 |
"""Parent class for :class:`PropertyDict` and :class:`AnnotationDict`""" |
| 347 |
|
| 348 |
def __init__( |
| 349 |
self, |
| 350 |
validation_fn: Callable[[_T, bool], _T], |
| 351 |
e: Optional[Any] = None, |
| 352 |
raise_on_error: bool = True, |
| 353 |
**kwargs |
| 354 |
) -> None: |
| 355 |
super(RestrictedKeyDict, self).__init__() |
| 356 |
self.validation_fn = validation_fn |
| 357 |
self.raise_on_error = raise_on_error |
| 358 |
self.update(e, **kwargs) |
| 359 |
|
| 360 |
def __setitem__(self, key: Union[symbol, str], value: Any) -> None: |
| 361 |
"""Checks if the key is a :class:`symbol` type before setting the value""" |
| 362 |
try: |
| 363 |
return super(RestrictedKeyDict, self).__setitem__(self.validation_fn(key, self.raise_on_error), value) |
| 364 |
except TypeError: |
| 365 |
pass |
| 366 |
# __setitem__() must raise a KeyError, not TypeError |
| 367 |
raise KeyError('invalid non-symbol key: %s: %s' % (type(key), key)) |
| 368 |
|
| 369 |
def update(self, e: Optional[Any] = None, **kwargs) -> None: |
| 370 |
""" |
| 371 |
Equivalent to dict.update(), but it was needed to call :meth:`__setitem__()` |
| 372 |
instead of ``dict.__setitem__()``. |
| 373 |
""" |
| 374 |
if e: |
| 375 |
try: |
| 376 |
for k in e: |
| 377 |
self.__setitem__(k, e[k]) |
| 378 |
except TypeError: |
| 379 |
self.__setitem__(k[0], k[1]) # use tuple consumed from from zip |
| 380 |
for (k, v) in e: |
| 381 |
self.__setitem__(k, v) |
| 382 |
for k in kwargs: |
| 383 |
self.__setitem__(k, kwargs[k]) |
| 384 |
|
| 385 |
|
| 386 |
class PropertyDict(RestrictedKeyDict): |
| 387 |
""" |
| 388 |
A dictionary that only takes :class:`symbol` types as a key. |
| 389 |
However, if a string key is provided, it will be silently converted |
| 390 |
into a symbol key. |
| 391 |
|
| 392 |
>>> from proton import symbol, ulong, PropertyDict |
| 393 |
>>> a = PropertyDict(one=1, two=2) |
| 394 |
>>> b = PropertyDict({'one':1, symbol('two'):2}) |
| 395 |
>>> c = PropertyDict(zip(['one', symbol('two')], [1, 2])) |
| 396 |
>>> d = PropertyDict([(symbol('one'), 1), ('two', 2)]) |
| 397 |
>>> e = PropertyDict(a) |
| 398 |
>>> a == b == c == d == e |
| 399 |
True |
| 400 |
|
| 401 |
By default, non-string and non-symbol keys cause a ``KeyError`` to be raised: |
| 402 |
|
| 403 |
>>> PropertyDict({'one':1, 2:'two'}) |
| 404 |
... |
| 405 |
KeyError: "invalid non-symbol key: <type 'int'>: 2" |
| 406 |
|
| 407 |
but by setting ``raise_on_error=False``, non-string and non-symbol keys will be ignored: |
| 408 |
|
| 409 |
>>> PropertyDict({'one':1, 2:'two'}, raise_on_error=False) |
| 410 |
PropertyDict({2: 'two', symbol(u'one'): 1}) |
| 411 |
|
| 412 |
:param e: Initialization for ``dict`` |
| 413 |
:type e: ``dict`` or ``list`` of ``tuple`` or ``zip`` object |
| 414 |
:param raise_on_error: If ``True``, will raise an ``KeyError`` if a non-string or non-symbol |
| 415 |
is encountered as a key in the initialization, or in a subsequent operation which |
| 416 |
adds such an key. If ``False``, non-strings and non-symbols will be added as keys |
| 417 |
to the dictionary without an error. |
| 418 |
:param kwargs: Keyword args for initializing a ``dict`` of the form key1=val1, key2=val2, ... |
| 419 |
""" |
| 420 |
|
| 421 |
def __init__(self, e: Optional[Any] = None, raise_on_error: bool = True, **kwargs) -> None: |
| 422 |
super(PropertyDict, self).__init__(_check_is_symbol, e, raise_on_error, **kwargs) |
| 423 |
|
| 424 |
def __repr__(self): |
| 425 |
""" Representation of PropertyDict """ |
| 426 |
return 'PropertyDict(%s)' % super(PropertyDict, self).__repr__() |
| 427 |
|
| 428 |
|
| 429 |
class AnnotationDict(RestrictedKeyDict): |
| 430 |
""" |
| 431 |
A dictionary that only takes :class:`symbol` or :class:`ulong` types |
| 432 |
as a key. However, if a string key is provided, it will be silently |
| 433 |
converted into a symbol key. |
| 434 |
|
| 435 |
>>> from proton import symbol, ulong, AnnotationDict |
| 436 |
>>> a = AnnotationDict(one=1, two=2) |
| 437 |
>>> a[ulong(3)] = 'three' |
| 438 |
>>> b = AnnotationDict({'one':1, symbol('two'):2, ulong(3):'three'}) |
| 439 |
>>> c = AnnotationDict(zip([symbol('one'), 'two', ulong(3)], [1, 2, 'three'])) |
| 440 |
>>> d = AnnotationDict([('one', 1), (symbol('two'), 2), (ulong(3), 'three')]) |
| 441 |
>>> e = AnnotationDict(a) |
| 442 |
>>> a == b == c == d == e |
| 443 |
True |
| 444 |
|
| 445 |
By default, non-string, non-symbol and non-ulong keys cause a ``KeyError`` to be raised: |
| 446 |
|
| 447 |
>>> AnnotationDict({'one': 1, 2: 'two'}) |
| 448 |
... |
| 449 |
KeyError: "invalid non-symbol key: <type 'int'>: 2" |
| 450 |
|
| 451 |
but by setting ``raise_on_error=False``, non-string, non-symbol and non-ulong keys will be ignored: |
| 452 |
|
| 453 |
>>> AnnotationDict({'one': 1, 2: 'two'}, raise_on_error=False) |
| 454 |
AnnotationDict({2: 'two', symbol(u'one'): 1}) |
| 455 |
|
| 456 |
:param e: Initializer for ``dict``: a ``dict`` or ``list`` of ``tuple`` or ``zip`` object |
| 457 |
:param raise_on_error: If ``True``, will raise an ``KeyError`` if a non-string, non-symbol or |
| 458 |
non-ulong is encountered as a key in the initialization, or in a subsequent |
| 459 |
operation which adds such an key. If ``False``, non-strings, non-ulongs and non-symbols |
| 460 |
will be added as keys to the dictionary without an error. |
| 461 |
:param kwargs: Keyword args for initializing a ``dict`` of the form key1=val1, key2=val2, ... |
| 462 |
""" |
| 463 |
|
| 464 |
def __init__( |
| 465 |
self, |
| 466 |
e: Optional[Union[Dict, List, Tuple, Iterable]] = None, |
| 467 |
raise_on_error: bool = True, |
| 468 |
**kwargs |
| 469 |
) -> None: |
| 470 |
super(AnnotationDict, self).__init__(_check_is_symbol_or_ulong, e, raise_on_error, **kwargs) |
| 471 |
|
| 472 |
def __repr__(self): |
| 473 |
""" Representation of AnnotationDict """ |
| 474 |
return 'AnnotationDict(%s)' % super(AnnotationDict, self).__repr__() |
| 475 |
|
| 476 |
|
| 477 |
class SymbolList(list): |
| 478 |
""" |
| 479 |
A list that can only hold :class:`symbol` elements. However, if any string elements |
| 480 |
are present, they will be converted to symbols. |
| 481 |
|
| 482 |
>>> a = SymbolList(['one', symbol('two'), 'three']) |
| 483 |
>>> b = SymbolList([symbol('one'), 'two', symbol('three')]) |
| 484 |
>>> c = SymbolList(a) |
| 485 |
>>> a == b == c |
| 486 |
True |
| 487 |
|
| 488 |
By default, using any key other than a symbol or string will result in a ``TypeError``: |
| 489 |
|
| 490 |
>>> SymbolList(['one', symbol('two'), 3]) |
| 491 |
... |
| 492 |
TypeError: Non-symbol type <class 'int'>: 3 |
| 493 |
|
| 494 |
but by setting ``raise_on_error=False``, non-symbol and non-string keys will be ignored: |
| 495 |
|
| 496 |
>>> SymbolList(['one', symbol('two'), 3], raise_on_error=False) |
| 497 |
SymbolList([symbol(u'one'), symbol(u'two'), 3]) |
| 498 |
|
| 499 |
:param t: Initializer for list |
| 500 |
:param raise_on_error: If ``True``, will raise an ``TypeError`` if a non-string or non-symbol is |
| 501 |
encountered in the initialization list, or in a subsequent operation which adds such |
| 502 |
an element. If ``False``, non-strings and non-symbols will be added to the list without |
| 503 |
an error. |
| 504 |
""" |
| 505 |
|
| 506 |
def __init__( |
| 507 |
self, |
| 508 |
t: Optional[List[Any]] = None, |
| 509 |
raise_on_error: bool = True |
| 510 |
) -> None: |
| 511 |
super(SymbolList, self).__init__() |
| 512 |
self.raise_on_error = raise_on_error |
| 513 |
if t: |
| 514 |
self.extend(t) |
| 515 |
|
| 516 |
def _check_list(self, t: Iterable[Any]) -> List[Any]: |
| 517 |
""" Check all items in list are :class:`symbol`s (or are converted to symbols). """ |
| 518 |
l = [] |
| 519 |
if t: |
| 520 |
for v in t: |
| 521 |
l.append(_check_is_symbol(v, self.raise_on_error)) |
| 522 |
return l |
| 523 |
|
| 524 |
def append(self, v: str) -> None: |
| 525 |
""" Add a single value v to the end of the list """ |
| 526 |
return super(SymbolList, self).append(_check_is_symbol(v, self.raise_on_error)) |
| 527 |
|
| 528 |
def extend(self, t: Iterable[str]) -> None: |
| 529 |
""" Add all elements of an iterable t to the end of the list """ |
| 530 |
return super(SymbolList, self).extend(self._check_list(t)) |
| 531 |
|
| 532 |
def insert(self, i: int, v: str) -> None: |
| 533 |
""" Insert a value v at index i """ |
| 534 |
return super(SymbolList, self).insert(i, _check_is_symbol(v, self.raise_on_error)) |
| 535 |
|
| 536 |
def __add__(self, t: Iterable[Any]) -> 'SymbolList': |
| 537 |
""" Handles list1 + list2 """ |
| 538 |
return SymbolList(super(SymbolList, self).__add__(self._check_list(t)), raise_on_error=self.raise_on_error) |
| 539 |
|
| 540 |
def __iadd__(self, t): |
| 541 |
""" Handles list1 += list2 """ |
| 542 |
return super(SymbolList, self).__iadd__(self._check_list(t)) |
| 543 |
|
| 544 |
def __setitem__(self, i: int, t: Any) -> None: |
| 545 |
""" Handles list[i] = v """ |
| 546 |
return super(SymbolList, self).__setitem__(i, _check_is_symbol(t, self.raise_on_error)) |
| 547 |
|
| 548 |
def __repr__(self) -> str: |
| 549 |
""" Representation of SymbolList """ |
| 550 |
return 'SymbolList(%s)' % super(SymbolList, self).__repr__() |
| 551 |
|
| 552 |
|
| 553 |
class Data: |
| 554 |
""" |
| 555 |
The :class:`Data` class provides an interface for decoding, extracting, |
| 556 |
creating, and encoding arbitrary AMQP data. A :class:`Data` object |
| 557 |
contains a tree of AMQP values. Leaf nodes in this tree correspond |
| 558 |
to scalars in the AMQP type system such as :const:`ints <INT>` or |
| 559 |
:const:`strings <STRING>`. Non-leaf nodes in this tree correspond to |
| 560 |
compound values in the AMQP type system such as :const:`lists <LIST>`, |
| 561 |
:const:`maps <MAP>`, :const:`arrays <ARRAY>`, or :const:`described values <DESCRIBED>`. |
| 562 |
The root node of the tree is the :class:`Data` object itself and can have |
| 563 |
an arbitrary number of children. |
| 564 |
|
| 565 |
A :class:`Data` object maintains the notion of the current sibling node |
| 566 |
and a current parent node. Siblings are ordered within their parent. |
| 567 |
Values are accessed and/or added by using the :meth:`next`, :meth:`prev`, |
| 568 |
:meth:`enter`, and :meth:`exit` methods to navigate to the desired location in |
| 569 |
the tree and using the supplied variety of ``put_*`` / ``get_*`` methods to |
| 570 |
access or add a value of the desired type. |
| 571 |
|
| 572 |
The ``put_*`` methods will always add a value *after* the current node |
| 573 |
in the tree. If the current node has a next sibling the ``put_*`` method |
| 574 |
will overwrite the value on this node. If there is no current node |
| 575 |
or the current node has no next sibling then one will be added. The |
| 576 |
``put_*`` methods always set the added/modified node to the current |
| 577 |
node. The ``get_*`` methods read the value of the current node and do |
| 578 |
not change which node is current. |
| 579 |
|
| 580 |
The following types of scalar values are supported: |
| 581 |
|
| 582 |
* :const:`NULL` |
| 583 |
* :const:`BOOL` |
| 584 |
* :const:`UBYTE` |
| 585 |
* :const:`USHORT` |
| 586 |
* :const:`SHORT` |
| 587 |
* :const:`UINT` |
| 588 |
* :const:`INT` |
| 589 |
* :const:`ULONG` |
| 590 |
* :const:`LONG` |
| 591 |
* :const:`FLOAT` |
| 592 |
* :const:`DOUBLE` |
| 593 |
* :const:`BINARY` |
| 594 |
* :const:`STRING` |
| 595 |
* :const:`SYMBOL` |
| 596 |
|
| 597 |
The following types of compound values are supported: |
| 598 |
|
| 599 |
* :const:`DESCRIBED` |
| 600 |
* :const:`ARRAY` |
| 601 |
* :const:`LIST` |
| 602 |
* :const:`MAP` |
| 603 |
""" |
| 604 |
|
| 605 |
NULL = PN_NULL #: A null value. |
| 606 |
BOOL = PN_BOOL #: A boolean value. |
| 607 |
UBYTE = PN_UBYTE #: An unsigned byte value. |
| 608 |
BYTE = PN_BYTE #: A signed byte value. |
| 609 |
USHORT = PN_USHORT #: An unsigned short value. |
| 610 |
SHORT = PN_SHORT #: A short value. |
| 611 |
UINT = PN_UINT #: An unsigned int value. |
| 612 |
INT = PN_INT #: A signed int value. |
| 613 |
CHAR = PN_CHAR #: A character value. |
| 614 |
ULONG = PN_ULONG #: An unsigned long value. |
| 615 |
LONG = PN_LONG #: A signed long value. |
| 616 |
TIMESTAMP = PN_TIMESTAMP #: A timestamp value. |
| 617 |
FLOAT = PN_FLOAT #: A float value. |
| 618 |
DOUBLE = PN_DOUBLE #: A double value. |
| 619 |
DECIMAL32 = PN_DECIMAL32 #: A DECIMAL32 value. |
| 620 |
DECIMAL64 = PN_DECIMAL64 #: A DECIMAL64 value. |
| 621 |
DECIMAL128 = PN_DECIMAL128 #: A DECIMAL128 value. |
| 622 |
UUID = PN_UUID #: A UUID value. |
| 623 |
BINARY = PN_BINARY #: A binary string. |
| 624 |
STRING = PN_STRING #: A unicode string. |
| 625 |
SYMBOL = PN_SYMBOL #: A symbolic string. |
| 626 |
DESCRIBED = PN_DESCRIBED #: A described value. |
| 627 |
ARRAY = PN_ARRAY #: An array value. |
| 628 |
LIST = PN_LIST #: A list value. |
| 629 |
MAP = PN_MAP #: A map value. |
| 630 |
|
| 631 |
type_names = { |
| 632 |
NULL: "null", |
| 633 |
BOOL: "bool", |
| 634 |
BYTE: "byte", |
| 635 |
UBYTE: "ubyte", |
| 636 |
SHORT: "short", |
| 637 |
USHORT: "ushort", |
| 638 |
INT: "int", |
| 639 |
UINT: "uint", |
| 640 |
CHAR: "char", |
| 641 |
LONG: "long", |
| 642 |
ULONG: "ulong", |
| 643 |
TIMESTAMP: "timestamp", |
| 644 |
FLOAT: "float", |
| 645 |
DOUBLE: "double", |
| 646 |
DECIMAL32: "decimal32", |
| 647 |
DECIMAL64: "decimal64", |
| 648 |
DECIMAL128: "decimal128", |
| 649 |
UUID: "uuid", |
| 650 |
BINARY: "binary", |
| 651 |
STRING: "string", |
| 652 |
SYMBOL: "symbol", |
| 653 |
DESCRIBED: "described", |
| 654 |
ARRAY: "array", |
| 655 |
LIST: "list", |
| 656 |
MAP: "map" |
| 657 |
} |
| 658 |
""" |
| 659 |
A map which uses the enumerated type as a key to get a text name for the type. |
| 660 |
""" |
| 661 |
|
| 662 |
@classmethod |
| 663 |
def type_name(cls, amqptype: int) -> str: |
| 664 |
""" |
| 665 |
Return a string name for an AMQP type. |
| 666 |
|
| 667 |
:param amqptype: Numeric Proton AMQP type (`enum pn_type_t`) |
| 668 |
:returns: String describing the AMQP type with numeric value `amqptype` |
| 669 |
""" |
| 670 |
return Data.type_names[amqptype] |
| 671 |
|
| 672 |
def __init__(self, capacity: int = 16) -> None: |
| 673 |
if isinstance(capacity, (int, long)): |
| 674 |
self._data = pn_data(capacity) |
| 675 |
self._free = True |
| 676 |
else: |
| 677 |
self._data = capacity |
| 678 |
self._free = False |
| 679 |
|
| 680 |
def __del__(self) -> None: |
| 681 |
if self._free and hasattr(self, "_data"): |
| 682 |
pn_data_free(self._data) |
| 683 |
del self._data |
| 684 |
|
| 685 |
def _check(self, err: int) -> int: |
| 686 |
if err < 0: |
| 687 |
exc = EXCEPTIONS.get(err, DataException) |
| 688 |
raise exc("[%s]: %s" % (err, pn_error_text(pn_data_error(self._data)))) |
| 689 |
else: |
| 690 |
return err |
| 691 |
|
| 692 |
def clear(self) -> None: |
| 693 |
""" |
| 694 |
Clears the data object. |
| 695 |
""" |
| 696 |
pn_data_clear(self._data) |
| 697 |
|
| 698 |
def rewind(self) -> None: |
| 699 |
""" |
| 700 |
Clears current node and sets the parent to the root node. Clearing the |
| 701 |
current node sets it _before_ the first node, calling next() will advance to |
| 702 |
the first node. |
| 703 |
""" |
| 704 |
assert self._data is not None |
| 705 |
pn_data_rewind(self._data) |
| 706 |
|
| 707 |
def next(self) -> Optional[int]: |
| 708 |
""" |
| 709 |
Advances the current node to its next sibling and returns its |
| 710 |
type. If there is no next sibling the current node remains |
| 711 |
unchanged and ``None`` is returned. |
| 712 |
|
| 713 |
:return: Node type or ``None`` |
| 714 |
""" |
| 715 |
found = pn_data_next(self._data) |
| 716 |
if found: |
| 717 |
return self.type() |
| 718 |
else: |
| 719 |
return None |
| 720 |
|
| 721 |
def prev(self) -> Optional[int]: |
| 722 |
""" |
| 723 |
Advances the current node to its previous sibling and returns its |
| 724 |
type. If there is no previous sibling the current node remains |
| 725 |
unchanged and ``None`` is returned. |
| 726 |
|
| 727 |
:return: Node type or ``None`` |
| 728 |
""" |
| 729 |
found = pn_data_prev(self._data) |
| 730 |
if found: |
| 731 |
return self.type() |
| 732 |
else: |
| 733 |
return None |
| 734 |
|
| 735 |
def enter(self) -> bool: |
| 736 |
""" |
| 737 |
Sets the parent node to the current node and clears the current node. |
| 738 |
Clearing the current node sets it *before* the first child, |
| 739 |
call :meth:`next` to advance to the first child. |
| 740 |
|
| 741 |
:return: ``True`` iff the pointers to the current/parent nodes are changed, |
| 742 |
``False`` otherwise. |
| 743 |
""" |
| 744 |
return pn_data_enter(self._data) |
| 745 |
|
| 746 |
def exit(self) -> bool: |
| 747 |
""" |
| 748 |
Sets the current node to the parent node and the parent node to |
| 749 |
its own parent. |
| 750 |
|
| 751 |
:return: ``True`` iff the pointers to the current/parent nodes are changed, |
| 752 |
``False`` otherwise. |
| 753 |
""" |
| 754 |
return pn_data_exit(self._data) |
| 755 |
|
| 756 |
def lookup(self, name: str) -> bool: |
| 757 |
return pn_data_lookup(self._data, name) |
| 758 |
|
| 759 |
def narrow(self) -> None: |
| 760 |
""" |
| 761 |
Modify this :class:`Data` object to behave as if the current node is the |
| 762 |
root node of the tree. This impacts the behavior of :meth:`rewind`, |
| 763 |
:meth:`next`, :meth:`prev`, and anything else that depends on the |
| 764 |
navigational state of the :class:`Data` object. Use :meth:`widen` |
| 765 |
to reverse the effect of this operation. |
| 766 |
""" |
| 767 |
pn_data_narrow(self._data) |
| 768 |
|
| 769 |
def widen(self) -> None: |
| 770 |
""" Reverse the effect of :meth:`narrow`. """ |
| 771 |
pn_data_widen(self._data) |
| 772 |
|
| 773 |
def type(self) -> Optional[int]: |
| 774 |
""" |
| 775 |
Returns the type of the current node. Returns `None` if there is no |
| 776 |
current node. |
| 777 |
|
| 778 |
:return: The current node type enumeration. |
| 779 |
""" |
| 780 |
dtype = pn_data_type(self._data) |
| 781 |
if dtype == -1: |
| 782 |
return None |
| 783 |
else: |
| 784 |
return dtype |
| 785 |
|
| 786 |
def encoded_size(self) -> int: |
| 787 |
""" |
| 788 |
Returns the size in bytes needed to encode the data in AMQP format. |
| 789 |
|
| 790 |
:return: The size of the encoded data or an error code if data is invalid. |
| 791 |
""" |
| 792 |
return pn_data_encoded_size(self._data) |
| 793 |
|
| 794 |
def encode(self) -> bytes: |
| 795 |
""" |
| 796 |
Returns a binary representation of the data encoded in AMQP format. |
| 797 |
|
| 798 |
:return: The encoded data |
| 799 |
:raise: :exc:`DataException` if there is a Proton error. |
| 800 |
""" |
| 801 |
size = 1024 |
| 802 |
while True: |
| 803 |
cd, enc = pn_data_encode(self._data, size) |
| 804 |
if cd == PN_OVERFLOW: |
| 805 |
size *= 2 |
| 806 |
elif cd >= 0: |
| 807 |
return enc |
| 808 |
else: |
| 809 |
self._check(cd) |
| 810 |
|
| 811 |
def decode(self, encoded: bytes) -> int: |
| 812 |
""" |
| 813 |
Decodes the first value from supplied AMQP data and returns the |
| 814 |
number of bytes consumed. |
| 815 |
|
| 816 |
:param encoded: AMQP encoded binary data |
| 817 |
:raise: :exc:`DataException` if there is a Proton error. |
| 818 |
""" |
| 819 |
return self._check(pn_data_decode(self._data, encoded)) |
| 820 |
|
| 821 |
def put_list(self) -> None: |
| 822 |
""" |
| 823 |
Puts a list value. Elements may be filled by entering the list |
| 824 |
node and putting element values. |
| 825 |
|
| 826 |
>>> data = Data() |
| 827 |
>>> data.put_list() |
| 828 |
>>> data.enter() |
| 829 |
>>> data.put_int(1) |
| 830 |
>>> data.put_int(2) |
| 831 |
>>> data.put_int(3) |
| 832 |
>>> data.exit() |
| 833 |
|
| 834 |
:raise: :exc:`DataException` if there is a Proton error. |
| 835 |
""" |
| 836 |
self._check(pn_data_put_list(self._data)) |
| 837 |
|
| 838 |
def put_map(self) -> None: |
| 839 |
""" |
| 840 |
Puts a map value. Elements may be filled by entering the map node |
| 841 |
and putting alternating key value pairs. |
| 842 |
|
| 843 |
>>> data = Data() |
| 844 |
>>> data.put_map() |
| 845 |
>>> data.enter() |
| 846 |
>>> data.put_string("key") |
| 847 |
>>> data.put_string("value") |
| 848 |
>>> data.exit() |
| 849 |
|
| 850 |
:raise: :exc:`DataException` if there is a Proton error. |
| 851 |
""" |
| 852 |
self._check(pn_data_put_map(self._data)) |
| 853 |
|
| 854 |
def put_array(self, described: bool, element_type: int) -> None: |
| 855 |
""" |
| 856 |
Puts an array value. Elements may be filled by entering the array |
| 857 |
node and putting the element values. The values must all be of the |
| 858 |
specified array element type. If an array is described then the |
| 859 |
first child value of the array is the descriptor and may be of any |
| 860 |
type. |
| 861 |
|
| 862 |
>>> data = Data() |
| 863 |
>>> |
| 864 |
>>> data.put_array(False, Data.INT) |
| 865 |
>>> data.enter() |
| 866 |
>>> data.put_int(1) |
| 867 |
>>> data.put_int(2) |
| 868 |
>>> data.put_int(3) |
| 869 |
>>> data.exit() |
| 870 |
>>> |
| 871 |
>>> data.put_array(True, Data.DOUBLE) |
| 872 |
>>> data.enter() |
| 873 |
>>> data.put_symbol("array-descriptor") |
| 874 |
>>> data.put_double(1.1) |
| 875 |
>>> data.put_double(1.2) |
| 876 |
>>> data.put_double(1.3) |
| 877 |
>>> data.exit() |
| 878 |
|
| 879 |
:param described: specifies whether the array is described |
| 880 |
:param element_type: the type of the array elements |
| 881 |
:raise: :exc:`DataException` if there is a Proton error. |
| 882 |
""" |
| 883 |
self._check(pn_data_put_array(self._data, described, element_type)) |
| 884 |
|
| 885 |
def put_described(self) -> None: |
| 886 |
""" |
| 887 |
Puts a described value. A described node has two children, the |
| 888 |
descriptor and the value. These are specified by entering the node |
| 889 |
and putting the desired values. |
| 890 |
|
| 891 |
>>> data = Data() |
| 892 |
>>> data.put_described() |
| 893 |
>>> data.enter() |
| 894 |
>>> data.put_symbol("value-descriptor") |
| 895 |
>>> data.put_string("the value") |
| 896 |
>>> data.exit() |
| 897 |
|
| 898 |
:raise: :exc:`DataException` if there is a Proton error. |
| 899 |
""" |
| 900 |
self._check(pn_data_put_described(self._data)) |
| 901 |
|
| 902 |
def put_null(self) -> None: |
| 903 |
""" |
| 904 |
Puts a null value. |
| 905 |
|
| 906 |
:raise: :exc:`DataException` if there is a Proton error. |
| 907 |
""" |
| 908 |
self._check(pn_data_put_null(self._data)) |
| 909 |
|
| 910 |
def put_bool(self, b: Union[bool, int]) -> None: |
| 911 |
""" |
| 912 |
Puts a boolean value. |
| 913 |
|
| 914 |
:param b: a boolean value |
| 915 |
:raise: :exc:`DataException` if there is a Proton error. |
| 916 |
""" |
| 917 |
self._check(pn_data_put_bool(self._data, b)) |
| 918 |
|
| 919 |
def put_ubyte(self, ub: Union[ubyte, int]) -> None: |
| 920 |
""" |
| 921 |
Puts an unsigned byte value. |
| 922 |
|
| 923 |
:param ub: an integral value in the range :math:`0` to :math:`2^8 - 1` inclusive |
| 924 |
:raise: * ``AssertionError`` if parameter is out of the range :math:`0` to :math:`2^8 - 1` inclusive. |
| 925 |
* :exc:`DataException` if there is a Proton error. |
| 926 |
""" |
| 927 |
self._check(pn_data_put_ubyte(self._data, ub)) |
| 928 |
|
| 929 |
def put_byte(self, b: Union[byte, int]) -> None: |
| 930 |
""" |
| 931 |
Puts a signed byte value. |
| 932 |
|
| 933 |
:param b: an integral value in the range :math:`-(2^7)` to :math:`2^7 - 1` inclusive. |
| 934 |
:raise: * ``AssertionError`` if parameter is out of the range :math:`-(2^7)` to :math:`2^7 - 1` inclusive. |
| 935 |
* :exc:`DataException` if there is a Proton error. |
| 936 |
""" |
| 937 |
self._check(pn_data_put_byte(self._data, b)) |
| 938 |
|
| 939 |
def put_ushort(self, us: Union[ushort, int]) -> None: |
| 940 |
""" |
| 941 |
Puts an unsigned short value. |
| 942 |
|
| 943 |
:param us: an integral value in the range :math:`0` to :math:`2^{16} - 1` inclusive. |
| 944 |
:raise: * ``AssertionError`` if parameter is out of the range :math:`0` to :math:`2^{16} - 1` inclusive. |
| 945 |
* :exc:`DataException` if there is a Proton error. |
| 946 |
""" |
| 947 |
self._check(pn_data_put_ushort(self._data, us)) |
| 948 |
|
| 949 |
def put_short(self, s: Union[short, int]) -> None: |
| 950 |
""" |
| 951 |
Puts a signed short value. |
| 952 |
|
| 953 |
:param s: an integral value in the range :math:`-(2^{15})` to :math:`2^{15} - 1` inclusive. |
| 954 |
:raise: * ``AssertionError`` if parameter is out of the range :math:`-(2^{15})` to :math:`2^{15} - 1` inclusive. |
| 955 |
* :exc:`DataException` if there is a Proton error. |
| 956 |
""" |
| 957 |
self._check(pn_data_put_short(self._data, s)) |
| 958 |
|
| 959 |
def put_uint(self, ui: Union[uint, int]) -> None: |
| 960 |
""" |
| 961 |
Puts an unsigned int value. |
| 962 |
|
| 963 |
:param ui: an integral value in the range :math:`0` to :math:`2^{32} - 1` inclusive. |
| 964 |
:raise: * ``AssertionError`` if parameter is out of the range :math:`0` to :math:`2^{32} - 1` inclusive. |
| 965 |
* :exc:`DataException` if there is a Proton error. |
| 966 |
""" |
| 967 |
self._check(pn_data_put_uint(self._data, ui)) |
| 968 |
|
| 969 |
def put_int(self, i: Union[int32, int]) -> None: |
| 970 |
""" |
| 971 |
Puts a signed int value. |
| 972 |
|
| 973 |
:param i: an integral value in the range :math:`-(2^{31})` to :math:`2^{31} - 1` inclusive. |
| 974 |
:raise: * ``AssertionError`` if parameter is out of the range :math:`-(2^{31})` to :math:`2^{31} - 1` inclusive. |
| 975 |
* :exc:`DataException` if there is a Proton error. |
| 976 |
""" |
| 977 |
self._check(pn_data_put_int(self._data, i)) |
| 978 |
|
| 979 |
def put_char(self, c: Union[char, str]) -> None: |
| 980 |
""" |
| 981 |
Puts a char value. |
| 982 |
|
| 983 |
:param c: a single character |
| 984 |
:raise: :exc:`DataException` if there is a Proton error. |
| 985 |
""" |
| 986 |
self._check(pn_data_put_char(self._data, ord(c))) |
| 987 |
|
| 988 |
def put_ulong(self, ul: Union[ulong, int]) -> None: |
| 989 |
""" |
| 990 |
Puts an unsigned long value. |
| 991 |
|
| 992 |
:param ul: an integral value in the range :math:`0` to :math:`2^{64} - 1` inclusive. |
| 993 |
:raise: * ``AssertionError`` if parameter is out of the range :math:`0` to :math:`2^{64} - 1` inclusive. |
| 994 |
* :exc:`DataException` if there is a Proton error. |
| 995 |
""" |
| 996 |
self._check(pn_data_put_ulong(self._data, ul)) |
| 997 |
|
| 998 |
def put_long(self, l: Union[long, int]) -> None: |
| 999 |
""" |
| 1000 |
Puts a signed long value. |
| 1001 |
|
| 1002 |
:param l: an integral value in the range :math:`-(2^{63})` to :math:`2^{63} - 1` inclusive. |
| 1003 |
:raise: * ``AssertionError`` if parameter is out of the range :math:`-(2^{63})` to :math:`2^{63} - 1` inclusive. |
| 1004 |
* :exc:`DataException` if there is a Proton error. |
| 1005 |
""" |
| 1006 |
self._check(pn_data_put_long(self._data, l)) |
| 1007 |
|
| 1008 |
def put_timestamp(self, t: Union[timestamp, int]) -> None: |
| 1009 |
""" |
| 1010 |
Puts a timestamp value. |
| 1011 |
|
| 1012 |
:param t: a positive integral value |
| 1013 |
:raise: * ``AssertionError`` if parameter is negative. |
| 1014 |
* :exc:`DataException` if there is a Proton error. |
| 1015 |
""" |
| 1016 |
self._check(pn_data_put_timestamp(self._data, t)) |
| 1017 |
|
| 1018 |
def put_float(self, f: Union[float32, float, int]) -> None: |
| 1019 |
""" |
| 1020 |
Puts a float value. |
| 1021 |
|
| 1022 |
:param f: a floating point value |
| 1023 |
:raise: :exc:`DataException` if there is a Proton error. |
| 1024 |
""" |
| 1025 |
self._check(pn_data_put_float(self._data, f)) |
| 1026 |
|
| 1027 |
def put_double(self, d: Union[float, int]) -> None: |
| 1028 |
""" |
| 1029 |
Puts a double value. |
| 1030 |
|
| 1031 |
:param d: a floating point value. |
| 1032 |
:raise: :exc:`DataException` if there is a Proton error. |
| 1033 |
""" |
| 1034 |
self._check(pn_data_put_double(self._data, d)) |
| 1035 |
|
| 1036 |
def put_decimal32(self, d: Union[decimal32, int]) -> None: |
| 1037 |
""" |
| 1038 |
Puts a decimal32 value. |
| 1039 |
|
| 1040 |
:param d: a decimal32 number encoded in an 32-bit integral value. |
| 1041 |
:raise: :exc:`DataException` if there is a Proton error. |
| 1042 |
""" |
| 1043 |
self._check(pn_data_put_decimal32(self._data, d)) |
| 1044 |
|
| 1045 |
def put_decimal64(self, d: Union[decimal64, int]) -> None: |
| 1046 |
""" |
| 1047 |
Puts a decimal64 value. |
| 1048 |
|
| 1049 |
:param d: a decimal64 number encoded in an 32-bit integral value. |
| 1050 |
:raise: :exc:`DataException` if there is a Proton error. |
| 1051 |
""" |
| 1052 |
self._check(pn_data_put_decimal64(self._data, d)) |
| 1053 |
|
| 1054 |
def put_decimal128(self, d: Union[decimal128, bytes]) -> None: |
| 1055 |
""" |
| 1056 |
Puts a decimal128 value. |
| 1057 |
|
| 1058 |
:param d: a decimal128 value encoded in a 16-byte binary value. |
| 1059 |
:raise: :exc:`DataException` if there is a Proton error. |
| 1060 |
""" |
| 1061 |
self._check(pn_data_put_decimal128(self._data, d)) |
| 1062 |
|
| 1063 |
def put_uuid(self, u: uuid.UUID) -> None: |
| 1064 |
""" |
| 1065 |
Puts a UUID value. |
| 1066 |
|
| 1067 |
:param u: a uuid value. |
| 1068 |
:raise: :exc:`DataException` if there is a Proton error. |
| 1069 |
""" |
| 1070 |
self._check(pn_data_put_uuid(self._data, u.bytes)) |
| 1071 |
|
| 1072 |
def put_binary(self, b: bytes) -> None: |
| 1073 |
""" |
| 1074 |
Puts a binary value. |
| 1075 |
|
| 1076 |
:param b: a binary value |
| 1077 |
:raise: :exc:`DataException` if there is a Proton error. |
| 1078 |
""" |
| 1079 |
self._check(pn_data_put_binary(self._data, b)) |
| 1080 |
|
| 1081 |
def put_memoryview(self, mv: memoryview) -> None: |
| 1082 |
""" |
| 1083 |
Put a Python memoryview object as an AMQP binary value. |
| 1084 |
|
| 1085 |
:param mv: A Python memoryview object |
| 1086 |
:raise: :exc:`DataException` if there is a Proton error. |
| 1087 |
""" |
| 1088 |
self.put_binary(mv.tobytes()) |
| 1089 |
|
| 1090 |
def put_buffer(self, buff: Iterable[int]) -> None: |
| 1091 |
""" |
| 1092 |
Put a Python buffer object as an AMQP binary value. |
| 1093 |
|
| 1094 |
:param buff: A Python buffer object (**CHECK THIS**) |
| 1095 |
:raise: :exc:`DataException` if there is a Proton error. |
| 1096 |
""" |
| 1097 |
self.put_binary(bytes(buff)) |
| 1098 |
|
| 1099 |
def put_string(self, s: str) -> None: |
| 1100 |
""" |
| 1101 |
Puts a unicode value. |
| 1102 |
|
| 1103 |
:param s: a unicode string |
| 1104 |
:raise: :exc:`DataException` if there is a Proton error. |
| 1105 |
""" |
| 1106 |
self._check(pn_data_put_string(self._data, s.encode("utf8"))) |
| 1107 |
|
| 1108 |
def put_symbol(self, s: Union[str, symbol]) -> None: |
| 1109 |
""" |
| 1110 |
Puts a symbolic value. |
| 1111 |
|
| 1112 |
:param s: the symbol name |
| 1113 |
:raise: :exc:`DataException` if there is a Proton error. |
| 1114 |
""" |
| 1115 |
self._check(pn_data_put_symbol(self._data, s.encode('ascii'))) |
| 1116 |
|
| 1117 |
def get_list(self) -> int: |
| 1118 |
""" |
| 1119 |
If the current node is a list, return the number of elements, |
| 1120 |
otherwise return 0. List elements can be accessed by entering |
| 1121 |
the list. |
| 1122 |
|
| 1123 |
>>> count = data.get_list() |
| 1124 |
>>> data.enter() |
| 1125 |
>>> for i in range(count): |
| 1126 |
... type = data.next() |
| 1127 |
... if type == Data.STRING: |
| 1128 |
... print data.get_string() |
| 1129 |
... elif type == ...: |
| 1130 |
... ... |
| 1131 |
>>> data.exit() |
| 1132 |
|
| 1133 |
:return: the number of child elements of a list node |
| 1134 |
""" |
| 1135 |
return pn_data_get_list(self._data) |
| 1136 |
|
| 1137 |
def get_map(self) -> int: |
| 1138 |
""" |
| 1139 |
If the current node is a map, return the number of child elements, |
| 1140 |
otherwise return 0. Key value pairs can be accessed by entering |
| 1141 |
the map. |
| 1142 |
|
| 1143 |
>>> count = data.get_map() |
| 1144 |
>>> data.enter() |
| 1145 |
>>> for i in range(count/2): |
| 1146 |
... type = data.next() |
| 1147 |
... if type == Data.STRING: |
| 1148 |
... print data.get_string() |
| 1149 |
... elif type == ...: |
| 1150 |
... ... |
| 1151 |
>>> data.exit() |
| 1152 |
|
| 1153 |
:return: the number of child elements of a map node |
| 1154 |
""" |
| 1155 |
return pn_data_get_map(self._data) |
| 1156 |
|
| 1157 |
def get_array(self) -> Tuple[int, bool, Optional[int]]: |
| 1158 |
""" |
| 1159 |
If the current node is an array, return a tuple of the element |
| 1160 |
count, a boolean indicating whether the array is described, and |
| 1161 |
the type of each element, otherwise return ``None``. Array |
| 1162 |
data can be accessed by entering the array. |
| 1163 |
|
| 1164 |
>>> # read an array of strings with a symbolic descriptor |
| 1165 |
>>> count, described, type = data.get_array() |
| 1166 |
>>> data.enter() |
| 1167 |
>>> data.next() |
| 1168 |
>>> print "Descriptor:", data.get_symbol() |
| 1169 |
>>> for i in range(count): |
| 1170 |
... data.next() |
| 1171 |
... print "Element:", data.get_string() |
| 1172 |
>>> data.exit() |
| 1173 |
|
| 1174 |
:return: A tuple containing the number of array elements, a bool indicating |
| 1175 |
whether the array is described, and the enumerated array element type. |
| 1176 |
""" |
| 1177 |
count = pn_data_get_array(self._data) |
| 1178 |
described = pn_data_is_array_described(self._data) |
| 1179 |
type = pn_data_get_array_type(self._data) |
| 1180 |
if type == -1: |
| 1181 |
type = None |
| 1182 |
return count, described, type |
| 1183 |
|
| 1184 |
def is_described(self) -> bool: |
| 1185 |
""" |
| 1186 |
Checks if the current node is a described value. The descriptor |
| 1187 |
and value may be accessed by entering the described value. |
| 1188 |
|
| 1189 |
>>> # read a symbolically described string |
| 1190 |
>>> assert data.is_described() # will error if the current node is not described |
| 1191 |
>>> data.enter() |
| 1192 |
>>> data.next() |
| 1193 |
>>> print data.get_symbol() |
| 1194 |
>>> data.next() |
| 1195 |
>>> print data.get_string() |
| 1196 |
>>> data.exit() |
| 1197 |
|
| 1198 |
:return: ``True`` if the current node is a described type, ``False`` otherwise. |
| 1199 |
""" |
| 1200 |
return pn_data_is_described(self._data) |
| 1201 |
|
| 1202 |
def is_null(self) -> bool: |
| 1203 |
""" |
| 1204 |
Checks if the current node is the AMQP null type. |
| 1205 |
|
| 1206 |
:return: ``True`` if the current node is the AMQP null type, ``False`` otherwise. |
| 1207 |
""" |
| 1208 |
return pn_data_is_null(self._data) |
| 1209 |
|
| 1210 |
def get_bool(self) -> bool: |
| 1211 |
""" |
| 1212 |
Get the current node value as a ``bool``. |
| 1213 |
|
| 1214 |
:return: If the current node is a boolean type, returns its value, |
| 1215 |
``False`` otherwise. |
| 1216 |
""" |
| 1217 |
return pn_data_get_bool(self._data) |
| 1218 |
|
| 1219 |
def get_ubyte(self) -> ubyte: |
| 1220 |
""" |
| 1221 |
Get the current node value as a :class:`ubyte`. |
| 1222 |
|
| 1223 |
:return: If the current node is an unsigned byte, its value, 0 otherwise. |
| 1224 |
""" |
| 1225 |
return ubyte(pn_data_get_ubyte(self._data)) |
| 1226 |
|
| 1227 |
def get_byte(self) -> byte: |
| 1228 |
""" |
| 1229 |
Get the current node value as a :class:`byte`. |
| 1230 |
|
| 1231 |
:return: If the current node is a signed byte, its value, 0 otherwise. |
| 1232 |
""" |
| 1233 |
return byte(pn_data_get_byte(self._data)) |
| 1234 |
|
| 1235 |
def get_ushort(self) -> ushort: |
| 1236 |
""" |
| 1237 |
Get the current node value as a :class:`ushort`. |
| 1238 |
|
| 1239 |
:return: If the current node is an unsigned short, its value, 0 otherwise. |
| 1240 |
""" |
| 1241 |
return ushort(pn_data_get_ushort(self._data)) |
| 1242 |
|
| 1243 |
def get_short(self) -> short: |
| 1244 |
""" |
| 1245 |
Get the current node value as a :class:`short`. |
| 1246 |
|
| 1247 |
:return: If the current node is a signed short, its value, 0 otherwise. |
| 1248 |
""" |
| 1249 |
return short(pn_data_get_short(self._data)) |
| 1250 |
|
| 1251 |
def get_uint(self) -> uint: |
| 1252 |
""" |
| 1253 |
Get the current node value as a :class:`uint`. |
| 1254 |
|
| 1255 |
:return: If the current node is an unsigned int, its value, 0 otherwise. |
| 1256 |
""" |
| 1257 |
return uint(pn_data_get_uint(self._data)) |
| 1258 |
|
| 1259 |
def get_int(self) -> int32: |
| 1260 |
""" |
| 1261 |
Get the current node value as a :class:`int32`. |
| 1262 |
|
| 1263 |
:return: If the current node is a signed int, its value, 0 otherwise. |
| 1264 |
""" |
| 1265 |
return int32(pn_data_get_int(self._data)) |
| 1266 |
|
| 1267 |
def get_char(self) -> char: |
| 1268 |
""" |
| 1269 |
Get the current node value as a :class:`char`. |
| 1270 |
|
| 1271 |
:return: If the current node is a char, its value, 0 otherwise. |
| 1272 |
""" |
| 1273 |
return char(chr(pn_data_get_char(self._data))) |
| 1274 |
|
| 1275 |
def get_ulong(self) -> ulong: |
| 1276 |
""" |
| 1277 |
Get the current node value as a :class:`ulong`. |
| 1278 |
|
| 1279 |
:return: If the current node is an unsigned long, its value, 0 otherwise. |
| 1280 |
""" |
| 1281 |
return ulong(pn_data_get_ulong(self._data)) |
| 1282 |
|
| 1283 |
def get_long(self) -> long: |
| 1284 |
""" |
| 1285 |
Get the current node value as a :class:`long`. |
| 1286 |
|
| 1287 |
:return: If the current node is an signed long, its value, 0 otherwise. |
| 1288 |
""" |
| 1289 |
return long(pn_data_get_long(self._data)) |
| 1290 |
|
| 1291 |
def get_timestamp(self) -> timestamp: |
| 1292 |
""" |
| 1293 |
Get the current node value as a :class:`timestamp`. |
| 1294 |
|
| 1295 |
:return: If the current node is a timestamp, its value, 0 otherwise. |
| 1296 |
""" |
| 1297 |
return timestamp(pn_data_get_timestamp(self._data)) |
| 1298 |
|
| 1299 |
def get_float(self) -> float32: |
| 1300 |
""" |
| 1301 |
Get the current node value as a :class:`float32`. |
| 1302 |
|
| 1303 |
:return: If the current node is a float, its value, 0 otherwise. |
| 1304 |
""" |
| 1305 |
return float32(pn_data_get_float(self._data)) |
| 1306 |
|
| 1307 |
def get_double(self) -> float: |
| 1308 |
""" |
| 1309 |
Get the current node value as a ``double``. |
| 1310 |
|
| 1311 |
:return: If the current node is a double, its value, 0 otherwise. |
| 1312 |
""" |
| 1313 |
return pn_data_get_double(self._data) |
| 1314 |
|
| 1315 |
# XXX: need to convert |
| 1316 |
def get_decimal32(self) -> decimal32: |
| 1317 |
""" |
| 1318 |
Get the current node value as a :class:`decimal32`. |
| 1319 |
|
| 1320 |
:return: If the current node is a decimal32, its value, 0 otherwise. |
| 1321 |
""" |
| 1322 |
return decimal32(pn_data_get_decimal32(self._data)) |
| 1323 |
|
| 1324 |
# XXX: need to convert |
| 1325 |
def get_decimal64(self) -> decimal64: |
| 1326 |
""" |
| 1327 |
Get the current node value as a :class:`decimal64`. |
| 1328 |
|
| 1329 |
:return: If the current node is a decimal64, its value, 0 otherwise. |
| 1330 |
""" |
| 1331 |
return decimal64(pn_data_get_decimal64(self._data)) |
| 1332 |
|
| 1333 |
# XXX: need to convert |
| 1334 |
def get_decimal128(self) -> decimal128: |
| 1335 |
""" |
| 1336 |
Get the current node value as a :class:`decimal128`. |
| 1337 |
|
| 1338 |
:return: If the current node is a decimal128, its value, 0 otherwise. |
| 1339 |
""" |
| 1340 |
return decimal128(pn_data_get_decimal128(self._data)) |
| 1341 |
|
| 1342 |
def get_uuid(self) -> Optional[uuid.UUID]: |
| 1343 |
""" |
| 1344 |
Get the current node value as a ``uuid.UUID``. |
| 1345 |
|
| 1346 |
:return: If the current node is a UUID, its value, ``None`` otherwise. |
| 1347 |
""" |
| 1348 |
if pn_data_type(self._data) == Data.UUID: |
| 1349 |
return uuid.UUID(bytes=pn_data_get_uuid(self._data)) |
| 1350 |
else: |
| 1351 |
return None |
| 1352 |
|
| 1353 |
def get_binary(self) -> bytes: |
| 1354 |
""" |
| 1355 |
Get the current node value as ``bytes``. |
| 1356 |
|
| 1357 |
:return: If the current node is binary, its value, ``b""`` otherwise. |
| 1358 |
""" |
| 1359 |
return pn_data_get_binary(self._data) |
| 1360 |
|
| 1361 |
def get_string(self) -> str: |
| 1362 |
""" |
| 1363 |
Get the current node value as ``str``. |
| 1364 |
|
| 1365 |
:return: If the current node is a string, its value, ``""`` otherwise. |
| 1366 |
""" |
| 1367 |
return pn_data_get_string(self._data).decode("utf8") |
| 1368 |
|
| 1369 |
def get_symbol(self) -> symbol: |
| 1370 |
""" |
| 1371 |
Get the current node value as :class:`symbol`. |
| 1372 |
|
| 1373 |
:return: If the current node is a symbol, its value, ``""`` otherwise. |
| 1374 |
""" |
| 1375 |
return symbol(pn_data_get_symbol(self._data).decode('ascii')) |
| 1376 |
|
| 1377 |
def copy(self, src: 'Data') -> None: |
| 1378 |
""" |
| 1379 |
Copy the contents of another pn_data_t object. Any values in the |
| 1380 |
data object will be lost. |
| 1381 |
|
| 1382 |
:param src: The source object from which to copy |
| 1383 |
:raise: :exc:`DataException` if there is a Proton error. |
| 1384 |
""" |
| 1385 |
self._check(pn_data_copy(self._data, src._data)) |
| 1386 |
|
| 1387 |
def format(self) -> str: |
| 1388 |
""" |
| 1389 |
Formats the contents of this :class:`Data` object in a human readable way. |
| 1390 |
|
| 1391 |
:return: A Formatted string containing contents of this :class:`Data` object. |
| 1392 |
:raise: :exc:`DataException` if there is a Proton error. |
| 1393 |
""" |
| 1394 |
sz = 16 |
| 1395 |
while True: |
| 1396 |
err, result = pn_data_format(self._data, sz) |
| 1397 |
if err == PN_OVERFLOW: |
| 1398 |
sz *= 2 |
| 1399 |
continue |
| 1400 |
else: |
| 1401 |
self._check(err) |
| 1402 |
return result |
| 1403 |
|
| 1404 |
def dump(self) -> None: |
| 1405 |
""" |
| 1406 |
Dumps a debug representation of the internal state of this :class:`Data` |
| 1407 |
object that includes its navigational state to ``cout`` (``stdout``) for |
| 1408 |
debugging purposes. |
| 1409 |
""" |
| 1410 |
pn_data_dump(self._data) |
| 1411 |
|
| 1412 |
def put_dict(self, d: Dict[Any, Any]) -> None: |
| 1413 |
""" |
| 1414 |
A convenience method for encoding the contents of a Python ``dict`` |
| 1415 |
as an AMQP map. |
| 1416 |
|
| 1417 |
:param d: The dictionary to be encoded |
| 1418 |
:raise: :exc:`DataException` if there is a Proton error. |
| 1419 |
""" |
| 1420 |
self.put_map() |
| 1421 |
self.enter() |
| 1422 |
try: |
| 1423 |
for k, v in d.items(): |
| 1424 |
self.put_object(k) |
| 1425 |
self.put_object(v) |
| 1426 |
finally: |
| 1427 |
self.exit() |
| 1428 |
|
| 1429 |
def get_dict(self) -> Dict[Any, Any]: |
| 1430 |
""" |
| 1431 |
A convenience method for decoding an AMQP map as a Python ``dict``. |
| 1432 |
|
| 1433 |
:returns: The decoded dictionary. |
| 1434 |
""" |
| 1435 |
if self.enter(): |
| 1436 |
try: |
| 1437 |
result = {} |
| 1438 |
while self.next(): |
| 1439 |
k = self.get_object() |
| 1440 |
if self.next(): |
| 1441 |
v = self.get_object() |
| 1442 |
else: |
| 1443 |
v = None |
| 1444 |
result[k] = v |
| 1445 |
finally: |
| 1446 |
self.exit() |
| 1447 |
return result |
| 1448 |
|
| 1449 |
def put_sequence(self, s: List[Any]) -> None: |
| 1450 |
""" |
| 1451 |
A convenience method for encoding a Python ``list`` as an |
| 1452 |
AMQP list. |
| 1453 |
|
| 1454 |
:param s: The sequence to be encoded |
| 1455 |
:raise: :exc:`DataException` if there is a Proton error. |
| 1456 |
""" |
| 1457 |
self.put_list() |
| 1458 |
self.enter() |
| 1459 |
try: |
| 1460 |
for o in s: |
| 1461 |
self.put_object(o) |
| 1462 |
finally: |
| 1463 |
self.exit() |
| 1464 |
|
| 1465 |
def get_sequence(self) -> List[Any]: |
| 1466 |
""" |
| 1467 |
A convenience method for decoding an AMQP list as a Python ``list``. |
| 1468 |
|
| 1469 |
:returns: The decoded list. |
| 1470 |
""" |
| 1471 |
if self.enter(): |
| 1472 |
try: |
| 1473 |
result = [] |
| 1474 |
while self.next(): |
| 1475 |
result.append(self.get_object()) |
| 1476 |
finally: |
| 1477 |
self.exit() |
| 1478 |
return result |
| 1479 |
|
| 1480 |
def get_py_described(self) -> Described: |
| 1481 |
""" |
| 1482 |
A convenience method for decoding an AMQP described value. |
| 1483 |
|
| 1484 |
:returns: The decoded AMQP descriptor. |
| 1485 |
""" |
| 1486 |
if self.enter(): |
| 1487 |
try: |
| 1488 |
self.next() |
| 1489 |
descriptor = self.get_object() |
| 1490 |
self.next() |
| 1491 |
value = self.get_object() |
| 1492 |
finally: |
| 1493 |
self.exit() |
| 1494 |
return Described(descriptor, value) |
| 1495 |
|
| 1496 |
def put_py_described(self, d: Described) -> None: |
| 1497 |
""" |
| 1498 |
A convenience method for encoding a :class:`Described` object |
| 1499 |
as an AMQP described value. This method encapsulates all the steps |
| 1500 |
described in :func:`put_described` into a single method. |
| 1501 |
|
| 1502 |
:param d: The descriptor to be encoded |
| 1503 |
:type d: :class:`Described` |
| 1504 |
:raise: :exc:`DataException` if there is a Proton error. |
| 1505 |
""" |
| 1506 |
self.put_described() |
| 1507 |
self.enter() |
| 1508 |
try: |
| 1509 |
self.put_object(d.descriptor) |
| 1510 |
self.put_object(d.value) |
| 1511 |
finally: |
| 1512 |
self.exit() |
| 1513 |
|
| 1514 |
def get_py_array(self) -> Optional[Array]: |
| 1515 |
""" |
| 1516 |
A convenience method for decoding an AMQP array into an |
| 1517 |
:class:`Array` object. This method encapsulates all the |
| 1518 |
steps described in :func:`get_array` into a single function. |
| 1519 |
|
| 1520 |
If the current node is an array, return an Array object |
| 1521 |
representing the array and its contents. Otherwise return ``None``. |
| 1522 |
|
| 1523 |
:returns: The decoded AMQP array. |
| 1524 |
""" |
| 1525 |
|
| 1526 |
count, described, type = self.get_array() |
| 1527 |
if type is None: |
| 1528 |
return None |
| 1529 |
if self.enter(): |
| 1530 |
try: |
| 1531 |
if described: |
| 1532 |
self.next() |
| 1533 |
descriptor = self.get_object() |
| 1534 |
else: |
| 1535 |
descriptor = UNDESCRIBED |
| 1536 |
elements = [] |
| 1537 |
while self.next(): |
| 1538 |
elements.append(self.get_object()) |
| 1539 |
finally: |
| 1540 |
self.exit() |
| 1541 |
return Array(descriptor, type, *elements) |
| 1542 |
|
| 1543 |
def put_py_array(self, a: Array) -> None: |
| 1544 |
""" |
| 1545 |
A convenience method for encoding an :class:`Array` object as |
| 1546 |
an AMQP array. This method encapsulates the steps described in |
| 1547 |
:func:`put_array` into a single function. |
| 1548 |
|
| 1549 |
:param a: The array object to be encoded |
| 1550 |
:raise: :exc:`DataException` if there is a Proton error. |
| 1551 |
""" |
| 1552 |
described = a.descriptor != UNDESCRIBED |
| 1553 |
self.put_array(described, a.type) |
| 1554 |
self.enter() |
| 1555 |
try: |
| 1556 |
if described: |
| 1557 |
self.put_object(a.descriptor) |
| 1558 |
for e in a.elements: |
| 1559 |
self.put_object(e) |
| 1560 |
finally: |
| 1561 |
self.exit() |
| 1562 |
|
| 1563 |
put_mappings = { |
| 1564 |
None.__class__: lambda s, _: s.put_null(), |
| 1565 |
bool: put_bool, |
| 1566 |
ubyte: put_ubyte, |
| 1567 |
ushort: put_ushort, |
| 1568 |
uint: put_uint, |
| 1569 |
ulong: put_ulong, |
| 1570 |
byte: put_byte, |
| 1571 |
short: put_short, |
| 1572 |
int32: put_int, |
| 1573 |
long: put_long, |
| 1574 |
float32: put_float, |
| 1575 |
float: put_double, |
| 1576 |
decimal32: put_decimal32, |
| 1577 |
decimal64: put_decimal64, |
| 1578 |
decimal128: put_decimal128, |
| 1579 |
char: put_char, |
| 1580 |
timestamp: put_timestamp, |
| 1581 |
uuid.UUID: put_uuid, |
| 1582 |
bytes: put_binary, |
| 1583 |
unicode: put_string, |
| 1584 |
symbol: put_symbol, |
| 1585 |
list: put_sequence, |
| 1586 |
tuple: put_sequence, |
| 1587 |
dict: put_dict, |
| 1588 |
Described: put_py_described, |
| 1589 |
Array: put_py_array, |
| 1590 |
AnnotationDict: put_dict, |
| 1591 |
PropertyDict: put_dict, |
| 1592 |
SymbolList: put_sequence, |
| 1593 |
memoryview: put_memoryview |
| 1594 |
} |
| 1595 |
get_mappings = { |
| 1596 |
NULL: lambda s: None, |
| 1597 |
BOOL: get_bool, |
| 1598 |
BYTE: get_byte, |
| 1599 |
UBYTE: get_ubyte, |
| 1600 |
SHORT: get_short, |
| 1601 |
USHORT: get_ushort, |
| 1602 |
INT: get_int, |
| 1603 |
UINT: get_uint, |
| 1604 |
CHAR: get_char, |
| 1605 |
LONG: get_long, |
| 1606 |
ULONG: get_ulong, |
| 1607 |
TIMESTAMP: get_timestamp, |
| 1608 |
FLOAT: get_float, |
| 1609 |
DOUBLE: get_double, |
| 1610 |
DECIMAL32: get_decimal32, |
| 1611 |
DECIMAL64: get_decimal64, |
| 1612 |
DECIMAL128: get_decimal128, |
| 1613 |
UUID: get_uuid, |
| 1614 |
BINARY: get_binary, |
| 1615 |
STRING: get_string, |
| 1616 |
SYMBOL: get_symbol, |
| 1617 |
DESCRIBED: get_py_described, |
| 1618 |
ARRAY: get_py_array, |
| 1619 |
LIST: get_sequence, |
| 1620 |
MAP: get_dict |
| 1621 |
} |
| 1622 |
|
| 1623 |
def put_object(self, obj: Any) -> None: |
| 1624 |
putter = self.put_mappings[obj.__class__] |
| 1625 |
putter(self, obj) |
| 1626 |
|
| 1627 |
def get_object(self) -> Optional[Any]: |
| 1628 |
type = self.type() |
| 1629 |
if type is None: |
| 1630 |
return None |
| 1631 |
getter = self.get_mappings.get(type) |
| 1632 |
if getter: |
| 1633 |
return getter(self) |
| 1634 |
else: |
| 1635 |
return UnmappedType(str(type)) |
| 1636 |
|
| 1637 |
|
| 1638 |
def dat2obj(dimpl): |
| 1639 |
if dimpl: |
| 1640 |
d = Data(dimpl) |
| 1641 |
d.rewind() |
| 1642 |
d.next() |
| 1643 |
obj = d.get_object() |
| 1644 |
d.rewind() |
| 1645 |
return obj |
| 1646 |
|
| 1647 |
|
| 1648 |
def obj2dat(obj, dimpl): |
| 1649 |
if obj is not None: |
| 1650 |
d = Data(dimpl) |
| 1651 |
d.put_object(obj) |