
    R"              	          d 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dlZ G d d ej                  ej                  e            Z G d	 d
e      Zd ZdZddZd Zd Zddededeedf   fdZddZddZd Zd ZddZy)z@Utilities for adding help text for flags with an argparser type.    )absolute_import)division)unicode_literalsN)Unionc                       e Zd ZdZeej                  d               Zej                  d        Zej                  d        Z	ej                  d        Z
y)ArgTypeUsagezEInterface for flags types that need to provide additional usage info.c                      y)zWhether the argument is hidden.N selfs    5lib/googlecloudsdk/calliope/arg_parsers_usage_text.pyhiddenzArgTypeUsage.hidden           c                      y)z,Returns the metavar for flag with type self.Nr
   )r   is_custom_metavarmetavars      r   GetUsageMetavarzArgTypeUsage.GetUsageMetavar#   r   r   c                      y)z=Returns the example user input value for flag with type self.Nr
   )r   	shorthands     r   GetUsageExamplezArgTypeUsage.GetUsageExample'   r   r   c                      y)z.Returns the help text for flag with type self.Nr
   )r   
field_namerequired	flag_names       r   GetUsageHelpTextzArgTypeUsage.GetUsageHelpText+   r   r   N)__name__
__module____qualname____doc__propertyabcabstractmethodr   r   r   r   r
   r   r   r   r      su    M*  * 7 7 H H 9 9r   r   c                   T     e Zd ZdZ fdZed        Zed        Zd Zd Z	d Z
 xZS )DefaultArgTypeWrapperzBase class for processing arg_type output but maintaining usage help text.

  Attributes:
    arg_type: type function used to parse input string into correct type ie
      ArgObject(value_type=int, repeating=true), int, bool, etc
  c                 8    t         t        |           || _        y N)superr%   __init__arg_type)r   r*   	__class__s     r   r)   zDefaultArgTypeWrapper.__init__8   s    	
/1DMr   c                 6    t        | j                  t              S r'   )
isinstancer*   r   r   s    r   _is_usage_typez$DefaultArgTypeWrapper._is_usage_type<   s    dmm\22r   c                 H    | j                   r| j                  j                  S y r'   )r.   r*   r   r   s    r   r   zDefaultArgTypeWrapper.hidden@   s    ]]!!!r   c                 T    | j                   r | j                  j                  |i |S y)z,Forwards default usage metavar for arg_type.N)r.   r*   r   r   argskwargss      r   r   z%DefaultArgTypeWrapper.GetUsageMetavarG   *    *T]]**D;F;;r   c                 T    | j                   r | j                  j                  |i |S y)z,Forwards default usage example for arg_type.N)r.   r*   r   r1   s      r   r   z%DefaultArgTypeWrapper.GetUsageExampleN   r4   r   c                 T    | j                   r | j                  j                  |i |S y)z(Forwards default help text for arg_type.N)r.   r*   r   r1   s      r   r   z&DefaultArgTypeWrapper.GetUsageHelpTextU   s*    +T]]++T<V<<r   )r   r   r   r    r)   r!   r.   r   r   r   r   __classcell__)r+   s   @r   r%   r%   0   sD     3 3  r   r%   c                 F    t        | t              xr | j                  xs dS )zReturns whether arg_type is hidden.

  Args:
    arg_type: Callable, arg type that may contain hidden attribute

  Returns:
    bool, whether the type is considered hidden
  F)r-   r   r   r*   s    r   IsHiddenr:   ]   s     X|
,
@	JUJr   z::
c                 F    d|z  }| j                  t        |t        z         S )z3Tabs over all lines in text using ascii doc syntax.:)replaceASCII_INDENT)textdepthadditional_tabss      r   IndentAsciiDocrB   l   s!    %K/	lOl$B	CCr   c                 d    | syt        | t              r| S | t        u ry| t        u ry| t        u ryy)zReturns a user friendly name of a primitive arg_type.

  Args:
    arg_type: type | str | None, expected user input type

  Returns:
    String representation of the type
  Nintfloatbooleanstring)r-   strrD   rE   boolr9   s    r   _FormatBasicTypeStrrJ   r   s@     
#O _ 
r   c                 R    | j                         }|j                  d      r|S |dz   S )z5Adds punctuation to text if it doesn't already exist..)rstripendswith)r?   
clean_texts     r   
_PunctuaterP      s,    {{}*r   r?   ignorereturnc                 @    | r|r| S | d   j                         | dd z   S )zCapitalizes the first letter of text.

  Args:
    text: The text to capitalize.
    ignore: Whether to ignore the capitalization request.

  Returns:
    The capitalized text.
  r      N)upper)r?   rQ   s     r   _CapitalizerV      s)     
K	a48	##r   c                     |rt        |      }n*| rt        dj                  |       |      }nt        d|      }|rdj                  |      S |S )a-  Defaults and formats specific attribute of help text.

  Args:
    field_name: None | str, attribute that is being set by flag
    required: bool, whether the flag is required
    help_text: None | str, text that describes the flag

  Returns:
    help text formatted as `{type} {required}, {help}`
  zsets `{}` value.zsets value.zRequired, {})rP   rV   format)r   r   	help_textdefaulted_help_texts       r   FormatHelpTextr[      sZ     $Y/%!!*-x &mX>  !455r   c                     d|v rdj                  | |      }ndj                  | |      }|rdj                  |      S dj                  |      S )zFormats flag in markdown code snippet.

  Args:
    arg_name: str, name of the flag in snippet
    arg_value: str, flag value in snippet
    append: bool, whether to use append syntax for flag

  Returns:
    markdown string of example user input
   z{}='{}'z{}={}z```

{input} {input}

```)inputz```

{}

```)rX   )arg_name	arg_valueappendexample_flags       r   FormatCodeSnippetrc      sV     	I##Hi8L>>(I6L*111EE$$\22r   c                     | syt        | t              r| j                  |      }nt        |       }t        | t              }|t        t              k(  }|s|s|rdj                  |      S |S )a
  Gets an example input value for flag of arg_type.

  Args:
    arg_type: Callable[[str], Any] | str | None, expected user input type
    shorthand: bool, whether to display example in shorthand

  Returns:
    string representation of user input for type arg_type
  N)r   z"{}")r-   r   r   rJ   rH   rX   )r*   r   arg_stris_string_literalis_string_types        r   _GetNestedValueExamplerh      sj     
,'&&&;G!(+G 3/1#66.	)^==!!Nr   c                     t        | |      }t        ||      }t        |       st        |      ry|r|s|xs |S |r|dk7  r| d| S |S | d| S )a  Formats example key-value input for flag of arg_type.

  If key_type and value_type are callable types str, returns

    string=string (shorthand) or
    "string": "string" (non-shorthand)

  If key_type is a static string value such as x, returns

    x=string (shorthand) or
    "x": "string" (non-shorthand).

  If key_type or value_type are None, returns string representation of
  key or value

  Args:
    key_type: Callable[[str], Any] | str | None, type function for the key
    value_type: Callable[[str], Any] | None, type function for the value
    shorthand: bool, whether to display the example in shorthand

  Returns:
    str, example of key-value pair
  Nz{}=z: )rh   r:   )key_type
value_typer   key_str	value_strs        r   GetNestedKeyValueExamplero      so    0 #8Y7'$Z;)h8J/)i'0D'8gYa	{#EgEYb$$r   c                    t        | |      }t        |t              r|j                  rd}n6t        |t              r$|j                  s|j	                  | |      xs |}n|}|r"dj                  | t        t        |d            S y)a  Returns help text for flag with arg_type.

  Generates help text based on schema such that the final output will
  look something like...

    *Foo*
        Required, Foo help text

  Args:
    field_name: str, attribute we are generating help text for
    arg_type: Callable[[str], Any] | None, type of the attribute we are getting
      help text for
    required: bool, whether the attribute is required

  Returns:
    string help text for specific attribute
  N)r   z*{}*{}{}rT   )r@   )r[   r-   r   r   r   rX   r>   rB   )r   r*   r   default_usageusages        r   GetNestedUsageHelpTextrs     s    $ !X6-,'HOOE(L)(//!!*x!@ 	 

 E L.a"@  r   )r   )Fr'   )r    
__future__r   r   r   r"   typingr   sixwith_metaclassABCMetaobjectr   r%   r:   r>   rB   rJ   rP   rH   rI   rV   r[   rc   rh   ro   rs   r
   r   r   <module>rz      s    G &  ' 
  
9%3%%ckk6: 9**L *Z	K D8$c $4 $E#t)4D $43,4"%J$r   