
    s$                         d 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ej                  rdZdZdZndZdZdZd Zd Zd Zd Z G d de	j,                        Zd Zy)a<  The cli_tree command help document markdown generator.

This module generates command help markdown from the tree generated by:

  gcloud --quiet alpha  # make sure the alpha component is installed
  gcloud --quiet beta   # make sure the beta component is installed
  gcloud meta list-gcloud --format=json |
  python -c "
    import json
    import sys
    data = json.load(sys.stdin)
    print 'gcloud_tree =', data" > gcloud_tree.py

Usage:

  from googlecloudsdk.calliope import cli_tree_markdown
  from googlecloudsdk.command_lib.shell import gcloud_tree

  command = <command node in gcloud tree>
  flag = <flag node in gcloud tree>
  generator = cli_tree_markdown.CliTreeMarkdownGenerator(command, gcloud_tree)
  generator.PrintSynopsisSection()
  generator.PrintFlagDefinition(flag)
    ...
  markdown = generator.Edit()
    )absolute_import)division)unicode_literals)arg_parsers)base)cli_tree)markdown)
usage_text)
propertiesNs   flags
   positionals   groupflag
positionalgroupc                 N    | dk(  rd} t         j                  j                  |       S )z-Returns the base.ReleaseTrack for release_id.INTERNALGA)r   ReleaseTrackFromId)
release_ids    0lib/googlecloudsdk/calliope/cli_tree_markdown.py_GetReleaseTrackFromIdr   B   s&    :J				!	!*	--    c                    t        t        t        f|       }d|_        | j	                  t
        j                  | j	                  dd            |_        |j                  |_        d|_	        | j	                  t
        j                  | j	                  t
        j                  d            |_        |j                  |_        |j                  |_        |j                   j#                         j%                  dd      |_        |j(                  |_        |j                   g|_        t/        |d      sd|_        |j                   dk(  rd|_        n|j2                  d	vrd
|_        |j                   dk(  rt5        j6                         |_         n?|j                   dk(  rt5        j8                         |_         n|j                   dk(  rd|_         |j:                  j	                  t
        j<                        rd|_        |j:                  j	                  d      }|rkt
        j@                  |v rd}|t
        j@                     }nd}|j                   dk(  rdnd}tC        jD                  |t
        jF                           ||f|_$        |S )z@Returns a flag object suitable for the calliope.markdown module.Fhidden-_defaultNboolr   )?*+   dictliststringTpropertyvalue)%typeFLAG_TYPE_NAMEobjectis_groupgetr   LOOKUP_IS_HIDDEN	is_hiddenr   is_positionalLOOKUP_IS_REQUIREDLOOKUP_REQUIREDis_requiredrequireddescriptionhelpnamelowerreplacedestr&   metavaroption_stringshasattrr   nargsr   ArgDictArgListattrLOOKUP_INVERTED_SYNOPSISinverted_synopsisLOOKUP_VALUEr   
FromStringLOOKUP_NAMEstore_property)dr   propkindr&   s        r   FlagrI   I   s   	nvi	+$$-5522AEE(E4JK$.$+$UU!!155)A)A5#I$ ""$-$)iioo''S1$)$,$	y	!DL	YY&DJzz(DJ	YY&##%DIyyF##%DIyyHDI	YY]]8445!D	z	"$	$d8(()eeyyF*Vdd8#7#789D 
+r   c                    t        t        t        f|       }|j                  |_        d|_        d|_        d|_        |j                  dk7  |_	        |j                  j                         j                  dd      |_        |j                  |_        g |_        	 t!        |j                        |_        |S # t"        $ r Y |S w xY w)zFReturns a positional object suitable for the calliope.markdown module.FTr   r   r   )r'   POSITIONAL_TYPE_NAMEr)   r3   r4   r*   r-   r.   r<   r1   r&   r6   r7   r8   r9   r:   int
ValueError)rF   r   s     r   
PositionalrN   y   s    (6)Q7****/**!*%++s2*$$**,44S#>*/!''* *	:++,J 
 
 			s   B5 5	CCc                    | j                  t        j                  d      rt        |       S | j                  t        j                  d      st        |       S t        t        t        f|       }| j                  t        j                  g       D cg c]  }t        |       c}|_        d|_        |j                  |_        d|_        d|_        d|_        d|_        |S c c}w )zEReturns an argument object suitable for the calliope.markdown module.FNT)r+   r   LOOKUP_IS_POSITIONALrN   LOOKUP_IS_GROUPrI   r'   GROUP_TYPE_NAMEr)   LOOKUP_ARGUMENTSArgument	argumentscategoryr3   r4   	is_globalr-   	sort_argsdisable_default_heading)rF   r   as      r   rT   rT      s    UU8((%0a=	
x''	/7N
	1
-%*+%%0I0I2*NO*NQXa[*NO%/%.  %*%/%/%/"'%	, Ps   Cc                   b     e Zd ZdZ fdZd Zd Zd ZddZddZ	d Z
d	 Zd fd
	Zd Z xZS )CliTreeMarkdownGeneratora  cli_tree command help markdown document generator.

  Attributes:
    _capsule: The help text capsule.
    _command: The tree node for command.
    _command_path: The command path list.
    _tree: The (sub)tree root.
    _sections: The help text sections indexed by SECTION name.
    _subcommands: The dict of subcommand help indexed by subcommand name.
    _subgroups: The dict of subgroup help indexed by subcommand name.
  c                 F   || _         || _        |t        j                     | _        t
        t        |   | j                  t        | j                  t        j                           | j                  j                  t        j                  | j                  j                  dd                   | j                  t        j                     | _        | j                  t        j                     | _        | j!                         | _        | j%                         | _        d| _        y)znConstructor.

    Args:
      command: The command node in the root tree.
      tree: The (sub)tree root.
    r   FTN)_tree_commandr   LOOKUP_PATH_command_pathsuperr\   __init__r   LOOKUP_RELEASEr+   r,   LOOKUP_CAPSULE_capsuleLOOKUP_SECTIONS	_sectionsGetSubCommandHelp_subcommandsGetSubGroupHelp
_subgroups_sort_top_level_args)selfcommandtree	__class__s      r   rc   z!CliTreeMarkdownGenerator.__init__   s     DJDM !5!56D	
"D2t}}X-D-DEF%%t}}'8'85'I	
 MM("9"9:DM]]8#;#;<DN..0D**,DO $Dr   c                     | j                   t        j                     }|r|dd |k7  ry|dd }| j                   }|D ]   }|t        j                     }||vr y||   }" |S )z*Returns the command node for command_path.Nr!   )r^   r   r`   LOOKUP_COMMANDS)rn   command_pathpathro   r5   commandss         r   _GetCommandFromPathz,CliTreeMarkdownGenerator._GetCommandFromPath   su    ::h**+D	bq	T	!!!"%ljjG112h	X	g	 
 Nr   c                 L    | j                  t        j                  g|z         duS )z>Returns True if the given command path after the top is valid.N)rw   r   DEFAULT_CLI_NAME)rn   rt   s     r   IsValidSubPathz'CliTreeMarkdownGenerator.IsValidSubPath   s-     	  (";";!<|!KL	r   c                     | j                  | j                        }	 |t        j                     t        j                     D cg c]  }t        |       c}S c c}w # t        t        f$ r g cY S w xY w)zReturns the command arguments.)rw   ra   r   LOOKUP_CONSTRAINTSrS   rT   KeyError	TypeError)rn   ro   rZ   s      r   GetArgumentsz%CliTreeMarkdownGenerator.GetArguments   s    &&t'9'9:G 8667''a 1+   i  is#   &A AA A A10A1c                     |j                   S )a  Returns the help text with auto-generated details for arg.

    The help text was already generated on the cli_tree generation side.

    Args:
      arg: The arg to auto-generate help text for.
      depth: The indentation depth at which the details should be printed. Added
        here only to maintain consistency with superclass during testing.

    Returns:
      The help text with auto-generated details for arg.
    )r4   )rn   argdepths      r   GetArgDetailsz&CliTreeMarkdownGenerator.GetArgDetails   s     88Or   c                    t        j                  | j                  t        j                           D ci c]  \  }}|t        j
                     |k(  rr|t        j                  |t        j                     |j                  t        j                  |j                  dd            t        |t        j                                  c}}S c c}}w )zDReturns the help dict indexed by command for sub commands or groups.r   F)	help_textr-   release_track)six	iteritemsr_   r   rs   rQ   r
   HelpInfore   r+   r,   r   rd   )rn   r*   r5   
subcommands       r   _GetSubHelpz$CliTreeMarkdownGenerator._GetSubHelp   s     !$MM(223!
!
D* h../8; 	j!! !8!89 nn)):>>(E+J 18223
 	
!
  s   BC
c                 &    | j                  d      S )z7Returns the subcommand help dict indexed by subcommand.Fr*   r   rn   s    r   ri   z*CliTreeMarkdownGenerator.GetSubCommandHelp  s    U++r   c                 &    | j                  d      S )z3Returns the subgroup help dict indexed by subgroup.Tr   r   r   s    r   rk   z(CliTreeMarkdownGenerator.GetSubGroupHelp
  s    T**r   c                 f    t        |t              rt        |      }t        t        |   ||       y)z$Prints a flags definition list item.)disable_headerN)
isinstancer"   rI   rb   r\   PrintFlagDefinition)rn   r   r   rq   s      r   r   z,CliTreeMarkdownGenerator.PrintFlagDefinition  s0    $$Zd	
"D=^ > r   c                     |S )z7{...} references were done when the tree was generated. )rn   docs     r   _ExpandHelpTextz(CliTreeMarkdownGenerator._ExpandHelpText  s    Jr   )N)F)__name__
__module____qualname____doc__rc   rw   rz   r   r   r   ri   rk   r   r   __classcell__)rq   s   @r   r\   r\      s:    
%."$,+r   r\   c                 6    t        | |      j                         S )zReturns the help markdown document string for the command node in tree.

  Args:
    command: The command node in the root tree.
    tree: The (sub)tree root.

  Returns:
    The markdown document string.
  )r\   Generate)ro   rp   s     r   Markdownr     s     
"'4	0	9	9	;;r   )r   
__future__r   r   r   googlecloudsdk.callioper   r   r   r	   r
   googlecloudsdk.corer   r   PY2r(   rK   rR   r   rI   rN   rT   MarkdownGeneratorr\   r   r   r   r   <module>r      s    6 '  ' / ( , , . * 
 77.&/.%/.-`$"|x99 |~
<r   