
    *                         S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKJ	r	  SSK
Jr  SSKJr  SSKrS	S
SSSSSSSSSS	S	SSS.rSrSrSrSrS rS rS rS rS rS rg) z4Methods for suggesting corrections to command typos.    )absolute_import)division)unicode_literalsN)lookup)log)filesupdatescpaddremovegetzauth-configure-dockerdescribeimages	instancesinstancecreatedelete)changez
copy-filesr   r   r   dockerr   imager   r   makemodifypatchr   showgffffff?   
   d   c                 Z   [         R                  " [        5      nU (       d  U$ Sn[        [        R
                  " U 5      SS9SS nU H  n[        R                  R                  X5      n[        R                  " U5       nU HS  n[        R                  " [        R                  U5      nU(       d  M1  UR                  S5      n	US-  nX==   S-  ss'   MU     SSS5        M     [        R                   " U5       V	V
s0 s H
  u  pXU-  _M     sn
n	$ ! , (       d  f       M  = fs  sn
n	f )zLoad the last 100 surfaces user used today from local command history.

Args:
  logs_dir: str, the path to today's logs directory

Returns:
  dict mapping surfaces to normalized frequencies.
r   T)reverseNr      )collectionsdefaultdictintsortedoslistdirpathjoinr   
FileReaderresearchr   USED_SURFACE_PATTERNgroupsix	iteritems)logs_dirsurfaces_counttotallast_100_invocationsfilename	file_pathlog_filelinematchsurfacecounts              /lib/googlecloudsdk/calliope/suggest_commands.py_GetSurfaceHistoryFrequenciesr<   ;   s     **3/.	
%

8 4dCDSI&hX0I			)	$$		#22D95KKN'
1*%

!Q
&
!  
%	$ ' 47==3P
3Pgu}3P
  
%	$
s   0D5&D D'
D$	c                 0   U R                  [        R                  5      (       d  UR                  USS 5        g[        R
                  " U [        R                     5       H2  u  p4UR                  U5        [        XAU5        UR                  5         M4     g)aT  Helper method to _GetCanonicalCommands.

Args:
  tree: The root of the tree that will be traversed to find commands.
  results: The results list to append to.
  prefix: [str], the canonical command line words so far. Once we reach a leaf
    node, prefix contains a canonical command and a copy is appended to
    results.

Returns:
  None
N)r   r   LOOKUP_COMMANDSappendr.   r/   _GetCanonicalCommandsHelperpop)treeresultsprefixcommandcommand_trees        r;   r@   r@   X   sm     
&((	)	)NN6!9
"}}T&2H2H-IJg
MM'v>
JJL  K    c                     / n[        X/ S9  U$ )a  Return list of all canonical commands in CLI tree in arbitrary order.

Args:
  tree: The root of the tree that will be traversed to find commands.

Returns:
  [[canonical_command_words]]: List of lists, all possible sequences of
    canonical command words in the tree.
)rD   )r@   )rB   rC   s     r;   _GetCanonicalCommandsrI   n   s     'dB7	.rG   c                    SnX;   a  UnUnOX!;   a  UnUnOU$ UR                  U5      nUS:  a  XgS-
     S:w  a  U$ US-  nUS:X  a  US-  nOUS:X  a  US-  nOUS:X  a  US-  nU S:X  a  US	-  nOU S:X  a  US
-  nOUS-  n[        U5      [        U5      -
  nUS::  a$  SU-
  nUR                  U5      (       a  US-  nXH-  nU S:X  a  US:  a  US-  nU$ U S:  a  X0S-   :  a  US
-  nU$ )ab  Returns the integer word match score for a command word.

Args:
  index: The position of the word in the command.
  normalized_command_word: The normalized command word.
  canonical_command_word: The actual command word to compare with.
  canonical_command_length: The length of the actual command.

Returns:
  The integer word match score, always >= 0.
r   r    -r                        )findlen
startswith)	indexnormalized_command_wordcanonical_command_wordcanonical_command_lengthscoreshorter_wordlonger_wordhitextras	            r;   
_WordScorer_   }   sP   " % 6*L(K8)L)KL 	&#1W1W%,L 2+% "	RKE1$	RKE1$	RKE aZ	RKEz	RKE	QJE k
S.
.%
aZIEl++qje	NE aZ,q0	RKE
 
, qy-	9	RKE	,rG   c           	         [         R                  " 5       n[        [        R                  " 5       5      nU  Vs/ s H"  o3R                  5       R                  SS5      PM$     nn/ n[        U5      n[        [        [        U5      5      nU GHg  n[        U5      n	[        5       n
Sn[        U5       Hl  u  pU Ha  n[        UUUU	5      n[        R                  U5      nU(       a  [        UUUU	5      nUU:  a  UnU(       d  ML  U
R!                  U5        X-  nMc     Mn     [        U
5      [        U5      :X  a  US-  nUS:  d  M  SR#                  USS 5      nUU;   a  U[%        UU   [&        -  5      -  nSnS	US   :X  a1  US
-  n[        USS 5      U;   a  Sn[        S/USS -   5      U;   a  SnSUS   :X  a  US
-  n[        USS 5      U;   a  SnU(       a  GMU  UR)                  X45        GMj     UR+                  S S9  U$ s  snf )a  Return scored canonical commands containing input command words.

Args:
  command_words: List of input command words.

Returns:
  [(canonical_command_words, score)]: List of tuples, where
    canonical_command_words is a list of strings and score is an integer > 0.
    The tuples are sorted from highest score to lowest, and commands with
    the same score appear in lexicographic order.
_rK   r   r   .NFalpharR   r    Tbetac                     U S   * U S   4$ )Nr    r    )tuples    r;   <lambda>._GetScoredCommandsContaining.<locals>.<lambda>  s    58)U1X)>rG   )key)r   LoadCompletionCliTreer<   r   	GetLogDirlowerreplacerI   setmaprh   rT   	enumerater_   SYNONYMSr   r   r(   r#   FREQUENCY_FACTORr?   sort)command_wordsrootsurface_historycommand_wordnormalized_command_wordsscored_commandsall_canonical_commandscanonical_command_setcanonical_command_wordsrY   matchedrZ   rV   rX   rW   	incrementalternate_command_wordalternate_incrementr9   better_track_existss                       r;   _GetScoredCommandsContainingr      sP    
	%	%	'$1#--/B/ANAN""3,   /06c%)?@A!7"#:;eGE)23J)K%%=
!#"$	
	 "*.E!F! *$$&	!
 ,,+I9
++-
.

%) &> *L0 7|s344rkeqy0"56g	O	#_W-0@@AA "	+A.	.
(,-1FF $
6(4QR889$% !%
	*1-	-
(,-1FF $
   7?@k "8p >?	s   )H	c           	      B   / n [        U 5      nU(       d  U$ [        US   S   5      nSn[	        5       nU Hq  u  pgXs-  [
        :  d  M  UR                  SR                  S/U-   5      5        UR                  SR                  USS 5      5        [        U5      [        :  d  Mo  Sn  O   U(       a  [        U5      [        :  a  [        US   S   5      nU H/  u  pgXs-  [
        :  a    O U[        U5      :  d  M$  [        U5      nM1     US-
  n	U	(       aj  [	        5       nU HO  u  pgXs-  [
        :  a    O@UR                  SR                  S/USU	 -   5      5        [        U5      [        :  d  MO    O   [        U5      nU$ ! [        R                   a    Sn GNf = f)	a  Return suggested commands containing input command words.

Args:
  command_words: List of input command words.

Returns:
  [command]: A list of canonical command strings with 'gcloud' prepended. Only
    commands whose scores have a ratio of at least MIN_RATIO against the top
    score are returned. At most MAX_SUGGESTIONS command strings are returned.
    If many commands from the same group are being suggested, then the common
    groups are suggested instead.
Nr   r    F gcloudrc   T)r   r   CannotHandleCompletionErrorfloatrp   	MIN_RATIOr?   r(   r   rT   MAX_SUGGESTIONSMIN_SUGGESTED_GROUPSr$   )
rv   suggested_commandsr{   	top_scoretoo_manysuggested_groupsrE   rZ   
min_lengthcommon_lengths
             r;   GetCommandSuggestionsr     s    2=AO 
 OA&q)*)(U'ngI%(g)= >?388GCRL12		 O	3 ( #&'*>>_Q'*+J)		Y	&	c'l	"\
	 *
 NM+.'y(
SXXxj7>M3J&JKL O3
 , ""23	K 
	+	+ Os   F FF)__doc__
__future__r   r   r   r!   r%   r*   ,googlecloudsdk.command_lib.static_completionr   googlecloudsdk.corer   googlecloudsdk.core.utilr   r.   rs   r   r   r   rt   r<   r@   rI   r_   r   r   rg   rG   r;   <module>r      s     ; &  '  	 	 ? # * 

 %$ 	   :,EPM`5rG   