ó
!Ž}Mc           @   sÌ   d  Z  d d l Z d d l Z d d l Z d d l m Z d d l m Z m Z m Z d e	 f d „  ƒ  YZ
 e
 Z d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d d „ Z e d „ Z d „  Z d e _ d S(   s  
twisted.python.usage is a module for parsing/handling the
command line of your program.

For information on how to use it, see
U{http://twistedmatrix.com/projects/core/documentation/howto/options.html},
or doc/core/howto/options.xhtml in your Twisted directory.
iÿÿÿÿN(   t   path(   t   reflectt   textt   utilt
   UsageErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyR      s   t   CoerceParameterc           B   s    e  Z d  Z d „  Z d „  Z RS(   sE   
    Utility class that can corce a parameter before storing it.
    c         C   s.   | |  _  | |  _ t |  j d d ƒ |  _ d S(   sq   
        @param options: parent Options object
        @param coerce: callable used to coerce the value.
        t	   coerceDoct    N(   t   optionst   coercet   getattrt   doc(   t   selfR
   R   (    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyt   __init__$   s    		c         C   sr   | d k r" t d | f ƒ ‚ n  y |  j | ƒ } Wn& t k
 r] } t d | f ƒ ‚ n X| |  j j | <d S(   sj   
        When called in dispatch, do the coerce for C{value} and save the
        returned value.
        s$   Parameter '%s' requires an argument.s%   Parameter type enforcement failed: %sN(   t   NoneR   R   t
   ValueErrorR
   t   opts(   R   t   parameterNamet   valuet   e(    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyt   dispatch-   s    (   R   R   t   __doc__R   R   (    (    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyR       s   		t   Optionsc           B   s§   e  Z d  Z d Z d Z d Z d „  Z d „  Z d „  Z	 d „  Z
 d d „ Z d „  Z d „  Z d d „ Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d d „ Z RS(   sÎ  
    An option list parser class

    C{optFlags} and C{optParameters} are lists of available parameters
    which your program can handle. The difference between the two
    is the 'flags' have an on(1) or off(0) state (off by default)
    whereas 'parameters' have an assigned value, with an optional
    default. (Compare '--verbose' and '--verbosity=2')

    optFlags is assigned a list of lists. Each list represents
    a flag parameter, as so::

    |    optFlags = [['verbose', 'v', 'Makes it tell you what it doing.'],
    |                ['quiet', 'q', 'Be vewy vewy quiet.']]

    As you can see, the first item is the long option name
    (prefixed with '--' on the command line), followed by the
    short option name (prefixed with '-'), and the description.
    The description is used for the built-in handling of the
    --help switch, which prints a usage summary.

    C{optParameters} is much the same, except the list also contains
    a default value::

    | optParameters = [['outfile', 'O', 'outfile.log', 'Description...']]

    A coerce function can also be specified as the last element: it will be
    called with the argument and should return the value that will be stored
    for the option. This function can have a C{coerceDoc} attribute which
    will be appended to the documentation of the option.

    subCommands is a list of 4-tuples of (command name, command shortcut,
    parser class, documentation).  If the first non-option argument found is
    one of the given command names, an instance of the given parser class is
    instantiated and given the remainder of the arguments to parse and
    self.opts[command] is set to the command name.  For example::

    | subCommands = [
    |      ['inquisition', 'inquest', InquisitionOptions,
    |           'Perform an inquisition'],
    |      ['holyquest', 'quest', HolyQuestOptions,
    |           'Embark upon a holy quest']
    |  ]

    In this case, C{"<program> holyquest --horseback --for-grail"} will cause
    C{HolyQuestOptions} to be instantiated and asked to parse
    C{['--horseback', '--for-grail']}.  Currently, only the first sub-command
    is parsed, and all options following it are passed to its parser.  If a
    subcommand is found, the subCommand attribute is set to its name and the
    subOptions attribute is set to the Option instance that parses the
    remaining options. If a subcommand is not given to parseOptions,
    the subCommand attribute will be None. You can also mark one of
    the subCommands to be the default.

    | defaultSubCommand = 'holyquest'

    In this case, the subCommand attribute will never be None, and
    the subOptions attribute will always be set.

    If you want to handle your own options, define a method named
    C{opt_paramname} that takes C{(self, option)} as arguments. C{option}
    will be whatever immediately follows the parameter on the
    command line. Options fully supports the mapping interface, so you
    can do things like C{'self["option"] = val'} in these methods.

    Advanced functionality is covered in the howto documentation,
    available at
    U{http://twistedmatrix.com/projects/core/documentation/howto/options.html},
    or doc/core/howto/options.xhtml in your Twisted directory.
    c   	      C   s
  t  t |  ƒ j ƒ  |  |  _ i  |  _ g  |  _ d |  _ i  |  _ i  |  _ i  |  _	 |  j
 |  j |  j g } x™ | D]‘ } | ƒ  \ } } } } } } |  j j | ƒ |  j | |  _ |  j j | ƒ |  j j | ƒ |  j j | ƒ |  j j | ƒ |  j	 j | ƒ qq Wd  S(   NR	   (   t   superR   R   R   t   defaultst   longOptt   shortOptt   docst   synonymst	   _dispatcht   _gather_flagst   _gather_parameterst   _gather_handlerst   extendt   update(	   R   t
   collectorst   cR   R   R   t   settingsR   R   (    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyR   ˆ   s(    							c         C   s   t  t |  ƒ t j ƒ S(   sÜ   
        Define a custom hash function so that Options instances can be used
        as dictionary keys.  This is an internal feature used to implement
        the parser.  Do not rely on it in application code.
        (   t   intt   idt   syst   maxint(   R   (    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyt   __hash__¨   s    c         C   s   |  j  ƒ  GHt j d ƒ d S(   s-   
        Display this help and exit.
        i    N(   t   __str__R*   t   exit(   R   (    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyt   opt_help°   s    c         C   s-   d d l  m } d G| j GHt j d ƒ d  S(   Niÿÿÿÿ(   t	   copyrights   Twisted version:i    (   t   twistedR0   t   versionR*   R.   (   R   R0   (    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyt   opt_version·   s    c         C   sZ  | d
 k r t j d } n  y% t j | |  j |  j ƒ \ } } Wn( t j k
 rk } t t | ƒ ƒ ‚ n XxÛ | D]Ó \ } } | d d k rœ | d } n
 | d } | } | |  j	 k rõ | j
 d d ƒ } | |  j	 k rõ t d | f ƒ ‚ qõ n  |  j	 | } t |  j | t ƒ r2|  j | j | | ƒ qs |  j | | | ƒ qs Wt |  d d
 ƒ r| sq|  j d
 k	 r| s†|  j g } n  | d | d } }	 x® |  j D]_ \ }
 } } } | |
 k sÏ| | k r¥|
 |  _ | ƒ  |  _ |  |  j _ |  j j |	 ƒ Pq¥q¥Wt d | ƒ ‚ n1 y |  j | Œ  Wn t k
 rKt d	 ƒ ‚ n X|  j ƒ  d
 S(   s6   
        The guts of the command-line parser.
        i   t   -i   t   _s   No such option '%s't   subCommandsi    s   Unknown command: %ss   Wrong number of arguments.N(   R   R*   t   argvt   getoptR   R   t   errorR   t   strR   t   replacet
   isinstanceR   R   R   R   t   defaultSubCommandR6   t
   subCommandt
   subOptionst   parentt   parseOptionst	   parseArgst	   TypeErrort   postOptions(   R   R
   R   t   argsR   t   optt   argt
   optMangledt   subt   restt   cmdt   shortt   parserR   (    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyRA   ¾   sL    	
	c         C   s   d S(   sä   
        I am called after the options are parsed.

        Override this method in your subclass to do something after
        the options have been parsed and assigned, like validate that
        all options are sane.
        N(    (   R   (    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyRD   ó   s    c         C   s   d S(   s  
        I am called with any leftover arguments which were not options.

        Override me to do something with the remaining arguments on
        the command line, those which were not flags or options. e.g.
        interpret them as a list of files to operate on.

        Note that if there more arguments on the command line
        than this method accepts, parseArgs will blow up with
        a getopt.error.  This means if you don't override me,
        parseArgs will blow up if I am passed any arguments at
        all!
        N(    (   R   (    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyRB   ü   s    c         C   s6   | d k r% t d | | f ƒ ‚ n  d |  j | <d  S(   NR	   s+   Flag '%s' takes no argument. Not even "%s".i   (   R	   N(   R   R   R   (   R   t   flagNameR   (    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyt   _generic_flag  s    c         C   só   g  d } } i  i  i  i  f \ } } } } g  } t  j |  j d | ƒ x“ | D]‹ } t j d | ƒ \ }	 }
 } |	 s„ t d ƒ ‚ n  | | |	 <d | |	 <|
 rµ | |
 } |	 | |
 <n  | j |	 ƒ |	 | |	 <|  j | |	 <qN W| | | | | | f S(   s3   
        Gather up boolean (flag) options.
        R	   t   optFlagsi   s    A flag cannot be without a name.i    (   R   t   accumulateClassListt	   __class__R   t   padToR   t   appendRO   (   R   R   R   R   R'   R   R   t   flagst   flagt   longRL   R   (    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyR      s"    



c         C   sm  g  d } } i  i  i  i  f \ } } } } g  } t  j |  j d | ƒ | ro d d l } | j d d d ƒn  t  j |  j d | ƒ i  } xÉ | D]Á }	 t j d	 |	 ƒ \ }
 } } } } |
 sÎ t d
 ƒ ‚ n  | | |
 <| | |
 <| r| | d } |
 | | <n  | j |
 d ƒ |
 | |
 <| d k	 r@t
 |  | ƒ | |
 <q’ t
 |  t ƒ | |
 <q’ W| | | | | | f S(   s4   
        Gather options which take a value.
        R	   t
   optStringsiÿÿÿÿNsC   Options.optStrings is deprecated, please use optParameters instead.t
   stackleveli   t   optParametersi   s%   A parameter cannot be without a name.t   :t   =(   R   RQ   RR   t   warningst   warnR   RS   R   RT   R   R   R:   (   R   R   R   R   R'   R   R   t
   parametersR]   t	   parameterRW   RL   t   defaultR   t	   paramType(    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyR!   -  s6    
!


c         C   sM  g  d } } i  i  i  i  f \ } } } } i  } t  j |  j | d ƒ x| j ƒ  D] } t |  d | ƒ }	 t |	 | ƒ }
 | j d d ƒ } t |	 d d ƒ } | r´ | | | <n |  j j	 | ƒ | | <| | | <|
 ré |	 d „ } n d |	 d „ } | | | <t
 | ƒ d k r4| | } |
 rT| d	 } qTqT |
 rG| d
 } n  | j | ƒ qT Wi  } x] | j ƒ  D]O } t |  d | ƒ }	 |	 | k rg  | |	 <n  | |	 j | j d d ƒ ƒ qkWd „  } xm | j ƒ  D]_ \ }	 } t
 | ƒ d k  røqÔn  | } | j | ƒ | j ƒ  } x | D] } | | | <qWqÔW| | | | | | f S(   sÔ  
        Gather up options with their own handler methods.

        This returns a tuple of many values.  Amongst those values is a
        synonyms dictionary, mapping all of the possible aliases (C{str})
        for an option to the longest spelling of that option's name
        C({str}).

        Another element is a dispatch dictionary, mapping each user-facing
        option name (with - substituted for _) to a callable to handle that
        option.
        R	   t   opt_R5   R4   R   c         S   s
   | | ƒ S(   N(    (   t   nameR   t   m(    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyt   <lambda>~  s    c         S   s   | ƒ  S(   N(    (   Rd   R   Re   (    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyRf   ‚  s    i   R[   R\   c         S   s   t  t |  ƒ t | ƒ ƒ S(   N(   t   cmpt   len(   t   at   b(    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyRf   —  s    i   N(   R   t   addMethodNamesToDictRR   t   keysR   t   flagFunctionR;   R   R   t   getRh   RT   t   itemst   sortt   pop(   R   R   R   R   R'   R   R   t   dctRd   t   methodt   takesArgt
   prettyNameR   t   fnt   reverse_dctt	   cmpLengtht   namest   names_t   longest(    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyR"   V  sP    


!	c         C   s   |  j  ƒ  d |  j d d  ƒ S(   Ns   
t   width(   t   getSynopsist   getUsageR   (   R   (    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyR-   ¥  s    c         C   sä   d t  j t j d ƒ |  j r% d p( d f } |  j d
 k rp d t  j t j d ƒ |  j rc d pf d f } n d |  j r‚ d p… d } t |  d | ƒ } | j ƒ  } |  j d
 k	 rà d	 j	 |  j j
 ƒ  |  j j | f ƒ } n  | S(   s   
        Returns a string containing a description of these options and how to
        pass them to the executed file.
        s   %s%si    s
    [options]R	   s   Usage: %s%ss   %ss	   [options]t   synopsist    N(   R    t   basenameR*   R7   R   R@   R   R   t   rstript   joinR}   R>   (   R   Ra   R   (    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyR}   ¨  s    c         C   s¬  t  |  d ƒ r" |  j j d | ƒ S| sF t t j j d d ƒ ƒ } n  t  |  d ƒ rÐ g  } xM |  j D]B \ } } } } | j i | d 6| d 6| d 6d	 d
 6d  d 6ƒ qe Wt
 | | ƒ } d d j | ƒ } n d } i  }	 xg |  j j ƒ  D]V \ }
 } | } |
 | k r)t |
 ƒ d k r)|
 |	 | <qì | |	 k rì d  |	 | <qì qì Wg  } xš |  j D] } | d d k rd } | d  } n d } | j i | d 6|	 | d 6|  j | d 6| d
 6|  j j | d  ƒ d 6|  j j | d  ƒ d 6ƒ qVWt |  d d  ƒ d  k	 r|  j } n0 d d  l } t | d d  ƒ r7| j } n d } | rod d j t j | | ƒ ƒ j ƒ  d } n  | ršt
 | | ƒ } d d j | ƒ } n d } | | | S(   NR?   R|   t   COLUMNSt   80R6   RW   RL   R   t   commandt   optTypeRa   s
   Commands:
R	   i   iÿÿÿÿR\   R`   RV   R   t   longdescR   s   
s   Options:
%ss   Options: None
(   t   hasattrR?   R~   R(   t   ost   environRn   R6   RT   R   t   docMakeChunksRƒ   R   Ro   Rh   R   R   R   R   R   Rˆ   t   __main__R   R   t   wordWrapt   strip(   R   R|   t   cmdDictsRK   RL   RM   t   desct   chunkst   commandst   longToShortt   keyR   t   longnamet   optDictsRF   R‡   Rˆ   R   t   s(    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyR~   ¿  sd    

,N(   R   R   R   R   R>   R=   R@   R   R,   R/   R3   RA   RD   RB   RO   R    R!   R"   R-   R}   R~   (    (    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyR   =   s$   F	 			5					)	O		iP   c      
   C   su  d } xc |  D][ } t  | j d d ƒ ƒ } | r | j d d ƒ d k rV | d } n  t | | ƒ } q q W| t  d ƒ } | | } d | } g  } i  }	 xÒ|  D]Ê} | j d	 d ƒ |	 k s£ | j d d ƒ |	 k rß q£ n  xE | j d	 d ƒ | j d d ƒ f D] }
 |
 d k	 rd |	 |
 <qqWg  } d } | j d	 d ƒ rYd
 | d	 f } n d } | j d d ƒ rÂ| d } | j d d ƒ d k r | d } n  d | | f } | rÖd } qÖn d | t  d ƒ } | j d d ƒ d k rûd | } n d | | | f } | j d d ƒ r3| d j ƒ  } n d } | j d d ƒ d k r€| j d d ƒ d k	 r€d | | d f } n  | j d d ƒ d k rë| j d d ƒ d k	 rë| d } t | t ƒ rë| j rëd | | j f } qën  | rt j	 | | ƒ } n	 d g } | j
 d | | j d ƒ f ƒ x% | D] } | j
 d | | f ƒ q6W| j
 d j | ƒ ƒ q£ W| S(   s"  
    Makes doc chunks for option declarations.

    Takes a list of dictionaries, each of which may have one or more
    of the keys 'long', 'short', 'doc', 'default', 'optType'.

    Returns a list of strings.
    The strings may be multiple lines,
    all of them end with a newline.
    i    RW   R	   R‡   R`   i   s
     -s, --  R€   RL   s   -%cR\   s   %-*st   ,s   --R†   s       %s      s     %2s%c --%s  R   Ra   s   %s [default: %s]R   s   %s. %ss   %s%s
N(   Rh   Rn   R   t   maxR   R<   R   R   R   RŽ   RT   Rq   Rƒ   (   t   optListR|   t	   maxOptLenRF   t   optLent	   colWidth1t	   colWidth2t
   colFiller1t	   optChunkst   seent   xt   optLinest   commaRL   RW   t   column1R   t   dt	   column2_lt   line(    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyRŒ     sj    

0+

	 c         C   sK   |  j  j j } | d k r7 t d | p- |  j ƒ ‚ n  | d k rG d Sd S(   Ni   s   Invalid Option function for %si    i   (   t   im_funct	   func_codet   co_argcountR   t	   func_name(   Rs   Rd   t   reqArgs(    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyRm   k  s    c         C   s>   t  |  ƒ }  |  d k  s$ |  d k r: t d |  f ƒ ‚ n  |  S(   sO   
    Coerce a string value to an int port number, and checks the validity.
    i    iÿÿ  s   Port number not in range: %s(   R(   R   (   R   (    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyt
   portCoercev  s    s#   Must be an int between 0 and 65535.(   R   RŠ   R*   R8   R    t   twisted.pythonR   R   R   t	   ExceptionR   R9   t   objectR   t   dictR   RŒ   R   Rm   R¯   R   (    (    (    s8   /usr/lib/python2.7/dist-packages/twisted/python/usage.pyt   <module>   s   ÿ Ò]	