
    Ձj)                        d Z ddlZddlmZmZ ddlmZ dxZZddd	d
ddddZ	i Z
d Zd Zd Z G d d      Z ed      Z ed      Z ed      Zd#dZd ZedfdZd#dZd ZedfdZd Z G d d       Z G d! d"      Zy)$a  

    webencodings
    ~~~~~~~~~~~~

    This is a Python implementation of the `WHATWG Encoding standard
    <http://encoding.spec.whatwg.org/>`. See README for details.

    :copyright: Copyright 2012 by Simon Sapin
    :license: BSD, see LICENSE for details.

    N   )replacement_codec_infouser_codec_info)LABELSz0.6.1z	iso8859-8zmac-cyrillicz	mac-romancp874cp932	big5hkscscp949)ziso-8859-8-izx-mac-cyrillic	macintoshzwindows-874	shift_jisbig5zeuc-krc                 Z    | j                         j                         j                         S )a2  Transform (only) ASCII letters to lower case: A-Z is mapped to a-z.

    :param string: An Unicode string.
    :returns: A new Unicode string.

    This is used for `ASCII case-insensitive
    <http://encoding.spec.whatwg.org/#ascii-case-insensitive>`_
    matching of encoding labels.
    The same matching is also used, among other things,
    for `CSS keywords <http://dev.w3.org/csswg/css-values/#keywords>`_.

    This is different from the :meth:`str.lower` method of Unicode strings
    which also affect non-ASCII characters,
    sometimes mapping them into the ASCII range:

        >>> keyword = 'Bac\N{KELVIN SIGN}ground'
        >>> assert keyword.lower() == 'background'
        >>> assert ascii_lower(keyword) != keyword.lower()
        >>> assert ascii_lower(keyword) == 'bac\N{KELVIN SIGN}ground'

    )encodelowerdecode)strings    </tmp/pip-target-jldlmaw8/lib/python/webencodings/__init__.pyascii_lowerr   &   s"    . ==?  "))++    c                 H   t        | j                  d            } t        j                  |       }|yt        j                  |      }|X|dk(  rt
        }n7|dk(  rt        }n+t        j                  ||      }t        j                  |      }t        ||      }|t        |<   |S )a6  Look for an encoding by its label.

    This is the spec's `get an encoding
    <http://encoding.spec.whatwg.org/#concept-encoding-get>`_ algorithm.
    Supported labels are listed there.

    :param label: A string.
    :returns:
        An :class:`Encoding` object, or :obj:`None` for an unknown label.

    z	
 Nzx-user-definedreplacement)r   stripr   getCACHEr   r   PYTHON_NAMEScodecslookupEncoding)labelnameencoding
codec_infopython_names        r   r   r   @   s     K01E::eD|yyH##(J]"/J&**46K{3JD*-dOr   c                 X    t        | d      r| S t        |       }|t        d|       |S )zAccept either an encoding object or label.

    :param encoding: An :class:`Encoding` object or a label string.
    :returns: An :class:`Encoding` object.
    :raises: :exc:`LookupError` for an unknown label.

    r"   zUnknown encoding label: )hasattrr   LookupError)encoding_or_labelr!   s     r   _get_encodingr(   `   s@      ,/  '(H45F4IJKKOr   c                       e Zd ZdZd Zd Zy)r   a2  A character encoding that can be used for decoding or encoding.

    .. attribute:: name

        Canonical name of the encoding

    .. attribute:: codec_info

        The actual implementation of the encoding,
        a stdlib :class:`~codecs.CodecInfo` object.
        See :func:`codecs.register`.

    c                      || _         || _        y N)r    r"   )selfr    r"   s      r   __init__zEncoding.__init__   s    	$r   c                 "    d| j                    dS )Nz
<Encoding >)r    )r,   s    r   __repr__zEncoding.__repr__   s    DII;a((r   N)__name__
__module____qualname____doc__r-   r0    r   r   r   r   q   s    %)r   r   zutf-8zutf-16lezutf-16bec                     t        |      }t        |       \  }} |xs |}|j                  j                  | |      d   |fS )a  Decode a single string.

    :param input: A byte string
    :param fallback_encoding:
        An :class:`Encoding` object or a label string.
        The encoding to use if :obj:`input` does not have a BOM.
    :param errors: Type of error handling. See :func:`codecs.register`.
    :raises: :exc:`LookupError` for an unknown encoding label.
    :return:
        A ``(output, encoding)`` tuple of an Unicode string
        and an :obj:`Encoding`.

    r   )r(   _detect_bomr"   r   )inputfallback_encodingerrorsbom_encodingr!   s        r   r   r      sM     &&78%e,L%00H%%eV4Q7AAr   c                     | j                  d      rt        | dd fS | j                  d      rt        | dd fS | j                  d      rt        | dd fS d| fS )zBReturn (bom_encoding, input), with any BOM removed from the input.s      Ns   s   ﻿   )
startswith_UTF16LE_UTF16BEUTF8)r8   s    r   r7   r7      sh    $qr""$qr""(U12Y;r   strictc                 R    t        |      j                  j                  | |      d   S )a*  Encode a single string.

    :param input: An Unicode string.
    :param encoding: An :class:`Encoding` object or a label string.
    :param errors: Type of error handling. See :func:`codecs.register`.
    :raises: :exc:`LookupError` for an unknown encoding label.
    :return: A byte string.

    r   )r(   r"   r   )r8   r!   r:   s      r   r   r      s'     "--44UFCAFFr   c                 P    t        ||      }t        | |      }t        |      }||fS )a  "Pull"-based decoder.

    :param input:
        An iterable of byte strings.

        The input is first consumed just enough to determine the encoding
        based on the precense of a BOM,
        then consumed on demand when the return value is.
    :param fallback_encoding:
        An :class:`Encoding` object or a label string.
        The encoding to use if :obj:`input` does not have a BOM.
    :param errors: Type of error handling. See :func:`codecs.register`.
    :raises: :exc:`LookupError` for an unknown encoding label.
    :returns:
        An ``(output, encoding)`` tuple.
        ``output`` is an iterable of Unicode strings,
        ``encoding`` is the :obj:`Encoding` that is being used.

    )IncrementalDecoder_iter_decode_generatornext)r8   r9   r:   decoder	generatorr!   s         r   iter_decoderK      s1    * !!2F;G&ug6IIHhr   c              #   H  K   |j                   }t        |       } | D ].  } ||      }|s|j                  J |j                   |  n.  |dd      }|j                  J |j                   |r| y| D ]  } ||      }|s|   |dd      }|r| yyw)z|Return a decode generator.

    It first yields the :obj:`Encoding`, then yields output chunks as Unicode
    strings.

    Nr   Tfinal)r   iterr!   )r8   rI   r   chunckoutputs        r   rG   rG      s      ^^FKE ##///"""L 4(+++L L Ct$F s   'B"AB"
B"c                 F    t        ||      j                  }t        | |      S )aD  "Pull"-based encoder.

    :param input: An iterable of Unicode strings.
    :param encoding: An :class:`Encoding` object or a label string.
    :param errors: Type of error handling. See :func:`codecs.register`.
    :raises: :exc:`LookupError` for an unknown encoding label.
    :returns: An iterable of byte strings.

    )IncrementalEncoderr   _iter_encode_generator)r8   r!   r:   r   s       r   iter_encoderU      s#      &188F!%00r   c              #   Z   K   | D ]  } ||      }|s|   |dd      }|r| yyw)zReturn an encode generator. TrM   Nr5   )r8   r   rP   rQ   s       r   rT   rT     sA      L Bd#F s   ++c                        e Zd ZdZddZddZy)rF   a9  "Push"-based decoder.

    :param fallback_encoding:
        An :class:`Encoding` object or a label string.
        The encoding to use if :obj:`input` does not have a BOM.
    :param errors: Type of error handling. See :func:`codecs.register`.
    :raises: :exc:`LookupError` for an unknown encoding label.

    c                 \    t        |      | _        || _        d| _        d | _        d | _        y )Nr   )r(   _fallback_encoding_errors_buffer_decoderr!   )r,   r9   r:   s      r   r-   zIncrementalDecoder.__init__  s.    "/0A"B
 r   c                 B   | j                   }|	 |||      S | j                  |z   }t        |      \  }}|$t        |      dk  r
|s|| _        y| j                  }|j
                  j                  | j                        j                  }|| _         || _	         |||      S )zDecode one chunk of the input.

        :param input: A byte string.
        :param final:
            Indicate that no more input is available.
            Must be :obj:`True` if this is the last call.
        :returns: An Unicode string.

        r>   rW   )
r]   r\   r7   lenrZ   r"   incrementaldecoderr[   r   r!   )r,   r8   rN   rI   r!   s        r   r   zIncrementalDecoder.decode(  s     --5%((u$%e,%5zA~e$22%%88FMM ue$$r   Nreplace)F)r1   r2   r3   r4   r-   r   r5   r   r   rF   rF     s    
%r   rF   c                       e Zd ZdZedfdZy)rS   a  "Push"-based encoder.

    :param encoding: An :class:`Encoding` object or a label string.
    :param errors: Type of error handling. See :func:`codecs.register`.
    :raises: :exc:`LookupError` for an unknown encoding label.

    .. method:: encode(input, final=False)

        :param input: An Unicode string.
        :param final:
            Indicate that no more input is available.
            Must be :obj:`True` if this is the last call.
        :returns: A byte string.

    rC   c                 n    t        |      }|j                  j                  |      j                  | _        y r+   )r(   r"   incrementalencoderr   )r,   r!   r:   s      r   r-   zIncrementalEncoder.__init__T  s*     *))<<VDKKr   N)r1   r2   r3   r4   rB   r-   r5   r   r   rS   rS   D  s     !%X Lr   rS   ra   )r4   r   customr   r   labelsr   VERSION__version__r   r   r   r   r(   r   rB   r@   rA   r   r7   r   rK   rG   rU   rT   rF   rS   r5   r   r   <module>rj      s     ;  +
  $  	,4@") ). g**B*   
G6D !%X 1/% /%dL Lr   