Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib/python3.7/site-packages/logilab/common/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/logilab/common/__pycache__/optik_ext.cpython-37.pyc

B

�d{T�4�@spdZddlmZdZddlZddlZddlZddlmZddlm	Z	ddl
mZm
ZmZmZmZmZmZmZmZmZyddlmZd	ZWnek
r�d
ZYnXddlmZmZmZmZdd
�Z dd�Z!dd�Z"dd�Z#dd�Z$dd�Z%dd�Z&dd�Z'dd�Z(dd�Z)Gd d!�d!e�Z
Gd"d#�d#e�Zde_*d$d%�Z+d&d'�Z,e,e_,Gd(d)�d)e�Z-d*ej.dfd+d,�Z/d-Z0dS).aAdd 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)
�)�print_functionzrestructuredtext enN)�copy)�exists)
�OptionParser�Option�OptionGroup�OptionContainer�OptionValueError�OptionError�Values�
HelpFormatter�
NO_DEFAULT�
SUPPRESS_HELP)�DateTimeTF)�
splitstrip�
TIME_UNITS�
BYTE_UNITS�apply_unitscCsBt|d�r|Sy
t�|�Stk
r<td||f��YnXdS)zPcheck a regexp value by trying to compile it
    return the compiled regexp
    �patternz#option %s: invalid regexp value: %rN)�hasattr�re�compile�
ValueErrorr	)�option�opt�value�r�I/opt/alt/python37/lib/python3.7/site-packages/logilab/common/optik_ext.py�check_regexpJs

rcCsDt|ttf�r|Syt|�Stk
r>td||f��YnXdS)zTcheck a csv value by trying to split it
    return the list of separated values
    z option %s: invalid csv value: %rN)�
isinstance�list�tuplerrr	)rrrrrr�	check_csvVsr"cCsBt|t�rt|�S|dkrdS|dkr*dSd}t|||f��dS)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)N)r�int�boolr	)rrr�msgrrr�check_ynbs
r*cCs�t|t�r|Sg}xVt|||�D]F}|�d�dkrF|�|�dd��q |�d�dkr |�|�dd��q W|rvt|�Sd}t|||f��dS)zVcheck a named value
    return a dictionary containing (name, value) associations
    �=�����:zMoption %s: invalid named value %r, should be <NAME>=<VALUE> or <NAME>:<VALUE>N)r�dictr"�find�append�splitr	)rrr�valuesr)rrr�check_namedos
r4cCs|S)z,check a password value (can't be empty)
    r)rrrrrr�check_password�sr5cCs$t|�r|Sd}t|||f��dS)z/check a file value
    return the filepath
    z!option %s: file %r does not existN)rr	)rrrr)rrr�
check_file�sr6cCs4yt�|d�Stjk
r.td|��YnXdS)z/check a file value
    return the filepath
    z%Y/%m/%dz#expected format of %s is yyyy/mm/ddN)r�strptime�Errorr	)rrrrrr�
check_date�s
r9cCs@t�d|tj�r|St�d|tj�r(|Sd}t|||f��dS)z�check 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 colorN)r�match�Ir	)rrrr)rrr�check_color�sr<cCst|tttf�r|St|t�S)N)rr'�long�floatrr)rrrrrr�
check_time�sr?cCst|d�r|St|t�S)N�__int__)rrr)rrrrrr�check_bytes�s
rAc@s�eZdZdZejdZejddgZeej�Ze	ed<e
ed<eed<eed<e
ed	<e
ed
<eed<eed<eed
<eed<er�ed7Zeed<dd�Zdd�Zeejd<dd�ZdS)rz7override optik.Option to add some new option types
    )
�regexp�csv�yn�named�password�multiple_choice�file�color�time�bytes�hide�levelrBrCrDrErGrHrIrFrJrK)�daterNcOs,tj|f|�|�t|d�r(|jr(t|_dS)NrL)�
BaseOption�__init__rrLr�help)�self�opts�attrsrrrrP�szOption.__init__cCsr|jdkrT|jdkr td|��qnt|jttf�sntdtt|j���d�d|��n|jdk	rntd|j|��dS)z3FIXME: need to override this due to optik misdesign)�choicerGNz/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
rr!r �strr2)rRrrr�
_check_choice�s


 
zOption._check_choice�cCsL|�||�}|jdkr4t||j�}|r4|�|�|}|�|j|j||||�S)NrE)Z
convert_valuerW�getattr�dest�updateZtake_action�action)rRrrr3�parserZexistantrrr�process�s

zOption.processN)�__name__�
__module__�__qualname__�__doc__rOZTYPESZATTRSrZTYPE_CHECKERrr"r*r4r6r<r5r?rA�HAS_MX_DATETIMEr9rPrZZ
CHECK_METHODSrarrrrr�s*


rc@s&eZdZdZefdd�Zddd�ZdS)rz8override optik.OptionParser to use our Option class
    cOstj|f|�dti|��dS)N�option_class)�
BaseParserrPr)rRrg�args�kwargsrrrrP�szOptionParser.__init__NcCs�|dkr|j}t|dd�}|�|�g}|�|�d��|��|jrb|�t�||��|�d�xB|j	D]8}|j
|krj|js�t||�rj|�|�
|��|�d�qjW|��d�|dd��S)N�output_levelrZOptions�
�r,)�	formatterr\Zstore_option_stringsr1�format_heading�indent�option_listr�format_option_helpZ
option_groupsrM�description�
level_options�format_help�dedent�join)rRrn�outputlevel�result�grouprrrrr�s"


zOptionParser.format_option_help)N)rbrcrdrerrPrrrrrrr�srcs�fdd�|jD�S)Ncs.g|]&}t|dd�pd�kr|jtk	r|�qS)rMr)r\rQr)�.0r)rxrr�
<listcomp>sz!level_options.<locals>.<listcomp>)rq)rzrxr)rxrrtsrtcCsBg}t|dd�pd}x"t||�D]}|�|�|��q Wd�|�S)Nrkrrm)r\rtr1�
format_optionrw)rRrnryrxrrrrrrs
rrc@sdeZdZdZddd�Zdd�Zd	d
�Zdd�Zddd�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dS)�ManHelpFormatterz'Format help using man pages ROFF formatr��OcCst�|||||�dS)N)rrP)rRZindent_incrementZmax_help_position�widthZshort_firstrrrrPszManHelpFormatter.__init__cCsd|��S)Nz.SH %s
)�upper)rRZheadingrrrro'szManHelpFormatter.format_headingcCs|S)Nr)rRrsrrr�format_description*sz#ManHelpFormatter.format_descriptioncCsdy
|j}Wntk
r(|�|�}YnX|jrT|�|�}d�dd�|��D��}nd}d||fS)N� cSsg|]}|���qSr)�strip)r{�lrrrr|4sz2ManHelpFormatter.format_option.<locals>.<listcomp>rmz.IP "%s"
%s
)Zoption_strings�AttributeErrorZformat_option_stringsrQZexpand_defaultrw�
splitlines)rRr�	optstringZ	help_textrQrrrr}-s

zManHelpFormatter.format_optionr-cCsrd}y|��}Wntk
r,|��}YnX|�||j�}t|d�rT|�||j�}d|�||�||�	|�|fS)Nrm�	long_descz%s
%s
%s
%s)
Z_get_prog_namer�Z
get_prog_name�format_short_descriptionrsr�format_long_descriptionr��format_title�format_synopsis)rR�	optparser�pkginfo�sectionr��pgm�
short_descrrr�format_head;s

zManHelpFormatter.format_headcCs0d�dd�t��dd�D��}d||||fS)N�-cSsg|]}t|��qSr)rY)r{�numrrrr|Jsz1ManHelpFormatter.format_title.<locals>.<listcomp>�z.TH %s %s "%s" %s)rwrJ�	localtime)rRr�r�rNrrrr�Is zManHelpFormatter.format_titlecCsd||��fS)Nz.SH NAME
.B %s
\- %s
)r�)rRr�r�rrrr�Msz)ManHelpFormatter.format_short_descriptioncCsd|S)Nz6.SH SYNOPSIS
.B  %s
[
.I OPTIONS
] [
.I <arguments>
]
r)rRr�rrrr�Ssz ManHelpFormatter.format_synopsiscCsRd�dd�|��D��}|�dd�}|���|�rB|t|�d�}d||��fS)NrlcSsg|]}|���qSr)�lstrip)r{�linerrrr|^sz<ManHelpFormatter.format_long_description.<locals>.<listcomp>z
.
z

z.SH DESCRIPTION
.B %s
%s
)rwr��replace�lower�
startswith�lenr�)rRr�r�rrrr�]s
z(ManHelpFormatter.format_long_descriptioncCs<dt|d|j�|j|j|jf}t|d�r8|d|j7}|S)Nz}.SH SEE ALSO
/usr/share/doc/pythonX.Y-%s/

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

.SH AUTHOR
%s <%s>
Zdebian_name�	copyrightz
.SH COPYRIGHT
%s
)r\�modname�mailinglist�author�author_emailrr�)rRr��tailrrr�format_tailhs

zManHelpFormatter.format_tailN)rrr�r)r-)rbrcrdrerPror�r}r�r�r�r�r�r�rrrrr~s


r~r-cCsPt�}||_||_t|�|||�|d�t|�|�|d�t|�|�|d�dS)z(generate a man page from an optik parser)rHN)r~rkr`�printr�rrr�)r�r�r��streamrMrnrrr�generate_manpage}sr�)rrrr	r)1re�
__future__r�
__docformat__r�sysrJr�os.pathr�optparserrhrrOrrr	r
rrr
rZmxrrf�ImportErrorZlogilab.common.textutilsrrrrrr"r*r4r5r6r9r<r?rArMrtrrr~�stdoutr��__all__rrrr�<module>0sB0



;a