multiformats.multicodec
Implementation of the multicodec spec.
Suggested usage:
>>> from multiformats import multicodec
Multicodec
- class Multicodec(name, tag, code, status='draft', description='')[source]
Bases:
object
Container class for a multicodec.
Example usage:
>>> Multicodec(**{ ... 'name': 'cidv1', 'tag': 'cid', 'code': '0x01', ... 'status': 'permanent', 'description': 'CIDv1'}) Multicodec(name='cidv1', tag='cid', code=1, status='permanent', description='CIDv1')
- Parameters:
- property hexcode
Multicodec code as a hex string (with hex digits zero-padded to even length):
Example usage:
>>> m = multicodec.get(1) >>> m.code 1 >>> m.hexcode '0x01'
- Return type:
- property is_private_use
Whether this multicodec code is reserved for private use, i.e. whether it is in
range(0x300000, 0x400000)
.- Return type:
- property name
Multicodec name. Must satisfy the following:
re.match(r"^[a-z][a-z0-9_-]+$", name)
- Return type:
- property status
Multicodec status.
- Return type:
- to_json()[source]
Returns a JSON dictionary representation of this multicodec object.
Example usage:
>>> m = multicodec.get(1) >>> m.to_json() {'name': 'cidv1', 'tag': 'cid', 'code': '0x01', 'status': 'permanent', 'description': 'CIDv1'}
- unwrap(multicodec_data)[source]
Unwraps multicodec binary data to raw data:
<code><raw data> --> <raw data>
Additionally checks that the code listed by the data matches the code of this multicodec.
Example usage:
>>> multicodec_data = bytes.fromhex("c0a800fe") >>> raw_data = ip4.unwrap(multicodec_data) >>> multicodec_data.hex() '04c0a800fe' >>> raw_data.hex() 'c0a800fe' >>> varint.encode(0x04).hex() '04' # 0x04 ^^^^ is the multicodec code for 'ip4'
- Parameters:
multicodec_data (
BytesLike
) – the multicodec data to be unwrapped- Raises:
ValueError – if the unwrapped multicodec code does not match this multicodec’s code
- Return type:
- static validate_code(code)[source]
Validates a multicodec code and transforms it to unsigned integer format (if in hex format).
- Parameters:
code (
int
orstr
) – the multicodec code, as integer or 0xYZ hex-string- Raises:
ValueError – if the code is invalid
- Return type:
- wrap(raw_data)[source]
Wraps raw binary data into multicodec data:
<raw data> --> <code><raw data>
Example usage:
>>> ip4 = multicodec.get("ip4") >>> ip4 Multicodec(name='ip4', tag='multiaddr', code='0x04', status='permanent', description='') >>> raw_data = bytes([192, 168, 0, 254]) >>> multicodec_data = ip4.wrap(raw_data) >>> raw_data.hex() 'c0a800fe' >>> multicodec_data.hex() '04c0a800fe' >>> varint.encode(0x04).hex() '04' # 0x04 ^^^^ is the multicodec code for 'ip4'
- Parameters:
raw_data (
BytesLike
) – the raw data to be wrapped- Raises:
ValueError – see
encode
- Return type:
MulticodecStatusValues
- MulticodecStatusValues = ('draft', 'permanent', 'deprecated')
Collection of possible values for the
Multicodec.status
property.
exists
get
- get(name=None, *, code=None)[source]
Gets the multicodec with given name or code.
Example usage:
>>> multicodec.get("identity") Multicodec(name='identity', tag='multihash', code=0, status='permanent', description='raw binary') >>> multicodec.get(code=0x01) Multicodec(name='cidv1', tag='ipld', code=1, status='permanent', description='CIDv1')
- Parameters:
- Raises:
KeyError – if no such multicodec exists
ValueError – unless exactly one of
name
andcode
is specified
- Return type:
multicodec
- multicodec(name, *, tag=None)[source]
An alias for
get
, for use with multicodec name only. If a tag is passed, ensures that the multicodec tag matches the one given.Example usage:
>>> from multiformats.multicodec import multicodec >>> multicodec("identity") Multicodec(name='identity', tag='multihash', code=0, status='permanent', description='raw binary')
register
- register(codec, *, overwrite=False)[source]
Registers a given multicodec.
Example usage:
>>> m = Multicodec("my-multicodec", "my-tag", 0x300001, "draft", "...") >>> multicodec.register(m) >>> multicodec.exists(code=0x300001) True >>> multicodec.get(code=0x300001).name 'my-multicodec' >>> multicodec.get(code=0x300001).is_private_use True
- Parameters:
codec (
Multicodec
) – the multicodec to registeroverwrite (
bool
, optional) – whether to overwrite a multicodec with existing code (optional, defaultFalse
)
- Raises:
ValueError – if
overwrite
isFalse
and a multicodec with the same name or code already existsValueError – if
overwrite
isTrue
and a multicodec with the same name but different code already exists
- Return type:
table
- table(*, tag=None, status=None)[source]
Iterates through the registered multicodecs, in order of ascending code.
Example usage:
>>> len(list(multicodec.table())) # multicodec.table() returns an iterator 482 >>> selected = multicodec.table(tag=["cid", "cid", "multiaddr"], status="permanent") >>> [m.code for m in selected] [1, 4, 6, 41, 53, 54, 55, 56, 81, 85, 112, 113, 114, 120, 144, 145, 146, 147, 148, 149, 150, 151, 152, 176, 177, 178, 192, 193, 290, 297, 400, 421, 460, 477, 478, 479, 512]
unregister
unwrap
- unwrap(multicodec_data)[source]
Unwraps multicodec binary data to multicodec and raw data:
Example usage:
>>> multicodec_data = bytes.fromhex("04c0a800fe") >>> codec, raw_data = multicodec.unwrap(multicodec_data) >>> multicodec_data.hex() '04c0a800fe' >>> raw_data.hex() 'c0a800fe' >>> codec Multicodec(name='ip4', tag='multiaddr', code='0x04', status='permanent', description='')
unwrap_raw
- unwrap_raw(multicodec_data)[source]
Similar to
unwrap
, but returns a triple of multicodec code, number of bytes read and remaining bytes.Example usage:
>>> multicodec_data = bytes.fromhex("04c0a800fe") >>> code, num_bytes_read, remaining_bytes = multicodec.unwrap_raw(multicodec_data) >>> code 4 >>> num_bytes_read 1 >>> remaining_bytes <memory at 0x000001BE46B17640> >>> multicodec_data.hex() '04c0a800fe' >>> bytes(remaining_bytes).hex() 'c0a800fe'
- Parameters:
multicodec_data (
BytesLike
) – the binary data prefixed with multicodec code- Raises:
KeyError – if the code does not correspond to a know multicodec
- Return type:
Tuple
[int
,int
,Union
[memoryview
,BufferedIOBase
]]
validate_multicodec
- validate_multicodec(codec)[source]
Validates an instance of
Multicodec
. If the multicodec is registered (i.e. valid), no error is raised.- Parameters:
codec (
Multicodec
) – the instance to be validated- Raises:
KeyError – if no multicodec with the given name is registered
ValueError – if a multicodec with the given name is registered, but is different from the one given
- Return type:
wrap
- wrap(codec, raw_data)[source]
Wraps raw binary data into multicodec data:
<raw data> --> <code><raw data>
Example usage:
>>> raw_data = bytes([192, 168, 0, 254]) >>> multicodec_data = multicodec.wrap("ip4", raw_data) >>> raw_data.hex() 'c0a800fe' >>> multicodec_data.hex() '04c0a800fe' >>> varint.encode(0x04).hex() '04' # 0x04 ^^^^ is the multicodec code for 'ip4'