Skip to content

Commit

Permalink
v0.2.0
Browse files Browse the repository at this point in the history
Closes #6 .
  • Loading branch information
sg495 committed Feb 3, 2023
1 parent 4cb2705 commit 99da3bb
Show file tree
Hide file tree
Showing 18 changed files with 198 additions and 4,496 deletions.
30 changes: 0 additions & 30 deletions ADDITIONAL-LICENSES

This file was deleted.

2 changes: 0 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -332,5 +332,3 @@ License
-------

`MIT © Hashberg Ltd. <LICENSE>`_

See `additional Licenses <ADDITIONAL-LICENSES>`_ for licensing of the multicodec table, the multibase table and test vectors for multihashes.
38 changes: 36 additions & 2 deletions docs/getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ while :class:`~multiformats.cid.CID` is a class for Content IDentifiers.
The following are mandatory dependencies for this module:

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

The following are optional dependencies for this module:

Expand All @@ -50,4 +51,37 @@ You can install the latest release together with all optional dependencies as fo
$ pip install --upgrade multiformats[full]
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
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:

.. code-block:: python
import multiformats_config
multiformats_config.enable(codecs=["sha1", 0x29], bases=["base64url", "9"])
from multiformats import *
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.
Using ``multiformats_config.enable(codecs=[], bases=[])`` results in a minimal set of (mandatory) multicodecs and multibases to be loaded:

.. code-block:: python
_minimal_multicodecs = frozenset([
0x00, # 'identity'
0x01, # 'cidv1'
0x02, # 'cidv2'
0x12, # 'sha2-256'
0x14, # 'sha3-512'
0x16, # 'sha3-256'
0x70, # 'dag-pb'
0x71, # 'dag-cbor'
0x72, # 'libp2p-key'
])
_minimal_multibases = frozenset([
"identity",
"base16",
"base32",
"base58btc",
])
GitHub repo: https://github.com/hashberg-io/multiformats
2 changes: 1 addition & 1 deletion multiformats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
while :class:`~multiformats.cid.CID` is a class for Content IDentifiers.
"""

__version__ = "0.1.4"
__version__ = "0.2.0"

from . import varint
from . import multicodec
Expand Down
53 changes: 27 additions & 26 deletions multiformats/multibase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@

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

from multiformats.multibase import raw
from multiformats.varint import BytesLike
from .raw import RawEncoder, RawDecoder
from .err import MultibaseKeyError, MultibaseValueError


class Multibase:
"""
Container class for a multibase encoding.
Expand Down Expand Up @@ -528,35 +528,36 @@ def decode_raw(s: str) -> Tuple[Multibase, bytes]:
base = from_str(s)
return base, base.decode(s)

_code_table, _name_table = load_multibase_table()

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

Example usage:
# Example usage:

>>> code_table, name_table = build_multicodec_tables(bases)
# >>> code_table, name_table = build_multicodec_tables(bases)

:param bases: the multibases to add to the table
::
# :param bases: the multibases to add to the table
# ::

:raises ValueError: if the same encoding code or name is encountered multiple times
"""
# validate(multicodecs, Iterable[Multicodec]) # TODO: not yet properly supported by typing-validation
code_table: Dict[str, Multibase] = {}
name_table: Dict[str, Multibase] = {}
for e in bases:
if e.code in code_table:
raise MultibaseValueError(f"Multicodec name {e.name} appears multiple times in table.")
code_table[e.code] = e
if e.name in name_table:
raise MultibaseValueError(f"Multicodec name {e.name} appears multiple times in table.")
name_table[e.name] = e
return code_table, name_table
# :raises ValueError: if the same encoding code or name is encountered multiple times
# """
# # validate(multicodecs, Iterable[Multicodec]) # TODO: not yet properly supported by typing-validation
# code_table: Dict[str, Multibase] = {}
# name_table: Dict[str, Multibase] = {}
# for e in bases:
# if e.code in code_table:
# raise MultibaseValueError(f"Multicodec name {e.name} appears multiple times in table.")
# code_table[e.code] = e
# if e.name in name_table:
# raise MultibaseValueError(f"Multicodec name {e.name} appears multiple times in table.")
# name_table[e.name] = e
# return code_table, name_table

# Create the global code->multibase and name->multibase mappings.
_code_table: Dict[str, Multibase]
_name_table: Dict[str, Multibase]
with importlib_resources.open_text("multiformats.multibase", "multibase-table.json", encoding="utf8") as _table_f:
_table_json = json.load(_table_f)
_code_table, _name_table = build_multibase_tables(Multibase(**row) for row in _table_json)
# _code_table: Dict[str, Multibase] = {}
# _name_table: Dict[str, Multibase] = {}
# with importlib_resources.open_text("multiformats.multibase", "multibase-table.json", encoding="utf8") as _table_f:
# _table_json = json.load(_table_f)
# _code_table, _name_table = build_multibase_tables(Multibase(**row) for row in _table_json)
26 changes: 0 additions & 26 deletions multiformats/multibase/multibase-table.csv

This file was deleted.

152 changes: 0 additions & 152 deletions multiformats/multibase/multibase-table.json

This file was deleted.

12 changes: 6 additions & 6 deletions multiformats/multibase/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class CustomEncoding:
def __init__(self, raw_encoder: RawEncoder, raw_decoder: RawDecoder):
# validate(raw_encoder, Callable[[bytes], str]) # TODO: not yet supported by typing-validation
# validate(raw_decoder, Callable[[str], bytes]) # TODO: not yet supported by typing-validation
self._raw_encoder = raw_encoder # type: ignore
self._raw_decoder = raw_decoder # type: ignore
self._raw_encoder = raw_encoder
self._raw_decoder = raw_decoder

def encode(self, b: BytesLike) -> str:
"""
Expand All @@ -70,7 +70,7 @@ def encode(self, b: BytesLike) -> str:
:param b: the bytestring to be encoded
:type b: :obj:`~multiformats.varint.BytesLike`
"""
raw_encoder: RawEncoder = self._raw_encoder # type: ignore
raw_encoder: RawEncoder = self._raw_encoder
return raw_encoder(b)

def decode(self, s: str) -> bytes:
Expand All @@ -80,12 +80,12 @@ def decode(self, s: str) -> bytes:
:param s: the string to be decoded
:type s: :obj:`str`
"""
raw_decoder: RawDecoder = self._raw_decoder # type: ignore
raw_decoder: RawDecoder = self._raw_decoder
return raw_decoder(s)

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


Expand Down
Loading

0 comments on commit 99da3bb

Please sign in to comment.