
                         n    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
r
 " S	 S
\5      rg)z,A module for walking the Cloud SDK CLI tree.    )absolute_import)division)unicode_literals)Any)
console_io)progress_trackerNc                   F    \ rS rSrSr SS jrS rSS jrS rS r	S	 r
S
rg)Walker   a1  Base class for walking the Cloud SDK CLI tree.

Attributes:
  _roots: The root elements of the CLI tree that will be walked.
  _num_nodes: The total number of nodes in the tree.
  _num_visited: The count of visited nodes so far.
  _progress_callback: The progress bar function to call to update progress.
Nc           
         UR                  5       nU(       aA  U Vs/ s H  o`R                  XV5      PM     nnU Vs/ s H  of(       d  M  UPM     snU l        OU/U l        SU l        U(       aY  [        R
                  " S5         U R                   H)  nU =R                  SUR                  SUS9-   -  sl        M+     SSS5        O9U R                   H)  nU =R                  SUR                  SUS9-   -  sl        M+     SU l        U=(       d    [        R                  U l
        gs  snf s  snf ! , (       d  f       N>= f)a  Constructor.

Args:
  cli: The Cloud SDK CLI object.
  progress_callback: f(float), The function to call to update the progress
    bar or None for no progress bar.
  ignore_load_errors: bool, True to ignore command load failures. This
    should only be used when it is not critical that all data is returned,
    like for optimizations like static tab completion.
  restrict: Restricts the walk to the command/group dotted paths in this
    list. For example, restrict=['gcloud.alpha.test', 'gcloud.topic']
    restricts the walk to the 'gcloud topic' and 'gcloud alpha test'
    commands/groups. When provided here, any groups above the restrictions
    in the tree will not be loaded or visited.
r   zLoading CLI Treeg      ?T)	recursiveignore_load_errorsN)_TopElement_GetSubElement_roots
_num_nodesr   ProgressTrackerLoadAllSubElements_num_visitedr   DefaultProgressBarCallback_progress_callback)	selfcliprogress_callbackr   restricttoprrootsroots	            %lib/googlecloudsdk/calliope/walker.py__init__Walker.__init__'   s   $ //
C4<=Hq""3*He= %+1Q+dkEdkDO++,>?KKD
//S4#:#:1C $; $  /   @? ++$3!8!8/A "9 "
 
 	
 
 DBZBB 	' >+ @?s   D'
D,
D,:D11
D?c                 x    UR                  S5      SS  nUnU H  nUR                  U5      nU(       a  M    g    U$ )N.   )splitLoadSubElement)r   top_elementpathpartscurrentparts         r    r   Walker._GetSubElementR   sD    JJsOABEG&&t,gW  N    c                    ^ ^^^^^^ S[         S[        4S jmSUUUU4S jjmUUU 4S jmST l        SnT R                   H  nT" US5      nM     T R	                  5         U$ )	a  Calls self.Visit() on each node in the CLI tree.

The walk is DFS, ordered by command name for reproducability.

Args:
  hidden: Include hidden groups and commands if True.
  universe_compatible: Exclusively include commands which are marked
    universe compatible.
  restrict: Restricts the walk to the command/group dotted paths in this
    list. For example, restrict=['gcloud.alpha.test', 'gcloud.topic']
    restricts the walk to the 'gcloud topic' and 'gcloud alpha test'
    commands/groups. When provided here, parent groups will still be visited
    as the walk progresses down to these leaves, but only parent groups
    between the restrictions and the root.

Returns:
  The return value of the top level Visit() call.
commandreturnc                 Z    [        U [        5      (       + =(       a    U R                  5       $ )zDetermines if a command is universe compatible.

Args:
  command: CommandCommon command node.

Returns:
  True if command is universe compatible.
)
isinstancedictIsUniverseCompatible)r0   s    r    _IsUniverseCompatible*Walker.Walk.<locals>._IsUniverseCompatibleo   s"     GT**O0L0L0NOr.   c                 <  > T(       d  U R                  5       (       a  gT(       a  T" U 5      (       d  gT(       d  gSR                  U R                  5       5      nT H<  nUR                  U5      (       a    gU(       d  M$  UR                  U5      (       d  M<    g   g)zDetermines if command should be included in the walk.

Args:
  command: CommandCommon command node.
  traverse: If True then check traversal through group to subcommands.

Returns:
  True if command should be included in the walk.
FTr$   )IsHiddenjoinGetPath
startswith)r0   traverser)   itemr6   hiddenr   universe_compatibles       r    _IncludeWalker.Walk.<locals>._Includez   s|     ((**	%:7%C%CXXgoo'(d$??4  8--	 
 r.   c                 L  > U R                   (       d  T	R                  XSS9  U$ T	R                  XSS9n/ nU R                  (       aK  [        R                  " U R                  5       H'  u  p4T" U5      (       d  M  UR                  X4S45        M)     U R                  (       aJ  [        R                  " U R                  5       H&  u  p4T" USS9(       d  M  UR                  X4S45        M(     [        U5       H'  u  pTnU(       a
  T" XA5        M  T	R                  XASS9  M)     U$ )zWalk() helper that calls self.Visit() on each node in the CLI tree.

Args:
  node: CommandCommon tree node.
  parent: The parent Visit() return value, None at the top level.

Returns:
  The return value of the outer Visit() call.
F)is_groupT)r=   )rD   _Visitcommandssix	iteritemsappendgroupssorted)
nodeparentcommands_and_groupsnamer0   _rD   rA   _Walkr   s
          r    rQ   Walker.Walk.<locals>._Walk   s     ]]D51{{4${7f	 ]]4==9MDg&&u'=> : 
 ]]4;;7MDg--&&t'<= 8 #))<"=
!h

 
++g+
6	 #>
 mr.   r   N)F)r   boolr   r   Done)	r   r?   r@   r   rM   r   rA   r6   rQ   s	   ````  @@@r    WalkWalker.Walk[   s`    (	Ps 	Pt 	P 0@ DFT4 f IIKMr.   c                     U =R                   S-  sl         U R                  U R                   U R                  -  5        U R                  XU5      $ )Nr%   )r   r   r   Visitr   rL   rM   rD   s       r    rE   Walker._Visit   sA    D--@A::dH--r.   c                     g)a  Visits each node in the CLI command tree.

Called preorder by WalkCLI() using DFS.

Args:
  node: group/command CommandCommon info.
  parent: The parent Visit() return value, None at the top level.
  is_group: True if node is a group, otherwise its is a command.

Returns:
  A new parent value for the node subtree. This value is the parent arg
  for the Visit() calls for the children of this node.
N rY   s       r    rX   Walker.Visit   s     	r.   c                     g)z<Cleans up after all nodes in the CLI tree have been visited.Nr\   )r   s    r    rT   Walker.Done   s    r.   )r   r   r   r   )NFN)FFN)__name__
__module____qualname____firstlineno____doc__r!   r   rU   rE   rX   rT   __static_attributes__r\   r.   r    r
   r
      s/     MQ)V\|.
	 	r.   r
   )rd   
__future__r   r   r   typingr   googlecloudsdk.core.consoler   r   rG   objectr
   r\   r.   r    <module>rj      s-     3 &  '  2 8 
s	V s	r.   