Skip to content

Commit 99da3bb

Browse files
committed
v0.2.0
Closes #6 .
1 parent 4cb2705 commit 99da3bb

18 files changed

+198
-4496
lines changed

ADDITIONAL-LICENSES

Lines changed: 0 additions & 30 deletions
This file was deleted.

README.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,5 +332,3 @@ License
332332
-------
333333

334334
`MIT © Hashberg Ltd. <LICENSE>`_
335-
336-
See `additional Licenses <ADDITIONAL-LICENSES>`_ for licensing of the multicodec table, the multibase table and test vectors for multihashes.

docs/getting-started.rst

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ while :class:`~multiformats.cid.CID` is a class for Content IDentifiers.
3232
The following are mandatory dependencies for this module:
3333

3434
- `typing-extensions <https://github.com/python/typing_extensions>`_, for backward compatibility of static typing.
35-
- `typing-validation <https://github.com/hashberg-io/typing-validation>`_, for dynamic typechecking
36-
- `bases <https://github.com/hashberg-io/bases>`_, for implementation of base encodings used by Multibase
35+
- `typing-validation <https://github.com/hashberg-io/typing-validation>`_, for dynamic typechecking.
36+
- `bases <https://github.com/hashberg-io/bases>`_, for implementation of base encodings used by Multibase.
37+
- `multiformats-config <https://github.com/hashberg-io/multiformats-config>`_, handling pre-loading configuration of multicodec/multibase tables.
3738

3839
The following are optional dependencies for this module:
3940

@@ -50,4 +51,37 @@ You can install the latest release together with all optional dependencies as fo
5051
5152
$ pip install --upgrade multiformats[full]
5253
54+
If you'd like to only load a selection of multicodecs and/or multibases, you can do so by calling ``multiformats_config.enable()`` **before** importing the
55+
multiformats library, passing the desired multicodec names (as :obj:`str`) orcodes (as :obj:`int`) and the desired multibase names (as :obj:`str`) or codes (as :obj:`str` of length 1) to the ``codecs`` and ``bases`` keyword arguments, respectively:
56+
57+
.. code-block:: python
58+
59+
import multiformats_config
60+
multiformats_config.enable(codecs=["sha1", 0x29], bases=["base64url", "9"])
61+
from multiformats import *
62+
63+
If ``codecs`` is not set (or set to :obj:`None`), all multicodecs are loaded. If ``bases`` is not set (or set to :obj:`None`), all multibases are loaded.
64+
Using ``multiformats_config.enable(codecs=[], bases=[])`` results in a minimal set of (mandatory) multicodecs and multibases to be loaded:
65+
66+
.. code-block:: python
67+
68+
_minimal_multicodecs = frozenset([
69+
0x00, # 'identity'
70+
0x01, # 'cidv1'
71+
0x02, # 'cidv2'
72+
0x12, # 'sha2-256'
73+
0x14, # 'sha3-512'
74+
0x16, # 'sha3-256'
75+
0x70, # 'dag-pb'
76+
0x71, # 'dag-cbor'
77+
0x72, # 'libp2p-key'
78+
])
79+
80+
_minimal_multibases = frozenset([
81+
"identity",
82+
"base16",
83+
"base32",
84+
"base58btc",
85+
])
86+
5387
GitHub repo: https://github.com/hashberg-io/multiformats

multiformats/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
while :class:`~multiformats.cid.CID` is a class for Content IDentifiers.
1616
"""
1717

18-
__version__ = "0.1.4"
18+
__version__ = "0.2.0"
1919

2020
from . import varint
2121
from . import multicodec

multiformats/multibase/__init__.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121

2222
from bases import (base2, base16, base8, base10, base36, base58btc, base58flickr, base58ripple,
2323
base32, base32hex, base32z, base64, base64url, base45,)
24+
from multiformats_config.multibase import load_multibase_table
2425

2526
from multiformats.multibase import raw
2627
from multiformats.varint import BytesLike
2728
from .raw import RawEncoder, RawDecoder
2829
from .err import MultibaseKeyError, MultibaseValueError
2930

30-
3131
class Multibase:
3232
"""
3333
Container class for a multibase encoding.
@@ -528,35 +528,36 @@ def decode_raw(s: str) -> Tuple[Multibase, bytes]:
528528
base = from_str(s)
529529
return base, base.decode(s)
530530

531+
_code_table, _name_table = load_multibase_table()
531532

532-
def build_multibase_tables(bases: Iterable[Multibase]) -> Tuple[Dict[str, Multibase], Dict[str, Multibase]]:
533-
"""
534-
Creates code->encoding and name->encoding mappings from a finite iterable of encodings, returning the mappings.
533+
# def build_multibase_tables(bases: Iterable[Multibase]) -> Tuple[Dict[str, Multibase], Dict[str, Multibase]]:
534+
# """
535+
# Creates code->encoding and name->encoding mappings from a finite iterable of encodings, returning the mappings.
535536

536-
Example usage:
537+
# Example usage:
537538

538-
>>> code_table, name_table = build_multicodec_tables(bases)
539+
# >>> code_table, name_table = build_multicodec_tables(bases)
539540

540-
:param bases: the multibases to add to the table
541-
::
541+
# :param bases: the multibases to add to the table
542+
# ::
542543

543-
:raises ValueError: if the same encoding code or name is encountered multiple times
544-
"""
545-
# validate(multicodecs, Iterable[Multicodec]) # TODO: not yet properly supported by typing-validation
546-
code_table: Dict[str, Multibase] = {}
547-
name_table: Dict[str, Multibase] = {}
548-
for e in bases:
549-
if e.code in code_table:
550-
raise MultibaseValueError(f"Multicodec name {e.name} appears multiple times in table.")
551-
code_table[e.code] = e
552-
if e.name in name_table:
553-
raise MultibaseValueError(f"Multicodec name {e.name} appears multiple times in table.")
554-
name_table[e.name] = e
555-
return code_table, name_table
544+
# :raises ValueError: if the same encoding code or name is encountered multiple times
545+
# """
546+
# # validate(multicodecs, Iterable[Multicodec]) # TODO: not yet properly supported by typing-validation
547+
# code_table: Dict[str, Multibase] = {}
548+
# name_table: Dict[str, Multibase] = {}
549+
# for e in bases:
550+
# if e.code in code_table:
551+
# raise MultibaseValueError(f"Multicodec name {e.name} appears multiple times in table.")
552+
# code_table[e.code] = e
553+
# if e.name in name_table:
554+
# raise MultibaseValueError(f"Multicodec name {e.name} appears multiple times in table.")
555+
# name_table[e.name] = e
556+
# return code_table, name_table
556557

557558
# Create the global code->multibase and name->multibase mappings.
558-
_code_table: Dict[str, Multibase]
559-
_name_table: Dict[str, Multibase]
560-
with importlib_resources.open_text("multiformats.multibase", "multibase-table.json", encoding="utf8") as _table_f:
561-
_table_json = json.load(_table_f)
562-
_code_table, _name_table = build_multibase_tables(Multibase(**row) for row in _table_json)
559+
# _code_table: Dict[str, Multibase] = {}
560+
# _name_table: Dict[str, Multibase] = {}
561+
# with importlib_resources.open_text("multiformats.multibase", "multibase-table.json", encoding="utf8") as _table_f:
562+
# _table_json = json.load(_table_f)
563+
# _code_table, _name_table = build_multibase_tables(Multibase(**row) for row in _table_json)

multiformats/multibase/multibase-table.csv

Lines changed: 0 additions & 26 deletions
This file was deleted.

multiformats/multibase/multibase-table.json

Lines changed: 0 additions & 152 deletions
This file was deleted.

multiformats/multibase/raw.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ class CustomEncoding:
6060
def __init__(self, raw_encoder: RawEncoder, raw_decoder: RawDecoder):
6161
# validate(raw_encoder, Callable[[bytes], str]) # TODO: not yet supported by typing-validation
6262
# validate(raw_decoder, Callable[[str], bytes]) # TODO: not yet supported by typing-validation
63-
self._raw_encoder = raw_encoder # type: ignore
64-
self._raw_decoder = raw_decoder # type: ignore
63+
self._raw_encoder = raw_encoder
64+
self._raw_decoder = raw_decoder
6565

6666
def encode(self, b: BytesLike) -> str:
6767
"""
@@ -70,7 +70,7 @@ def encode(self, b: BytesLike) -> str:
7070
:param b: the bytestring to be encoded
7171
:type b: :obj:`~multiformats.varint.BytesLike`
7272
"""
73-
raw_encoder: RawEncoder = self._raw_encoder # type: ignore
73+
raw_encoder: RawEncoder = self._raw_encoder
7474
return raw_encoder(b)
7575

7676
def decode(self, s: str) -> bytes:
@@ -80,12 +80,12 @@ def decode(self, s: str) -> bytes:
8080
:param s: the string to be decoded
8181
:type s: :obj:`str`
8282
"""
83-
raw_decoder: RawDecoder = self._raw_decoder # type: ignore
83+
raw_decoder: RawDecoder = self._raw_decoder
8484
return raw_decoder(s)
8585

8686
def __repr__(self) -> str:
87-
_raw_encoder: Callable[[bytes], str] = self._raw_encoder # type: ignore
88-
_raw_decoder: Callable[[str], bytes] = self._raw_decoder # type: ignore
87+
_raw_encoder: Callable[[bytes], str] = self._raw_encoder
88+
_raw_decoder: Callable[[str], bytes] = self._raw_decoder
8989
return f"CustomEncoding({repr(_raw_encoder)}, {repr(_raw_decoder)})"
9090

9191

0 commit comments

Comments
 (0)