
'![c           @   s  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l m Z m	 Z	 m
 Z
 d d l m Z m Z d d l m Z d d l m Z e j d  d d	 f k Z e j d  d d
 f k Z i d d d f d 6d d d f d 6d d d f d 6d d d f d 6d d d f d 6d d  d! f d" 6d# d$ d% f d& 6d' d( d) f d* 6d+ d, d- f d. 6d/ d0 d1 f d2 6d3 d4 d5 i d d
 f d6 6f d7 6d8 d9 d: i d d
 f d6 6f d; 6d< d= d> i d d
 f d6 6f d? 6d@ dA dB i d d
 f d6 6f dC 6dD dE dF i d d
 f d6 6f dG 6dH dI dJ i d d
 f d6 6f dK 6Z e j e j e j e j e j e j e j e j f Z e rd d l Z dL   Z n dM   Z  dN   Z dO   Z! dP   Z" dQ   Z# dR   Z$ dS e f dT     YZ% dU e f dV     YZ& dW   Z' d S(X   s*   Checker for string formatting operations.
iN(   t   ITokenCheckert   IAstroidCheckert   IRawChecker(   t   BaseCheckert   BaseTokenChecker(   t   utils(   t   check_messagesi   i   i    i   s3   Unsupported format character %r (%#02x) at index %ds   bad-format-charactersQ   Used when a unsupported format character is used in a format              string.t   E1300s4   Format string ends in middle of conversion specifiers   truncated-format-strings\   Used when a format string terminates before the end of a               conversion specifier.t   E1301s?   Mixing named and unnamed conversion specifiers in format strings   mixed-format-strings  Used when a format string contains both named (e.g. '%(foo)d')               and unnamed (e.g. '%d') conversion specifiers.  This is also               used when a named conversion specifier contains * for the               minimum field width and/or precision.t   E1302s*   Expected mapping for format string, not %ss   format-needs-mappings}   Used when a format string that uses named conversion specifiers               is used with an argument that is not a mapping.t   E1303s7   Format string dictionary key should be a string, not %ss   bad-format-string-keys   Used when a format string that uses named conversion specifiers               is used with a dictionary whose keys are not all strings.t   W1300s)   Unused key %r in format string dictionarys   unused-format-string-keys   Used when a format string that uses named conversion specifiers               is used with a dictionary that contains keys not required by the               format string.t   W1301s*   Missing key %r in format string dictionarys   missing-format-string-keys   Used when a format string that uses named conversion specifiers               is used with a dictionary that doesn't contain all the keys               required by the format string.t   E1304s$   Too many arguments for format strings   too-many-format-argssl   Used when a format string that uses unnamed conversion               specifiers is given too many arguments.t   E1305s&   Not enough arguments for format strings   too-few-format-argssj   Used when a format string that uses unnamed conversion               specifiers is given too few argumentst   E1306s!   Suspicious argument in %s.%s calls   bad-str-strip-callsG   The argument to a str.{l,r,}strip call contains a duplicate character, t   E1310s   Invalid format strings   bad-format-strings.   Used when a PEP 3101 format string is invalid.t
   minversiont   W1302s-   Missing keyword argument %r for format strings   missing-format-argument-keysh   Used when a PEP 3101 format string that uses named fields doesn't receive one or more required keywords.t   W1303s   Unused format argument %rs   unused-format-string-arguments}   Used when a PEP 3101 format string that uses named fields is used with an argument that is not required by the format string.t   W1304sT   Format string contains both automatic field numbering and manual field specifications   format-combined-specifications   Used when a PEP 3101 format string contains both automatic field numbering (e.g. '{}') and manual field specification (e.g. '{0}').t   W1305s2   Missing format attribute %r in format specifier %rs   missing-format-attributes   Used when a PEP 3101 format string uses an attribute specifier ({0.length}), but the argument passed for formatting doesn't have that attribute.t   W1306s2   Using invalid lookup key %r in format specifier %rs   invalid-format-indexs   Used when a PEP 3101 format string uses a lookup specifier ({a[1]}), but the argument passed for formatting doesn't contain or doesn't have that key as an attribute.t   W1307c         C   s   t  j |   S(   N(   t   _stringt   formatter_field_name_split(   t   format_string(    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pyt   split_format_field_names   s    c         c   sL   xE |  D]= \ } } t  | t j  r9 | t |  f Vq | | f Vq Wd  S(   N(   t
   isinstancet   numberst   Numbert   int(   t   iteratort   is_attrt   key(    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pyt   _field_iterator_convertor   s    c         C   sC   y |  j    \ } } Wn t k
 r2 t j  n X| t |  f S(   N(   t   _formatter_field_name_splitt
   ValueErrorR   t   IncompleteFormatStringR#   (   R   t   keynamet   fielditerator(    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pyR      s
    c         c   s   t  j   } y | j |   } xl | D]d } t d   | d D  rK q% n  | d } | d } | V| r% x t |  D] } | Vqw Wq% q% WWnF t k
 r } | j d j d  r d Vd Vd St j	 |    n Xd S(	   sy    Given a format string, return an iterator
    of all the valid format fields. It handles nested fields
    as well.
    c         s   s   |  ] } | d  k Vq d  S(   N(   t   None(   t   .0t   item(    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pys	   <genexpr>   s    i   i   i    s   cannot switch from manualt    t   1N(
   t   stringt	   Formattert   parset   allt   collect_string_fieldsR%   t   argst
   startswithR   R&   (   R   t	   formattert   parseiteratort   resultt   namet   nestedt   fieldt   exc(    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pyR2      s$    

c         C   s  g  } d } t    } x t |   D] } | rV t |  j   rV | j t |   q" | r t |  \ } } t | t j  r | j t |   t	 |  } n  y | j
 | t |  f  Wq t k
 r t j    q Xq" | d 7} q" W| | t |  f S(   s7  
    Parses a PEP 3101 format string, returning a tuple of
    (keys, num_args, manual_pos_arg),
    where keys is the set of mapping keys in the format string, num_args
    is the number of arguments required by the format string and
    manual_pos_arg is the number of arguments passed with the position.
    i    i   (   t   setR2   t   strt   isdigitt   addR   R   R   R   R   t   appendt   listR%   R   R&   t   len(   R   t   keyst   num_argst   manual_pos_argR8   R'   R(   (    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pyt   parse_format_method_string   s"    	c         C   s>   |  j  r d   |  j  D } n i  } t |  j  } | | f S(   s   Get the arguments from the given `Call` node.

    Return a tuple, where the first element is the
    number of positional arguments and the second element
    is the keyword arguments in a dict.
    c         S   s(   i  |  ] } t  j | j  | j  q S(    (   R   t
   safe_infert   valuet   arg(   R*   RI   (    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pys
   <dictcomp>   s   	(   t   keywordsRB   R3   (   t   callt   namedt
   positional(    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pyt   get_args   s    	c         C   si   g  } xI | D]A \ } } | r8 | j  d j |   q | j  d j |   q Wt |   d j |  S(   s^    Given a list of format specifiers, returns
    the final access path (e.g. a.b.c[0][1]).
    s   .{}s   [{!r}]R,   (   R@   t   formatR=   t   join(   R"   t   partst   patht   is_attributet	   specifier(    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pyt   get_access_path   s    t   StringFormatCheckerc           B   sk   e  Z d  Z e f Z d Z e Z e e j	     d    Z
 e e j	     d    Z d   Z d   Z RS(   s   Checks string formatting operations to ensure that the format string
    is valid and the arguments match the format string.
    R.   c         C   sH  | j  d k r d  S| j } | j } t | t j  oI t | j t j  sP d  S| j } y t	 j
 |  \ } } Wnv t	 j k
 r } | | j } |  j d d | d | t |  | j f d  St	 j k
 r |  j d d | d  SX| r| r|  j d d | n7| rrt | t j  r+t   }	 t }
 xv | j D]k \ } } t | t j  r| j } t | t j  r|	 j |  q|  j d d | d | q>t }
 q>W|
 sx9 | D]. } | |	 k r|  j d d | d | qqWn  x} |	 D]. } | | k r|  j d	 d | d | qqWqDt | t t j f  rDt |  j } |  j d
 d | d | qDn t | t j  rt	 j |  } d  } | d  t j f k rt | j  } qn. t | t t j t j f  rd } n d  } | d  k	 rD| | k r|  j d d | qD| | k  rD|  j d d | qDn  d  S(   Nt   %s   bad-format-charactert   nodeR3   s   truncated-format-strings   mixed-format-strings   bad-format-string-keys   missing-format-string-keys   unused-format-string-keys   format-needs-mappingi   s   too-many-format-argss   too-few-format-args(    t   opt   leftt   rightR   t   astroidt   ConstRH   t   sixt   string_typesR   t   parse_format_stringt   UnsupportedFormatCharactert   indext   add_messaget   ordR&   t   DictR<   t   Falset   itemsR?   t   Truet   OTHER_NODESt   Tuplet   typet   __name__RG   R)   t   UninferableRB   t   eltst   DictComp(   t   selfRX   RZ   R3   R   t   required_keyst   required_num_argst   et   cRC   t   unknown_keyst   kt   _R"   t	   type_namet	   rhs_tupleRD   (    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pyt   visit_binop   st    					

	c         C   s  t  j | j  } t | t j  rt | j t j  r| j j d k r| j d k r | j	 r t  j | j	 d  } t | t j
  s d  St | j  t t | j   k r|  j d d	 | d
 | j j | j f qq| j d k rt s t r|  j | |  qqn  d  S(   NR=   t   unicodet   bytest   stript   lstript   rstripi    s   bad-str-strip-callRX   R3   RO   (   R=   R{   R|   (   R}   R~   R   (   R   RG   t   funcR   R\   t   BoundMethodt   boundt   InstanceR8   R3   R]   RB   RH   R<   Rc   t   _PY27t   _PY3Kt   _check_new_format(   Rp   RX   R   RI   (    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pyt
   visit_callP  s    $c         C   s  t  | j t j  r2 t  | j j t j  r2 d Sy t | j j    } Wn t j	 k
 rb d SXt  | t j  sy d St  | j
 t j  s d S| j s | j r d Sy t |  \ } } Wn t j	 k
 r d SXy t | j
  \ } } } Wn( t j k
 r|  j d d | d SXt d   | D  }	 | rV| rV|  j d d | d St }
 | t d   |	 D  7} |	 rCx? |	 D]7 } | | k r| r|  j d d | d | f qqWx9 | D]1 } | |	 k r|  j d	 d | d | f qqW| p| } | s| rIt d
   |	 D  } | s4| r@t }
 q@qIn t }
 |
 r| pX| } | | k r}|  j d d | q| | k  r|  j d d | qn  |  j | | |  d S(   s"    Check the new string formatting. Ns   bad-format-stringRX   c         s   s/   |  ]% } t  | d  t j  r | d  Vq d S(   i    N(   R   R^   R_   (   R*   R:   (    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pys	   <genexpr>  s    s   format-combined-specificationc         s   s!   |  ] } | d  k r d Vq d S(   R,   i   N(    (   R*   R:   (    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pys	   <genexpr>  s    s   missing-format-argument-keyR3   s   unused-format-string-argumentc         s   s!   |  ] } | d  k r t  Vq d S(   R,   N(   Rh   (   R*   R:   (    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pys	   <genexpr>  s    s   too-many-format-argss   too-few-format-args(   R   R   R\   t	   Attributet   exprR]   t   nextR   t   infert   InferenceErrorRH   R^   R_   t   starargst   kwargsRN   RF   R   R&   Rc   R<   Rf   t   sumt   anyRh   t   _check_new_format_specifiers(   Rp   RX   R   t   strnodeRM   RL   t   fieldsRD   t
   manual_post   named_fieldst
   check_argsR:   t   empty(    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pyR   a  sj    c      	   C   s  x| D]\ } } | d k r( d } n  t  | t j  rm y t j | |  } Wq t j k
 ri q q Xn | | k r q n  | | } | t j d
 f k r q n  y t	 | j
    } Wn t j k
 r q n X| s | t j k r q n  | j rt  | j t j  rq n  | } g  }	 x| D]\ }
 } | t j k rFPn  |	 j |
 | f  |
 ry | j |  d } Wqt j k
 rt | d  r| j   rPn  t | |	  } |  j d d | | f d | PqXn t } t | d  rey | j t j |   } Wn= t j t j t j f k
 r:t } n t j k
 rNPn X| t j k rPqn/ y | j d  PWn t j k
 rt } n X| rt | |	  } |  j d	 d | | f d | Pn  y t	 | j
    } Wq't j k
 rPq'Xq'Wq Wd
 S(   sg   
        Check attribute and index access in the format
        string ("{0.a}" and "{0[a]}").
        R,   i    t   has_dynamic_getattrs   missing-format-attributeR3   RX   t   getitemt   __getitem__s   invalid-format-indexN(   R   R   R   R   t   get_argument_from_callt   NoSuchArgumentErrorR\   t   YESR)   R   R   R   Rm   t   parentt	   ArgumentsR@   t   getattrt   NotFoundErrort   hasattrR   RU   Rc   Rf   R   R]   t   AstroidIndexErrort   AstroidTypeErrort   AttributeInferenceErrorRh   (   Rp   RX   R   RL   R"   t
   specifierst   argnamet   argumentt   previoust   parsedRS   RT   RR   t
   warn_error(    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pyR     s    	

	
(   Rl   t
   __module__t   __doc__R   t   __implements__R8   t   MSGSt   msgsR   RC   Rz   R   R   R   (    (    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pyRV      s   	Y	St   StringConstantCheckerc           B   sd   e  Z d  Z e e f Z d Z i d d 6d d	 6Z d
 Z d Z	 d   Z
 d   Z d   Z d   Z RS(   s   Check string literalst   string_constantsR   Anomalous backslash in string: '%s'. String constant might be missing an r prefix.s   anomalous-backslash-in-stringsB   Used when a backslash is in a literal string but not as an escape.t   W1401sa   Anomalous Unicode escape in byte string: '%s'. String constant might be missing an r or u prefix.s"   anomalous-unicode-escape-in-stringsS   Used when an escape like \u is encountered in a byte string where it has no effect.t   W1402s   abfnrtvx
	\'"01234567t   uUNc         C   s   d | j  k |  _ d  S(   Nt   unicode_literals(   t   future_importst   _unicode_literals(   Rp   t   module(    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pyt   process_module+  s    c         C   sL   xE | D]= \ } } \ } } } } | t  j k r |  j | |  q q Wd  S(   N(   t   tokenizet   STRINGt   process_string_token(   Rp   t   tokenst   tok_typet   tokent	   start_rowRw   (    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pyt   process_tokens.  s    "c   	      C   s   x0 t  |  D]" \ } } | d k r | } Pq q W| |  j   } | | } | d  | d k op d | k n r | d d !} n | d d !} d | k r |  j | | |  n  d  S(   Ns   '"i   ii   it   r(   t	   enumeratet   lowert   process_non_raw_string_token(	   Rp   R   R   t   iRt   t
   quote_chart   prefixt   after_prefixt   string_body(    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pyR   5  s    
(c         C   s   d } x t  r | j d |  } | d k r1 Pn  | | d } | | | d !} | |  j k r d | k rn q t s} |  j r d | k r q |  j d d	 | d
 | f n. | |  j k r |  j d d	 | d
 | f n  | d 7} q	 Wd S(   s  check for bad escapes in a non-raw string.

        prefix: lowercase string of eg 'ur' string prefix markers.
        string_body: the un-parsed body of the string, not including the quote
        marks.
        start_row: integer line number in the source.
        i    s   \ii   i   t   ut   bs"   anomalous-unicode-escape-in-stringt   lineR3   s   anomalous-backslash-in-stringN(   Rh   t   findt   UNICODE_ESCAPE_CHARACTERSR   R   Rc   t   ESCAPE_CHARACTERS(   Rp   R   R   R   R   t	   next_chart   match(    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pyR   E  s$    	(   sR   Anomalous backslash in string: '%s'. String constant might be missing an r prefix.s   anomalous-backslash-in-stringsB   Used when a backslash is in a literal string but not as an escape.(   sa   Anomalous Unicode escape in byte string: '%s'. String constant might be missing an r or u prefix.s"   anomalous-unicode-escape-in-stringsS   Used when an escape like \u is encountered in a byte string where it has no effect.(   Rl   R   R   R    R   R   R8   R   R   R   R   R   R   R   (    (    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pyR     s        
			c         C   s*   |  j  t |    |  j  t |    d S(   s.   required method to auto register this checker N(   t   register_checkerRV   R   (   t   linter(    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pyt   registeru  s    ((   R   t   sysR   R.   R   R^   R\   t   pylint.interfacesR    R   R   t   pylint.checkersR   R   R   t   pylint.checkers.utilsR   t   version_infoR   R   R   R]   t   Listt   Reprt   Lambdat   FunctionDeft   ListCompt   SetCompt   GeneratorExpRi   R   R   R#   R2   RF   RN   RU   RV   R   R   (    (    (    s6   lib/python2.7/site-packages/pylint/checkers/strings.pyt   <module>   s   









		
	!			 "f