
ŽàW%  ã               @   s¤   d  Z  d d l Z d d d „ Z d d d „ Z Gd d	 „  d	 e j ƒ Z Gd
 d „  d e j ƒ Z Gd d „  d e j ƒ Z Gd d „  d e j ƒ Z d d „  Z	 d S)a   Python 'utf-8-sig' Codec
This work similar to UTF-8 with the following changes:

* On encoding/writing a UTF-8 encoded BOM will be prepended/written as the
  first three bytes.

* On decoding/reading if the first three bytes are a UTF-8 encoded BOM, these
  bytes will be skipped.
é    NÚstrictc             C   s'   t  j t  j |  | ƒ d t |  ƒ f S)Nr   )ÚcodecsÚBOM_UTF8Úutf_8_encodeÚlen)ÚinputÚerrors© r	   ú>/afs/.cs/s/python-3.5.2/amd64_rhel6/lib/python3.5/utf_8_sig.pyÚencode   s    r   c             C   s^   d } |  d  d … t  j k r5 |  d d  … }  d } t  j |  | d ƒ \ } } | | | f S)Nr   é   T)r   r   Úutf_8_decode)r   r   ÚprefixÚoutputÚconsumedr	   r	   r
   Údecode   s    r   c               @   sR   e  Z d  Z d d d „ Z d d d „ Z d d „  Z d	 d
 „  Z d d „  Z d S)ÚIncrementalEncoderr   c             C   s    t  j j |  | ƒ d |  _ d  S)Né   )r   r   Ú__init__Úfirst)Úselfr   r	   r	   r
   r      s    zIncrementalEncoder.__init__Fc             C   sK   |  j  r0 d |  _  t j t j | |  j ƒ d St j | |  j ƒ d Sd  S)Nr   )r   r   r   r   r   )r   r   Úfinalr	   r	   r
   r      s
    		zIncrementalEncoder.encodec             C   s   t  j j |  ƒ d |  _ d  S)Nr   )r   r   Úresetr   )r   r	   r	   r
   r   '   s    zIncrementalEncoder.resetc             C   s   |  j  S)N)r   )r   r	   r	   r
   Úgetstate+   s    zIncrementalEncoder.getstatec             C   s   | |  _  d  S)N)r   )r   Ústater	   r	   r
   Úsetstate.   s    zIncrementalEncoder.setstateN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r	   r	   r	   r
   r      s
   r   c               @   sO   e  Z d  Z d d d „ Z d d „  Z d d „  Z d d	 „  Z d
 d „  Z d S)ÚIncrementalDecoderr   c             C   s    t  j j |  | ƒ d |  _ d  S)Nr   )r   ÚBufferedIncrementalDecoderr   r   )r   r   r	   r	   r
   r   2   s    zIncrementalDecoder.__init__c             C   s¥   |  j  r’ t | ƒ d k  r= t j j | ƒ r1 d Sd |  _  nU d |  _  | d  d … t j k r’ t j | d d  … | | ƒ \ } } | | d f St j | | | ƒ S)Nr   Ú r   )r!   r   )r   r   r   r   Ú
startswithr   )r   r   r   r   r   r   r	   r	   r
   Ú_buffer_decode6   s    		%z!IncrementalDecoder._buffer_decodec             C   s   t  j j |  ƒ d |  _ d  S)Nr   )r   r    r   r   )r   r	   r	   r
   r   G   s    zIncrementalDecoder.resetc             C   s#   t  j j |  ƒ } | d |  j f S)Nr   )r   r    r   r   )r   r   r	   r	   r
   r   K   s    zIncrementalDecoder.getstatec             C   s$   t  j j |  | ƒ | d |  _ d  S)Nr   )r   r    r   r   )r   r   r	   r	   r
   r   P   s    zIncrementalDecoder.setstateN)r   r   r   r   r#   r   r   r   r	   r	   r	   r
   r   1   s
   r   c               @   s+   e  Z d  Z d d „  Z d d d „ Z d S)ÚStreamWriterc             C   s3   t  j j |  ƒ y
 |  ` Wn t k
 r. Yn Xd  S)N)r   r$   r   r   ÚAttributeError)r   r	   r	   r
   r   V   s
    
zStreamWriter.resetr   c             C   s   t  j |  _ t | | ƒ S)N)r   r   r   )r   r   r   r	   r	   r
   r   ]   s    zStreamWriter.encodeN)r   r   r   r   r   r	   r	   r	   r
   r$   U   s   r$   c               @   s+   e  Z d  Z d d „  Z d d d „ Z d S)ÚStreamReaderc             C   s3   t  j j |  ƒ y
 |  ` Wn t k
 r. Yn Xd  S)N)r   r&   r   r   r%   )r   r	   r	   r
   r   b   s
    
zStreamReader.resetr   c             C   sœ   t  | ƒ d k  r+ t j j | ƒ r€ d SnU | d  d … t j k r€ t j |  _ t j | d d  … | ƒ \ } } | | d f St j |  _ t j | | ƒ S)Nr   r!   r   )r!   r   )r   r   r   r"   r   r   )r   r   r   r   r   r	   r	   r
   r   i   s    "zStreamReader.decodeN)r   r   r   r   r   r	   r	   r	   r
   r&   a   s   r&   c               C   s4   t  j d d d t d t d t d t d t d t ƒ S)	NÚnamez	utf-8-sigr   r   ÚincrementalencoderÚincrementaldecoderÚstreamreaderÚstreamwriter)r   Ú	CodecInfor   r   r   r   r&   r$   r	   r	   r	   r
   Úgetregentryy   s    	r-   )
Ú__doc__r   r   r   r   r    r   r$   r&   r-   r	   r	   r	   r
   Ú<module>	   s   $