ó
ñsºec           @   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	"	;	