o
    ThaM                     @   s   d dl Z d dlmZmZmZmZmZ zddlmZ ddl	m
Z
mZmZ W n ey=   d dlmZ d dlm
Z
mZmZ Y nw ddlmZ ddlmZ erRdd	lmZ d
edefddZd
edefddZG dd ded ZG dd dZG dd de
ZdS )    N)TYPE_CHECKINGAnyIterableListOptional   )Lexicon)StateMachinestate
transition)
ParseError)debug   )ParserContextvaluereturnc                 C   
   |  dS )N-
startswithr    r   G/var/www/html/venv/lib/python3.10/site-packages/invoke/parser/parser.pyis_flag      
r   c                 C   r   )N--r   r   r   r   r   is_long_flag   r   r   c                       s.   e Zd ZdZdededdf fddZ  ZS )ParseResulta(  
    List-like object with some extra parse-related attributes.

    Specifically, a ``.remainder`` attribute, which is the string found after a
    ``--`` in any parsed argv list; and an ``.unparsed`` attribute, a list of
    tokens that were unable to be parsed.

    .. versionadded:: 1.0
    argskwargsr   Nc                    s"   t  j|i | d| _g | _d S )N )super__init__	remainderunparsed)selfr   r   	__class__r   r   r"   )   s   
zParseResult.__init__)__name__
__module____qualname____doc__r   r"   __classcell__r   r   r&   r   r      s    "
r   r   c                	   @   sP   e Zd ZdZ			dded ded ded	dfd
dZdee	 d	e
fddZdS )Parseray  
    Create parser conscious of ``contexts`` and optional ``initial`` context.

    ``contexts`` should be an iterable of ``Context`` instances which will be
    searched when new context names are encountered during a parse. These
    Contexts determine what flags may follow them, as well as whether given
    flags take values.

    ``initial`` is optional and will be used to determine validity of "core"
    options/flags at the start of the parse run, if any are encountered.

    ``ignore_unknown`` determines what to do when contexts are found which do
    not map to any members of ``contexts``. By default it is ``False``, meaning
    any unknown contexts result in a parse error exception. If ``True``,
    encountering an unknown context halts parsing and populates the return
    value's ``.unparsed`` attribute with the remaining parse tokens.

    .. versionadded:: 1.0
    r   NFcontextsr   initialignore_unknownr   c                 C   s   || _ t | _|| _|D ]A}td| |jstdd}|j| jv r,t||j|| j|j< |jD ]}|| jv rCt||| jj	||jd q5qd S )Nz	Adding {}z%Non-initial contexts must have names.z7A context named/aliased {!r} is already in this parser!)to)
r/   r   r.   r0   r   formatname
ValueErroraliasesalias)r%   r.   r/   r0   contextexistsr6   r   r   r   r"   D   s"   

zParser.__init__argvc                 C   sD  t | j| j| jd}td| z|d}W n ty%   t|}Y nw |d| }||d dd }|r@td|| t	|D ]\}}g }|}	t
|r|jjsd|v ru|d\}}
}d}t||	|| ||d |f nrt|st|d	kr|dd }|d	d |dd	 }}d
}t|||| ||jjv o|jdk}|r|jj| jrd}t||| ||d |f n"dd |D }d}t||	|| t|D ]}||d |f q|jr|jo|jj}||jjv }|r|s|	}g }|D ]\}}||| q|| qD|  |j}d||_|S )a  
        Parse an argv-style token list ``argv``.

        Returns a list (actually a subclass, `.ParseResult`) of
        `.ParserContext` objects matching the order they were found in the
        ``argv`` and containing `.Argument` objects with updated values based
        on any flags given.

        Assumes any program name has already been stripped out. Good::

            Parser(...).parse_argv(['--core-opt', 'task', '--task-opt'])

        Bad::

            Parser(...).parse_argv(['invoke', '--core-opt', ...])

        :param argv: List of argument string tokens.
        :returns:
            A `.ParseResult` (a ``list`` subclass containing some number of
            `.ParserContext` objects).

        .. versionadded:: 1.0
        )r/   r.   r0   zStarting argv: {!r}r   Nr   z"Remainder: argv[{!r}:][1:] => {!r}=z1Splitting x=y expr {!r} into tokens {!r} and {!r}r   z,Splitting {!r} into token {!r} and rest {!r}unknownzE{!r} is a flag for current context & it takes a value, giving it {!r}c                 S   s   g | ]}d  |qS )z-{})r2   .0xr   r   r   
<listcomp>   s    z%Parser.parse_argv.<locals>.<listcomp>z1Splitting multi-flag glob {!r} into {!r} and {!r} )ParseMachiner/   r.   r0   r   r2   indexr4   len	enumerater   resultr$   	partitionappendr   r7   flagscurrent_statetakes_valuereversedwaiting_for_flag_valueflagoptionalinserthandlefinishjoinr#   )r%   r9   machineddashbodyr#   rB   token	mutationsorig_r   msg
full_tokenresterr	have_flag_restitemrN   subtoken_is_valid_flagrE   r   r   r   
parse_argvZ   sp   	
zParser.parse_argv)r   NF)r(   r)   r*   r+   r   r   boolr"   r   strr   rb   r   r   r   r   r-   /   s    
r-   c                       sb  e Zd ZdZedddgd edddgd edddgd edddd	 edd
ddd eddddd dededdfddZdddede	ddf fddZ
ede	fddZdeddfddZdeddfd d!Zd8d"d#Zd$eddfd%d&Zd8d'd(Zd)ede	fd*d+Zd9d-ed.e	ddfd/d0Zd)eddfd1d2Zd)eddfd3d4Zd5eddfd6d7Z  ZS ):rA   r7   complete_flagcomplete_context)enterr;   end)r7   r;   rQ   )from_eventr1   see_contextswitch_to_context)ri   rj   actionr1   see_unknown
store_onlyri   r1   r   Nc                 C   s   t d|| d S )NzParseMachine: {!r} => {!r})r   r2   )r%   ri   r1   r   r   r   changing_state   s   zParseMachine.changing_stater/   r   r.   r0   c                    sf   || _ t| | _| _td| j d | _d| _t	 | _
t|| _td| j t   d S )NzInitialized with context: {!r}FzAvailable contexts: {!r})r0   copydeepcopyr/   r7   r   r2   rM   flag_got_valuer   rE   r.   r!   r"   )r%   r/   r.   r0   r&   r   r   r"      s   zParseMachine.__init__c                 C   s>   | j o| j j}|sdS | j jtu r| jsdS | j jd u}| S )NFT)rM   rJ   kindlistrs   	raw_value)r%   rJ   	has_valuer   r   r   rL      s   z#ParseMachine.waiting_for_flag_valuerV   c                 C   s  t d| | jdkrt d| | | d S | jr1|| jjv r1t d| | | d S | jrJ|| jjv rJt d| | j|dd d S | jr[t d| | 	| d S | jrt| jj
rtd	}t || j| | | d S || jv r| | d S | jr|| jjv rt d
| | jj| }|jdkr| jj|_d}t ||j d S | | d S | jst d| | d| d S t d| | | d S )NzHandling token: {!r}r;   z!Top-of-handle() see_unknown({!r})zSaw flag {!r}zSaw inverse flag {!r}T)inversez2We're waiting for a flag value so {!r} must be it?z2Context {!r} requires positional args, eating {!r}zSaw (initial-context) flag {!r}helpzGSaw --help in a per-task context, setting task name ({!r}) as its valuez'Can't find context named {!r}, erroringzNo idea what {!r} is!z$Bottom-of-handle() see_unknown({!r}))r   r2   rI   rn   r7   rH   switch_to_flaginverse_flagsrL   	see_valuemissing_positional_argssee_positional_argr.   rk   r/   r3   r   r0   error)r%   rV   rZ   rM   r   r   r   rP     sJ   




zParseMachine.handlec                 C   s    t d| | jj| d S )NzStoring unknown token {!r})r   r2   rE   r$   rG   )r%   rV   r   r   r   ro   J  s   zParseMachine.store_onlyc                 C   s   t d| jr
| jjn| j | jr/| jjr/d}ddd | jjD }| || jj| | jrA| j| jvrC| j| j d S d S d S )NzWrapping up context {!r}z6'{}' did not receive required positional arguments: {}z, c                 s   s    | ]	}d  |jV  qdS )z'{}'N)r2   r3   r<   r   r   r   	<genexpr>X  s
    

z0ParseMachine.complete_context.<locals>.<genexpr>)	r   r2   r7   r3   r}   rR   r   rE   rG   )r%   r]   namesr   r   r   rf   O  s   
zParseMachine.complete_contextr3   c                 C   sZ   t | j| | _td| td| jj td| jj td| jj d S )NzMoving to context {!r}zContext args: {!r}zContext flags: {!r}zContext inverse_flags: {!r})	rq   rr   r.   r7   r   r2   r   rH   r{   )r%   r3   r   r   r   rl   `  s
   zParseMachine.switch_to_contextc                 C   s   | j rd}t|| j  | j r)| j jr)| j jd u r)| j js)d}| || j  | j rK| j jd u rM| j jrOd}t|| j j | j jddd d S d S d S d S )Nz+Completing current flag {} before moving onz-Flag {!r} needed value and was not given one!z9Saw optional flag {!r} go by w/ no value; setting to TrueTF)cast)	rM   r   r2   rJ   rv   rN   r   r3   	set_value)r%   rZ   r]   r   r   r   re   g  s"   zParseMachine.complete_flagr   c                 C   sh   | j r| j js	dS | j jdurdS g }|| jo| jj ||| jv  t|r2d}t|	|dS )zs
        Guard against ambiguity when current flag takes an optional value.

        .. versionadded:: 1.0
        FNz9{!r} is ambiguous when given after an optional-value flag)
rM   rN   rv   rG   r7   r}   r.   anyr   r2   )r%   r   testsrZ   r   r   r   check_ambiguity}  s   zParseMachine.check_ambiguityFrM   rx   c                 C   s   |  | |   |r| jj| n|}z	| jj| | _W n% tyA } zz	| jj| | _W n	 ty6   |w W Y d }~nd }~ww td	| j d| _
| jrf| jjsh| }td	| j| || j_d S d S d S )NzMoving to flag {!r}FzMarking seen flag {!r} as {})r   re   r7   r{   rH   rM   KeyErrorr/   r   r2   rs   rJ   r   )r%   rM   rx   evalr   r   r   rz     s*   
zParseMachine.switch_to_flagc                 C   sR   |  | | jr| jjrtd| j| || j_d| _d S | d| j d S )NzSetting flag {!r} to value {!r}Tz!Flag {!r} doesn't take any value!)r   rM   rJ   r   r2   r   rs   r   )r%   r   r   r   r   r|     s   

zParseMachine.see_valuec                 C   s(   | j jD ]}|jd u r||_ d S qd S N)r7   positional_argsr   )r%   r   argr   r   r   r~     s   
zParseMachine.see_positional_argrZ   c                 C   s   t || jr   )r   r7   )r%   rZ   r   r   r   r     s   zParseMachine.error)r   N)F)r(   r)   r*   initial_stater
   r   rd   rp   r   rc   r"   propertyrL   rP   ro   rf   rl   re   r   r   rz   r|   r~   r   r,   r   r   r&   r   rA      sP    :

!	rA   )rq   typingr   r   r   r   r   vendor.lexiconr   vendor.fluidityr	   r
   r   ImportErrorlexiconfluidity
exceptionsr   utilr   r7   r   rd   rc   r   r   r   r-   rA   r   r   r   r   <module>   s&     