Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/idna/codec.py
7763 views
from .core import encode, decode, alabel, ulabel, IDNAError1import codecs2import re3from typing import Tuple, Optional45_unicode_dots_re = re.compile('[\u002e\u3002\uff0e\uff61]')67class Codec(codecs.Codec):89def encode(self, data: str, errors: str = 'strict') -> Tuple[bytes, int]:10if errors != 'strict':11raise IDNAError('Unsupported error handling \"{}\"'.format(errors))1213if not data:14return b"", 01516return encode(data), len(data)1718def decode(self, data: bytes, errors: str = 'strict') -> Tuple[str, int]:19if errors != 'strict':20raise IDNAError('Unsupported error handling \"{}\"'.format(errors))2122if not data:23return '', 02425return decode(data), len(data)2627class IncrementalEncoder(codecs.BufferedIncrementalEncoder):28def _buffer_encode(self, data: str, errors: str, final: bool) -> Tuple[str, int]: # type: ignore29if errors != 'strict':30raise IDNAError('Unsupported error handling \"{}\"'.format(errors))3132if not data:33return "", 03435labels = _unicode_dots_re.split(data)36trailing_dot = ''37if labels:38if not labels[-1]:39trailing_dot = '.'40del labels[-1]41elif not final:42# Keep potentially unfinished label until the next call43del labels[-1]44if labels:45trailing_dot = '.'4647result = []48size = 049for label in labels:50result.append(alabel(label))51if size:52size += 153size += len(label)5455# Join with U+002E56result_str = '.'.join(result) + trailing_dot # type: ignore57size += len(trailing_dot)58return result_str, size5960class IncrementalDecoder(codecs.BufferedIncrementalDecoder):61def _buffer_decode(self, data: str, errors: str, final: bool) -> Tuple[str, int]: # type: ignore62if errors != 'strict':63raise IDNAError('Unsupported error handling \"{}\"'.format(errors))6465if not data:66return ('', 0)6768labels = _unicode_dots_re.split(data)69trailing_dot = ''70if labels:71if not labels[-1]:72trailing_dot = '.'73del labels[-1]74elif not final:75# Keep potentially unfinished label until the next call76del labels[-1]77if labels:78trailing_dot = '.'7980result = []81size = 082for label in labels:83result.append(ulabel(label))84if size:85size += 186size += len(label)8788result_str = '.'.join(result) + trailing_dot89size += len(trailing_dot)90return (result_str, size)919293class StreamWriter(Codec, codecs.StreamWriter):94pass959697class StreamReader(Codec, codecs.StreamReader):98pass99100101def getregentry() -> codecs.CodecInfo:102# Compatibility as a search_function for codecs.register()103return codecs.CodecInfo(104name='idna',105encode=Codec().encode, # type: ignore106decode=Codec().decode, # type: ignore107incrementalencoder=IncrementalEncoder,108incrementaldecoder=IncrementalDecoder,109streamwriter=StreamWriter,110streamreader=StreamReader,111)112113114