o
    VhY3                     @  s   d dl mZ d dlZd dlZd dl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 d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ erbd dlmZ d dlmZ G dd deZdS )    )annotationsN)Path)TYPE_CHECKING)ClassVar)cast)helpers)shell_quote)Command)	TEMPLATES)CleoRuntimeError)Argument)Optionc                   @  s   e Zd ZU dZdZejddddgZded< ej	d	d
ddddgZ
ded< dZdZdZd2ddZd3ddZed4ddZd5ddZd4ddZd4d d!Zd4d"d#Zd4d$d%Zd6d(d)Zd7d+d,Zd8d9d0d1Zd
S ):CompletionsCommandcompletionsz+Generate completion scripts for your shell.shellz&The shell to generate the scripts for.T)optionalzClassVar[list[Argument]]	argumentsaliasNzAlias for the current command.F)flagmultiplezClassVar[list[Option]]options)bashzshfisha	  
One can generate a completion script for `<options=bold>{script_name}</>` that is compatible with a given shell. The script is output on `<options=bold>stdout</>` allowing one to re-direct the output to the file of their choosing. Where you place the file will depend on which shell, and which operating system you are using. Your particular configuration may also determine where these scripts need to be placed.

Here are some common set ups for the three supported shells under Unix and similar operating systems (such as GNU/Linux).

<options=bold>BASH</>:

Completion files are commonly stored in `<options=bold>/etc/bash_completion.d/</>`

Run the command:

`<options=bold>{script_name} {command_name} bash > /etc/bash_completion.d/{script_name}.bash-completion</>`

This installs the completion script. You may have to log out and log back in to your shell session for the changes to take effect.

<options=bold>FISH</>:

Fish completion files are commonly stored in`<options=bold>$HOME/.config/fish/completions</>`

Run the command:

`<options=bold>{script_name} {command_name} fish > ~/.config/fish/completions/{script_name}.fish</>`

This installs the completion script. You may have to log out and log back in to your shell session for the changes to take effect.

<options=bold>ZSH</>:

ZSH completions are commonly stored in any directory listed in your `<options=bold>$fpath</>` variable. To use these completions, you must either add the generated script to one of those directories, or add your own to this list.

Adding a custom directory is often the safest best if you're unsure of which directory to use. First create the directory, for this example we'll create a hidden directory inside our `<options=bold>$HOME</>` directory

`<options=bold>mkdir ~/.zfunc</>`

Then add the following lines to your `<options=bold>.zshrc</>` just before `<options=bold>compinit</>`

`<options=bold>fpath+=~/.zfunc</>`

Now you can install the completions script using the following command

`<options=bold>{script_name} {command_name} zsh > ~/.zfunc/_{script_name}</>`

You must then either log out and log back in, or simply run

`<options=bold>exec zsh</>`

For the new completions to take affect.

<options=bold>CUSTOM LOCATIONS</>:

Alternatively, you could save these files to the place of your choosing, such as a custom directory inside your $HOME. Doing so will require you to add the proper directives, such as `source`ing inside your login script. Consult your shells documentation for how to add such directives.
returnintc                 C  sJ   |  d}|s|  }|| jvrtdd| j | | | dS )Nr   z [shell] argument must be one of z, r   )argumentget_shell_typeSUPPORTED_SHELLS
ValueErrorjoinlinerenderselfr    r%   u/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/cleo/commands/completions_command.pyhandlew   s   

zCompletionsCommand.handlestrc                 C  s>   |dkr|   S |dkr|  S |dkr|  S td| )Nr   r   r   zUnrecognized shell: )render_bash
render_zshrender_fishRuntimeErrorr#   r%   r%   r&   r"      s   zCompletionsCommand.renderc                  C  s   d} t  }|d ur|jnd }|d ur|jnd }~|d ur:tt|d} | dkr1tt|d} | r:| dd } | s@td| S )N __name____main____package__.r   zCan not determine package name)	inspectcurrentframef_back	f_globalsr   r(   get	partitionr   )package_nameframer4   r5   r%   r%   r&   _get_prog_name_from_stack   s   z,CompletionsCommand._get_prog_name_from_stacktuple[str, str]c                 C  s.   | j jjp|  }t|}t|j}||fS N)_ioinputscript_namer:   	posixpathrealpathr   name)r$   r?   script_pathr%   r%   r&   _get_script_name_and_path   s   

z,CompletionsCommand._get_script_name_and_pathc           
        s@  |   \}}||g| d}| || | jsJ dd t| jjjdd dD }g }g }t| j  dd dD ]@}|j	sF|j
rF|jsGq;d|jv rQt|jn|j}|| dd	d
 t|jjdd dD }	|d| dd|	 dddg7 }q;td | d|d|d|d d d fdd
|D d S )Nr   c                 S  s   g | ]}d |j  qS --rB   .0optr%   r%   r&   
<listcomp>   s    
z2CompletionsCommand.render_bash.<locals>.<listcomp>c                 S     | j S r<   rG   or%   r%   r&   <lambda>       z0CompletionsCommand.render_bash.<locals>.<lambda>keyc                 S  
   | j pdS Nr-   rG   cr%   r%   r&   rO         
  c                 s  s$    | ]}d |j  ddV  qdS )rF   :\:N)rB   replacerH   r%   r%   r&   	<genexpr>   s
    
z1CompletionsCommand.render_bash.<locals>.<genexpr>c                 S  rL   r<   rG   rM   r%   r%   r&   rO      rP               ()z            opts="${opts} "            ;;r-   r   
c                 3       | ]}d   d| V  qdS )zcomplete -o default -F rX   Nr%   rI   r   functionr%   r&   r\      s    
r?   rf   optscmds	cmds_optscompdefs)rD   option_generate_function_nameapplicationsorted
definitionr   allvalueshiddenenabledrB   r   appendr    r
   
r$   r?   rC   aliasesrh   ri   rj   cmdcommand_namer   r%   re   r&   r)      sB   




zCompletionsCommand.render_bashc           
        sf    \}}|gd}|| dfddjs J fddtjjjd	d
 dD }g }g }tj  dd
 dD ]J}|j	sO|j
rO|jsPqDd|jv rZt|jn|j}|||j dfddt|jjdd
 dD }	|d| dd|	 dddg7 }qDtd | d|d|d|d d d fdd|D d S )Nr   sr(   r   c                   s    j jj| S r<   )r=   output	formatterremove_formatrz   r$   r%   r&   sanitize   s   z/CompletionsCommand.render_zsh.<locals>.sanitizec                   s&   g | ]} d |j  |jqS rE   _zsh_describerB   descriptionrH   r   r$   r%   r&   rK      s    z1CompletionsCommand.render_zsh.<locals>.<listcomp>c                 S  rL   r<   rG   rM   r%   r%   r&   rO      rP   z/CompletionsCommand.render_zsh.<locals>.<lambda>rQ   c                 S  rS   rT   rG   rU   r%   r%   r&   rO      rW   rX   c                 3  s*    | ]} d |j  |jV  qdS )rF   Nr   rH   r   r%   r&   r\      s
    
z0CompletionsCommand.render_zsh.<locals>.<genexpr>c                 S  rL   r<   rG   rM   r%   r%   r&   rO      rP   r]   r^   z            opts+=(r`   r-   r   ra   rb   c                 3  rc   )zcompdef rX   Nr%   rd   re   r%   r&   r\      s    rg   rz   r(   r   r(   )rD   rl   rm   rn   ro   rp   r   rq   rr   rs   rt   rB   r   ru   r   r   r    r
   rv   r%   )rf   r   r$   r&   r*      s@   


zCompletionsCommand.render_zshc                   s    \}|d"fddjsJ fddtjjjdd	 d
D }g }g }t }tj  dd	 d
D ]}|j	sK|j
rK|jsLq@|jd}|d }d|jv r_|d n|j}	t|dkr|d d d|	 d|j d	 d|	  n@||vr|d d d|  dd j|D }
|d d| dd|
 d|	 d|j d d| d|	  |d|j g fddt|jjdd	 d
D d7 }|| q@td d |d |d |d d dt|d! S )#Nrz   r(   r   c                   s    j jj| ddS )N'z\')r=   r{   r|   r}   r[   r~   r   r%   r&   r     s   z0CompletionsCommand.render_fish.<locals>.sanitizec                   2   g | ]}d  d  d|j  d|j d	qS )complete -c z -n '__fishz_no_subcommand' -l  -d 'r   rB   r   rH   )rf   r   r?   r%   r&   rK     s    z2CompletionsCommand.render_fish.<locals>.<listcomp>c                 S  rL   r<   rG   rM   r%   r%   r&   rO     rP   z0CompletionsCommand.render_fish.<locals>.<lambda>rQ   c                 S  rS   rT   rG   rU   r%   r%   r&   rO     rW   rX   r   rb      r   z -f -n '__fishz_no_subcommand' -a r   r   z__fish_seen_subcommand_from c                 S  s   g | ]	}| d d qS )rX   rb   )split)rI   rB   r%   r%   r&   rK   -  s    z$ -f -n '__fish_seen_subcommand_from z&; and not __fish_seen_subcommand_from z' -a z"; and __fish_seen_subcommand_from z# c                   r   )r   z -n 'z' -l r   r   r   rH   )	conditionr   r?   r%   r&   rK   <  s    c                 S  rL   r<   rG   rM   r%   r%   r&   rO   @  rP   r-   r   ra   )r?   rf   rh   ri   rj   
cmds_namesr   )rD   rm   rn   ro   rp   r   setrq   rr   rs   rt   rB   r   lenru   r   r    addr
   )r$   rC   rh   ri   rj   
namespacesrx   cmd_path	namespacecmd_namesubcmdsr%   )r   rf   r   r?   r$   r&   r+     s   



zCompletionsCommand.render_fishc                 C  s    t d}|stdt|jS )NSHELLznCould not read SHELL environment variable. Please specify your shell type by passing it as the first argument.)osgetenvr,   r   rB   r#   r%   r%   r&   r   O  s   

z!CompletionsCommand.get_shell_typer?   rC   c                 C  s6   |  |}t|  d d }d| d| dS )N   _	_complete)_sanitize_for_function_namehashlibmd5encode	hexdigest)r$   r?   rC   sanitized_namemd5_hashr%   r%   r&   rm   Y  s   
z*CompletionsCommand._generate_function_namerB   c                 C  s   | dd}tdd|S )N-r   z[^A-Za-z0-9_]+r-   )r[   resub)r$   rB   r%   r%   r&   r   ^  s   z.CompletionsCommand._sanitize_for_function_namevaluer   
str | Nonec                 C  sH   d| dd }|rtdd|}|dt|gd 7 }|d7 }|S )Nr_   rY   rZ   z%([\"'#&;`|*?~<>^()\[\]{}$\\\x0A\xFF])z\\\1)r[   r   r   
subprocesslist2cmdlinestrip)r$   r   r   r%   r%   r&   r   c  s   z CompletionsCommand._zsh_describe)r   r   )r   r(   r   r(   )r   r(   )r   r;   )r?   r(   rC   r(   r   r(   )rB   r(   r   r(   r<   )r   r(   r   r   r   r(   )r.   
__module____qualname__rB   r   r   r   r   __annotations__rl   r   r   rs   helpr'   r"   staticmethodr:   rD   r)   r*   r+   r   rm   r   r   r%   r%   r%   r&   r      s4   
 

I




*
+
L


r   )
__future__r   r   r2   r   r@   r   r   pathlibr   typingr   r   r   cleor   cleo._compatr   cleo.commands.commandr	   #cleo.commands.completions.templatesr
   cleo.exceptionsr   cleo.io.inputs.argumentr   cleo.io.inputs.optionr   r   r%   r%   r%   r&   <module>   s(    