|
1 | 1 | # coding=utf-8
|
2 | 2 | from __future__ import absolute_import, division, print_function, \
|
3 |
| - unicode_literals |
| 3 | + unicode_literals |
4 | 4 |
|
5 | 5 | from codecs import Codec, CodecInfo, register as lookup_function
|
6 | 6 | from warnings import warn
|
7 | 7 |
|
8 |
| -from iota.exceptions import with_context |
9 | 8 | from six import PY3, binary_type
|
10 | 9 |
|
| 10 | +from iota.exceptions import with_context |
| 11 | + |
11 | 12 | __all__ = [
|
12 |
| - 'AsciiTrytesCodec', |
13 |
| - 'TrytesDecodeError', |
| 13 | + 'AsciiTrytesCodec', |
| 14 | + 'TrytesDecodeError', |
14 | 15 | ]
|
15 | 16 |
|
16 | 17 |
|
17 | 18 | class TrytesDecodeError(ValueError):
|
18 |
| - """ |
19 |
| - Indicates that a tryte string could not be decoded to bytes. |
20 |
| - """ |
21 |
| - pass |
| 19 | + """ |
| 20 | + Indicates that a tryte string could not be decoded to bytes. |
| 21 | + """ |
| 22 | + pass |
22 | 23 |
|
23 | 24 |
|
24 | 25 | class AsciiTrytesCodec(Codec):
|
25 |
| - """ |
26 |
| - Legacy codec for converting byte strings into trytes, and vice versa. |
27 |
| -
|
28 |
| - This method encodes each pair of trytes as an ASCII code point (and |
29 |
| - vice versa when decoding). |
30 |
| -
|
31 |
| - The end result requires more space than if the trytes were converted |
32 |
| - mathematically, but because the result is ASCII, it's easier to work |
33 |
| - with. |
34 |
| -
|
35 |
| - Think of this kind of like Base 64 for balanced ternary (: |
36 |
| - """ |
37 |
| - name = 'trytes_ascii' |
38 |
| - |
39 |
| - compat_name = 'trytes' |
40 |
| - """ |
41 |
| - Old name for this codec. |
42 |
| - Note: Will be removed in PyOTA v2.1! |
43 |
| - """ |
44 |
| - |
45 |
| - # :bc: Without the bytearray cast, Python 2 will populate the dict |
46 |
| - # with characters instead of integers. |
47 |
| - # noinspection SpellCheckingInspection |
48 |
| - alphabet = dict(enumerate(bytearray(b'9ABCDEFGHIJKLMNOPQRSTUVWXYZ'))) |
49 |
| - """ |
50 |
| - Used to encode bytes into trytes. |
51 |
| - """ |
52 |
| - |
53 |
| - index = dict(zip(alphabet.values(), alphabet.keys())) |
54 |
| - """ |
55 |
| - Used to decode trytes into bytes. |
56 |
| - """ |
57 |
| - |
58 |
| - @classmethod |
59 |
| - def get_codec_info(cls): |
60 |
| - """ |
61 |
| - Returns information used by the codecs library to configure the |
62 |
| - codec for use. |
63 | 26 | """
|
64 |
| - codec = cls() |
| 27 | + Legacy codec for converting byte strings into trytes, and vice |
| 28 | + versa. |
65 | 29 |
|
66 |
| - codec_info = { |
67 |
| - 'encode': codec.encode, |
68 |
| - 'decode': codec.decode, |
69 |
| - } |
| 30 | + This method encodes each pair of trytes as an ASCII code point (and |
| 31 | + vice versa when decoding). |
70 | 32 |
|
71 |
| - # In Python 2, all codecs are made equal. |
72 |
| - # In Python 3, some codecs are more equal than others. |
73 |
| - if PY3: |
74 |
| - codec_info['_is_text_encoding'] = False |
| 33 | + The end result requires more space than if the trytes were converted |
| 34 | + mathematically, but because the result is ASCII, it's easier to work |
| 35 | + with. |
75 | 36 |
|
76 |
| - return CodecInfo(**codec_info) |
| 37 | + Think of this kind of like Base 64 for balanced ternary (: |
| 38 | + """ |
| 39 | + name = 'trytes_ascii' |
77 | 40 |
|
78 |
| - # noinspection PyShadowingBuiltins |
79 |
| - def encode(self, input, errors='strict'): |
| 41 | + compat_name = 'trytes' |
80 | 42 | """
|
81 |
| - Encodes a byte string into trytes. |
| 43 | + Old name for this codec. |
| 44 | + Note: Will be removed in PyOTA v2.1! |
82 | 45 | """
|
83 |
| - if isinstance(input, memoryview): |
84 |
| - input = input.tobytes() |
85 | 46 |
|
86 |
| - if not isinstance(input, (binary_type, bytearray)): |
87 |
| - raise with_context( |
88 |
| - exc = TypeError("Can't encode {type}; byte string expected.".format( |
89 |
| - type = type(input).__name__, |
90 |
| - )), |
91 |
| - |
92 |
| - context = { |
93 |
| - 'input': input, |
94 |
| - }, |
95 |
| - ) |
| 47 | + # :bc: Without the bytearray cast, Python 2 will populate the dict |
| 48 | + # with characters instead of integers. |
| 49 | + # noinspection SpellCheckingInspection |
| 50 | + alphabet = dict(enumerate(bytearray(b'9ABCDEFGHIJKLMNOPQRSTUVWXYZ'))) |
| 51 | + """ |
| 52 | + Used to encode bytes into trytes. |
| 53 | + """ |
96 | 54 |
|
97 |
| - # :bc: In Python 2, iterating over a byte string yields characters |
98 |
| - # instead of integers. |
99 |
| - if not isinstance(input, bytearray): |
100 |
| - input = bytearray(input) |
| 55 | + index = dict(zip(alphabet.values(), alphabet.keys())) |
| 56 | + """ |
| 57 | + Used to decode trytes into bytes. |
| 58 | + """ |
101 | 59 |
|
102 |
| - trytes = bytearray() |
| 60 | + @classmethod |
| 61 | + def get_codec_info(cls): |
| 62 | + """ |
| 63 | + Returns information used by the codecs library to configure the |
| 64 | + codec for use. |
| 65 | + """ |
| 66 | + codec = cls() |
| 67 | + |
| 68 | + codec_info = { |
| 69 | + 'encode': codec.encode, |
| 70 | + 'decode': codec.decode, |
| 71 | + } |
| 72 | + |
| 73 | + # In Python 2, all codecs are made equal. |
| 74 | + # In Python 3, some codecs are more equal than others. |
| 75 | + if PY3: |
| 76 | + codec_info['_is_text_encoding'] = False |
| 77 | + |
| 78 | + return CodecInfo(**codec_info) |
| 79 | + |
| 80 | + # noinspection PyShadowingBuiltins |
| 81 | + def encode(self, input, errors='strict'): |
| 82 | + """ |
| 83 | + Encodes a byte string into trytes. |
| 84 | + """ |
| 85 | + if isinstance(input, memoryview): |
| 86 | + input = input.tobytes() |
| 87 | + |
| 88 | + if not isinstance(input, (binary_type, bytearray)): |
| 89 | + raise with_context( |
| 90 | + exc=TypeError( |
| 91 | + "Can't encode {type}; byte string expected.".format( |
| 92 | + type=type(input).__name__, |
| 93 | + )), |
| 94 | + |
| 95 | + context={ |
| 96 | + 'input': input, |
| 97 | + }, |
| 98 | + ) |
| 99 | + |
| 100 | + # :bc: In Python 2, iterating over a byte string yields |
| 101 | + # characters instead of integers. |
| 102 | + if not isinstance(input, bytearray): |
| 103 | + input = bytearray(input) |
| 104 | + |
| 105 | + trytes = bytearray() |
| 106 | + |
| 107 | + for c in input: |
| 108 | + second, first = divmod(c, len(self.alphabet)) |
| 109 | + |
| 110 | + trytes.append(self.alphabet[first]) |
| 111 | + trytes.append(self.alphabet[second]) |
| 112 | + |
| 113 | + return binary_type(trytes), len(input) |
| 114 | + |
| 115 | + # noinspection PyShadowingBuiltins |
| 116 | + def decode(self, input, errors='strict'): |
| 117 | + """ |
| 118 | + Decodes a tryte string into bytes. |
| 119 | + """ |
| 120 | + if isinstance(input, memoryview): |
| 121 | + input = input.tobytes() |
| 122 | + |
| 123 | + if not isinstance(input, (binary_type, bytearray)): |
| 124 | + raise with_context( |
| 125 | + exc=TypeError( |
| 126 | + "Can't decode {type}; byte string expected.".format( |
| 127 | + type=type(input).__name__, |
| 128 | + )), |
| 129 | + |
| 130 | + context={ |
| 131 | + 'input': input, |
| 132 | + }, |
| 133 | + ) |
| 134 | + |
| 135 | + # :bc: In Python 2, iterating over a byte string yields |
| 136 | + # characters instead of integers. |
| 137 | + if not isinstance(input, bytearray): |
| 138 | + input = bytearray(input) |
| 139 | + |
| 140 | + bytes_ = bytearray() |
| 141 | + |
| 142 | + for i in range(0, len(input), 2): |
| 143 | + try: |
| 144 | + first, second = input[i:i + 2] |
| 145 | + except ValueError: |
| 146 | + if errors == 'strict': |
| 147 | + raise with_context( |
| 148 | + exc=TrytesDecodeError( |
| 149 | + "'{name}' codec can't decode value; " |
| 150 | + "tryte sequence has odd length.".format( |
| 151 | + name=self.name, |
| 152 | + ), |
| 153 | + ), |
| 154 | + |
| 155 | + context={ |
| 156 | + 'input': input, |
| 157 | + }, |
| 158 | + ) |
| 159 | + elif errors == 'replace': |
| 160 | + bytes_ += b'?' |
| 161 | + |
| 162 | + continue |
| 163 | + |
| 164 | + try: |
| 165 | + bytes_.append( |
| 166 | + self.index[first] |
| 167 | + + (self.index[second] * len(self.index)) |
| 168 | + ) |
| 169 | + except ValueError: |
| 170 | + # This combination of trytes yields a value > 255 when |
| 171 | + # decoded. |
| 172 | + # Naturally, we can't represent this using ASCII. |
| 173 | + if errors == 'strict': |
| 174 | + raise with_context( |
| 175 | + exc=TrytesDecodeError( |
| 176 | + "'{name}' codec can't decode trytes {pair} " |
| 177 | + "at position {i}-{j}: " |
| 178 | + "ordinal not in range(255)".format( |
| 179 | + name=self.name, |
| 180 | + pair=chr(first) + chr(second), |
| 181 | + i=i, |
| 182 | + j=i + 1, |
| 183 | + ), |
| 184 | + ), |
| 185 | + |
| 186 | + context={ |
| 187 | + 'input': input, |
| 188 | + } |
| 189 | + ) |
| 190 | + elif errors == 'replace': |
| 191 | + bytes_ += b'?' |
| 192 | + |
| 193 | + return binary_type(bytes_), len(input) |
103 | 194 |
|
104 |
| - for c in input: |
105 |
| - second, first = divmod(c, len(self.alphabet)) |
106 | 195 |
|
107 |
| - trytes.append(self.alphabet[first]) |
108 |
| - trytes.append(self.alphabet[second]) |
| 196 | +@lookup_function |
| 197 | +def check_trytes_codec(encoding): |
| 198 | + """ |
| 199 | + Determines which codec to use for the specified encoding. |
109 | 200 |
|
110 |
| - return binary_type(trytes), len(input) |
| 201 | + References: |
111 | 202 |
|
112 |
| - # noinspection PyShadowingBuiltins |
113 |
| - def decode(self, input, errors='strict'): |
114 |
| - """ |
115 |
| - Decodes a tryte string into bytes. |
| 203 | + - https://docs.python.org/3/library/codecs.html#codecs.register |
116 | 204 | """
|
117 |
| - if isinstance(input, memoryview): |
118 |
| - input = input.tobytes() |
119 |
| - |
120 |
| - if not isinstance(input, (binary_type, bytearray)): |
121 |
| - raise with_context( |
122 |
| - exc = TypeError("Can't decode {type}; byte string expected.".format( |
123 |
| - type = type(input).__name__, |
124 |
| - )), |
125 |
| - |
126 |
| - context = { |
127 |
| - 'input': input, |
128 |
| - }, |
129 |
| - ) |
130 |
| - |
131 |
| - # :bc: In Python 2, iterating over a byte string yields characters |
132 |
| - # instead of integers. |
133 |
| - if not isinstance(input, bytearray): |
134 |
| - input = bytearray(input) |
135 |
| - |
136 |
| - bytes_ = bytearray() |
137 |
| - |
138 |
| - for i in range(0, len(input), 2): |
139 |
| - try: |
140 |
| - first, second = input[i:i+2] |
141 |
| - except ValueError: |
142 |
| - if errors == 'strict': |
143 |
| - raise with_context( |
144 |
| - exc = TrytesDecodeError( |
145 |
| - "'{name}' codec can't decode value; " |
146 |
| - "tryte sequence has odd length.".format( |
147 |
| - name = self.name, |
148 |
| - ), |
| 205 | + if encoding == AsciiTrytesCodec.name: |
| 206 | + return AsciiTrytesCodec.get_codec_info() |
| 207 | + |
| 208 | + elif encoding == AsciiTrytesCodec.compat_name: |
| 209 | + warn( |
| 210 | + '"{old_codec}" codec will be removed in PyOTA v2.1. ' |
| 211 | + 'Use "{new_codec}" instead.'.format( |
| 212 | + new_codec=AsciiTrytesCodec.name, |
| 213 | + old_codec=AsciiTrytesCodec.compat_name, |
149 | 214 | ),
|
150 | 215 |
|
151 |
| - context = { |
152 |
| - 'input': input, |
153 |
| - }, |
154 |
| - ) |
155 |
| - elif errors == 'replace': |
156 |
| - bytes_ += b'?' |
157 |
| - |
158 |
| - continue |
159 |
| - |
160 |
| - try: |
161 |
| - bytes_.append( |
162 |
| - self.index[first] |
163 |
| - + (self.index[second] * len(self.index)) |
| 216 | + DeprecationWarning, |
164 | 217 | )
|
165 |
| - except ValueError: |
166 |
| - # This combination of trytes yields a value > 255 when |
167 |
| - # decoded. Naturally, we can't represent this using ASCII. |
168 |
| - if errors == 'strict': |
169 |
| - raise with_context( |
170 |
| - exc = TrytesDecodeError( |
171 |
| - "'{name}' codec can't decode trytes {pair} at position {i}-{j}: " |
172 |
| - "ordinal not in range(255)".format( |
173 |
| - name = self.name, |
174 |
| - pair = chr(first) + chr(second), |
175 |
| - i = i, |
176 |
| - j = i+1, |
177 |
| - ), |
178 |
| - ), |
179 |
| - |
180 |
| - context = { |
181 |
| - 'input': input, |
182 |
| - } |
183 |
| - ) |
184 |
| - elif errors == 'replace': |
185 |
| - bytes_ += b'?' |
| 218 | + return AsciiTrytesCodec.get_codec_info() |
186 | 219 |
|
187 |
| - return binary_type(bytes_), len(input) |
188 |
| - |
189 |
| - |
190 |
| -@lookup_function |
191 |
| -def check_trytes_codec(encoding): |
192 |
| - """ |
193 |
| - Determines which codec to use for the specified encoding. |
194 |
| -
|
195 |
| - References: |
196 |
| - - https://docs.python.org/3/library/codecs.html#codecs.register |
197 |
| - """ |
198 |
| - if encoding == AsciiTrytesCodec.name: |
199 |
| - return AsciiTrytesCodec.get_codec_info() |
200 |
| - |
201 |
| - elif encoding == AsciiTrytesCodec.compat_name: |
202 |
| - warn( |
203 |
| - '"{old_codec}" codec will be removed in PyOTA v2.1. ' |
204 |
| - 'Use "{new_codec}" instead.'.format( |
205 |
| - new_codec = AsciiTrytesCodec.name, |
206 |
| - old_codec = AsciiTrytesCodec.compat_name, |
207 |
| - ), |
208 |
| - |
209 |
| - DeprecationWarning, |
210 |
| - ) |
211 |
| - return AsciiTrytesCodec.get_codec_info() |
212 |
| - |
213 |
| - return None |
| 220 | + return None |
0 commit comments