
    c?                        d Z dZddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZmZmZmZmZmZ ddlmZ dd	lmZmZmZmZmZmZmZmZmZmZ 	 dd
l m!Z! dZ"ddl$m%Z%m&Z&m'Z'm(Z( d Z)ded   de*deee*   ee*df   e*f   deee*   ee*df   f   fdZ+ded   de*dee,e*f   de,fdZ-dee   de*deee*e*f   e*f   dee*e*f   fdZ.d Z/d Z0d Z1d Z2d Z3ded   de*dede4fdZ5 G d de      Z G d d e      Zde_6        d!ed"e4dee   fd#Z7d$ Z8e8e_8         G d% d&e      Z9d'ejt                  dfd(ed)e	d*e4d+ed,e4ddfd-Z;d.Z<y# e#$ r dZ"Y w xY w)/a  Add an abstraction level to transparently import optik classes from optparse
(python >= 2.3) or the optik package.

It also defines three new types for optik/optparse command line parser :

  * regexp
    argument of this type will be converted using re.compile
  * csv
    argument of this type will be converted using split(',')
  * yn
    argument of this type will be true if 'y' or 'yes', false if 'n' or 'no'
  * named
    argument of this type are in the form <NAME>=<VALUE> or <NAME>:<VALUE>
  * password
    argument of this type wont be converted but this is used by other tools
    such as interactive prompt for configuration to double check value and
    use an invisible field
  * multiple_choice
    same as default "choice" type but multiple choices allowed
  * file
    argument of this type wont be converted but checked that the given file exists
  * color
    argument of this type wont be converted but checked its either a
    named color or a color specified using hexadecimal notation (preceded by a #)
  * time
    argument of this type will be converted to a float value in seconds
    according to time units (ms, s, min, h, d)
  * bytes
    argument of this type will be converted to a float value in bytes
    according to byte units (b, kb, mb, gb, tb)
zrestructuredtext en    N)copy)exists)attrdict)AnyUnionListOptionalTupleDict)StringIO)
OptionParserOptionOptionGroupOptionContainerOptionValueErrorOptionErrorValuesHelpFormatterSUPPRESS_HELP
NO_DEFAULT)DateTimeTF)
splitstrip
TIME_UNITS
BYTE_UNITSapply_unitsc                     t        |d      r|S 	 t        j                  |      S # t        $ r t	        d|d|      w xY w)zPcheck a regexp value by trying to compile it
    return the compiled regexp
    patternoption z: invalid regexp value: )hasattrrecompile
ValueErrorr   optionoptvalues      :/usr/lib/python3/dist-packages/logilab/common/optik_ext.pycheck_regexpr(   V   sI     ui Uzz%   UUSTTUs	   % A r$   r   r%   r&   .returnc                     t        |t        t        f      r|S 	 t        |      S # t        $ r t        d|d|      w xY w)zTcheck a csv value by trying to split it
    return the list of separated values
    r   z: invalid csv value: )
isinstancelisttupler   r"   r   r#   s      r'   	check_csvr.   b   sI     %$'R%   RS%PQQRs	   
% A c                 p    t        |t              rt        |      S |dv ry|dv ryd}t        |||fz        )z>check a yn value
    return true for yes and false for no
    )yyesT)nnoFz<option %s: invalid yn value %r, should be in (y, yes, n, no))r+   intboolr   r$   r%   r&   msgs       r'   check_ynr8   p   sG     %E{
HC
3#u-
..    c                 d   t        |t              r|S g }t        | ||      D ]n  }|j                  d      dk7  r"|j	                  |j                  dd             9|j                  d      dk7  sN|j	                  |j                  dd             p |rt        |      S d}t        |||fz        )zVcheck a named value
    return a dictionary containing (name, value) associations
    =   :zMoption %s: invalid named value %r, should be <NAME>=<VALUE> or <NAME>:<VALUE>)r+   dictr.   findappendsplitr   )r$   r%   r&   valuesr7   s        r'   check_namedrD   ~   s     %$&F63. / ::c?b MM%++c1-.ZZ_"MM%++c1-./ F|C
3#u-
..r9   c                     |S )z'check a password value (can't be empty) r#   s      r'   check_passwordrG      s	     Lr9   c                 @    t        |      r|S d}t        |||fz        )/check a file value
    return the filepath
    z!option %s: file %r does not exist)r   r   r6   s       r'   
check_filerJ      s)     e}
-C
3#u-
..r9   c                 z    	 t        j                  |d      S # t         j                  $ r t        d|z        w xY w)rI   z%Y/%m/%dz#expected format of %s is yyyy/mm/dd)r   strptimeErrorr   r#   s      r'   
check_daterN      sA    L  
33>> LDsJKKLs    ":c                     t        j                  d|t         j                        r|S t        j                  d|t         j                        r|S d}t        |||fz        )zcheck a color value and returns it
    /!\ does *not* check color labels (like 'red', 'green'), only
    checks hexadecimal forms
    z[a-z0-9 ]+$z#[a-f0-9]{6}zYoption %s: invalid color : %r, should be either hexadecimal     value or predefined color)r    matchIr   r6   s       r'   check_colorrR      sQ     
xxubdd+	xxrtt,C
3#u-
..r9   c                 R    t        |t        t        f      r|S t        |t              S N)r+   r4   floatr   r   r#   s      r'   
check_timerV      s"    %#u&uj))r9   c                 J    t        |d      r|S t        |t        t              S )N__int__)final)r   r   r   r4   r#   s      r'   check_bytesrZ      s"    ui  uj44r9   c            
       F   e Zd ZdZej
                  dz   Zej                  ddgz   Z eej                        Ze	ed<   e
ed<   eed<   eed<   e
ed	<   eed
<   eed<   eed<   eed<   eed<   er
edz  Zeed<   dededdfdZddZeej0                  d<   dededededef
dZy)r   z2override optik.Option to add some new option types)
regexpcsvynnamedpasswordmultiple_choicefilecolortimebyteshidelevelr\   r]   r^   r_   ra   rb   rc   r`   rd   re   )daterh   optsattrsr)   Nc                     t        j                  | g|i | t        | d      r| j                  rt        | _        y y y )Nrf   )
BaseOption__init__r   rf   r   help)selfri   rj   s      r'   rm   zOption.__init__   s;    D14151 4 TYY%DI &/ r9   c                 V   | j                   dv rv| j                  t        d|       t        | j                  t        t
        f      s=t        dt        t        | j                              j                  d      d   z  |       y| j                  t        d| j                   z  |       y)z3FIXME: need to override this due to optik misdesign)choicera   Nz/must supply a list of choices for type 'choice'z1choices must be a list of strings ('%s' supplied)'r=   z#must not supply choices for type %r)typechoicesr   r+   r-   r,   strrB   )ro   s    r'   _check_choicezOption._check_choice   s    9955 ||#!"SUYZZudm<!G$t||,-33C8;<  = \\%CdiiOQUVV &r9      r%   r&   rC   parserc                 
   | j                  ||      }| j                  dk(  r9| j                  J t        || j                        }|r|j	                  |       |}| j                  | j                  | j                  ||||      S )Nr_   )convert_valuers   destgetattrupdatetake_actionaction)ro   r%   r&   rC   rx   existants         r'   processzOption.process
  s{     ""3.9999(((vtyy1H&  TYYUFFSSr9   )r)   N)__name__
__module____qualname____doc__rl   TYPESATTRSr   TYPE_CHECKERr(   r.   r8   rD   rJ   rR   rG   rV   rZ   HAS_MX_DATETIMErN   ru   r   rm   rv   CHECK_METHODSr   
BaseParserr4   r   rF   r9   r'   r   r      s   <   E 00E
//0L)L#L!L'L&/L"#%L'L-L%L'L)V&c &C &D &W$ #0JQT3 Ts TF TJ TSV Tr9   c            	       D    e Zd ZdZefdedededdfdZd
dee	   de
fd	Zy)r   z3override optik.OptionParser to use our Option classoption_classargskwargsr)   Nc                 :    t        j                  | g|d|i| y )Nr   )r   rm   )ro   r   r   r   s       r'   rm   zOptionParser.__init__   s!     	DMdM|MfMr9   	formatterc                 `   || j                   }t        |dd      }|j                  |        g }|j                  |j	                  d             |j                          | j                  r6|j                  t        j                  | |             |j                  d       | j                  D ]\  }|j                  |k  s|j                  st        ||      s,|j                  |j                  |             |j                  d       ^ |j                          dj                  |d d       S )Noutput_levelr   Options
 r<   )r   r|   store_option_stringsrA   format_headingindentoption_listr   format_option_helpoption_groupsrg   descriptionlevel_optionsformat_helpdedentjoin)ro   r   outputlevelresultgroups        r'   r   zOptionParser.format_option_help&  s    Ii;&&t,i..y9:MM/<<T9MNMM$'' 	$E {{k)!!]5+%Fe//	:;d#	$ 	wwvcr{##r9   rT   )r   r   r   r   r   rs   r   rm   r	   r   ru   r   rF   r9   r'   r   r     sG    =,2 NT N3 N# NRV N$H],C $s $r9   r   r   r   c                     | j                   D cg c]*  }t        |dd      xs d|k  r|j                  t        ur|, c}S c c}w )Nrg   r   )r   r|   rn   r   )r   r   r$   s      r'   r   r   C  sL    
 ''FGQ',1<TaAa 	  s   /Ac                     g }t        |dd      xs d}t        | |      D ]"  }|j                  |j                  |             $ dj	                  |      S )Nr   r   r   )r|   r   rA   format_optionr   )ro   r   r   r   r$   s        r'   r   r   M  sU    F)^Q7<1Kk2 7i--f567776?r9   c                       e Zd ZdZ	 	 	 	 ddededededdf
dZd	edefd
Zd Zde	defdZ
ddedededefdZdededefdZdededefdZdedefdZd ZdedefdZy)ManHelpFormatterz'Format help using man pages ROFF formatindent_incrementmax_help_positionwidthshort_firstr)   Nc                 6    t        j                  | ||||       y rT   )r   rm   )ro   r   r   r   r   s        r'   rm   zManHelpFormatter.__init__]  s     	t%57H%Q\]r9   headingc                 (    d|j                         z  S )Nz.SH %s
)upper)ro   r   s     r'   r   zManHelpFormatter.format_headingf  s    GMMO++r9   c                     |S rT   rF   )ro   r   s     r'   format_descriptionz#ManHelpFormatter.format_descriptioni  s    r9   r$   c                 .   	 |j                   }|j                  rM| j	                  |      }dj                  |j                         D cg c]  }|j                          c}      }nd}d|d|dS # t        $ r | j                  |      }Y w xY wc c}w )N r   z.IP "z"
r   )option_stringsAttributeErrorformat_option_stringsrn   expand_defaultr   
splitlinesstrip)ro   r$   	optstring	help_textlinern   s         r'   r   zManHelpFormatter.format_optionl  s    	; --I
 ;; ++F3I88i6J6J6LMdTZZ\MNDD 	
 	
  	;226:I	; Ns   A2 B2BB	optparserpkginfosectionc                    d}|j                         }| j                  ||j                        }t        |d      r| j	                  ||j
                        }| j                  ||      d|d| j                  |      d|S )Nr   	long_descr   )get_prog_nameformat_short_descriptionr   r   format_long_descriptionr   format_titleformat_synopsis)ro   r   r   r   r   pgm
short_descs          r'   format_headzManHelpFormatter.format_head  s}    	%%'2238K8KL
7K(44S':K:KLIc7+  %	
 	
r9   r   c                     dj                  t        j                         d d D cg c]  }t        |       c}      }d|d|d|d|S c c}w )N-   z.TH r   z "z" )r   rd   	localtimeru   )ro   r   r   numrh   s        r'   r   zManHelpFormatter.format_title  sB    xxT^^-=bq-ABcSBC&)7D#>> Cs   Ar   c                 0    d|d|j                         dS )Nz.SH NAME
.B z
\- r   )r   )ro   r   r   s      r'   r   z)ManHelpFormatter.format_short_description  s    
 
 	
r9   c                     d|z  S )Nz6.SH SYNOPSIS
.B  %s
[
.I OPTIONS
] [
.I <arguments>
]
rF   )ro   r   s     r'   r   z ManHelpFormatter.format_synopsis  s     
	
r9   c                 .   dj                  |j                         D cg c]  }|j                          c}      }|j                  dd      }|j	                         j                  |      r|t        |      d  }d|d|j                         dS c c}w )Nr   z
.
z

z.SH DESCRIPTION
.B )r   r   lstripreplacelower
startswithlenr   )ro   r   r   r   s       r'   r   z(ManHelpFormatter.format_long_description  s    II9M9M9OPt{{}PQ	%%gv6	??'',!#c(*-I
 OO
 	
	 Qs   Bc           	          dt        |d|j                        d|j                  d|j                  d|j                  d	}t        |d      r|d|j                  z  z  }|S )	Nz&.SH SEE ALSO
/usr/share/doc/pythonX.Y-debian_namez>/

.SH BUGS
Please report bugs on the project's mailing list:
z

.SH AUTHOR
z <z>
	copyrightz
.SH COPYRIGHT
%s
)r|   modnamemailinglistauthorauthor_emailr   r   )ro   r   tails      r'   format_tailzManHelpFormatter.format_tail  sf     G]GOO<NN  
  7K( ##	$D r9   )r      O   r   )r=   )r   r   r   r   r4   rm   ru   r   r   r   r   r   r   r   r   r   r   r   r   rF   r9   r'   r   r   Z  s    1 !"!#^^ ^ 	^
 ^ 
^,c ,c ,
J 
3 
0
\ 
H 
s 
[^ 
? ?c ?c ?
C 
S 
S 

3 
3 

8  r9   r   r=   r   r   r   streamrg   c                     t               }||_        | |_        t        |j	                  | ||      |       t        | j                  |      |       t        |j                  |      |       y)z(generate a man page from an optik parser)rb   N)r   r   rx   printr   r   r   )r   r   r   r   rg   r   s         r'   generate_manpager     sa     !"I #I I	)

	7G
<6J	)
&
&y
1?	)


(v6r9   )r   r   r   r   r   )=r   __docformat__r    sysrd   r   os.pathr   logilab.commonr   typingr   r   r   r	   r
   r   _ior   optparser   r   r   rl   r   r   r   r   r   r   r   r   mxr   r   ImportErrorlogilab.common.textutilsr   r   r   r   r(   ru   r.   r5   r8   rD   rG   rJ   rN   rR   rV   r4   rZ   rg   r   r   r   stdoutr   __all__rF   r9   r'   <module>r      s8  $@ & 	 
    # : :   O U T	URXR%(R16tCy%S/SV7V1WR
49eCHo%&R/Xh' /c /%c	:J /t //SM/ #/,1$sCx.#2E,F/	#s(^/0/L/"*5* 5 5S 5S 5JTZ JTZ$: $F   # $z:J  &8 "x} x| zz777 7 	7
 7 
7$ Ro  Os   E EE