ó
„¾^Yc           @   sm   d  Z  d d l Z d d l Z d d l m Z e j e ƒ Z d e f d „  ƒ  YZ	 d e f d „  ƒ  YZ
 d S(   s,   Option handling and Option management logic.iÿÿÿÿN(   t   utilst   Optionc           B   sw   e  Z d  Z d d d d d d d d d d d d d d e e e d „ Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(   s=   Our wrapper around an optparse.Option object to add features.c         C   s>  | |  _  | |  _ g  | | f D] } | d k	 r | ^ q |  _ i | d 6| d 6| d 6|  j | ƒ d 6| d 6| d 6|	 d 6|
 d 6| d	 6| d
 6| d 6| d 6|  _ x- |  j j ƒ  D] \ } } t |  | | ƒ q¶ W| |  _ | |  _	 | |  _
 d |  _ | r1| st d ƒ ‚ n  | d j d d ƒ |  _ n  d |  _ d S(   s4  Initialize an Option instance wrapping optparse.Option.

        The following are all passed directly through to optparse.

        :param str short_option_name:
            The short name of the option (e.g., ``-x``). This will be the
            first argument passed to :class:`~optparse.Option`.
        :param str long_option_name:
            The long name of the option (e.g., ``--xtra-long-option``). This
            will be the second argument passed to :class:`~optparse.Option`.
        :param str action:
            Any action allowed by :mod:`optparse`.
        :param default:
            Default value of the option.
        :param type:
            Any type allowed by :mod:`optparse`.
        :param dest:
            Attribute name to store parsed option value as.
        :param nargs:
            Number of arguments to parse for this option.
        :param const:
            Constant value to store on a common destination. Usually used in
            conjuntion with ``action="store_const"``.
        :param iterable choices:
            Possible values for the option.
        :param callable callback:
            Callback used if the action is ``"callback"``.
        :param iterable callback_args:
            Additional positional arguments to the callback callable.
        :param dictionary callback_kwargs:
            Keyword arguments to the callback callable.
        :param str help:
            Help text displayed in the usage information.
        :param str metavar:
            Name to use instead of the long option name for help text.

        The following parameters are for Flake8's option handling alone.

        :param bool parse_from_config:
            Whether or not this option should be parsed out of config files.
        :param bool comma_separated_list:
            Whether the option is a comma separated list when parsing from a
            config file.
        :param bool normalize_paths:
            Whether the option is expecting a path or list of paths and should
            attempt to normalize the paths to absolute paths.
        t   actiont   defaultt   typet   destt   nargst   constt   choicest   callbackt   callback_argst   callback_kwargst   helpt   metavarsR   When specifying parse_from_config=True, a long_option_name must also be specified.i   t   -t   _N(   t   short_option_namet   long_option_namet   Nonet   option_argst
   _make_destt   option_kwargst   itemst   setattrt   parse_from_configt   comma_separated_listt   normalize_pathst   config_namet
   ValueErrort   replacet   _opt(   t   selfR   R   R   R   R   R   R   R   R   R	   R
   R   R   R   R   R   R   t   xt   keyt   value(    (    s6   /tmp/pip-build-EndXZ2/flake8/flake8/options/manager.pyt   __init__   s6    8		.				c         C   s   d j  |  j |  j |  j  S(   s)   Simple representation of an Option class.sÚ   Option({0}, {1}, action={action}, default={default}, dest={dest}, type={type}, callback={callback}, help={help}, callback={callback}, callback_args={callback_args}, callback_kwargs={callback_kwargs}, metavar={metavar})(   t   formatR   R   R   (   R   (    (    s6   /tmp/pip-build-EndXZ2/flake8/flake8/options/manager.pyt   __repr__j   s    c         C   s5   | r
 | S|  j  r* |  j  d j d d ƒ S|  j d S(   Ni   R   R   i   (   R   R   R   (   R   R   (    (    s6   /tmp/pip-build-EndXZ2/flake8/flake8/options/manager.pyR   t   s
    	c         G   sN   |  j  r4 t j } |  j r' t j  } n  | | | Œ S|  j rJ t j | ƒ S| S(   s6   Normalize the value based on the option configuration.(   R   R    t   normalize_pathR   t   parse_comma_separated_list(   R   R"   t   normalize_argst	   normalize(    (    s6   /tmp/pip-build-EndXZ2/flake8/flake8/options/manager.pyR)   |   s    				c         C   s   |  j  | ƒ } |  j d k s- |  j d k r7 t | ƒ S|  j d k r{ t | ƒ j ƒ  } | d k rh t S| d k r{ t Sn  | S(   s-   Normalize the value received from setuptools.t   intt   countt
   store_truet   store_falset   1t   Tt   TRUEt   ONt   0t   Ft   FALSEt   OFF(   s
   store_trues   store_false(   R.   R/   s   TRUER1   (   R2   R3   s   FALSER5   (   R)   R   R   R*   t   strt   uppert   Truet   False(   R   R"   (    (    s6   /tmp/pip-build-EndXZ2/flake8/flake8/options/manager.pyt   normalize_from_setuptoolsˆ   s    
c         C   s4   |  j  d k r- t j |  j |  j Ž  |  _  n  |  j  S(   s.   Convert a Flake8 Option to an optparse Option.N(   R   R   t   optparseR   R   R   (   R   (    (    s6   /tmp/pip-build-EndXZ2/flake8/flake8/options/manager.pyt   to_optparse•   s    N(   t   __name__t
   __module__t   __doc__R   R9   R#   R%   R   R)   R:   R<   (    (    (    s6   /tmp/pip-build-EndXZ2/flake8/flake8/options/manager.pyR   
   s   	U	
			t   OptionManagerc           B   s¤   e  Z d  Z d d d d „ Z e d „  ƒ Z d „  Z d „  Z d „  Z	 d „  Z
 d d	 d
 „ Z d „  Z d „  Z d „  Z d d d „ Z d d d „ Z d „  Z RS(   s=   Manage Options and OptionParser while adding post-processing.s   %prog [options] file file ...c         C   sm   t  j d | d | d | ƒ |  _ i  |  _ g  |  _ | |  _ | |  _ t ƒ  |  _ t ƒ  |  _	 t ƒ  |  _
 d S(   s   Initialize an instance of an OptionManager.

        :param str prog:
            Name of the actual program (e.g., flake8).
        :param str version:
            Version string for the program.
        :param str usage:
            Basic usage string used by the OptionParser.
        t   progt   versiont   usageN(   R;   t   OptionParsert   parsert   config_options_dictt   optionst   program_nameRB   t   sett   registered_pluginst   extended_default_ignoret   extended_default_select(   R   RA   RB   RC   (    (    s6   /tmp/pip-build-EndXZ2/flake8/flake8/options/manager.pyR#       s    				c         C   s   t  t d d g |  ƒ ƒ S(   s?   Convert a plugin tuple into a dictionary mapping name to value.t   nameRB   (   t   dictt   zip(   t   plugin_tuple(    (    s6   /tmp/pip-build-EndXZ2/flake8/flake8/options/manager.pyt   format_pluginµ   s    c         O   s¼   t  | ƒ d k r8 | d j d ƒ r8 d | d f } n  t | | Ž  } |  j j | j ƒ  ƒ |  j j | ƒ | j	 r¨ | j
 } | |  j | <| |  j | j d d ƒ <n  t j d | ƒ d S(   s<  Create and register a new option.

        See parameters for :class:`~flake8.options.manager.Option` for
        acceptable arguments to this method.

        .. note::

            ``short_option_name`` and ``long_option_name`` may be specified
            positionally as they are with optparse normally.
        i   i    s   --R   R   s   Registered option "%s".N(   t   lent
   startswithR   R   RE   t
   add_optionR<   RG   t   appendR   R   RF   R   t   LOGt   debug(   R   t   argst   kwargst   optionRM   (    (    s6   /tmp/pip-build-EndXZ2/flake8/flake8/options/manager.pyRT   º   s    %		c         C   sc   t  j d | ƒ xL | D]D } y |  j j | ƒ Wq t t f k
 rZ t  j d | ƒ q Xq Wd S(   sê   Remove specified error codes from the default ignore list.

        :param list error_codes:
            List of strings that are the error/warning codes to attempt to
            remove from the extended default ignore list.
        s(   Removing %r from the default ignore listsO   Attempted to remove %s from default ignore but it was not a member of the list.N(   RV   RW   RK   t   removeR   t   KeyError(   R   t   error_codest
   error_code(    (    s6   /tmp/pip-build-EndXZ2/flake8/flake8/options/manager.pyt   remove_from_default_ignoreÐ   s    	c         C   s$   t  j d | ƒ |  j j | ƒ d S(   sß   Extend the default ignore list with the error codes provided.

        :param list error_codes:
            List of strings that are the error/warning codes with which to
            extend the default ignore list.
        s%   Extending default ignore list with %rN(   RV   RW   RK   t   update(   R   R]   (    (    s6   /tmp/pip-build-EndXZ2/flake8/flake8/options/manager.pyt   extend_default_ignoreß   s    c         C   s$   t  j d | ƒ |  j j | ƒ d S(   sß   Extend the default select list with the error codes provided.

        :param list error_codes:
            List of strings that are the error/warning codes with which
            to extend the default select list.
        s%   Extending default select list with %rN(   RV   RW   RL   R`   (   R   R]   (    (    s6   /tmp/pip-build-EndXZ2/flake8/flake8/options/manager.pyt   extend_default_selecté   s    s   %(name)s: %(version)ss   , c            s)   | j  ‡  ‡ f d †  t ˆ j ƒ Dƒ ƒ S(   s7   Generate a comma-separated list of versions of plugins.c         3   s"   |  ] } ˆ  ˆ j  | ƒ Vq d  S(   N(   RQ   (   t   .0t   plugin(   t
   format_strR   (    s6   /tmp/pip-build-EndXZ2/flake8/flake8/options/manager.pys	   <genexpr>÷   s   (   t   joint   sortedRJ   (   R   Re   t   join_on(    (   Re   R   s6   /tmp/pip-build-EndXZ2/flake8/flake8/options/manager.pyt   generate_versionsó   s    c         C   s/   |  j  d |  j ƒ  d t j ƒ  |  j _  d S(   s!   Update the flake8 version string.s    (s   ) N(   RB   Ri   R    t   get_python_versionRE   (   R   (    (    s6   /tmp/pip-build-EndXZ2/flake8/flake8/options/manager.pyt   update_version_stringû   s    c         C   s#   d } d |  j  | ƒ |  j _ d S(   s=   Create an epilog with the version and name of each of plugin.s   %(name)s: %(version)ss   Installed plugins: N(   Ri   RE   t   epilog(   R   t   plugin_version_format(    (    s6   /tmp/pip-build-EndXZ2/flake8/flake8/options/manager.pyt   generate_epilog  s    	c         C   sF   x? |  j  D]4 } t | | j ƒ } t | | j | j | ƒ ƒ q
 Wd  S(   N(   RG   t   getattrR   R   R)   (   R   RG   RZ   t	   old_value(    (    s6   /tmp/pip-build-EndXZ2/flake8/flake8/options/manager.pyt
   _normalize	  s    c         C   sF   |  j  ƒ  |  j ƒ  |  j j | | ƒ \ } } |  j | ƒ | | f S(   s=   Simple proxy to calling the OptionParser's parse_args method.(   Rn   Rk   RE   t
   parse_argsRq   (   R   RX   t   valuesRG   t   xargs(    (    s6   /tmp/pip-build-EndXZ2/flake8/flake8/options/manager.pyRr     s
    

c         C   s  |  j  ƒ  |  j ƒ  |  j j | ƒ } | d k rD |  j j ƒ  } n  | |  j _ g  |  j _ } | |  j _ x\ | rÊ y |  j j	 | | | ƒ Wqo t
 j t
 j f k
 rÆ } |  j j j | j ƒ qo Xqo W| | } |  j j | | ƒ \ } } |  j | ƒ | | f S(   s—   Parse only the known arguments from the argument values.

        Replicate a little argparse behaviour while we're still on
        optparse.
        N(   Rn   Rk   RE   t	   _get_argsR   t   get_default_valuest   rargst   largsRs   t   _process_argsR;   t   BadOptionErrort   OptionValueErrorRU   t   opt_strt   check_valuesRq   (   R   RX   Rs   Rw   Rx   t   errRG   Rt   (    (    s6   /tmp/pip-build-EndXZ2/flake8/flake8/options/manager.pyt   parse_known_args  s"    

	
c         C   s   |  j  j | | f ƒ d S(   s9  Register a plugin relying on the OptionManager.

        :param str name:
            The name of the checker itself. This will be the ``name``
            attribute of the class or function loaded from the entry-point.
        :param str version:
            The version of the checker that we're using.
        N(   RJ   t   add(   R   RM   RB   (    (    s6   /tmp/pip-build-EndXZ2/flake8/flake8/options/manager.pyt   register_plugin6  s    	N(   R=   R>   R?   R   R#   t   staticmethodRQ   RT   R_   Ra   Rb   Ri   Rk   Rn   Rq   Rr   R   R   (    (    (    s6   /tmp/pip-build-EndXZ2/flake8/flake8/options/manager.pyR@      s    			
	
			 (   R?   t   loggingR;   t   flake8R    t	   getLoggerR=   RV   t   objectR   R@   (    (    (    s6   /tmp/pip-build-EndXZ2/flake8/flake8/options/manager.pyt   <module>   s   “