-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path__init__.py
83 lines (61 loc) · 1.94 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""Known decoding and encoding profiles."""
import typing
from ..decodes import get_decode
from ..encodes import get_encode
class Profile:
"""Decoding and encoding profile"""
__slots__ = ["__encode", "__decode"]
def __init__(
self,
decode: typing.Union[str, typing.Callable[[bytes], str]],
encode: typing.Union[str, typing.Callable[[str], bytes]],
):
"""
Initialize the class.
:param decode: A profile decode
:param encode: A profile encode
"""
if isinstance(decode, str):
self.__decode = get_decode(decode)
elif callable(decode):
self.__decode = decode
else:
raise TypeError("Argument `decode` must be a string or a callable")
if isinstance(encode, str):
self.__encode = get_encode(encode)
elif callable(decode):
self.__encode = encode
else:
raise TypeError("Argument `encode` must be a string or a callable")
@property
def decode(self) -> typing.Callable[[bytes], str]:
"""
Get profile decode.
:return: The profile decode
"""
return self.__decode
@property
def encode(self) -> typing.Callable[[str], bytes]:
"""
Get profile encode.
:return: The profile encode
"""
return self.__encode
def get_profile(name: str) -> Profile:
"""
Get profile by name.
:param name: A profile name
:return: The profile
"""
return PROFILES.get(name, PROFILES["default"])
PROFILES = {
"ipfs": Profile(decode="b58_multi_hash", encode="ipfs"),
"ipns": Profile(decode="b58_multi_hash", encode="ipfs"),
"swarm": Profile(decode="hex_multi_hash", encode="swarm"),
"default": Profile(decode="utf8", encode="utf8"),
}
"""
A dict of known profiles.
`encode` should be chosen among the `encode` modules
`decode` should be chosen among the `decode` modules
"""