
^Yc           @   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 Z d d l	 Z	 e j
 d  Z d   Z e j d  Z e j d  Z d   Z d d  Z d	   Z d
   Z d   Z d   Z d d  Z e d  Z d   Z d   Z d S(   s   Utility methods for flake8.iNs*   ^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@.*$c         C   sN   |  s
 g  St  |  t t f  s1 |  j d  }  n  g  |  D] } | j   ^ q8 S(   s   Parse a comma-separated list.

    :param value:
        String or list of strings to be parsed and normalized.
    :returns:
        List of values with whitespace stripped.
    :rtype:
        list
    t   ,(   t
   isinstancet   listt   tuplet   splitt   strip(   t   valuet   item(    (    s,   /tmp/pip-build-EndXZ2/flake8/flake8/utils.pyt   parse_comma_separated_list   s
    c         C   s&   g  t  |   D] } t | |  ^ q S(   sr   Parse a comma-separated list of paths.

    :returns:
        The normalized paths.
    :rtype:
        [str]
    (   R   t   normalize_path(   t   pathst   parentt   p(    (    s,   /tmp/pip-build-EndXZ2/flake8/flake8/utils.pyt   normalize_paths"   s    	c         C   sq   t  j j } t  j j p d } | |  k s< | r` | |  k r` t  j j t  j j | |    }  n  |  j | |  S(   sa   Normalize a single-path.

    :returns:
        The normalized path.
    :rtype:
        str
    t    (   t   ost   patht   sept   altsept   abspatht   joint   rstrip(   R   R   t	   separatort   alternate_separator(    (    s,   /tmp/pip-build-EndXZ2/flake8/flake8/utils.pyR	   /   s    $c          C   sv   t  t d d  }  |  d k rl t j j   } t j d k  rH t j } n	 t j	 } | |  t _
 t j
 }  n  |  j   S(   s'   Get and cache it so plugins can use it.t   cached_stdini   i    N(   i   i    (   t   getattrt   stdin_get_valuet   Nonet   syst   stdint   readt   version_infot   iot   BytesIOt   StringIOR   t   getvalue(   t   cached_valuet   stdin_valuet   cached_type(    (    s,   /tmp/pip-build-EndXZ2/flake8/flake8/utils.pyR   D   s    	c         C   s1  |  d
 k r t   }  n  d
 } d
 } t j t  } x |  j   D] } | ro | d  d k r@ | d 8} q@ q@ n  | d  d k r | d j d d  d } | d  d	 k r@ | d } q@ q@ n  t j |  } | r@ g  | j	   D] } | s d n	 t
 |  ^ q \ } } | | j t | | |   q@ q@ W| S(   s   Parse the unified diff passed on stdin.

    :returns:
        dictionary mapping file names to sets of line numbers
    :rtype:
        dict
    i   t   -i   s   +++i   s   	i    i   s   b/N(   R   R   t   collectionst   defaultdictt   sett
   splitlinesR   t   DIFF_HUNK_REGEXPt   matcht   groupst   intt   updatet   range(   t   difft   number_of_rowst   current_patht   parsed_pathst   linet
   hunk_matcht   groupt   row(    (    s,   /tmp/pip-build-EndXZ2/flake8/flake8/utils.pyt   parse_unified_diffS   s,    
4
c           C   s   t  j d k S(   s   Determine if we're running on Windows.

    :returns:
        True if running on Windows, otherwise False
    :rtype:
        bool
    t   nt(   R   t   name(    (    (    s,   /tmp/pip-build-EndXZ2/flake8/flake8/utils.pyt
   is_windows   s    	c          C   s>   d t  j k o d k  n }  t  j d k } t o= |  p= | S(	   s  Determine if we can use multiprocessing on Windows.

    This presently will **always** return False due to a `bug`_ in the
    :mod:`multiprocessing` module on Windows. Once fixed, we will check
    to ensure that the version of Python contains that fix (via version
    inspection) and *conditionally* re-enable support on Windows.

    .. _bug:
        https://bugs.python.org/issue27649

    :returns:
        True if the version of Python is modern enough, otherwise False
    :rtype:
        bool
    i   i   i   i   i    (   i   i   i   (   i   i    (   i   i   (   R   R   t   False(   t   is_new_enough_python27t   is_new_enough_python3(    (    s,   /tmp/pip-build-EndXZ2/flake8/flake8/utils.pyt"   can_run_multiprocessing_on_windows   s    c         C   s
   d |  k S(   s   Determine if we're going to read from stdin.

    :param list paths:
        The paths that we're going to check.
    :returns:
        True if stdin (-) is in the path, otherwise False
    :rtype:
        bool
    R'   (    (   R
   (    (    s,   /tmp/pip-build-EndXZ2/flake8/flake8/utils.pyt   is_using_stdin   s    c          G   s   t  S(   N(   R>   (   t   args(    (    s,   /tmp/pip-build-EndXZ2/flake8/flake8/utils.pyt   _default_predicate   s    c         c   s  | d k r t } n  | |   r% d St j j |   rx t j |   D] \ } } } | |  ro g  | (qG n  xK | D]C } t j j | |  } | |  s | |  rv | j |  qv qv WxF | D]> } t j j | |  } | |  s | |  r q n  | Vq WqG Wn |  Vd S(   s  Generate filenames from an argument.

    :param str arg:
        Parameter from the command-line.
    :param callable predicate:
        Predicate to use to filter out filenames. If the predicate
        returns ``True`` we will exclude the filename, otherwise we
        will yield it. By default, we include every filename
        generated.
    :returns:
        Generator of paths
    N(   R   RD   R   R   t   isdirt   walkR   t   remove(   t   argt	   predicatet   roott   sub_directoriest   filest	   directoryt   joinedt   filename(    (    s,   /tmp/pip-build-EndXZ2/flake8/flake8/utils.pyt   filenames_from   s&    	c            s$   | s
 | St    f d   | D  S(   s  Wrap :func:`fnmatch.fnmatch` to add some functionality.

    :param str filename:
        Name of the file we're trying to match.
    :param list patterns:
        Patterns we're using to try to match the filename.
    :param bool default:
        The default value if patterns is empty
    :returns:
        True if a pattern matches the filename, False if it doesn't.
        ``default`` if patterns is empty.
    c         3   s!   |  ] } t  j   |  Vq d  S(   N(   t   _fnmatcht   fnmatch(   t   .0t   pattern(   RO   (    s,   /tmp/pip-build-EndXZ2/flake8/flake8/utils.pys	   <genexpr>  s    (   t   any(   RO   t   patternst   default(    (   RO   s,   /tmp/pip-build-EndXZ2/flake8/flake8/utils.pyRR      s    c   
      C   s/  |  j  } t j |  } | r. |  j  j } n  t j d k  r t j |  } t | d  t | d pi g   } | d } t j	 g  t
 |  D] \ } } | | | k  f ^ q  } n[ t j	 g  t j |  j j   D]3 }	 |	 j |	 j k r |	 j |	 j |	 j k f ^ q  } | r+| j d d  n  | S(   s  Return the parameters for the plugin.

    This will inspect the plugin and return either the function parameters
    if the plugin is a function or the parameters for ``__init__`` after
    ``self`` if the plugin is a class.

    :param plugin:
        The internal plugin object.
    :type plugin:
        flake8.plugins.manager.Plugin
    :returns:
        A dictionary mapping the parameter name to whether or not it is
        required (a.k.a., is positional only/does not have a default).
    :rtype:
        dict([(str, bool)])
    i   i    it   self(   i   i   N(   t   plugint   inspectt
   isfunctiont   __init__R   R   t
   getargspect   lenR(   t   OrderedDictt	   enumeratet	   signaturet
   parameterst   valuest   kindt   POSITIONAL_OR_KEYWORDR<   RW   t   emptyt   popR   (
   RY   t   funct   is_classt   argspect   start_of_optional_argst   parameter_namest   positionR<   Rb   t	   parameter(    (    s,   /tmp/pip-build-EndXZ2/flake8/flake8/utils.pyt   parameters_for  s$    	$
46c          C   sK   y t  j   d }  Wn t k
 r- d }  n Xd |  t  j   t  j   f S(   s   Find and format the python implementation and version.

    :returns:
        Implementation name, version, and platform as a string.
    :rtype:
        str
    t    R   s
   %s%s on %s(   t   platformt   python_implementationt   AttributeErrort   python_versiont   system(   t   impl(    (    s,   /tmp/pip-build-EndXZ2/flake8/flake8/utils.pyt   get_python_version:  s
    	
(   t   __doc__R(   RR   RQ   RZ   R    R   Rq   t   reR   t   compileR,   R   t   curdirR   R	   R   R   R:   R=   RA   RB   RD   RP   t   TrueRo   Rw   (    (    (    s,   /tmp/pip-build-EndXZ2/flake8/flake8/utils.pyt   <module>   s,   		H				*	,