o
    R"                  	   @   s   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 de	ej
eZG d	d
 d
eZdd ZdZd$ddZdd Zdd Zd%dededeedf fddZd&ddZd%ddZdd Zd d! Zd%d"d#ZdS )'z@Utilities for adding help text for flags with an argparser type.    )absolute_import)division)unicode_literalsN)Unionc                   @   sL   e Zd ZdZeejdd Zejdd Zejdd Z	ejdd	 Z
d
S )ArgTypeUsagezEInterface for flags types that need to provide additional usage info.c                 C      dS )zWhether the argument is hidden.N selfr   r   K/tmp/google-cloud-sdk/lib/googlecloudsdk/calliope/arg_parsers_usage_text.pyhidden       zArgTypeUsage.hiddenc                 C   r   )z,Returns the metavar for flag with type self.Nr   )r
   is_custom_metavarmetavarr   r   r   GetUsageMetavar#   r   zArgTypeUsage.GetUsageMetavarc                 C   r   )z=Returns the example user input value for flag with type self.Nr   )r
   	shorthandr   r   r   GetUsageExample'   r   zArgTypeUsage.GetUsageExamplec                 C   r   )z.Returns the help text for flag with type self.Nr   )r
   
field_namerequired	flag_namer   r   r   GetUsageHelpText+   r   zArgTypeUsage.GetUsageHelpTextN)__name__
__module____qualname____doc__propertyabcabstractmethodr   r   r   r   r   r   r   r   r      s    

r   c                       sP   e Zd ZdZ fddZedd Zedd Zdd	 Zd
d Z	dd Z
  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                    s   t t|   || _d S N)superr   __init__arg_type)r
   r"   	__class__r   r   r!   8   s   
zDefaultArgTypeWrapper.__init__c                 C   s   t | jtS r   )
isinstancer"   r   r	   r   r   r   _is_usage_type<   s   z$DefaultArgTypeWrapper._is_usage_typec                 C   s   | j r| jjS d S r   )r&   r"   r   r	   r   r   r   r   @   s   zDefaultArgTypeWrapper.hiddenc                 O      | j r| jj|i |S dS )z,Forwards default usage metavar for arg_type.N)r&   r"   r   r
   argskwargsr   r   r   r   G      z%DefaultArgTypeWrapper.GetUsageMetavarc                 O   r'   )z,Forwards default usage example for arg_type.N)r&   r"   r   r(   r   r   r   r   N   r+   z%DefaultArgTypeWrapper.GetUsageExamplec                 O   r'   )z(Forwards default help text for arg_type.N)r&   r"   r   r(   r   r   r   r   U   r+   z&DefaultArgTypeWrapper.GetUsageHelpText)r   r   r   r   r!   r   r&   r   r   r   r   __classcell__r   r   r#   r   r   0   s    

r   c                 C   s   t | tr| jp	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"   r   r   r   IsHidden]   s   	r.   z::
c                 C   s   d| }|  t|t S )z3Tabs over all lines in text using ascii doc syntax.:)replaceASCII_INDENT)textdepthadditional_tabsr   r   r   IndentAsciiDocl   s   r5   c                 C   s>   | sdS t | tr| S | tu rdS | tu rdS | tu rdS dS )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%   strr6   r7   boolr-   r   r   r   _FormatBasicTypeStrr   s   	
r<   c                 C   s   |   }|dr|S |d S )z5Adds punctuation to text if it doesn't already exist..)rstripendswith)r2   
clean_textr   r   r   
_Punctuate   s   
rA   Fr2   ignorereturnc                 C   s$   | r|r| S | d   | dd  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)r2   rB   r   r   r   _Capitalize   s   
rF   c                 C   s@   |rt |}n| rtd| |}ntd|}|rd|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, {})rA   rF   format)r   r   	help_textdefaulted_help_textr   r   r   FormatHelpText   s   



rJ   c                 C   s<   d|v rd | |}nd | |}|rdj |dS d |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```

{}

```)rG   )arg_name	arg_valueappendexample_flagr   r   r   FormatCodeSnippet   s   
rQ   c                 C   sX   | sdS t | tr| j|d}nt| }t | t}|ttk}|s*|s%|r*d|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   r<   r:   rG   )r"   r   arg_stris_string_literalis_string_typer   r   r   _GetNestedValueExample   s   



rU   c                 C   sd   t | |}t ||}t| st|rdS |r|s|p|S |r+|dk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: )rU   r.   )key_type
value_typer   key_str	value_strr   r   r   GetNestedKeyValueExample   s   

r[   c                 C   sf   t | |}t|tr|jrd}nt|tr"|js"|j| |dp |}n|}|r1d| tt|ddS dS )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*{}*{}{}rD   )r3   )rJ   r%   r   r   r   rG   r1   r5   )r   r"   r   default_usageusager   r   r   GetNestedUsageHelpText  s   
r^   )r   )Fr   )r   
__future__r   r   r   r   typingr   sixwith_metaclassABCMetaobjectr   r   r.   r1   r5   r<   rA   r:   r;   rF   rJ   rQ   rU   r[   r^   r   r   r   r   <module>   s(   -
 	

%