
                             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e      Zd Zd Zd Zd Z d Z!d Z"d Z#d%d Z$d! Z%d" Z&d# Z'd$ Z(y)&z<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                       e Zd ZdZy)CannotHandleCompletionErrorz-Error for when completions cannot be handled.N)__name__
__module____qualname____doc__     :lib/googlecloudsdk/command_lib/static_completion/lookup.pyr   r   3   s    5r   r   c                      t        j                  t        j                  t              } t        t        j                  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_points     r   _GetCmdLineFromEnvr(   8   sI     %%bjj,?(11"**mLM''((	/r   c                     t        j                  |       dd }| d   t        k(  r|j                  t               |j                          |S )zConverts 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_wordss     r   _GetCmdWordQueuer3   D   sI     kk(#AB') b\V]# 	r   c                  F    t         j                  j                         rdS dS )N%$)r   OperatingSystem	IsWindowsr   r   r   GetEnvVarPrefixr9   X   s    ))335>3>r   c                     g }| dd }|D ]^  }|j                  |      st        j                  j                         r|j	                  d|z   dz          K|j	                  d|z          ` |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*   Nr5   r6   )
startswithr   r7   r8   r/   )wordenv_varscompletionsprefixchilds        r   MatchEnvVarsrA   ]   so     +8&e		"	"	,	,	.3;,-3;'  
r   c                 H   t        |      }| }|t           }g }t        }t               }t        j
                  }|r|j                         }	|	j                  t              r\d}
|j                  t        i       }|j                  |       t        |	v r_|	j                  t        d      \  }	}|j                  |       n4|	j                  |      rd}
|}t        }nd}
|j                  t        i       }|rU|	|v r|
r||	   }nt        }||	   }n|t        k(  r|t        k7  rt        }|
s|j                  t              st        }g S |t         k(  rt#        d      |t$        k(  rg S |t        k(  r|t'        |	|      z  }n|t        k7  r+|D ]%  }|j                  |	      s|j                  |       ' na|st#        d      t)        j*                  |      D ]<  \  }}|j                  |	      s|
r|t        k7  r	|t        z  }|j                  |       > |r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)r3   LOOKUP_FLAGSFLAG_BOOLEANr9   r!   r"   popr;   FLAG_PREFIXgetupdate
_VALUE_SEPr-   r/   ENV_VARLOOKUP_COMMANDSFLAG_DYNAMICr   
FLAG_VALUErA   six	iteritemssorted)rootr&   wordsnodeglobal_flagsr>   	flag_modeenv_var_prefixr=   r<   is_flag_wordchild_nodes
flag_valuer   r@   s                  r   _FindCompletionsrZ   s   s    8
$%	$l#,+)"$.ZZ(99;D{#lHH\2.k&	t	::j!4j 	Z 		(lkilHH_b1k 		!$')")T"$$ 	DHH_$= !		 
l	")@B 	B
"	|D+66$Ed#u%  )CE 	E  MM+6LE5!!$'e|3ZE


U
# 7w 	B 
	r   c                      t         j                  j                  j                  t        j                  t         j                  j                        dd       S )z&Returns the SDK installation root dir.N)r!   pathsepjoin__file__r-   r   r   r   _GetInstallationRootDirra      s5     
		(..5cr:	;;r   c                  T    t         j                  j                  t               dd      S )z/Returns the SDK static completion CLI tree dir.datacli)r!   r]   r_   ra   r   r   r   _GetCompletionCliTreeDirre      s     
-/	??r   c                 Z    t         j                  j                  | xs
 t               d      S )z0Returns the SDK static completion CLI tree path.zgcloud_completions.py)r!   r]   r_   re   )	directorys    r   CompletionCliTreePathrh      s*     
-+-/F
H Hr   c                     	 t         j                  dd } t         j                  j                  t                      ddl}|j
                  }	 | t         _        |S # t        $ r t        d      w xY w#  t         _        w xY w)z1Loads and returns the static completion CLI tree.Nr   z.Cannot find static completion CLI tree module.)sysr]   r/   re   gcloud_completionsSTATIC_COMPLETION_CLI_TREEImportErrorr   )sys_pathrk   trees      r   LoadCompletionCliTreerp      so    	xx{HHHOO,./88D
 CH	+ 
 :
%8: :: CHs   A
A A//A2 2A?c                  6    t        j                  t        d      S )z&Returns the completions output stream.wb)r!   fdopenCOMPLETIONS_OUTPUT_FDr   r   r   _OpenCompletionsOutputStreamru      s    	($	//r   c                  B    t               } t               }t        | |      S )z7Returns the static completions, None if there are none.)rp   r(   rZ   )rQ   r&   s     r   _GetCompletionsrw      s    		 $!(	$	))r   c                  D   t               } | r|t        j                  t        j                  t
        t              }d}	 t               }|j                  |j                  |       j                                |r|j                          yyy# |r|j                          w w xY w)z>Attempts completions and writes them to the completion stream.N)rw   r   r    r!   r"   IFS_ENV_VARIFS_ENV_DEFAULTru   writer_   encodeclose)r>   ifsfs      r   Completer      s    !+

"
"2::{O
LCA
&
(aggchh{#**,-	
		 
  
		 
s   8B
 
B)N))r   
__future__r   r   r   r!   r,   rj   googlecloudsdk.core.utilr   r   rN   r#   r%   ry   rz   rt   rF   rD   rL   rM   rJ   rK   rC   r0   rI   r.   	Exceptionr   r(   r3   r9   rA   rZ   ra   re   rh   rp   ru   rw   r   r   r   r   <module>r      s     C &  ' 	  
 - . 
   


	) 
	(?
,Yx<@H0
*r   