o
    €Ï€  ã                   @   s  d Z ddlmZ ddlmZ ddlm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ZdZd	Zd
ZdZdZdZdZdZdZdZdZdZdZdZdZG dd„ deƒZdd„ Zdd„ Zdd„ Zdd „ Z d!d"„ Z!d#d$„ Z"d%d&„ Z#d1d'd(„Z$d)d*„ Z%d+d,„ Z&d-d.„ Z'd/d0„ Z(dS )2z<Methods for looking up completions from the static CLI tree.é    )Úabsolute_import)Údivision)Úunicode_literalsN)Úencoding)Ú	platformsÚ	COMP_LINEÚ
COMP_POINTÚ_ARGCOMPLETE_IFSúé   z--ÚboolÚdynamicÚvalueÚenv_varÚcommandsÚflagsÚ ú=ú c                   @   s   e Zd ZdZdS )ÚCannotHandleCompletionErrorz-Error for when completions cannot be handled.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r   r   úP/tmp/google-cloud-sdk/lib/googlecloudsdk/command_lib/static_completion/lookup.pyr   3   s    r   c                  C   s0   t  tjt¡} tt  tjt¡ƒ}| d|… } | S )zQGets the command line from the environment.

  Returns:
    str, Command line.
  N)r   ÚGetEncodedValueÚosÚenvironÚLINE_ENV_VARÚintÚPOINT_ENV_VAR)Úcmd_lineÚcompletion_pointr   r   r   Ú_GetCmdLineFromEnv8   s   r$   c                 C   s4   t  | ¡dd… }| d tkr| t¡ | ¡  |S )z²Converts the given cmd_line to a queue of command line words.

  Args:
    cmd_line: str, full command line before parsing.

  Returns:
    [str], Queue of command line words.
  é   Néÿÿÿÿ)ÚshlexÚsplitÚ_SPACEÚappendÚ_EMPTY_STRINGÚreverse)r"   Ú	cmd_wordsr   r   r   Ú_GetCmdWordQueueD   s
   	
r.   c                   C   s   t j ¡ rdS dS )Nú%ú$)r   ÚOperatingSystemÚ	IsWindowsr   r   r   r   ÚGetEnvVarPrefixX   s   r3   c                 C   sT   g }| dd… }|D ]}|  |¡r'tj ¡ r | d| d ¡ q
| d| ¡ q
|S )a  Returns environment variables beginning with `word`.

  Args:
    word: The word that is being compared to environment variables.
    env_vars: The list of environment variables.

  Returns:
    []: No completions.
    [completions]: List, all possible sorted completions.
  r%   Nr/   r0   )Ú
startswithr   r1   r2   r*   )ÚwordÚenv_varsÚcompletionsÚprefixÚchildr   r   r   ÚMatchEnvVars]   s   

€r:   c                 C   s®  t |ƒ}| }|t }g }t}tƒ }tj}|rÓ| ¡ }	|	 t¡r>d}
| 	ti ¡}| 
|¡ t|	v r=|	 td¡\}	}| |¡ n|	 |¡rJd}
|}t}nd}
| 	ti ¡}|r~|	|v rf|
r_||	 }nrt}||	 }nk|tkrkq|tkrrt}q|
s|| 	t¡s|t}qg S |tkr†tdƒ‚|tkrŒg S |tkr˜|t|	|ƒ7 }n9|tkr¬|D ]}| |	¡rª| |¡ qžn%|s²tdƒ‚t |¡D ]\}}| |	¡sÁq·|
rË|tkrË|t7 }| |¡ q·|st|ƒS )as  Try to perform a completion based on the static CLI tree.

  Args:
    root: The root of the tree that will be traversed to find completions.
    cmd_line: [str], original command line.

  Raises:
    CannotHandleCompletionError: If FindCompletions cannot handle completion.

  Returns:
    []: No completions.
    [completions]: List, all possible sorted completions.
  Tr%   Fz2Dynamic completions are not handled by this modulez5Positional completions are not handled by this module)r.   ÚLOOKUP_FLAGSÚFLAG_BOOLEANr3   r   r   Úpopr4   ÚFLAG_PREFIXÚgetÚupdateÚ
_VALUE_SEPr(   r*   ÚENV_VARÚLOOKUP_COMMANDSÚFLAG_DYNAMICr   Ú
FLAG_VALUEr:   ÚsixÚ	iteritemsÚsorted)Úrootr"   ÚwordsÚnodeÚglobal_flagsr7   Ú	flag_modeÚenv_var_prefixr6   r5   Úis_flag_wordÚchild_nodesÚ
flag_valuer   r9   r   r   r   Ú_FindCompletionss   s€   


€


ÿ

€þÿ
ÀArR   c                   C   s    t jj t t jj¡dd… ¡S )z&Returns the SDK installation root dir.Néûÿÿÿ)r   ÚpathÚsepÚjoinÚ__file__r(   r   r   r   r   Ú_GetInstallationRootDirÏ   s    rX   c                   C   s   t j tƒ dd¡S )z/Returns the SDK static completion CLI tree dir.ÚdataÚcli)r   rT   rV   rX   r   r   r   r   Ú_GetCompletionCliTreeDirÕ   s   r[   c                 C   s   t j | ptƒ d¡S )z0Returns the SDK static completion CLI tree path.zgcloud_completions.py)r   rT   rV   r[   )Ú	directoryr   r   r   ÚCompletionCliTreePathÛ   s   
ÿr]   c                  C   s\   z)zt jdd… } t j tƒ ¡ ddl}|j}W n ty#   tdƒ‚w W | t _|S | t _w )z1Loads and returns the static completion CLI tree.Nr   z.Cannot find static completion CLI tree module.)ÚsysrT   r*   r[   Úgcloud_completionsÚSTATIC_COMPLETION_CLI_TREEÚImportErrorr   )Úsys_pathr_   Útreer   r   r   ÚLoadCompletionCliTreeâ   s   
ÿÿÿÿrd   c                   C   s   t  td¡S )z&Returns the completions output stream.Úwb)r   ÚfdopenÚCOMPLETIONS_OUTPUT_FDr   r   r   r   Ú_OpenCompletionsOutputStreamñ   s   rh   c                  C   s   t ƒ } tƒ }t| |ƒS )z7Returns the static completions, None if there are none.)rd   r$   rR   )rI   r"   r   r   r   Ú_GetCompletionsö   s   
ri   c                  C   sd   t ƒ } | r0t tjtt¡}d}ztƒ }| | 	| ¡ 
¡ ¡ W |r&| ¡  dS dS |r/| ¡  w w dS )z>Attempts completions and writes them to the completion stream.N)ri   r   r   r   r   ÚIFS_ENV_VARÚIFS_ENV_DEFAULTrh   ÚwriterV   ÚencodeÚclose)r7   ÚifsÚfr   r   r   ÚCompleteý   s   ÿ
ÿörq   )N))r   Ú
__future__r   r   r   r   r'   r^   Úgooglecloudsdk.core.utilr   r   rF   r   r!   rj   rk   rg   r>   r<   rD   rE   rB   rC   r;   r+   rA   r)   Ú	Exceptionr   r$   r.   r3   r:   rR   rX   r[   r]   rd   rh   ri   rq   r   r   r   r   Ú<module>   sL   \
