ó
č¾bc           @   sż   d  Z  d Z d d l Z d d l Z d d l Z d d l Z d d l m Z m Z d Z	 e j
 d e	  Z e j
 d e	 e	 f  Z e j d d	  Z d
 f  d     YZ d   Z e j e j d e e j   Z d   Z d   Z d f  d     YZ d S(   s6  distutils.fancy_getopt

Wrapper around the standard getopt module that provides the following
additional features:
  * short and long options are tied together
  * options have help strings, so fancy_getopt could potentially
    create a complete usage summary
  * options set attributes of a passed-in object
s   $Id$i’’’’N(   t   DistutilsGetoptErrort   DistutilsArgErrors   [a-zA-Z](?:[a-zA-Z0-9-]*)s   ^%s$s   ^(%s)=!(%s)$t   -t   _t   FancyGetoptc           B   s¤   e  Z d  Z d d  Z d   Z d   Z d d d  Z d   Z d   Z	 d   Z
 d   Z d	   Z d
   Z d d d  Z d   Z d d  Z d d d  Z RS(   sÕ  Wrapper around the standard 'getopt()' module that provides some
    handy extra functionality:
      * short and long options are tied together
      * options have help strings, and help text can be assembled
        from them
      * options set attributes of a passed-in object
      * boolean options can have "negative aliases" -- eg. if
        --quiet is the "negative alias" of --verbose, then "--quiet"
        on the command line sets 'verbose' to false
    c         C   st   | |  _  i  |  _ |  j  r( |  j   n  i  |  _ i  |  _ g  |  _ g  |  _ i  |  _ i  |  _ i  |  _	 g  |  _
 d  S(   N(   t   option_tablet   option_indext   _build_indext   aliast   negative_aliast
   short_optst	   long_optst
   short2longt	   attr_namet	   takes_argt   option_order(   t   selfR   (    (    s,   /usr/lib/python2.7/distutils/fancy_getopt.pyt   __init__-   s    
										c         C   s6   |  j  j   x" |  j D] } | |  j  | d <q Wd  S(   Ni    (   R   t   clearR   (   R   t   option(    (    s,   /usr/lib/python2.7/distutils/fancy_getopt.pyR   Y   s    c         C   s   | |  _  |  j   d  S(   N(   R   R   (   R   R   (    (    s,   /usr/lib/python2.7/distutils/fancy_getopt.pyt   set_option_table^   s    	c         C   sO   | |  j  k r t d |  n, | | | f } |  j j |  | |  j  | <d  S(   Ns'   option conflict: already an option '%s'(   R   R    R   t   append(   R   t   long_optiont   short_optiont   help_stringR   (    (    s,   /usr/lib/python2.7/distutils/fancy_getopt.pyt
   add_optionb   s    c         C   s   | |  j  k S(   sc   Return true if the option table for this parser has an
        option with long name 'long_option'.(   R   (   R   R   (    (    s,   /usr/lib/python2.7/distutils/fancy_getopt.pyt
   has_optionl   s    c         C   s   t  j | t  S(   s   Translate long option name 'long_option' to the form it
        has as an attribute of some object: ie., translate hyphens
        to underscores.(   t   stringt	   translatet   longopt_xlate(   R   R   (    (    s,   /usr/lib/python2.7/distutils/fancy_getopt.pyt   get_attr_nameq   s    c         C   s   t  | t  s t  xj | j   D]\ \ } } | |  j k rV t d | | | f  n  | |  j k r" t d | | | f  q" q" Wd  S(   Ns(   invalid %s '%s': option '%s' not defineds0   invalid %s '%s': aliased option '%s' not defined(   t
   isinstancet   dictt   AssertionErrort   itemsR   R    (   R   t   aliasest   whatR   t   opt(    (    s,   /usr/lib/python2.7/distutils/fancy_getopt.pyt   _check_alias_dictx   s    c         C   s   |  j  | d  | |  _ d S(   s'   Set the aliases for this option parser.R   N(   R&   R   (   R   R   (    (    s,   /usr/lib/python2.7/distutils/fancy_getopt.pyt   set_aliases   s    c         C   s   |  j  | d  | |  _ d S(   sŚ   Set the negative aliases for this option parser.
        'negative_alias' should be a dictionary mapping option names to
        option names, both the key and value must already be defined
        in the option table.s   negative aliasN(   R&   R	   (   R   R	   (    (    s,   /usr/lib/python2.7/distutils/fancy_getopt.pyt   set_negative_aliases   s    c         C   s  g  |  _  g  |  _ |  j j   i  |  _ x]|  j D]R} t |  d k rb | \ } } } d } n7 t |  d k r | \ } } } } n t d | f  t | t	  s» t |  d k  rĖ t
 d |  n  | d k põ t | t	  oõ t |  d k st
 d |  n  | |  j | <|  j  j |  | d	 d
 k re| rH| d } n  | d d	 !} d |  j | <nk |  j j |  } | d k	 rĆ|  j | r¦t
 d | | f  n  | |  j  d	 <d |  j | <n d |  j | <|  j j |  } | d k	 r!|  j | |  j | k r!t
 d | | f  q!n  t j |  sDt
 d d |  n  |  j |  |  j | <| r2 |  j j |  | |  j | d <q2 q2 Wd S(   s   Populate the various data structures that keep tabs on the
        option table.  Called by 'getopt()' before it can do anything
        worthwhile.
        i   i    i   s   invalid option tuple: %ri   s9   invalid long option '%s': must be a string of length >= 2i   s:   invalid short option '%s': must a single character or Nonei’’’’t   =t   :s>   invalid negative alias '%s': aliased option '%s' takes a valuesg   invalid alias '%s': inconsistent with aliased option '%s' (one of them takes a value, the other doesn'ts   invalid long option name '%s' s'   (must be letters, numbers, hyphens onlyN(   R   R
   R   R   t   repeatR   t   lent
   ValueErrorR   t   strR    t   NoneR   R   R	   t   getR   t
   longopt_ret   matchR   R   (   R   R   t   longt   shortt   helpR+   t   alias_to(    (    s,   /usr/lib/python2.7/distutils/fancy_getopt.pyt   _grok_option_table   sd    				"
!
 c         C   s  | d k r t j d } n  | d k r: t   } d } n d } |  j   t j |  j  } y" t j | | |  j	  \ } } Wn t j
 k
 r } t |  n Xx\| D]T\ } } t |  d k ré | d d k ré |  j | d } n2 t |  d k r| d  d k st  | d } |  j j |  }	 |	 r<|	 } n  |  j | s| d k sat d   |  j j |  }	 |	 r|	 } d } qd } n  |  j | }
 | rÕ|  j j |
  d k	 rÕt | |
 d  d } n  t | |
 |  |  j j | | f  q§ W| r| | f S| Sd S(	   s  Parse command-line options in args. Store as attributes on object.

        If 'args' is None or not supplied, uses 'sys.argv[1:]'.  If
        'object' is None or not supplied, creates a new OptionDummy
        object, stores option values there, and returns a tuple (args,
        object).  If 'object' is supplied, it is modified in place and
        'getopt()' just returns 'args'; in both cases, the returned
        'args' is a modified copy of the passed-in 'args' list, which
        is left untouched.
        i   i    i   R   s   --t    s   boolean option can't have valueN(   R/   t   syst   argvt   OptionDummyR7   R   t   joinR
   t   getoptR   t   errorR   R,   R   R!   R   R0   R   R	   R   R+   t   getattrt   setattrR   R   (   R   t   argst   objectt   created_objectR
   t   optst   msgR%   t   valR   t   attr(    (    s,   /usr/lib/python2.7/distutils/fancy_getopt.pyR=   ź   sF    		
""(
			
c         C   s&   |  j  d k r t d  n |  j  Sd S(   s«   Returns the list of (option, value) tuples processed by the
        previous run of 'getopt()'.  Raises RuntimeError if
        'getopt()' hasn't been called yet.
        s!   'getopt()' hasn't been called yetN(   R   R/   t   RuntimeError(   R   (    (    s,   /usr/lib/python2.7/distutils/fancy_getopt.pyt   get_option_order*  s    c         C   sį  d } x| |  j  D]q } | d } | d } t |  } | d d k rS | d } n  | d k	 rl | d } n  | | k r | } q q W| d d d } d } | | }	 d | }
 | rĆ | g } n	 d	 g } x|  j  D]} | d
  \ } } } t | |	  } | d d k r| d d !} n  | d k rk| rQ| j d | | | d f  q³| j d | | f  nH d | | f } | r¢| j d | | | d f  n | j d |  x# | d D] } | j |
 |  q¾WqÖ W| S(   s   Generate help text (a list of strings, one per suggested line of
        output) from the option table for this FancyGetopt object.
        i    i   i’’’’R)   i   i   iN   t    s   Option summary:i   s     --%-*s  %ss
     --%-*s  s   %s (-%s)s     --%-*sN(   R   R,   R/   t	   wrap_textR   (   R   t   headert   max_optR   R3   R4   t   lt	   opt_widtht
   line_widtht
   text_widtht
   big_indentt   linesR5   t   textt	   opt_names(    (    s,   /usr/lib/python2.7/distutils/fancy_getopt.pyt   generate_help5  sF    



	!	c         C   sG   | d  k r t j } n  x( |  j |  D] } | j | d  q( Wd  S(   Ns   
(   R/   R9   t   stdoutRV   t   write(   R   RL   t   filet   line(    (    s,   /usr/lib/python2.7/distutils/fancy_getopt.pyt
   print_help  s    N(   t   __name__t
   __module__t   __doc__R/   R   R   R   R   R   R   R&   R'   R(   R7   R=   RI   RV   R[   (    (    (    s,   /usr/lib/python2.7/distutils/fancy_getopt.pyR   !   s   
,		
							X@	Tc         C   s)   t  |   } | j |  | j | |  S(   N(   R   R(   R=   (   t   optionst   negative_optRB   RA   t   parser(    (    s,   /usr/lib/python2.7/distutils/fancy_getopt.pyt   fancy_getopt  s    RJ   c         C   s~  |  d k r g  St |   | k r) |  g St j |   }  t j |  t  }  t j d |   } t d |  } g  } x| ryg  } d } xt | rü t | d  } | | | k rŌ | j	 | d  | d =| | } q | rų | d d d k rų | d =n  Pq W| r]| d k r<| j	 | d d | ! | d | | d <n  | d d d k r]| d =q]n  | j	 t j
 | d   qt W| S(   s¶   wrap_text(text : string, width : int) -> [string]

    Split 'text' into multiple lines of no more than 'width' characters
    each, and return the list of strings that results.
    s   ( +|-+)i    i’’’’RJ   R8   N(   R/   R,   R   t
   expandtabsR   t   WS_TRANSt   ret   splitt   filterR   R<   (   RT   t   widtht   chunksRS   t   cur_linet   cur_lenRN   (    (    s,   /usr/lib/python2.7/distutils/fancy_getopt.pyRK     s:    		
c         C   s   t  j |  t  S(   sX   Convert a long option name to a valid Python identifier by
    changing "-" to "_".
    (   R   R   R   (   R%   (    (    s,   /usr/lib/python2.7/distutils/fancy_getopt.pyt   translate_longoptÕ  s    R;   c           B   s   e  Z d  Z g  d  Z RS(   s_   Dummy class just used as a place to hold command-line option
    values as instance attributes.c         C   s%   x | D] } t  |  | d  q Wd S(   sk   Create a new OptionDummy instance.  The attributes listed in
        'options' will be initialized to None.N(   R@   R/   (   R   R_   R%   (    (    s,   /usr/lib/python2.7/distutils/fancy_getopt.pyR   ą  s    (   R\   R]   R^   R   (    (    (    s,   /usr/lib/python2.7/distutils/fancy_getopt.pyR;   Ü  s   (   R^   t   __revision__R9   R   Re   R=   t   distutils.errorsR    R   t   longopt_patt   compileR1   t   neg_alias_ret	   maketransR   R   Rb   t
   whitespaceR,   Rd   RK   Rl   R;   (    (    (    s,   /usr/lib/python2.7/distutils/fancy_getopt.pyt   <module>	   s"   ’ r	"	;	