ó
‹²,]c           @   sØ   d  Z  d d l Z d d l Z e j e ƒ Z d „  Z d d „ Z d Z e	 e
 k r e d j e e e d d	 ƒ ƒ ƒ Z d j e e e d	 d
 ƒ ƒ ƒ Z n. e e	 e d d	 ƒ ƒ Z e	 e d	 d
 ƒ ƒ Z d „  Z d S(   s±   
binaryornot.helpers
-------------------

Helper utilities used by BinaryOrNot.

Adapted from binaryornot/helpers.py of
`BinaryOrNot <https://github.com/audreyr/binaryornot>`_.
iÿÿÿÿNc         C   s   d j  d „  |  Dƒ ƒ GHd S(   s&   
    Print a string as hex bytes.
    t   :c         s   s$   |  ] } d  j  t | ƒ ƒ Vq d S(   s   {0:x}N(   t   formatt   ord(   t   .0t   c(    (    sH   lib/python2.7/site-packages/spyder/utils/external/binaryornot/helpers.pys	   <genexpr>   s    N(   t   join(   t   s(    (    sH   lib/python2.7/site-packages/spyder/utils/external/binaryornot/helpers.pyt   print_as_hex   s    i   c         C   s/   t  |  d ƒ  } | j | ƒ } | SWd QXd S(   s«   
    :param filename: File to open and get the first little chunk of.
    :param length: Number of bytes to read, default 1024.
    :returns: Starting chunk of bytes.
    t   rbN(   t   opent   read(   t   filenamet   lengtht   ft   chunk(    (    sH   lib/python2.7/site-packages/spyder/utils/external/binaryornot/helpers.pyt   get_starting_chunk"   s    s   
	t    i    i   i   c         C   s?  |  s
 t  S|  j d t ƒ } t t | ƒ ƒ t t |  ƒ ƒ } t j d t ƒ  ƒ |  j d t	 ƒ } t t | ƒ ƒ t t |  ƒ ƒ } t j d t ƒ  ƒ | d k r° | d k  pÅ | d k oÅ | d k } t j d t ƒ  ƒ t
 j |  ƒ } t j d t ƒ  ƒ t  } | d d	 k rÊ| d
 d k rÊy] y |  j d
 | d
 ƒ Wn% t k
 ret |  d
 | d
 ƒn Xt } t j d t ƒ  ƒ WqÊt k
 r£t j d | ƒ qÊt k
 rÆt j d t ƒ  ƒ qÊXn  t j d t ƒ  ƒ | rô| rít  St SnG | rþt  Sd |  k sd |  k r7t j d t d |  k ƒ ƒ t St  Sd S(   s  
    Uses a simplified version of the Perl detection algorithm,
    based roughly on Eli Bendersky's translation to Python:
    https://eli.thegreenplace.net/2011/10/19/perls-guess-if-file-is-text-or-binary-implemented-in-python/

    This is biased slightly more in favour of deeming files as text
    files than the Perl algorithm, since all ASCII compatible character
    sets are accepted as text, not just utf-8.

    :param bytes: A chunk of bytes to check.
    :returns: True if appears to be a binary, otherwise False.
    s"   nontext_ratio1: %(nontext_ratio1)rs"   nontext_ratio2: %(nontext_ratio2)rg333333Ó?gš™™™™™©?gš™™™™™é?s&   is_likely_binary: %(is_likely_binary)rs(   detected_encoding: %(detected_encoding)rt
   confidencegÍÌÌÌÌÌì?t   encodingt   asciis7   success: decodable_as_unicode: %(decodable_as_unicode)rs0   failure: could not look up encoding %(encoding)ss7   failure: decodable_as_unicode: %(decodable_as_unicode)rs    s   ÿs
   has nulls:N(   t   Falset	   translatet   Nonet   _printable_asciit   floatt   lent   loggert   debugt   localst   _printable_high_asciit   chardett   detectt   decodet	   TypeErrort   unicodet   Truet   LookupErrort   UnicodeDecodeErrort   repr(   t   bytes_to_checkt	   low_charst   nontext_ratio1t
   high_charst   nontext_ratio2t   is_likely_binaryt   detected_encodingt   decodable_as_unicode(    (    sH   lib/python2.7/site-packages/spyder/utils/external/binaryornot/helpers.pyt   is_binary_string9   sT    "
"		
		
(   t   __doc__R   t   loggingt	   getLoggert   __name__R   R   R   t   _control_charst   bytest   strR   t   mapt   chrt   rangeR   R   R/   (    (    (    sH   lib/python2.7/site-packages/spyder/utils/external/binaryornot/helpers.pyt   <module>   s   	%$