ó
ÁAÆKc           @   sÃ   d  d l  m Z m Z m Z m Z d  d l  m Z m Z m Z d  d l m	 Z	 d e
 f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d	 „  ƒ  YZ d
 e e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   Optiont   OptionParsert   OptParseErrort   OptionConflictError(   t   HelpFormattert   IndentedHelpFormattert   SUPPRESS_HELP(   t   sett   SubOptsHivec           B   sG   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z	 RS(   s4   
    Class for collecting unhandled suboptions.
    c         C   s   t  ƒ  |  _ i  |  _ d  S(   N(   R   t   optlistt   optdict(   t   self(    (    s8   /usr/lib/python2.7/dist-packages/fuseparts/subbedopts.pyt   __init__   s    c         C   sq   g  } x> |  j  j ƒ  D]- \ } } | j t | ƒ d t | ƒ ƒ q Wt |  j ƒ | p` d g } | j ƒ  | S(   Nt   =s   (none)(   R
   t	   iteritemst   appendt   strt   listR	   t   sort(   R   t   sat   kt   vt   ra(    (    s8   /usr/lib/python2.7/dist-packages/fuseparts/subbedopts.pyt	   _str_core   s    %
c         C   s   d d j  |  j ƒ  ƒ d S(   Ns   < opts: s   , s    >(   t   joinR   (   R   (    (    s8   /usr/lib/python2.7/dist-packages/fuseparts/subbedopts.pyt   __str__(   s    c         C   s…   x~ |  j  j ƒ  D]m \ } } | t k r; |  j  j | ƒ q | t k rj |  j  j | ƒ |  j j | ƒ q t | ƒ |  j  | <q Wd S(   s½   
        Transform self to an equivalent canonical form:
        delete optdict keys with False value, move optdict keys
        with True value to optlist, stringify other values.
        N(   R
   R   t   Falset   popt   TrueR	   t   addR   (   R   R   R   (    (    s8   /usr/lib/python2.7/dist-packages/fuseparts/subbedopts.pyt   canonify+   s    c         C   s˜   |  j  ƒ  | j  ƒ  |  j ƒ  } |  j j | j ƒ | _ |  j j | j ƒ xF |  j j ƒ  D]5 } | | j k r[ |  j j | ƒ d | j | <q[ q[ W| S(   s„   
        Throw away those options which are not in the other one.
        Returns a new instance with the rejected options.
        N(	   R   t	   __class__R	   t
   differencet   difference_updateR
   t   copyR   t   None(   R   t   othert   rejt   x(    (    s8   /usr/lib/python2.7/dist-packages/fuseparts/subbedopts.pyt   filter;   s    

c         C   s«   | j  d d ƒ } | d } t | ƒ d k r8 | d p; d } | re | d k r\ t d ‚ n  | } n  | t k ru d S| d t f k rš |  j j | ƒ n | |  j | <d S(   s   Add a suboption.R   i   i    s   ambiguous option valueN(	   t   splitt   lenR#   t   AttributeErrorR   R   R	   R   R
   (   R   t   optt   valt   ovt   oR   (    (    s8   /usr/lib/python2.7/dist-packages/fuseparts/subbedopts.pyR   N   s    
"	N(
   t   __name__t
   __module__t   __doc__R   R   R   R   R'   R#   R   (    (    (    s8   /usr/lib/python2.7/dist-packages/fuseparts/subbedopts.pyR      s   		
			t	   SubbedOptc           B   sÐ   e  Z d  Z e j d d d g Z e j d Z e j d Z e j d Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z g  Z x0 e j D]% Z e j  d k s˜ e j e ƒ q˜ q˜ We j e ƒ RS(   s¡   
    `Option` derivative enhanced with the attribute of being a suboption of
     some other option (like ``foo`` and ``bar`` for ``-o`` in ``-o foo,bar``).
    t   suboptt   subsept   subopts_hivet
   store_hivec         O   sq   i  |  _  d | k rZ g  |  _ g  |  _ |  j | ƒ |  j d pK |  j d |  _ d } n  t j |  | | Ž d  S(   NR3   i    (    (   t
   subopt_mapt   _short_optst
   _long_optst   _set_opt_stringst   baseoptR    R   (   R   t   optst   attrs(    (    s8   /usr/lib/python2.7/dist-packages/fuseparts/subbedopts.pyR   o   s    				c         C   sH   d } t  |  d ƒ r7 |  j r7 d |  j |  j f } n  t j |  ƒ | S(   Nt    R3   s    %s...,%s,...(   t   hasattrR3   R;   R    R   (   R   t   pf(    (    s8   /usr/lib/python2.7/dist-packages/fuseparts/subbedopts.pyR   |   s    c         C   s   | S(   N(    (   R   R<   (    (    s8   /usr/lib/python2.7/dist-packages/fuseparts/subbedopts.pyt   _check_opt_strings‚   s    c         C   si   y t  j |  ƒ WnQ t k
 rd |  j r^ d |  j |  j f |  _ |  j j d d ƒ |  _ qe ‚  n Xd  S(   Ns   __%s__%st   -R>   (   R    t   _check_destt
   IndexErrorR3   R;   t   destt   replace(   R   (    (    s8   /usr/lib/python2.7/dist-packages/fuseparts/subbedopts.pyRC   …   s    	c         C   s'   t  |  d ƒ r |  j St j |  ƒ Sd  S(   NR3   (   R?   R3   R    t   get_opt_string(   R   (    (    s8   /usr/lib/python2.7/dist-packages/fuseparts/subbedopts.pyRG      s    c         C   sA  | d k rt  | | ƒ s1 t | | ƒ d  k rt t  |  d ƒ rU |  j rU |  j } n | j ƒ  } t | | | ƒ n  x£ | j |  j p‰ d ƒ D]‰ } | j d ƒ }	 |	 d }
 d  } t |	 ƒ d k rÑ |	 d } n  |
 |  j	 k r |  j	 |
 j
 |
 | | | ƒ q t | | ƒ j |	 Œ  q Wd  St j |  | | | | | | ƒ d  S(   NR6   R5   t   ,R   i    i   (   R?   t   getattrR#   R5   t
   hive_classt   setattrR(   R4   R)   R7   t   processR   R    t   take_action(   R   t   actionRE   R+   t   valuet   valuest   parsert   hiveR.   t   oot   okR-   (    (    s8   /usr/lib/python2.7/dist-packages/fuseparts/subbedopts.pyRM   •   s"    %
 c         C   s?   | j  |  j k r+ t d | j  | ƒ ‚ n  | |  j | j  <d S(   s)   Register argument a suboption for `self`.s'   conflicting suboption handlers for `%s'N(   R3   R7   R   (   R   R.   (    (    s8   /usr/lib/python2.7/dist-packages/fuseparts/subbedopts.pyt   register_subª   s
    
RC   (   s
   store_hive(   s
   store_hive(   s
   store_hive(   R/   R0   R1   R    t   ATTRSt   ACTIONSt   STORE_ACTIONSt   TYPED_ACTIONSR   R   RA   RC   RG   RM   RU   t   CHECK_METHODSt   mR   (    (    (    s8   /usr/lib/python2.7/dist-packages/fuseparts/subbedopts.pyR2   d   s"   				
				t   SubbedOptFormatterc           B   s   e  Z d  „  Z RS(   c         C   se   t  | d ƒ rU | j rU d | j } | j ƒ  rQ | d 7} | | j pJ d 7} n  | St j |  | ƒ S(   NR3   s   -o R   t   FOO(   R?   R3   t   takes_valuet   metavarR   t   format_option_strings(   R   t   optiont   res(    (    s8   /usr/lib/python2.7/dist-packages/fuseparts/subbedopts.pyR`   ¾   s    
(   R/   R0   R`   (    (    (    s8   /usr/lib/python2.7/dist-packages/fuseparts/subbedopts.pyR\   ¼   s   t   SubbedOptIndentedFormatterc           B   s   e  Z d  „  Z RS(   c         C   s   t  j |  | ƒ S(   N(   R\   R`   (   R   Ra   (    (    s8   /usr/lib/python2.7/dist-packages/fuseparts/subbedopts.pyR`   Ì   s    (   R/   R0   R`   (    (    (    s8   /usr/lib/python2.7/dist-packages/fuseparts/subbedopts.pyRc   Ê   s   t   SubbedOptParsec           B   s    e  Z d  Z d „  Z d „  Z RS(   sY  
    This class alters / enhances `OptionParser` with *suboption handlers*.

    That is, calling `sop.add_option('-x', subopt=foo)` installs a handler
    which will be triggered if there is ``-x foo`` in the command line being
    parsed (or, eg., ``-x foo,bar``).

    Moreover, ``-x`` implicitly gets a handler which collects the unhandled
    suboptions of ``-x`` into a `SubOptsHive` instance (accessible post festam
    via the `x` attribute of the returned Values object). (The only exception
    is when ``-x`` has *explicitly*  been added with action ``store_hive``.
    This opens up the possibility of customizing the ``-x`` handler at some
    rate.)

    Suboption handlers have all the nice features of normal option handlers,
    eg. they are displayed in the automatically generated help message
    (and can have their own help info).
    c         O   sv   d | k r t  ƒ  | d <n  d | k r5 t | d <n  d | k rV | j d ƒ |  _ n	 t |  _ t j |  | | Ž d  S(   Nt	   formattert   option_classRJ   (   Rc   R2   R   RJ   R   R   R   (   R   t   argst   kw(    (    s8   /usr/lib/python2.7/dist-packages/fuseparts/subbedopts.pyR   å   s    	c         O   s  d | k rS | d d k rS d | k r7 t  d ƒ ‚ n  d | k rú d | d <qú n§ d | k rú |  j | | Ž  } |  j | j ƒ } | r­ | j d k rÛ t d | ƒ ‚ qÛ n. |  j | j d d d d	 ƒ|  j | j ƒ } | j | ƒ | f } i  } n  t j |  | | Ž S(
   NRN   R6   R3   sL   option can't have a `subopt' attr and `action="store_hive"' at the same timet   typet   stringsM   can't add subopt as option has already a handler that doesn't do `store_hive'R_   s   sub1,[sub2,...](	   R   Rf   t
   get_optionR;   RN   R   t
   add_optionRU   R   (   R   Rg   t   kwargsR.   RS   (    (    s8   /usr/lib/python2.7/dist-packages/fuseparts/subbedopts.pyRl   ò   s*    		(   R/   R0   R1   R   Rl   (    (    (    s8   /usr/lib/python2.7/dist-packages/fuseparts/subbedopts.pyRd   Ñ   s   	N(   t   optparseR    R   R   R   R   R   R   t   fuseparts.setcompatwrapR   t   objectR   R2   R\   Rc   Rd   (    (    (    s8   /usr/lib/python2.7/dist-packages/fuseparts/subbedopts.pyt   <module>   s   "
PX