o
    bK/                     @   s  d Z ddlZddlmZmZmZ G dd deZG dd deZG dd	 d	Z	G d
d de	Z
G dd de
ZG dd de
ZG dd de
ZG dd de
ZG dd de	ZG dd deZG dd de	ZG dd de	ZG dd de	ZG dd deZG d d! d!eZG d"d# d#e	ZG d$d% d%e	Zd&d' ZG d(d) d)e	ZG d*d+ d+eZG d,d- d-ZG d.d/ d/ZG d0d1 d1ZdS )2z
Form-based method objects.

This module contains support for descriptive method signatures that can be used
to format methods.
    N)AnyOptionalTuplec                   @      e Zd ZdZdd ZdS )FormExceptionz*An error occurred calling the form method.c                 O   s   t j| g|R   || _d S N)	Exception__init__descriptions)selfargskwargs r   ;/usr/lib/python3/dist-packages/twisted/python/formmethod.pyr	      s   
zFormException.__init__N)__name__
__module____qualname____doc__r	   r   r   r   r   r          r   c                   @      e Zd ZdZdS )
InputErrorz,
    An error occurred with some input.
    Nr   r   r   r   r   r   r   r   r          r   c                   @   sT   e Zd ZU dZdZeed< 	dddZdd Zdd	d
Z	dd Z
dd Zdd ZdS )ArgumentzBase class for form arguments.NdefaultDefault   c                 C   s>   || _ || _|d u r| j}|| _|| _|| _|si }|| _d S r   )name	allowNoner   default	shortDesclongDeschints)r   r   r   r   r    r!   r   r   r   r   r	   %   s   
zArgument.__init__c                 K   s   | j | d S r   )r!   update)r   r   r   r   r   addHints3      zArgument.addHintsc                 C   s   | j ||S r   )r!   get)r   r   r   r   r   r   getHint6   s   zArgument.getHintc                 C   s   | j p| j S r   )r   r   
capitalizer   r   r   r   getShortDescription9   r$   zArgument.getShortDescriptionc                 C   s
   | j pdS )N )r    r(   r   r   r   getLongDescription<   s   
zArgument.getLongDescriptionc                 C   s   t d)z(Convert the value to the correct format.zimplement in subclass)NotImplementedErrorr   valr   r   r   coerce?   s   zArgument.coerce)NNNNr   r   )r   r   r   r   r   r   __annotations__r	   r#   r&   r)   r+   r/   r   r   r   r   r      s   
 

r   c                   @   sF   e Zd ZU dZdZeed< dZdZ							dddZ	d	d
 Z
dS )StringzA single string.r*   r   r   Nr   c	           	   	   C   s(   t j| ||||||d || _|| _d S )N)r   r   r    r!   r   )r   r	   minmax)	r   r   r   r   r    r!   r   r2   r3   r   r   r   r	   K   s   	
zString.__init__c                 C   sR   t |}t|| jk rtd| j | jd ur%t|| jkr%td| j t |S )N)Value must be at least %s characters long(Value must be at most %s characters long)strlenr2   r   r3   )r   r.   sr   r   r   r/   b   s   zString.coerce)NNNNr   r   N)r   r   r   r   r   r6   r0   r2   r3   r	   r/   r   r   r   r   r1   D   s   
 
r1   c                   @   r   )TextzA long string.Nr   r   r   r   r   r9   k   r   r9   c                   @   r   )Passwordz-A string which should be obscured when input.Nr   r   r   r   r   r:   o   r   r:   c                   @   r   )VerifiedPasswordzCA string that should be obscured when input and needs verification.c                 C   sv   t |dks|d |d krtdt|d }t || jk r&td| j | jd ur9t || jkr9td| j |S )N   r   r   z%Please enter the same password twice.r4   r5   )r7   r   r6   r2   r3   )r   valsr8   r   r   r   r/   v   s   zVerifiedPassword.coerceN)r   r   r   r   r/   r   r   r   r   r;   s   r   r;   c                   @   r   )HiddenzSA string which is not displayed.

    The passed default is used as the value.
    Nr   r   r   r   r   r>      r   r>   c                   @   s6   e Zd ZU dZdZee ed< 	d	ddZdd Z	dS )
IntegerzA single integer.Nr   r   c              	   C      t | |||||| d S r   r   r	   r   r   r   r   r   r    r!   r   r   r   r	         zInteger.__init__c                 C   s:   |  s	| jr	d S zt|W S  ty   td|w )Nz5{} is not valid, please enter a whole number, e.g. 10)stripr   int
ValueErrorr   formatr-   r   r   r   r/      s   
zInteger.coercer   NNNN)
r   r   r   r   r   r   rE   r0   r	   r/   r   r   r   r   r?      s   
 

r?   c                   @   s(   e Zd Z					dddZdd ZdS )IntegerRanger   Nc	           	   	   C   s(   || _ || _tj| ||||||d d S )N)r   r   r   r    r!   )r2   r3   r?   r	   )	r   r   r2   r3   r   r   r   r    r!   r   r   r   r	      s   
zIntegerRange.__init__c                 C   sZ   t | |}| jr|d kr|S || jk rtd|| j|| jkr+td|| j|S )Nz/Value {} is too small, it should be at least {}z.Value {} is too large, it should be at most {})r?   r/   r   r2   r   rG   r3   )r   r.   resultr   r   r   r/      s    

zIntegerRange.coercerH   r   r   r   r	   r/   r   r   r   r   rI      s    
rI   c                   @   s2   e Zd ZU dZee ed< 	dddZdd ZdS )	FloatNr   r   c              	   C   r@   r   rA   rB   r   r   r   r	      rC   zFloat.__init__c                 C   s8   |  s	| jr	d S zt|W S  ty   td| w )NzInvalid float: %s)rD   r   floatrF   r   r-   r   r   r   r/      s   
zFloat.coercerH   )	r   r   r   r   r   rM   r0   r	   r/   r   r   r   r   rL      s
   
 

rL   c                   @   s.   e Zd ZdZg g ddddfddZdd ZdS )Choicea  
    The result of a choice between enumerated types.  The choices should
    be a list of tuples of tag, value, and description.  The tag will be
    the value returned if the user hits "Submit", and the description
    is the bale for the enumerated type.  default is a list of all the
    values (seconds element in choices).  If no defaults are specified,
    initially the first item will be selected.  Only one item can (should)
    be selected at once.
    Nr   c              	   C   s<   || _ |r|s||d d  tj| ||||||d d S )Nr   r   r   )choicesappendr   r	   )r   r   rP   r   r   r    r!   r   r   r   r   r	      s   

zChoice.__init__c                 C   s.   | j D ]\}}}||kr|  S qtd| )NzInvalid Choice: %s)rP   r   )r   inIdentidentr.   descr   r   r   r/      s
   zChoice.coercer   r   r   r   r	   r/   r   r   r   r   rN      s    
rN   c                   @   s.   e Zd ZdZ						d	ddZdd ZdS )
Flagsa  
    The result of a checkbox group or multi-menu.  The flags should be a
    list of tuples of tag, value, and description. The tag will be
    the value returned if the user hits "Submit", and the description
    is the bale for the enumerated type.  default is a list of all the
    values (second elements in flags).  If no defaults are specified,
    initially nothing will be selected.  Several items may be selected at
    once.
    r   Nr   c              	   C   s"   || _ tj| ||||||d d S NrO   )flagsr   r	   )r   r   rX   r   r   r    r!   r   r   r   r   r	     s   

zFlags.__init__c                 C   sN   |sg S g }|D ]}| j D ]\}}}||kr||  nqtd| q|S )NzInvalid Flag: %s)rX   rQ   r   )r   
inFlagKeysoutFlags	inFlagKeyflagKeyflagValflagDescr   r   r   r/   "  s   
zFlags.coerce)r   r   NNNr   rU   r   r   r   r   rV     s    
rV   c                   @      e Zd ZdS )
CheckGroupNr   r   r   r   r   r   r   r`   0      r`   c                   @   r_   )
RadioGroupNra   r   r   r   r   rc   4  rb   rc   c                   @   s   e Zd Zdd ZdS )Booleanc                 C   s$   |sdS t | }|dv rdS dS )Nr   )nonffalse0r   )r6   lower)r   inVallInValr   r   r   r/   9  s   zBoolean.coerceN)r   r   r   r/   r   r   r   r   rd   8  s    rd   c                   @   s   e Zd ZdddZdd ZdS )Filer   Nc              	   C   s   t j| |d ||||d d S rW   rA   )r   r   r   r   r    r!   r   r   r   r	   C  s   
zFile.__init__c                 C   s   |s| j rd S |r|S td)NzInvalid File)r   r   )r   filer   r   r   r/   H  s
   
zFile.coerce)r   NNNrK   r   r   r   r   rm   B  s    
rm   c                 C   s   t | } | dkr
t| S )Nr   )rE   rF   )xr   r   r   positiveIntQ  s   rp   c                   @   s@   e Zd ZU dZdZeeeeef  ed< 	d	ddZ	dd Z
dS )
Datez#A date -- (year, month, day) tuple.Nr   r   c                 C   s,   t | ||||| || _|sd| _d S d S )N)i  r   r   )r   r	   r   r   rB   r   r   r   r	   ]  s
   
zDate.__init__c                 C   s   t |dkr| jrdS z
tt|\}}}W n ty    tdw ||fdkr7t|s2td| |||fS ztj| }W n t	yI   tdw ||krRtd|||fS )z(Return tuple of ints (year, month, day).)r*   r*   r*   NzInvalid date)r<      z%d was not a leap year)
tupler   maprp   rF   r   calendarisleapmdays
IndexError)r   r   yearmonthdayrw   r   r   r   r/   e  s&   


zDate.coercerH   )r   r   r   r   r   r   r   rE   r0   r	   r/   r   r   r   r   rq   X  s   
 
rq   c                   @   s0   e Zd ZdZdgdddddfddZdd ZdS )	Submitz0Submit button or a reasonable facsimile thereof.)r|   submitzSubmit formr   Nc                 C   s&   t j| |||||d || _|| _d S )N)rP   r   r    r!   )rN   r	   r   reset)r   r   rP   r~   r   r    r   r!   r   r   r   r	     s   

zSubmit.__init__c                 C   s   | j r|sd S t| |S r   )r   rN   r/   )r   valuer   r   r   r/     s   
zSubmit.coercerU   r   r   r   r   r|   |  s    
r|   c                   @   r   )PresentationHintz(
    A hint to a particular system.
    Nr   r   r   r   r   r     r   r   c                   @   s*   e Zd ZdZdd Zdd Zd
ddZd	S )MethodSignaturez$
    A signature of a callable.
    c                 G   s
   || _ dS )r*   N)methodSignature)r   sigListr   r   r   r	     s   
zMethodSignature.__init__c                 C   s"   | j D ]}|j|kr|  S qd S r   )r   r   )r   r   ar   r   r   getArgument  s
   

zMethodSignature.getArgumentFc                 C   s   t | ||S r   )
FormMethod)r   callabletakesRequestr   r   r   method     zMethodSignature.methodNF)r   r   r   r   r	   r   r   r   r   r   r   r     s
    r   c                   @   s*   e Zd ZdZd
ddZdd Zdd Zd	S )r   z#A callable object with a signature.Fc                 C   s   || _ || _|| _d S r   )	signaturer   r   )r   r   r   r   r   r   r   r	     s   
zFormMethod.__init__c                 C   s   t | jjS r   )rs   r   r   r(   r   r   r   getArgs  r   zFormMethod.getArgsc                 O   s   | j |i |S r   )r   )r   r   kwr   r   r   call  r$   zFormMethod.callNr   )r   r   r   r   r	   r   r   r   r   r   r   r     s
    
r   )r   ru   typingr   r   r   r   r   r   r   r1   r9   r:   r;   r>   r?   rI   rL   rN   rV   r`   rc   rd   rm   rp   rq   r|   r   r   r   r   r   r   r   <module>   s4   %'+$(
$