
    s                         S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJ	r	  SSKJ
r
  SS	KJr  S
 r " S S\5      rS rg)z%gcloud search-help command resources.    )absolute_import)division)unicode_literals)cli_tree)lookup)rater)search_util)zipc                 b    [         R                  " USS9n[        X 5      nUR                  5       $ )zRuns search-help by opening and reading help table, finding commands.

Args:
  terms: [str], list of strings that must be found in the command.
  cli: the Calliope CLI object

Returns:
  a list of json objects representing gcloud commands.
T)clione_time_use_ok)r   LoadSearcherSearch)termsr   parentsearchers       4lib/googlecloudsdk/command_lib/help_search/search.py	RunSearchr      s+     ==S$7&f$(		    c                   0    \ rS rSrSrS rS rS rS rSr	g)	r   -   zClass to run help search.c                 P    Xl         X l        [        R                  " 5       U l        g )N)r   r   r   CumulativeRater_rater)selfr   r   s      r   __init__Searcher.__init__0   s    KJ'')DKr   c                 "   U R                  U R                  / 5      nUR                  S SS9  / n/ nU H=  n[        U5      nXTS   4nXc;  d  M  UR	                  U5        UR	                  U5        M?     U R
                  R                  5         U$ )a  Run a search and return a list of processed matching commands.

The search walks the command tree and returns a list of matching commands.
The commands are modified so that child commands in command groups are
replaced with just a list of their names, and include summaries and
"relevance" ratings as well.

Commands match if at least one of the searcher's terms is found in the
command.

Filters out duplicates with lower tracks.

Returns:
  [dict], a list of the matching commands in json form.
c                     U S   $ )Nrelease )es    r   <lambda>!Searcher.Search.<locals>.<lambda>G   s    a	lr   T)keyreverseresults)	_WalkTreer   sort!_GetCommandPathWithoutTrackPrefixappendr   RateAll)r   found_commandsde_duped_commandsunique_results_tracking_listcommandcommand_pathunique_combos          r   r   Searcher.Search5   s      ^^DKK4N2DA#% !6w?l"I$67l		9$++L9  ) " 	KKr   c                     U R                  U5      nU(       a  UR                  U5        UR                  [        R                  0 5      R                  5        H  nU R                  XB5      nM     U$ )a=  Recursively walks command tree, checking for matches.

If a command matches, it is postprocessed and added to found_commands.

Args:
  current_parent: dict, a json representation of a CLI command.
  found_commands: [dict], a list of matching commands.

Returns:
  [dict], a list of commands that have matched so far.
)_PossiblyGetResultr,   getr   COMMANDSvaluesr)   )r   current_parentr.   resultchild_commands        r   r)   Searcher._WalkTreeU   s^     $$^4FF#'++FOOR@GGI~~mDn Jr   c                 Z   U R                    Vs/ s H  n[        R                  " X5      PM     nn[        U5      (       af  [        R                  " [        [        U R                   U5      5      5      n[        R                  " X5      nU R                  R                  XT5        U$ gs  snf )a  Helper function to determine whether a command contains all terms.

Returns a copy of the command or command group with modifications to the
'commands' field and an added 'summary' field if the command matches
the searcher's search terms.

Args:
  command: dict, a json representation of a command.

Returns:
  a modified copy of the command if the command is a result, otherwise None.
N)
r   r	   
LocateTermanyCommandSearchResultsdictr
   ProcessResultr   AddFoundCommand)r   r1   term	locationsr(   new_commands         r   r6   Searcher._PossiblyGetResulth   s     DH::N:4''6:IN
9~~00
s4::y)
*,g--g?k
kk!!+7  Os    B()r   r   r   N)
__name__
__module____qualname____firstlineno____doc__r   r   r)   r6   __static_attributes__r"   r   r   r   r   -   s    !*
@&r   r   c                     SR                  U [        R                      Vs/ s H+  nU[        R                  [        R                  4;  d  M)  UPM-     sn5      $ s  snf )zHelper to get the path of a command without a track prefix.

Args:
  command: dict, json representation of a command.

Returns:
  a ' '-separated string representation of a command path without any
    track prefixes.
 )joinr   PATH
ALPHA_PATH	BETA_PATH)r1   segments     r   r+   r+   ~   s\     
%fkk2 @27
&++V-=-=>
> 2 @
A A@s   (AAN)rM   
__future__r   r   r   googlecloudsdk.callioper   &googlecloudsdk.command_lib.help_searchr   r   r	   	six.movesr
   r   objectr   r+   r"   r   r   <module>r[      s<     , &  ' , 9 8 > Nv NbAr   