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