ó
‹²,]c           @   sŽ  d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l Z d d l Z d d l	 m
 Z
 d d l m Z m Z m Z m Z d d l m Z e j ƒ  Z e e d „ Z d „  Z e ƒ  Z d	 „  Z d
 „  Z e j d ƒ Z d d d d d d d d d d d d d d d d d d d d g Z e d  „ Z d! „  Z d" „  Z d# „  Z  d d$ d% „ Z! d d$ d& „ Z" d d' „ Z# d d( „ Z$ d) „  Z% d S(*   sÂ   
Text encoding utilities, text file I/O

Functions 'get_coding', 'decode', 'encode' and 'to_unicode' come from Eric4
source code (Utilities/__init___.py) Copyright Â© 2003-2009 Detlev Offenbach
iÿÿÿÿ(   t   BOM_UTF8t	   BOM_UTF16t	   BOM_UTF32N(   t   UniversalDetector(   t	   is_stringt   to_text_stringt   is_binary_stringt
   is_unicode(   t	   is_binaryc         C   sa   y |  j  d ƒ j d ƒ SWn@ t k
 r\ y |  j  d ƒ j | ƒ SWq] t k
 rX |  SXn Xd S(   s   Transcode a text stringt   cp437t   cp1252N(   t   decodet   encodet   UnicodeError(   t   textt   inputt   output(    (    s4   lib/python2.7/site-packages/spyder/utils/encoding.pyt	   transcode    s    c          C   s%   t  j ƒ  }  |  d k r! t }  n  |  S(   sg   
    Query the filesystem for the encoding used to encode filenames
    and environment variables.
    N(   t   syst   getfilesystemencodingt   Nonet   PREFERRED_ENCODING(   t   encoding(    (    s4   lib/python2.7/site-packages/spyder/utils/encoding.pyR   1   s    	c         C   sh   t  |  ƒ s$ t |  j ƒ  d ƒ }  n@ t |  ƒ rd y |  j t ƒ } Wn t t f k
 r\ qd X| Sn  |  S(   sT   
    Return a unicode version of string decoded using the file system encoding.
    s   utf-8(   R   R   t   toUtf8R   R   t   FS_ENCODINGR   t	   TypeError(   t   stringt   unic(    (    s4   lib/python2.7/site-packages/spyder/utils/encoding.pyt   to_unicode_from_fs>   s    c         C   sD   t  |  ƒ r@ y |  j t ƒ } Wn t t f k
 r8 q@ X| Sn  |  S(   s[   
    Return a byte string version of unic encoded using the file 
    system encoding.
    (   R   R   R   R   R   (   R   R   (    (    s4   lib/python2.7/site-packages/spyder/utils/encoding.pyt   to_fs_from_unicodeN   s    s   coding[:=]\s*([-\w_.]+)s   utf-8s	   iso8859-1s
   iso8859-15t   asciis   koi8-rt   cp1251s   koi8-us	   iso8859-2s	   iso8859-3s	   iso8859-4s	   iso8859-5s	   iso8859-6s	   iso8859-7s	   iso8859-8s	   iso8859-9s
   iso8859-10s
   iso8859-13s
   iso8859-14s   latin-1s   utf-16c         C   sÜ   | sy xp |  j  ƒ  d  D][ } y t j t | ƒ ƒ } Wn t k
 rI q X| r | j d ƒ } | t k rr | Sq q Wn  t |  ƒ rØ t ƒ  } x2 |  j  ƒ  d  D]  } | j	 | ƒ | j
 rŸ PqŸ qŸ W| j ƒ  | j d Sd S(   sr   
    Function to get the coding of a text.
    @param text text to inspect (string)
    @return coding string
    i   i   R   N(   t
   splitlinest	   CODING_REt   searchR   t   UnicodeDecodeErrort   groupt   CODECSR   R   t   feedt   donet   closet   resultR   (   R   t   force_chardett   lineR)   t   codect   detector(    (    s4   lib/python2.7/site-packages/spyder/utils/encoding.pyt
   get_codingj   s&    		 
c         C   s  y­ |  j  t ƒ r/ t |  t t ƒ d ƒ d f S|  j  t ƒ r[ t |  t t ƒ d ƒ d f S|  j  t ƒ r‡ t |  t t ƒ d ƒ d f St |  ƒ } | r¬ t |  | ƒ | f SWn t t f k
 rÆ n Xy t |  d ƒ d f SWn t t f k
 r÷ n Xt |  d ƒ d f S(   sr   
    Function to decode a text.
    @param text text to decode (string)
    @return decoded text and encoding
    s   utf-8s	   utf-8-boms   utf-16s   utf-32s   utf-8-guesseds   latin-1s   latin-1-guessed(	   t
   startswithR    R   t   lenR   R   R.   R   t   LookupError(   R   t   coding(    (    s4   lib/python2.7/site-packages/spyder/utils/encoding.pyR      s"    c         C   sm  | d k r# t  |  j d ƒ d f S| r] y |  j | ƒ | f SWq] t t f k
 rY q] Xn  t |  ƒ } | r³ y |  j | ƒ | f SWq³ t t f k
 r¯ t d | ƒ ‚ q³ Xn  | rÈ | j d ƒ s× | j d ƒ r/| j d d ƒ } | j d d ƒ } y |  j | ƒ | f SWq/t t f k
 r+q/Xn  y |  j d ƒ d f SWn t k
 rYn X|  j d ƒ d f S(   s®   
    Function to encode a text.
    @param text text to encode (string)
    @param orig_coding type of the original coding (string)
    @return encoded text and encoding
    s	   utf-8-boms   utf-8s   Incorrect encoding (%s)s   -defaults   -guessedt    R   (   R    R   R   R1   R.   t   RuntimeErrort   endswitht   replace(   R   t   orig_codingR2   (    (    s4   lib/python2.7/site-packages/spyder/utils/encoding.pyR   ª   s4    c         C   s`   t  |  ƒ s\ xM t D]B } y t |  | ƒ } Wn" t k
 r? q t k
 rP Pq X| Sq Wn  |  S(   s   Convert a string to unicode(   R   R%   R   R   R   (   R   R,   R   (    (    s4   lib/python2.7/site-packages/spyder/utils/encoding.pyt
   to_unicodeÔ   s    t   wbc         C   s>   t  |  | ƒ \ }  } t | | ƒ  } | j |  ƒ Wd QX| S(   sd   
    Write 'text' to file ('filename') assuming 'encoding'
    Return (eventually new) encoding
    N(   R   t   opent   write(   R   t   filenameR   t   modet   textfile(    (    s4   lib/python2.7/site-packages/spyder/utils/encoding.pyR;   ã   s    c         C   s   t  t j j |  ƒ | | | ƒ S(   se   
    Write 'lines' to file ('filename') assuming 'encoding'
    Return (eventually new) encoding
    (   R;   t   ost   linesept   join(   t   linesR<   R   R=   (    (    s4   lib/python2.7/site-packages/spyder/utils/encoding.pyt
   writelinesí   s    c         C   s+   t  t |  d ƒ j ƒ  ƒ \ } } | | f S(   sG   
    Read text from file ('filename')
    Return text and encoding
    t   rb(   R   R:   t   read(   R<   R   R   (    (    s4   lib/python2.7/site-packages/spyder/utils/encoding.pyRE   ô   s    !c         C   s+   t  |  | ƒ \ } } | j t j ƒ | f S(   sI   
    Read lines from file ('filename')
    Return lines and encoding
    (   RE   t   splitR?   R@   (   R<   R   R   (    (    s4   lib/python2.7/site-packages/spyder/utils/encoding.pyt	   readlinesü   s    c         C   s.   y t  |  ƒ SWn t t f k
 r) t SXd S(   s5   
    Test if the given path is a text-like file.
    N(   R   t   OSErrort   IOErrort   False(   R<   (    (    s4   lib/python2.7/site-packages/spyder/utils/encoding.pyt   is_text_file  s    (&   t   __doc__t   codecsR    R   R   t   localet   reR?   R   t   chardet.universaldetectorR   t   spyder.py3compatR   R   R   R   t'   spyder.utils.external.binaryornot.checkR   t   getpreferredencodingR   R   R   R   R   R   t   compileR!   R%   RJ   R.   R   R   R8   R;   RC   RE   RG   RK   (    (    (    s4   lib/python2.7/site-packages/spyder/utils/encoding.pyt   <module>   s:   "					#		*	
	