
    >                         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Zdd	lmZ d
 Z ed      \  ZZZZZd Zd Zd Zd Zd Zd Zd Zd Zd Zy)zCommon resource topic text.    )absolute_import)division)unicode_literalsN)api_lib)resource_printer)resource_transform)rangec           	         d}ddddd}| |vr4t        dj                  dj                  t        |            | 	            i }t	        j
                  |      D ]5  \  }}|| k(  rd
j                  |      ||<   !dj                  |      ||<   7 t        j                  |      j                  |d   |d   |d   |d         S )a\  Generates resource help DESCRIPTION help text for name.

  This puts common text for the key, formats and projections topics in one
  place.

  Args:
    name: One of ['filter', 'format', 'key', 'projection'].

  Raises:
    ValueError: If name is not one of the expected topic names.

  Returns:
    A detailed_help DESCRIPTION markdown string.
  a    Most *gcloud* commands return a list of resources on success. By default they
  are pretty-printed on the standard output. The
  *--format=*_NAME_[_ATTRIBUTES_]*(*_PROJECTION_*)* and
  *--filter=*_EXPRESSION_ flags along with projections can be used to format and
  change the default output to a more meaningful result.

  Use the `--format` flag to change the default output format of a command.   {see_format}

  Use the `--filter` flag to select resources to be listed. {see_filter}

  Use resource-keys to reach resource items through a unique path of names from the root. {see_key}

  Use projections to list a subset of resource keys in a resource.   {see_projection}

  Note: To refer to a list of fields you can sort, filter, and format by for
  each resource, you can run a list command with the format set to `text` or
  `json`. For
  example, $ gcloud compute instances list --limit=1 --format=text.

  To work through an interactive tutorial about using the filter and format
  flags instead, see: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/cloud-shell-tutorials&page=editor&tutorial=cloudsdk/tutorial.md
  filtersformatszresource-keysprojections)filterformatkey
projectionz)Expected one of [{topics}], got [{name}].,)topicsnamez0Resource {topic}s are described in detail below.)topicz)For details run $ gcloud topic {command}.)commandr   r   r   r   )
see_filter
see_formatsee_keysee_projection)
ValueErrorr   joinsortedsix	iteritemstextwrapdedent)r   descriptionr   seer   r   s         3lib/googlecloudsdk/core/resource/resource_topics.pyResourceDescriptionr%   !   s    +6 !	& 

@GGxxv'd H 4 5 5
#f-neW}ELL M c%j ?EE F c%j . 
	%	,	,H8;H58Z<?<M 
- 
O O       c                     | rM| d   j                  d      s8| d   j                  d      r| j                  d       y| j                  d       yyy)a  Appends paragraph markdown to lines.

  Paragraph markdown is used to add paragraphs in nested lists at the list
  prevaling indent. _AppendParagraph does not append the markdown if the last
  line in lines is already a paragraph markdown.

  A line containing only the + character is a paragraph markdown. It renders
  a blank line and starts the next paragraph of lines using the prevailing
  indent. A blank line would also start a new paragraph but would decrease the
  prevailing indent.

  Args:
    lines: The lines to append to.
  z
+

z+
N)endswithappend)liness    r$   _AppendParagraphr.   d   sI     59%%g.Ry$ll5ll7 /Ur&   c                 p   |rd}t        |        n'| r%| d   j                  d      s| j                  d       |j                  d      s|j                  d      rK| r%| d   j                  d      s| j                  d       | j                  |       | j                  d       |S | j                  |       |S )zAppends line to lines handling list markdown.

  Args:
    lines: The lines to append to.
    line: The line to append.
    paragraph: Start a new paragraph if True.

  Returns:
    The new paragraph value. This will always be False.
  Fr)   r*    z* z::)r.   r+   r,   
startswith)r-   line	paragraphs      r$   _AppendLiner4   |   s     IUU2Y''-	LL	__TdmmD1U2Y''-ll4	LL	LL 
 
LL	r&   c                    g }g }g }| j                   sy| j                   j                  d      \  }}}t        }d}g }d}	t        j                  |      j                  d      D ]\  }
|
j                  d      sX|
j                  d      rGd}	|r$|j                  |dj                  |      f       d}|
dk(  rt        }Z|
d	k(  rt        }ft        }m|t        k(  rw|
sd
}	||t        k(  rt        ||
|	      }	|
j                  d      r&|t        k(  st        ||
j                         |	      }	|t        k(  r|
j                  d      rt        ||
|	      }	|r"|j                  |dj                  |      f       |
j                  d      \  }}}|j                         }|j                  d      }|j                         g}_ |r"|j                  |dj                  |      f       dj                  |      ||fS )a  Parses the doc string for printer.

  Args:
    printer: The doc string will be parsed from this resource format printer.

  Returns:
    A (description, attributes) tuple:
      description - The format description.
      attributes - A list of (name, description) tuples, one tuple for each
        format-specific attribute.

  Example resource printer docstring parsed by this method:
    '''This line is skipped. Printer attributes and Example sections optional.

    These lines describe the format.
    Another description line.

    Printer attributes:
      attribute-1-name: The description for attribute-1-name.
      attribute-N-name: The description for attribute-N-name.

    Example:
      One or more example lines for the 'For example:' section.
    '''
  ) r6   r6   r*   NFr0   :r6   zPrinter attributes:Example:T      *)__doc__	partition	_DOC_MAINr    r!   splitr1   r+   r,   r   _DOC_ATTRIBUTES_DOC_EXAMPLE	_DOC_SKIPr4   striplstrip)printerdescriptions
attributesexample_doccollect	attributeattribute_descriptionr3   r2   texts               r$   _ParseFormatDocStringrO      s   4 ,*'	oo''-)!Q'))ooc"((.d??3DMM#$6i	9bgg.C&DEF		&	&!:	I	i	I	lD)<i		 	O	# 5tzz|YO		L	 T__T%:gtY7i 
9bgg.C&DEF>>#.iD//#i""3'i#zz|nQ /R y"''*?"@AB		
G	33r&   c                     dg} t        t        j                  t        j                                     D ]  \  }}t        |      \  }}}| j                  dj                  ||             |rYt        |        | j                  d       |D ]'  \  }}| j                  dj                  ||             ) | j                  d       |st        |        | j                  dj                  d	j                  |      
              | j                  d       t        t        j                        \  }}}|rP| j                  dj                  |dd              |D ]'  \  }}| j                  dj                  ||             ) |r;t        |        | j                  dj                  d	j                  |      
             | j                  d       d	j                  |       S )zBReturns help markdown for all registered resource printer formats.z0The formats and format specific attributes are:
z
*{name}*::
{description}
)r   r"   zThe format attributes are:

z*{attribute}*:::
{description}
)rL   r"   :::
For example:
+
{example}
r6   rH   ::
z
{description}:
+
Nr)   )r"   z*{attribute}*::
{description}
r*   )r   r   r   r   GetFormatRegistryrO   r,   r   r.   r   PrinterAttributes)rF   r   rE   r"   rG   rH   rL   s          r$   FormatRegistryDescriptionsrW      s    FF,	mm$6689;mdG'<W'E$KW7>>{ ? , -|$:;$.
 )[?FF[ G : 	; %/ '"|$8??'''" @ $ %;  f &;((&*"+z7/66$ 7 & '",	;<CC; D 8 9 #- \"6==  > " #d		r&   c                     | j                  d      r| j                  d      S d}| j                  |      r| t        |      d S | S )z6Returns string with Pythonic unused notation stripped.rI   unused_N)r1   rD   len)stringunuseds     r$   _StripUnusedNotationr]     sH    s==&v#f+,	-r&   c                    d}| j                   sy| j                   j                  d      \  }}}t        }d}|g}g }g }	g }
d}t        j                  |      j                  d      D ]=  }|sd}	|dk(  r	t        }d}|dk(  r	t        }d}%|t        k(  r/|t        k(  rt        |||      }F|t        k(  r|j                  d	      rt        |
||      }n|t        k(  r"|j                  d
      rt        ||dd |      }|rt        |      }|r&||vr"|	j                  |dj                  |
      f       |j                  d      s|j                  d      rt        }|j                  d      \  }}}|j                         }|j!                  d      }|j                         g}
@ ddl}|j%                  |       }t'        |j(                        t'        |j*                  xs g       z
  }g }t-        |j(                        D ]  \  }}|rt        |      }||v r||z
  }|dk\  r|j*                  |   nd}|ct/        |      j1                  dd      }|j                  d      r|dd }|dk(  rd}n|dk(  rd}|j                  dj3                  ||             |j                  |        |j4                  r|j                  |j4                         dj3                  dj                  |            }dj                  |      ||	|fS )aK  Parses the doc string for func.

  Args:
    func: The doc string will be parsed from this function.

  Returns:
    A (description, prototype, args) tuple:
      description - The function description.
      prototype - The function prototype string.
      args - A list of (name, description) tuples, one tuple for each arg.

  Example transform function docstring parsed by this method:
    '''Transform description. Example sections optional.

    These lines are skipped.
    Another skipped line.

    Args:
      r: The resource arg is always sepcified but omitted from the docs.
      arg-2-name[=default-2]: The description for arg-2-name.
      arg-N-name[=default-N]: The description for arg-N-name.
      kwargs: Omitted from the description.

    Example:
      One or more example lines for the 'For example:' section.
    '''
  )kwargsr   r)r6   r6   r6   r6   r*   NFTzArgs:r8   r9   r:      r0   r7   r;   r   '"zu"   FalsefalseTruetruez{formal}={default_display})formaldefault_displayz({formals})z, )formalsr6   )r<   r=   r>   r    r!   r?   	_DOC_ARGSrA   rB   r4   r1   r]   r,   r   r+   rC   rD   inspectgetfullargspecrZ   argsdefaults	enumeratereprreplacer   varargs)funchidden_argsr"   rI   rJ   rK   argrF   rH   ro   arg_descriptionr3   r2   rN   rm   argspecdefault_index_startrk   formal_indexri   default_indexdefaultrj   	prototypes                           r$   _ParseTransformDocStringr     s   8 .+	..t4+q#'#,'	$/)ooc"((.di	gi		gi	I		I	lD)<i	I	$//&"9otY?i	L	 T__T%:gtABx;i 
"3'	K'S#((?345__S!dmmC&8^^C(lc1dIIKcJJsOcoK /P  ""4('GLL)C0@0@0FB,GG''5lF#F+f #66M1>!1Cg}-GW--c37o		#	#D	))!"-	G	#!f$ nn188 9 : ; nnV' 6( __NN7??#""499W+=">)			4	88r&   c                 2   g }t        t        j                  |             D ]  \  }}t        |      \  }}}}|s|j	                  dj                  |||             |rYt        |       |j	                  d       |D ]8  \  }}|j	                  dj                  ||             |j	                  d       : |r;t        |       |j	                  dj                  dj                  |      	             |j	                  d
        dj                  |      S )zGenerates resource transform help text markdown for transforms.

  Args:
    transforms: The transform name=>method symbol table.

  Returns:
    The resource transform help text markdown for transforms.
  z%

*{name}*{prototype}::
{description})r   r~   r"   zThe arguments are:
+
z*```{arg}```*:::
{description}
)rw   r"   rQ   rR   r6   rS   rT   )r   r   r   r   r,   r   r.   r   )	
transformsrF   r   	transformr"   r~   ro   rH   rw   s	            r$   TransformsDescriptionsr     s!    ,j 9:odI,DY,O)KD'BIIYK J A B|$34"
#{?FF G . 	/G$ # |$8??'''" @ $ %# ;$ 
	r&   c                     | dk(  rt        j                         S d}dj                  |       }	 t        ||g      }t	        ||      } |       S # t
        $ r Y yw xY w)z=Returns the transforms for api if it has a transforms module.builtinGetTransformsz'googlecloudsdk.api_lib.{api}.transforms)api)fromlistN)r   r   r   
__import__getattrImportError)r   method_namemodule_pathmodulemethods        r$   _GetApiTransformsr     si    I++--+9@@S@I+{m<FV[)F8O	 s    A 	AAc            	      T   g } t        t        j                  t        j                        D cg c]  \  }}}|s
| c}}      }dgt        |      z   D ];  }t        |      }|s| j                  dj                  |t        |                   = dj                  |       S c c}}w )z=Returns help markdown for all registered resource transforms.r   z+
The {api} transform functions are:
{desc}
)r   descr6   )setpkgutiliter_modulesr   __path__r   r   r,   r   r   r   )rF   rI   r   apisr   r   s         r$   TransformRegistryDescriptionsr     s    ,	%2273C3CDMD
4tDM
O$[6$<'c"3'J
:
A
A2:> B @A ( 
	 Ns
   B$
B$
)r<   
__future__r   r   r   r   r    googlecloudsdkr   googlecloudsdk.core.resourcer   r   r   	six.movesr	   r%   r>   rl   r@   rA   rB   r.   r4   rO   rW   r]   r   r   r   r    r&   r$   <module>r      sy     " &  '   " 9 ; 
 =O@ BGq >	9o|Y04O4d$Nn9b>r&   