
    \O                        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mZ ddlm	Z	 ddlm
Z
 dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlZg dZ G d dej,                        Z G d dej,                        Z G d dej,                        Z G d de      Z G d de      Z G d de      Z G d dej,                        Z G d dej,                        Zy) zA collection of CLI walkers.    )absolute_import)division)unicode_literalsN)actions)arg_parsers)cli_tree)markdown)walker)
properties)render_document)files)pkg_resources)zfavicon.icoz
index.htmlz
_menu_.cssz	_menu_.jsz_title_.htmlc                   :     e Zd ZdZdZdZ	 d fd	Zd Zd Z xZ	S )DevSiteGeneratora  Generates DevSite reference HTML in a directory hierarchy.

  This implements gcloud meta generate-help-docs --manpage-dir=DIRECTORY.

  Attributes:
    _directory: The DevSite reference output directory. _need_section_tag[]:
      _need_section_tag[i] is True if there are section subitems at depth i.
      This prevents the creation of empty 'section:' tags in the '_toc' files.
    _toc_root: The root TOC output stream.
    _toc_main: The current main (just under root) TOC output stream.
  z/sdk/gcloud/referencez	_toc.yamlc                 &   t         t        |   ||       || _        t	        j
                  | j                         g | _        t        j                  j                  | j                  | j                        }t	        j                  |      | _        | j                  j                  d       | j                  j                  d       | j                  j                  d| j                  z         | j                  j                  d       d| _        y)a0  Constructor.

    Args:
      cli: The Cloud SDK CLI object.
      directory: The devsite output directory path name.
      hidden: Boolean indicating whether to consider the hidden CLI.
      progress_callback: f(float), The function to call to update the progress
        bar or None for no progress bar.
      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.
    restricttoc:
z- title: "gcloud Reference"
  path: %s
z  section:
N)superr   __init__
_directoryr   MakeDir_need_section_tagospathjoin_TOC
FileWriter	_toc_rootwrite
_REFERENCE	_toc_main)selfcli	directoryhiddenprogress_callbackr   toc_path	__class__s          *lib/googlecloudsdk/calliope/walker_util.pyr   zDevSiteGenerator.__init__>   s      

D*3*BDO	MM$//"Dww||DOOTYY7H%%h/DNNN"NN89NN$//9:NN(DN    c                    	
 	
 fd}|j                         	rEt        j                  j                   j                  g	dd  
t        j                  
d       n-t        j                  j                   j                  g	dd  
t        j                  j                  
rdn	d         dz   }d}t        j                  j                  j                  j                         r2t        j                  j                  j                  j                         }t        j                  j                  j                  j                  d	       t        j                  |      5 }t        j                   |      }t#        j$                  d
dj                  	      t'        j(                  |      ||       ddd       t        j                  j                  j                  j                  |        |        |S # 1 sw Y   ExY w)aE  Updates the TOC and Renders a DevSite doc for each node in the CLI tree.

    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:
      The parent value, ignored here.
    c            
         t              dz
  } | sydj                        }| t        j                        k\  r4j                  j                  d       | t        j                        k\  r4| dk(  r,rԉj                  rj                  j                          t        j                  j                  j                        }t        j                  |      }|_        |j                  d       |j                  d|z         |j                  ddj                  j                  gdd z         z         d	j                  | <   j                  }d
}r|j                  |ddj                  j                  gdd z   j                  gz         d       yj                  }d
| dz
  z  }j                  | dz
     r&dj                  | dz
  <   |j                  d|z         d   }|j                  |d|d       |j                  |ddj                  j                  gdd z         d       j                  | <   y)zUpdates the DevSIte TOC.   N Fr   z- title: "%s"
r   /Tz  z- include: 
z%ssection:
z
- title: "z"
z  path: )lenr   r   appendr#   closer   r   r   r   r   r!   r"   r    )	depthtitler)   tocindentcommandr&   is_groupr$   s	        r+   
_UpdateTOCz*DevSiteGenerator.Visit.<locals>._UpdateTOCf   s	   'lQehhweS//00%%e, S//00	!^^NN  "WW\\)TYY7(  *#$.
))H

))%-
.
))NSXXt.?'!"+.M%NN
O*.$
 
 
'nn
)) ((DOO,wqr{:dii[HI nn#!!%!),.3$
 
 
+
))NV+
,	iivu56	ii$chh/@712;/N&O
P '/dU#r,   r/   N  moder3   index.htmluniversedevsiter0   )styler8   finoutcommand_node)GetPathr   r   r   r   r   r   r   VALUEScoreuniverse_domainIsExplicitlySetGetSetr   r	   Markdownr   RenderDocumentioStringIO)r$   nodeparentr<   r=   r   rL   fmdr;   r&   s   `  `     @@r+   VisitzDevSiteGenerator.VisitZ   s|   ,/^ llnG'',,t==immIE*'',,t?2?i 	Y8EO 	 O--==?"))..>>BBDo**..z:			$	1T"b$$!kk"o 
  **..?LM 
 	s   )AG??Hc                     | j                   j                          | j                  r| j                  j                          yy)z5Closes the TOC files after the CLI tree walk is done.N)r    r6   r#   )r$   s    r+   DonezDevSiteGenerator.Done   s.    NN~~
nn r,   FNN)
__name__
__module____qualname____doc__r"   r   r   rX   rZ   __classcell__r*   s   @r+   r   r   .   s,    
 '*	$ LP8]~r,   r   c                   ,     e Zd ZdZ	 d fd	Zd Z xZS )HelpTextGeneratorzuGenerates help text files in a directory hierarchy.

  Attributes:
    _directory: The help text output directory.
  c                 ~    t         t        |   |||       || _        t	        j
                  | j                         y)a2  Constructor.

    Args:
      cli: The Cloud SDK CLI object.
      directory: The Help Text output directory path name.
      hidden: Boolean indicating whether to consider the hidden CLI.
      progress_callback: f(float), The function to call to update the progress
        bar or None for no progress bar.
      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.
    )r(   r   N)r   rc   r   r   r   r   r$   r%   r&   r'   r(   r   r*   s         r+   r   zHelpTextGenerator.__init__   s;      

T+08 ,   DO	MM$//"r,   c                 2   |j                         }|r.t        j                  j                  | j                  g|dd  }n-t        j                  j                  | j                  g|dd  }t        j                  |d       t        j                  j                  ||rdn|d         }t        j                  |      5 }t        j                  |      }t        j                  dt        j                  |      |       ddd       |S # 1 sw Y   |S xY w)	a3  Renders a help text doc for each node in the CLI tree.

    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:
      The parent value, ignored here.
    r/   Nr3   r>   r?   GROUPtext)rE   rF   rG   )rI   r   r   r   r   r   r   r   r	   rP   r   rQ   rR   rS   )	r$   rT   rU   r<   r;   r&   r   rV   rW   s	            r+   rX   zHelpTextGenerator.Visit   s     llnG'',,t==i'',,t?2?i	MM)%(77<<	h7GBKHD			$	1T"b$$6r{{2AN 
  M 
  Ms   ADDr[   r\   r]   r^   r_   r   rX   r`   ra   s   @r+   rc   rc      s     LP#,r,   rc   c                   *     e Zd ZdZd fd	Zd Z xZS )DocumentGeneratorzGenerates style manpage files with suffix in an output directory.

  All files will be generated in one directory.

  Attributes:
    _directory: The document output directory.
    _style: The document style.
    _suffix: The output file suffix.
  c                     t         t        |   ||       || _        || _        || _        t        j                  | j                         y)a  Constructor.

    Args:
      cli: The Cloud SDK CLI object.
      directory: The manpage output directory path name.
      style: The document style.
      suffix: The generate document file suffix. None for .<SECTION>.
      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.
    r   N)r   rk   r   r   _style_suffixr   r   )r$   r%   r&   rE   suffixr   r*   s         r+   r   zDocumentGenerator.__init__  s?     

T+C(+CDODKDL	MM$//"r,   c                    | j                   dk(  rt        j                  |      }nd}|j                         }t        j
                  j                  | j                  dj                  |            | j                  z   }t        j                  |      5 }t        j                  |      }t        j                  | j                   dj                  |      t        j                   |      ||       ddd       |S # 1 sw Y   |S xY w)a1  Renders document file for each node in the CLI tree.

    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:
      The parent value, ignored here.
    linterN_r0   )rE   r8   rF   rG   command_metadata)rm   r   GetCommandMetaDatarI   r   r   r   r   rn   r   r   r	   rP   r   rQ   rR   rS   )	r$   rT   rU   r<   	meta_datar;   r   rV   rW   s	            r+   rX   zDocumentGenerator.Visit  s     {{h,,T2iillnG77<<'):;dllJD			$	1T"b$$!kk"o$ 
  M 
  Ms   AC99D)Nri   ra   s   @r+   rk   rk      s    #&r,   rk   c                   2     e Zd ZdZd Zd Z	 d fd	Z xZS )HtmlGeneratorzGenerates HTML manpage files with suffix .html in an output directory.

  The output directory will contain a man1 subdirectory containing all of the
  HTML manpage files.
  c                 t    d fdj                  d        |g        j                  d       y)zWrites the command menu tree HTML on out.

    Args:
      command: dict, The tree (nested dict) of command/group names.
      out: stream, The output stream.
    c                 $    dj                  |       S )Nrr   )r   )r   s    r+   ConvertPathToIdentifierz<HtmlGenerator.WriteHtmlMenu.<locals>.ConvertPathToIdentifierD  s    XXd^r,   c           
      (   t        |      }|dk  rdnd}|dz  dz   }| j                  d      }||gz   }j                  dj                  d|z  || 	|                   | j                  d	g       | j                  d
g       z   }|ryj                  d       t	        |d       D ]  } 
||        j                  dj                  d|dz   z               j                  dj                  d|z               yj                  d       y)zVisit each command and group in the CLI command tree.

      Args:
        command: dict, The tree (nested dict) of command/group names.
        prefix: [str], The subcommand arg prefix.
      r/   visibler'      _name_zT{indent}<li class="{visibility}" id="{item}" onclick="select(event, this.id)">{name}r0   )r:   
visibilitynameitemcommandsgroupsz<ul>
c                     | d   S )Nr~    )xs    r+   <lambda>zFHtmlGenerator.WriteHtmlMenu.<locals>.WalkCommandTree.<locals>.<lambda>_  s    (r,   )keyz{indent}</ul>
)r:   z{indent}</li>
z</li>
N)r4   getr!   formatsorted)r;   prefixlevelr   r:   r   argsr   crz   WalkCommandTreerG   s            r+   r   z4HtmlGenerator.WriteHtmlMenu.<locals>.WalkCommandTreeG  s    &ke %
9jqy1}f[["dtf_d	ii44:F6\#*40	 5; 5 Z,w{{8R/HHh			(&;<A
!T
" =		#**#!2D*EF		#**#,*?@		)r,   a  <html>
<head>
<meta name="description" content="man page tree navigation">
<meta name="generator" content="gcloud meta generate-help-docs --html-dir=.">
<title> man page tree navigation </title>
<base href="." target="_blank">
<link rel="stylesheet" type="text/css" href="_menu_.css">
<script type="text/javascript" src="_menu_.js"></script>
</head>
<body>

<div class="menu">
 <ul>
z </ul>
</div>

</body>
</html>
N)r!   )r$   r;   rG   rz   r   s     `@@r+   WriteHtmlMenuzHtmlGenerator.WriteHtmlMenu<  s=    > II   GR II  r,   c                    t        |      j                  ||      }t        j                  t        j
                  j                  |d            5 }| j                  ||       ddd       t        D ]L  }t        j                  d|      }t        j                  t        j
                  j                  ||      |       N y# 1 sw Y   _xY w)z&Generates html nav files in directory.z_menu_.htmlNz+googlecloudsdk.api_lib.meta.help_html_data.)CommandTreeGeneratorWalkr   r   r   r   r   r   _HELP_HTML_DATA_FILESr   GetResourceWriteBinaryFileContents)	r$   r&   r%   r'   r   treerG   	file_namefile_contentss	            r+   _GenerateHtmlNavzHtmlGenerator._GenerateHtmlNav~  s    $))&(;D			"'',,y-@	AS
s# 
B*	#//
7m ##
'',,y)
,m	 + 
B	As   C  C	c                 ^    t         t        |   ||dd|       | j                  ||||       y)a-  Constructor.

    Args:
      cli: The Cloud SDK CLI object.
      directory: The HTML output directory path name.
      hidden: Boolean indicating whether to consider the hidden CLI.
      progress_callback: f(float), The function to call to update the progress
        bar or None for no progress bar.
      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.
    htmlrB   r&   rE   ro   r   N)r   rw   r   r   re   s         r+   r   zHtmlGenerator.__init__  s>      
-' (  	)S&(;r,   r[   )r\   r]   r^   r_   r   r   r   r`   ra   s   @r+   rw   rw   5  s%    @D LP< <r,   rw   c                   *     e Zd ZdZdZ	 d fd	Z xZS )ManPageGeneratorzGenerates manpage files with suffix .1 in an output directory.

  The output directory will contain a man1 subdirectory containing all of the
  manpage files.
  zman{section}c                     | j                   j                  d      }t        j                  j	                  ||      }t
        t        |   ||dd|       y)a0  Constructor.

    Args:
      cli: The Cloud SDK CLI object.
      directory: The manpage output directory path name.
      hidden: Boolean indicating whether to consider the hidden CLI.
      progress_callback: f(float), The function to call to update the progress
        bar or None for no progress bar.
      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.
    r/   )sectionmanz.1r   N)_SECTION_FORMATr   r   r   r   r   r   r   )	r$   r%   r&   r'   r(   r   section_subdirsection_dirr*   s	           r+   r   zManPageGenerator.__init__  sR    $ ))000;N'',,y.9K	
D*{%x + r,   r[   )r\   r]   r^   r_   r   r   r`   ra   s   @r+   r   r     s      #/ LP r,   r   c                   &     e Zd ZdZ	 d fd	Z xZS )LinterGeneratorz@Generates linter files with suffix .json in an output directory.c                 6    t         t        |   ||dd|       y)a/  Constructor.

    Args:
      cli: The Cloud SDK CLI object.
      directory: The linter output directory path name.
      hidden: Boolean indicating whether to consider the hidden CLI.
      progress_callback: f(float), The function to call to update the progress
        bar or None for no progress bar.
      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.
    rq   z.jsonr   N)r   r   r   re   s         r+   r   zLinterGenerator.__init__  s(    " 
/4) * r,   r[   )r\   r]   r^   r_   r   r`   ra   s   @r+   r   r     s    H LP r,   r   c                   *     e Zd ZdZd fd	Zd Z xZS )r   a_  Constructs a CLI command dict tree.

  This implements the resource generator for gcloud meta list-commands.

  Attributes:
    _with_flags: Include the non-global flags for each command/group if True.
    _with_flag_values: Include flag value choices or :type: if True.
    _global_flags: The set of global flags, only listed for the root command.
  c                 p    t        t        | 
  |fi | |xs || _        || _        t               | _        y)a'  Constructor.

    Args:
      cli: The Cloud SDK CLI object.
      with_flags: Include the non-global flags for each command/group if True.
      with_flag_values: Include flags and flag value choices or :type: if True.
      **kwargs: Other keyword arguments to pass to Walker constructor.
    N)r   r   r   _with_flags_with_flag_valuesset_global_flags)r$   r%   
with_flagswith_flag_valueskwargsr*   s        r+   r   zCommandTreeGenerator.__init__  s:     

.s=f=!5%5D-DDr,   c           	         |j                   j                  dd      }d|i}| j                  rg }|j                         D ]`  }d}| j                  r"|j
                  rOt        |j
                        }	|	ddgk7  rdj                  |	D 
cg c]  }
t        j                  |
       c}
      }nt        |j                  t              rd}nt        |j                  t              rd	}nt        |j                  t        j                        rd
}nft        |j                  t        j                         rd}n?|j"                  dk7  r0|j$                  xs |j&                  j)                         }d|z   dz   }|j*                  D ]  }|r|d|z   z  }|j-                  |        c d}g }|D ]I  }|| j.                  v r|j1                  |      rd|t3        |      d z   }||v r9|j-                  |       K |r5t        |      |d<   | j.                  s| j.                  j5                  |       |rK|rGt6        j8                  |vrg |t6        j8                  <   |t6        j8                     j-                  |       |S t6        j:                  |vrg |t6        j:                  <   |t6        j:                     j-                  |       yc c}
w )a_  Visits each node in the CLI command tree to construct the dict tree.

    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:
      The subtree parent value, used here to construct a dict tree.
    rr   -r~   Nfalsetrue,z:int:z:float:z:dict:z:list:r   :=z--no-z--_flags_)r   replacer   GetAllAvailableFlagsr   choicesr   r   six	text_type
isinstancetypeintfloatr   ArgDictArgListnargsmetavardestupperoption_stringsr5   r   
startswithr4   updater   LOOKUP_GROUPSLOOKUP_COMMANDS)r$   rT   rU   r<   r   info	all_flagsargvaluer   choicer   rV   	no_prefixflagsflagpositives                    r+   rX   zCommandTreeGenerator.Visit  sh    99S#&DdDi**,#!![[S[[)G7F++hhGLG&f 5GLMe#((C(E#((E*E#((K$7$78E#((K$7$78EyyA~kk5SXX^^%5G'MC'E##AuA


1
 $% -, ie$4%%%
??9%DY!122("T  
 -Y!! 


#
#E
*	!!/+-&''
(x%%&--d3kv-)+fX%%&
8##$++D1Y  Ms   
K
)FFri   ra   s   @r+   r   r     s    Ar,   r   c                       e Zd ZdZd Zy)GCloudTreeGeneratorzGenerates an external representation of the gcloud CLI tree.

  This implements the resource generator for gcloud meta list-gcloud.
  c                 .    t        j                  ||      S )ak  Visits each node in the CLI command tree to construct the external rep.

    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:
      The subtree parent value, used here to construct an external rep node.
    )r   Command)r$   rT   rU   r<   s       r+   rX   zGCloudTreeGenerator.VisitG  s     D&))r,   N)r\   r]   r^   r_   rX   r   r,   r+   r   r   A  s    
*r,   r   )r_   
__future__r   r   r   rR   r   googlecloudsdk.callioper   r   r   r	   r
   googlecloudsdk.corer   &googlecloudsdk.core.document_renderersr   googlecloudsdk.core.utilr   r   r   r   Walkerr   rc   rk   rw   r   r   r   r   r   r,   r+   <module>r      s     # &  ' 	 	 + / , , * * B * 2 
 Ov}} Od6 6r9 9xm<% m<`( D' :Z6== Zz*&-- *r,   