U
    £�©j·  ã                   @   s¾   d dl Z d dlmZmZmZ ddlmZmZmZm	Z	m
Z
mZ G dd„ de jƒZG dd„ de jƒZG d	d
„ d
e jƒZG dd„ dee jƒZG dd„ dee jƒZeee j dœdd„Ze  e¡ dS )é    N)ÚAnyÚOptionalÚTupleé   )Ú	IDNAErrorÚ_unicode_dots_reÚalabelÚdecodeÚencodeÚulabelc                   @   sH   e Zd ZdZd	eeeeef dœdd„Zd
eeeeef dœdd„Z	dS )ÚCodecaf  Stateless IDNA 2008 codec.

    Implements the :class:`codecs.Codec` protocol so that the whole-domain
    encoder (:func:`idna.encode`) and decoder (:func:`idna.decode`) are
    accessible through the standard codec machinery as ``"idna2008"``.

    Only the ``"strict"`` error handler is supported; any other handler
    raises :exc:`~idna.IDNAError`.
    Ústrict)ÚdataÚerrorsÚreturnc                 C   s0   |dkrt d|› d�ƒ‚|s dS t|ƒt|ƒfS )Nr   úUnsupported error handling "ú"©ó    r   )r   r
   Úlen©Úselfr   r   © r   ú./tmp/pip-unpacked-wheel-j_8njjuc/idna/codec.pyr
      s
    zCodec.encodec                 C   s0   |dkrt d|› d�ƒ‚|s dS t|ƒt|ƒfS )Nr   r   r   ©Ú r   )r   r	   r   r   r   r   r   r	      s
    zCodec.decodeN)r   )r   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústrr   ÚbytesÚintr
   r	   r   r   r   r   r      s   
	r   c                   @   s,   e Zd ZdZeeeeeef dœdd„Z	dS )ÚIncrementalEncodera·  Incremental IDNA 2008 encoder.

    Buffers a partial trailing label across calls until either the next
    label separator is seen or ``final=True``, so that streamed input is
    encoded one whole label at a time. Any of the four Unicode label
    separators (``U+002E``, ``U+3002``, ``U+FF0E``, ``U+FF61``) ends a
    label; the result always uses ``U+002E`` as the separator.

    Only the ``"strict"`` error handler is supported.
    ©r   r   Úfinalr   c           
      C   s²   |dkrt d|› d�ƒ‚|s dS t |¡}d}|rX|d sFd}|d= n|sX|d= |rXd}g }d}|D ]*}| t|ƒ¡ |r‚|d	7 }|t|ƒ7 }qdd |¡| }	|t|ƒ7 }|	|fS )
Nr   r   r   r   r   éÿÿÿÿó   .r   r   )r   r   ÚsplitÚappendr   r   Újoin)
r   r   r   r%   ÚlabelsÚtrailing_dotÚresultÚsizeÚlabelZresult_bytesr   r   r   Ú_buffer_encode1   s0    
z!IncrementalEncoder._buffer_encodeN)
r   r   r   r   r    Úboolr   r!   r"   r0   r   r   r   r   r#   %   s   r#   c                   @   s,   e Zd ZdZeeeeeef dœdd„Z	dS )ÚIncrementalDecodera  Incremental IDNA 2008 decoder.

    Buffers a partial trailing label across calls until either the next
    label separator is seen or ``final=True``, so that streamed input is
    decoded one whole label at a time.

    Only the ``"strict"`` error handler is supported.
    r$   c           
      C   sÆ   |dkrt d|› d�ƒ‚|s dS t|tƒs4t|dƒ}t |¡}d}|rl|d sZd}|d= n|sl|d= |rld}g }d	}|D ]*}| t|ƒ¡ |r–|d
7 }|t|ƒ7 }qxd |¡| }	|t|ƒ7 }|	|fS )Nr   r   r   r   Úasciir   r&   Ú.r   r   )	r   Ú
isinstancer    r   r(   r)   r   r   r*   )
r   r   r   r%   r+   r,   r-   r.   r/   Z
result_strr   r   r   Ú_buffer_decode\   s4    


z!IncrementalDecoder._buffer_decodeN)
r   r   r   r   r   r    r1   r   r"   r6   r   r   r   r   r2   R   s   	r2   c                   @   s   e Zd ZdS )ÚStreamWriterN©r   r   r   r   r   r   r   r7      s   r7   c                   @   s   e Zd ZdS )ÚStreamReaderNr8   r   r   r   r   r9   ƒ   s   r9   )Únamer   c              	   C   s,   | dkrdS t j| tƒ jtƒ jttttd�S )a£  Codec search function registered with :mod:`codecs`.

    Returns a :class:`codecs.CodecInfo` for the ``"idna2008"`` codec name
    so that ``str.encode("idna2008")`` and ``bytes.decode("idna2008")``
    invoke the IDNA 2008 codec defined in this module.

    :param name: The codec name being looked up.
    :returns: A :class:`codecs.CodecInfo` instance if ``name`` is
        ``"idna2008"``, otherwise ``None``.
    Zidna2008N)r:   r
   r	   ÚincrementalencoderÚincrementaldecoderÚstreamwriterÚstreamreader)	ÚcodecsÚ	CodecInfor   r
   r	   r#   r2   r7   r9   )r:   r   r   r   Úsearch_function‡   s    ùrA   )r?   Útypingr   r   r   Úcorer   r   r   r	   r
   r   r   ÚBufferedIncrementalEncoderr#   ÚBufferedIncrementalDecoderr2   r7   r9   r    r@   rA   Úregisterr   r   r   r   Ú<module>   s    --