CborReader

class cometa.cbor.cbor_reader.CborReader(ptr)[source]

Bases: object

Represents a reader for parsing Concise Binary Object Representation (CBOR) encoded data.

This class provides a stream-like interface to decode CBOR data items sequentially.

property refcount: int

Returns the number of active references to the underlying C object.

property remaining_bytes: int

Returns the number of unread bytes remaining in the buffer.

property last_error: str

Returns the last error message recorded for this reader.

classmethod from_bytes(data)[source]

Creates a CborReader from raw bytes.

Parameters:

data (bytes) – The CBOR encoded data.

Return type:

CborReader

classmethod from_hex(hex_string)[source]

Creates a CborReader from a hexadecimal string.

Parameters:

hex_string (str) – The hex-encoded CBOR data.

Return type:

CborReader

peek_state()[source]

Inspects the type of the next CBOR token without consuming it.

Returns:

The state enum indicating the next token type.

Return type:

CborReaderState

clone()[source]

Creates a deep copy of the reader with its own independent cursor.

Return type:

CborReader

read_remainder()[source]

Reads all remaining unparsed bytes.

Return type:

bytes

skip_value()[source]

Skips the next CBOR data item completely (including nested items).

Return type:

None

read_encoded_value()[source]

Reads the next CBOR data item as-is and returns the raw bytes.

Return type:

bytes

read_array_len()[source]

Reads the start of an array.

Returns:

The number of elements in the array, or None if it is indefinite-length.

Return type:

int | None

read_array_end()[source]

Consumes the ‘break’ code ending an indefinite-length array.

Return type:

None

read_map_len()[source]

Reads the start of a map.

Returns:

The number of pairs in the map, or None if it is indefinite-length.

Return type:

int | None

read_map_end()[source]

Consumes the ‘break’ code ending an indefinite-length map.

Return type:

None

read_int()[source]

Reads a signed integer (Major type 0 or 1).

Return type:

int

read_uint()[source]

Reads an unsigned integer (Major type 0).

Return type:

int

read_bigint()[source]

Reads a bignum (Major type 6, tag 2 or 3).

Return type:

int

read_float()[source]

Reads a double-precision float (Major type 7).

Return type:

float

read_simple_value()[source]

Reads a simple value (e.g., boolean, null, or undefined).

Returns:

The simple value.

Return type:

int

read_bool()[source]

Reads a boolean value.

Return type:

bool

read_null()[source]

Consumes a null value.

Return type:

None

read_bytes()[source]

Reads a byte string (Major type 2).

Return type:

bytes

read_str()[source]

Reads a text string (Major type 3). Returns a string (decoded UTF-8).

Return type:

str

read_tag()[source]

Reads a semantic tag (Major type 6).

Returns:

The tag value.

Return type:

int

peek_tag()[source]

Peeks at the next semantic tag without consuming it.

Returns:

The tag value if present.

Return type:

int

__init__(ptr)[source]

Internal constructor. Use factories like from_bytes or from_hex.

Return type:

None

__enter__()[source]

Enables use as a context manager.

Return type:

CborReader

__exit__(exc_type, exc_val, exc_tb)[source]

Cleans up when exiting a context manager.

Return type:

None

__repr__()[source]

Returns a string representation of the CborReader.

Return type:

str