o
    ThW&                     @   s   d dl Z d dlmZmZmZmZmZmZmZ zddl	m
Z
 W n ey-   d dlm
Z
 Y nw ddlmZ dedefd	d
ZdedefddZdedefddZdedeeeef  fddZG dd dZdS )    N)AnyDictListIterableOptionalTupleUnion   )Lexicon   )Argumentnamereturnc                 C   s   |  ddddS )N_-)lstriprstripreplacer    r   H/var/www/html/venv/lib/python3.10/site-packages/invoke/parser/context.pytranslate_underscores   s   r   c                 C   s$   t | } t| dkrd|  S d|  S )Nr   r   z--)r   lenr   r   r   r   to_flag   s   r   argc                    s>   | j }dd |D   fdd|D }tt r n|d S )Nc                 S   s"   h | ]}t |d dkr|qS )r   r   )r   strip.0xr   r   r   	<setcomp>   s   " z!sort_candidate.<locals>.<setcomp>c                    s   h | ]}| vr|qS r   r   r   shortsr   r   r      s    r   )namesstrsorted)r   r"   longsr   r    r   sort_candidate   s   r&   c                 C   sl   g }t | }|t|dkrdnd ||  d}|D ]}|| r)| n| 7 }q|| |S )zZ
    Obtain useful key list-of-ints for sorting CLI flags.

    .. versionadded:: 1.0
    r   r    )r&   appendr   lowerisupperupper)r   retr   inversedcharr   r   r   flag_key   s   
r/   c                	   @   s   e Zd ZdZ			ddee dee dee ddfdd	Zdefd
dZ	de
de
ddfddZedee fddZedeee
f fddZdedee fddZdedeeef fddZdeeeee f  fddZdeedf fddZdS )ParserContextz
    Parsing context with knowledge of flags & their format.

    Generally associated with the core program or a task.

    When run through a parser, will also hold runtime values filled in by the
    parser.

    .. versionadded:: 1.0
    Nr   r   aliasesargsr   c                 C   s@   t  | _g | _t  | _i | _|| _|| _|D ]}| | qdS )a  
        Create a new ``ParserContext`` named ``name``, with ``aliases``.

        ``name`` is optional, and should be a string if given. It's used to
        tell ParserContext objects apart, and for use in a Parser when
        determining what chunk of input might belong to a given ParserContext.

        ``aliases`` is also optional and should be an iterable containing
        strings. Parsing will honor any aliases when trying to "find" a given
        context in its input.

        May give one or more ``args``, which is a quick alternative to calling
        ``for arg in args: self.add_arg(arg)`` after initialization.
        N)r
   r2   positional_argsflagsinverse_flagsr   r1   add_arg)selfr   r1   r2   r   r   r   r   __init__G   s   zParserContext.__init__c                 C   sV   d}| j rdd| j }| jrd| j|nd}| jr#d| jnd}d||S )Nr'   z ({}), z {!r}{}z: {!r}z<parser/Context{}{}>)r1   formatjoinr   r2   )r7   r1   r   r2   r   r   r   __repr__d   s   zParserContext.__repr__kwargsc                 O   s  t |dkrt|d tr|d }nt|i |}|jD ]}|| jv r,d}t||q|jd }|| j|< |jr@| j	| || j
t|< |jD ]}| jj||d | j
jt|t|d qJ|jrm| jj|j|d |jtkr|jdu rtd|}t|| j|< dS dS dS )a  
        Adds given ``Argument`` (or constructor args for one) to this context.

        The Argument in question is added to the following dict attributes:

        * ``args``: "normal" access, i.e. the given names are directly exposed
          as keys.
        * ``flags``: "flaglike" access, i.e. the given names are translated
          into CLI flags, e.g. ``"foo"`` is accessible via ``flags['--foo']``.
        * ``inverse_flags``: similar to ``flags`` but containing only the
          "inverse" versions of boolean flags which default to True. This
          allows the parser to track e.g. ``--no-myflag`` and turn it into a
          False value for the ``myflag`` Argument.

        .. versionadded:: 1.0
        r   r   z;Tried to add an argument named {!r} but one already exists!)toTzno-{}N)r   
isinstancer   r"   r2   
ValueErrorr:   
positionalr3   r(   r4   r   	nicknamesalias	attr_namekindbooldefaultr5   )r7   r2   r=   r   r   msgmaininverse_namer   r   r   r6   l   s,   





zParserContext.add_argc                 C   s   dd | j D S )Nc                 S   s   g | ]	}|j d u r|qS N)valuer   r   r   r   
<listcomp>   s    z9ParserContext.missing_positional_args.<locals>.<listcomp>)r3   r7   r   r   r   missing_positional_args   s   z%ParserContext.missing_positional_argsc                 C   s$   i }| j  D ]}|j||j< q|S )a  
        This context's arguments' values keyed by their ``.name`` attribute.

        Results in a dict suitable for use in Python contexts, where e.g. an
        arg named ``foo-bar`` becomes accessible as ``foo_bar``.

        .. versionadded:: 1.0
        )r2   valuesrL   r   )r7   r,   r   r   r   r   	as_kwargs   s   
zParserContext.as_kwargsflagc                 C   s   t t|g| j| S rK   )listsetr4   
aliases_of)r7   rR   r   r   r   	names_for   s   zParserContext.names_forc                 C   s   || j vrd}t||| j  | j | }tdtdi|j}g }| |D ]D}|rSt	|
ddkrE|jr=d|n|}d|}n d|}|jrRd|}n|| j v rcd	|d
d }d}|||  q(dt|t	d}	|jpzd}
|	|
fS )zs
        Return 2-tuple of ``(flag-spec, help-string)`` for given ``flag``.

        .. versionadded:: 1.0
        z@{!r} is not a valid flag for this context! Valid flags are: {!r}STRINGINTr   r   z[{}]z {}z={}z	--[no-]{}r	   Nr'   r9   key)r4   r@   r:   keysr#   intgetrE   rV   r   r   optionalr5   rP   r(   r;   r$   help)r7   rR   errr   rL   
full_namesr   value_valuestrnamestrhelpstrr   r   r   help_for   s,   




zParserContext.help_forc                    s$   t t fddt j tdS )a  
        Return sorted iterable of help tuples for all member Arguments.

        Sorts like so:

        * General sort is alphanumerically
        * Short flags win over long flags
        * Arguments with *only* long flags and *no* short flags will come
          first.
        * When an Argument has multiple long or short flags, it will sort using
          the most favorable (lowest alphabetically) candidate.

        This will result in a help list like so::

            --alpha, --zeta # 'alpha' wins
            --beta
            -a, --query # short flag wins
            -b, --argh
            -c

        .. versionadded:: 1.0
        c                    s     t| jS rK   )rf   r   r   )r   rN   r   r   <lambda>   s    z+ParserContext.help_tuples.<locals>.<lambda>rY   )rS   mapr$   r4   rP   r/   rN   r   rN   r   help_tuples   s   
zParserContext.help_tuples.c                    sH   t  j td} fdd|D }|t j  tt	j
|S )z
        Similar to `help_tuples` but returns flag names only, no helpstrs.

        Specifically, all flag names, flattened, in rough order.

        .. versionadded:: 1.0
        rY   c                    s   g | ]
}  t|jqS r   )rV   r   r   r   rN   r   r   rM     s    z,ParserContext.flag_names.<locals>.<listcomp>)r$   r4   rP   r/   r(   rS   r5   r[   tuple	itertoolschainfrom_iterable)r7   r4   r"   r   rN   r   
flag_names   s   	zParserContext.flag_names)Nr   r   )__name__
__module____qualname____doc__r   r#   r   r   r8   r<   r   r6   propertyr   rO   r   rQ   rV   r   rf   ri   rn   r   r   r   r   r0   ;   s0    
1'"r0   )rk   typingr   r   r   r   r   r   r   vendor.lexiconr
   ImportErrorlexiconargumentr   r#   r   r   r&   r\   r/   r0   r   r   r   r   <module>   s    $