1 from .core
import encode
, decode
, alabel
, ulabel
, IDNAError
4 from typing
import Tuple
, Optional
6 _unicode_dots_re
= re
.compile('[\u002e\u3002\uff0e\uff61]')
8 class Codec(codecs
.Codec
):
10 def encode(self
, data
: str, errors
: str = 'strict') -> Tuple
[bytes, int]:
11 if errors
!= 'strict':
12 raise IDNAError('Unsupported error handling \"{}\"'.format(errors
))
17 return encode(data
), len(data
)
19 def decode(self
, data
: bytes, errors
: str = 'strict') -> Tuple
[str, int]:
20 if errors
!= 'strict':
21 raise IDNAError('Unsupported error handling \"{}\"'.format(errors
))
26 return decode(data
), len(data
)
28 class IncrementalEncoder(codecs
.BufferedIncrementalEncoder
):
29 def _buffer_encode(self
, data
: str, errors
: str, final
: bool) -> Tuple
[str, int]: # type: ignore
30 if errors
!= 'strict':
31 raise IDNAError('Unsupported error handling \"{}\"'.format(errors
))
36 labels
= _unicode_dots_re
.split(data
)
43 # Keep potentially unfinished label until the next call
51 result
.append(alabel(label
))
57 result_str
= '.'.join(result
) + trailing_dot
# type: ignore
58 size
+= len(trailing_dot
)
59 return result_str
, size
61 class IncrementalDecoder(codecs
.BufferedIncrementalDecoder
):
62 def _buffer_decode(self
, data
: str, errors
: str, final
: bool) -> Tuple
[str, int]: # type: ignore
63 if errors
!= 'strict':
64 raise IDNAError('Unsupported error handling \"{}\"'.format(errors
))
69 labels
= _unicode_dots_re
.split(data
)
76 # Keep potentially unfinished label until the next call
84 result
.append(ulabel(label
))
89 result_str
= '.'.join(result
) + trailing_dot
90 size
+= len(trailing_dot
)
91 return (result_str
, size
)
94 class StreamWriter(Codec
, codecs
.StreamWriter
):
98 class StreamReader(Codec
, codecs
.StreamReader
):
102 def getregentry() -> codecs
.CodecInfo
:
103 # Compatibility as a search_function for codecs.register()
104 return codecs
.CodecInfo(
106 encode
=Codec().encode
, # type: ignore
107 decode
=Codec().decode
, # type: ignore
108 incrementalencoder
=IncrementalEncoder
,
109 incrementaldecoder
=IncrementalDecoder
,
110 streamwriter
=StreamWriter
,
111 streamreader
=StreamReader
,