How to use the cbor2.types.CBORTag function in cbor2

To help you get started, we’ve selected a few cbor2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github agronholm / cbor2 / tests / test_types.py View on Github external
def test_tag_repr():
    assert repr(CBORTag(600, 'blah')) == "CBORTag(600, 'blah')"
github agronholm / cbor2 / cbor2 / encoder.py View on Github external
# Semantic tag 0
        if not value.tzinfo:
            if self._timezone:
                value = value.replace(tzinfo=self._timezone)
            else:
                raise CBOREncodeValueError(
                    'naive datetime {!r} encountered and no default timezone '
                    'has been set'.format(value))

        if self.datetime_as_timestamp:
            from calendar import timegm
            if not value.microsecond:
                timestamp = timegm(value.utctimetuple())
            else:
                timestamp = timegm(value.utctimetuple()) + value.microsecond / 1000000
            self.encode_semantic(CBORTag(1, timestamp))
        else:
            datestring = as_unicode(value.isoformat().replace('+00:00', 'Z'))
            self.encode_semantic(CBORTag(0, datestring))
github agronholm / cbor2 / cbor2 / encoder.py View on Github external
def encode_decimal(self, value):
        # Semantic tag 4
        if value.is_nan():
            self._fp_write(b'\xf9\x7e\x00')
        elif value.is_infinite():
            self._fp_write(b'\xf9\x7c\x00' if value > 0 else b'\xf9\xfc\x00')
        else:
            dt = value.as_tuple()
            sig = 0
            for digit in dt.digits:
                sig = (sig * 10) + digit
            if dt.sign:
                sig = -sig
            with self.disable_value_sharing():
                self.encode_semantic(CBORTag(4, [dt.exponent, sig]))
github agronholm / cbor2 / cbor2 / decoder.py View on Github external
def decode_ipaddress(self):
        # Semantic tag 260
        from ipaddress import ip_address
        buf = self.decode()
        if not isinstance(buf, bytes) or len(buf) not in (4, 6, 16):
            raise CBORDecodeValueError("invalid ipaddress value %r" % buf)
        elif len(buf) in (4, 16):
            return self.set_shareable(ip_address(buf))
        elif len(buf) == 6:
            # MAC address
            return self.set_shareable(CBORTag(260, buf))
github agronholm / cbor2 / cbor2 / encoder.py View on Github external
def encode_uuid(self, value):
        # Semantic tag 37
        self.encode_semantic(CBORTag(37, value.bytes))
github agronholm / cbor2 / cbor2 / __init__.py View on Github external
from .encoder import default_encoders, canonical_encoders
        from .types import CBORTag, CBORSimpleValue, undefined  # noqa
        import _cbor2
        _cbor2.default_encoders = OrderedDict([
            ((
                _cbor2.CBORSimpleValue if type_ is CBORSimpleValue else
                _cbor2.CBORTag if type_ is CBORTag else
                type(_cbor2.undefined) if type_ is type(undefined) else
                type_
            ), getattr(_cbor2.CBOREncoder, method.__name__))
            for type_, method in default_encoders.items()
        ])
        _cbor2.canonical_encoders = OrderedDict([
            ((
                _cbor2.CBORSimpleValue if type_ is CBORSimpleValue else
                _cbor2.CBORTag if type_ is CBORTag else
                type(_cbor2.undefined) if type_ is type(undefined) else
                type_
            ), getattr(_cbor2.CBOREncoder, method.__name__))
            for type_, method in canonical_encoders.items()
        ])
github agronholm / cbor2 / cbor2 / encoder.py View on Github external
def encode_int(self, value):
        # Big integers (2 ** 64 and over)
        if value >= 18446744073709551616 or value < -18446744073709551616:
            if value >= 0:
                major_type = 0x02
            else:
                major_type = 0x03
                value = -value - 1

            payload = int2bytes(value)
            self.encode_semantic(CBORTag(major_type, payload))
        elif value >= 0:
            self.encode_length(0, value)
        else:
            self.encode_length(1, -(value + 1))
github agronholm / cbor2 / cbor2 / encoder.py View on Github external
def encode_ipnetwork(self, value):
        # Semantic tag 261
        self.encode_semantic(
            CBORTag(261, {value.network_address.packed: value.prefixlen}))