U
    +cm                     @   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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ZdZdZdUddZdVddZdWddZdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) ZdXd*d+Z dYd-d.Z!d/d0 Z"d1d2 Z#dZd3d4Z$d5d6 Z%d7d8 Z&d[d:d;Z'd<d= Z(d>d? Z)d@dA Z*dBdC Z+d\dDdEZ,dFdG Z-dHdI Z.dJdK Z/d]dLdMZ0dNdO Z1d9dPefdQdRZ2G dSdT dTe3Z4dS )^a  Utilities for producing help strings for use in Fire CLIs.

Can produce help strings suitable for display in Fire CLIs for any type of
Python object, module, class, or function.

There are two types of informative strings: Usage and Help screens.

Usage screens are shown when the user accesses a group or accesses a command
without calling it. A Usage screen shows information about how to use that group
or command. Usage screens are typically short and show the minimal information
necessary for the user to determine how to proceed.

Help screens are shown when the user requests help with the help flag (--help).
Help screens are shown in a less-style console view, and contain detailed help
information.
    )absolute_import)division)print_functionN)
completion)custom_descriptions)
decorators)
docstrings)
formatting)inspectutils)value_typesP      Fc                 C   s   t | }t| |d}t | }t| }t| |||d}t| ||||d}t| |}	t	| rpt
|||\}
}ng }
g }t| |}|||	g|
 | | }ddd |D S )a  Gets the help string for the current component, suitable for a help screen.

  Args:
    component: The component to construct the help string for.
    trace: The Fire trace of the command so far. The command executed so far
      can be extracted from this trace.
    verbose: Whether to include private members in the help screen.

  Returns:
    The full help screen as a string.
  verbose)tracer   )r   

c                 s   s   | ]}|d k	rt | V  qd S N)_CreateOutputSection).0section r   1/tmp/pip-unpacked-wheel-1xt1w7un/fire/helptext.py	<genexpr>]   s    zHelpText.<locals>.<genexpr>)r
   Info_GetActionsGroupedByKindGetFullArgSpecr   GetMetadata_NameSection_SynopsisSection_DescriptionSectioncallable_ArgsAndFlagsSections_UsageDetailsSectionsjoin)	componentr   r   infoactions_grouped_by_kindspecmetadataZname_sectionZsynopsis_sectionZdescription_sectionargs_and_flags_sectionsnotes_sectionsZusage_details_sectionssectionsr   r   r   HelpText5   sF    


    
  

r,   c                 C   s^   t ||d}t|}t| r@tt t|d  }t| |t}|rR|d | }n|}d|fS )z&The "Name" section of the help string.include_separatorsz - NAME)_GetCurrentCommand_GetSummaryr   NeedsCustomDescriptionLINE_LENGTHSECTION_INDENTATIONlen
GetSummary)r$   r%   r   r   current_commandsummaryavailable_spacetextr   r   r   r   c   s    

r   c                 C   s   t |dd}t|}g }|r*|t| t| r\t||}|rL|| n|r\||j d|}	d}
|
j||	d}d|fS )z*The "Synopsis" section of the help string.T)r   r.    | z {current_command} {continuation})r7   continuationZSYNOPSIS)	r0   _GetPossibleActionsappend_GetPossibleActionsStringr    _GetArgsAndFlagsString	separatorr#   format)r$   r&   r'   r(   r   r7   possible_actionscontinuationsZcallable_continuationr<   Zsynopsis_templater:   r   r   r   r   x   s$    

r   c                 C   s`   t | r0tt }t | |t}t | |t}nt|}t|}|pJ|pJd}|rXd|fS dS dS )a5  The "Description" sections of the help string.

  Args:
    component: The component to produce the description section for.
    info: The info dict for the component of interest.

  Returns:
    Returns the description if available. If not, returns the summary.
    If neither are available, returns None.
  NZDESCRIPTION)r   r2   r3   r4   ZGetDescriptionr6   _GetDescriptionr1   )r$   r%   r9   descriptionr8   r:   r   r   r   r      s    
r   c                 C   s   t | ||| |jk|dS )Nrequired	short_arg)_CreateFlagItemkwonlydefaults)flagdocstring_infor'   rI   r   r   r   _CreateKeywordOnlyFlagItem   s       rN   c                    s*   dd | D }t |  fdd|D S )zGets a list of single-character flags that uniquely identify a flag.

  Args:
    flags: list of strings representing flags

  Returns:
    List of single character short flags,
    where the character occurred at the start of a flag once.
  c                 S   s   g | ]}|d  qS )r   r   r   fr   r   r   
<listcomp>   s     z"_GetShortFlags.<locals>.<listcomp>c                    s   g | ]} | d kr|qS )   r   )r   vZshort_flag_countsr   r   rQ      s      )collectionsCounter)flagsZshort_flagsr   rT   r   _GetShortFlags   s    

rX   c                    sH  j dtj tj  }j tj tj d }|tj}g }g }| d   fdd|D }jr|tj  |r|rdnd}	|	d	|
df}
||
 |r|r|d t| fd	d|D }tj fd
djD }|| }jr"g }d}d} j p,g }dd |D }t|}|D ]f}t|tjrH|jd |kr|j|j|jd d}n|j|jd}t|j |d}|| qH|r|rd}t|ddd}|| || tj }|rd}n|rd}nd}t||dd}|| |r@dd	|f}|| ||fS )z1The "Args and Flags" sections of the help string.NrM   c                    s   g | ]}t | qS r   )_CreateArgItemr   arg)rM   r'   r   r   rQ      s   z)_ArgsAndFlagsSections.<locals>.<listcomp>zPOSITIONAL ARGUMENTSZ	ARGUMENTS
)ZNOTESz6You can also use flags syntax for POSITIONAL ARGUMENTSc              	      s&   g | ]}t | d |d kdqS )Fr   rG   )rJ   r   rL   )rM   r'   unique_short_argsr   r   rQ      s      
c                    s$   g | ]}t | |d  kdqS )r   )rI   )rN   r]   )rM   r'   unique_short_kwonly_flagsr   r   rQ      s     
z--{name}z-{short_name}, --{name}c                 S   s   g | ]
}|j qS r   namerO   r   r   r   rQ      s     r   )ra   Z
short_namer`   )flag_stringz&The following flags are also accepted.r   indentz3Additional undocumented flags may also be accepted.zAdditional flags are accepted.zFlags are accepted.FLAGS)argsr5   defaultsgetr   ACCEPTS_POSITIONAL_ARGSvarargsr>   rY   r#   rstriprX   
kwonlyargsvarkw
isinstancer   Z	KwargInfora   rB   rJ   _CreateItemextend_GetArgDescription)r%   r'   r(   args_with_no_defaultsargs_with_defaultsaccepts_positional_argsr)   r*   Z	arg_itemstitleZarguments_sectionZpositional_flag_itemsZkwonly_flag_itemsZ
flag_itemsZdocumented_kwargsrb   Zshort_flag_stringrW   Z
flag_namesZunique_short_flagsrL   Z	flag_itemmessageitemrF   Zflags_sectionr   )rM   r'   r^   r_   r   r!      s    

   



r!   c                 C   sn   |\}}}}g }|j r$|t| |j r8|t| |j rN|t| | |j rj|dtd|jf |S )z.The usage details sections of the help string.ZINDEXESZINDEX)membersr>   _MakeUsageDetailsSection_ValuesUsageDetailsSection_NewChoicesSectionnames)r$   r&   groupscommandsvaluesindexesr+   r   r   r   r"   (  s    r"   c                 C   s   | d }|j r|j S d S NrM   )r8   r%   rM   r   r   r   r1   9  s    r1   c                 C   s   | d }|j r|j S d S r   )rF   r   r   r   r   rE   >  s    rE   c                 C   s   | j dt| j t| j  }| j t| j t| j d }|tj}g }|r||rddd |D }ndd |D }|| |s| js| jr|	d | j
rdjt| j
 d}|	| d|S )	a-  The args and flags string for showing how to call a function.

  If positional arguments are accepted, the args will be shown as positional.
  E.g. "ARG1 ARG2 [--flag=FLAG]"

  If positional arguments are disallowed, the args will be shown with flags
  syntax.
  E.g. "--arg1=ARG1 [--flag=FLAG]"

  Args:
    spec: The full arg spec for the component to construct the args and flags
      string for.
    metadata: Metadata for the component, including whether it accepts
      positional arguments.

  Returns:
    The constructed args and flags string.
  Nc                 S   s   g | ]}t | qS r   r	   	UnderlineupperrZ   r   r   r   rQ   _  s   z*_GetArgsAndFlagsString.<locals>.<listcomp>c                 S   s$   g | ]}d j |t| dqS )z--{arg}={arg_upper})r[   Z	arg_upper)rB   r	   r   r   rZ   r   r   r   rQ   b  s
    <flags>[{varargs}]...rj    )rf   r5   rg   rh   r   ri   rp   rl   rm   r>   rj   rB   r	   r   r   r#   )r'   r(   rr   rs   rt   Zarg_and_flag_stringsarg_stringsZvarargs_stringr   r   r   r@   C  s*    


r@   c                 C   s$   g }| D ]}|j r||j q|S )z"The list of possible action kinds.)rx   r>   ra   )r&   rC   action_groupr   r   r   r=   t  s
    r=   c                 C   s   d dd | D S )zAA help screen string listing the possible action kinds available.r;   c                 s   s   | ]}t | V  qd S r   r   )r   actionr   r   r   r     s   z,_GetPossibleActionsString.<locals>.<genexpr>)r#   rC   r   r   r   r?   }  s    
r?   c           
      C   s  t ddd}t ddd}t ddd}t dd	d}tj| |d
}|D ]X\}}t|}t|rj|j||d t|r|j||d t|rB|j||d qBt	| t
tfr| rt| }	|	dk r|jddd t|	D d n|jdj|	d dd ||||gS )z8Gets lists of available actions, grouped by action kind.groupr}   )ra   pluralcommandr~   valuer   indexr   r   )ra   member
   z, c                 s   s   | ]}t |V  qd S r   )str)r   xr   r   r   r     s     z+_GetActionsGroupedByKind.<locals>.<genexpr>r`   z0..{max}rR   )max)ActionGroupr   ZVisibleMembersr   r   ZIsGroupAddZ	IsCommandZIsValuern   listtupler5   r#   rangerB   )
r$   r   r}   r~   r   r   rx   member_namer   Zcomponent_lenr   r   r   r     s&    


"r   Tc                 C   s   | r| j |d}nd}|S )z@Returns current command for the purpose of generating help text.r-    )
GetCommand)r   r.   r7   r   r   r   r0     s    r0   c                 C   s   dj t| t|tdS )Nz{name}
{content}ra   content)rB   r	   BoldIndentr4   r   r   r   r   r     s    
r   c                 C   s   t t t }t| |}t|  }t| |}|r<d|nd}|t	| }t
|||}ddd ||fD }t||tdS )a  Returns a string describing a positional argument.

  Args:
    arg: The name of the positional argument.
    docstring_info: A docstrings.DocstringInfo namedtuple with information about
      the containing function's docstring.
    spec: An instance of fire.inspectutils.FullArgSpec, containing type and
     default information about the arguments to a callable.

  Returns:
    A string to be used in constructing the help screen for the function.
  Type: {}r   r\   c                 s   s   | ]}|r|V  qd S r   r   r   partr   r   r   r     s      z!_CreateArgItem.<locals>.<genexpr>rc   )r3   r4   SUBSECTION_INDENTATIONrq   r	   ZBoldUnderliner   _GetArgTyperB   r5   EllipsisTruncater#   ro   )r[   rM   r'   max_str_lengthrF   
arg_stringarg_typer9   r   r   r   rY     s    

rY   c                 C   s   t t t }t| |}|s6d}|j| t|  d}|rB|d7 }|rZdj| d d| }t| |}	t	| |}
|
dkrd|	}	|	rd	|	nd
}	|t
|	 }t|	||}	|
rd|
nd
}
|t
|
 }t|
||}
ddd |	|
|fD }t||tdS )a  Returns a string describing a flag using docstring and FullArgSpec info.

  Args:
    flag: The name of the flag.
    docstring_info: A docstrings.DocstringInfo namedtuple with information about
      the containing function's docstring.
    spec: An instance of fire.inspectutils.FullArgSpec, containing type and
     default information about the arguments to a callable.
    required: Whether the flag is required.
    flag_string: If provided, use this string for the flag, rather than
      constructing one from the flag name.
    short_arg: Whether the flag has a short variation or not.
  Returns:
    A string to be used in constructing the help screen for the function.
  z--{flag_name}={flag_name_upper})Z	flag_nameZflag_name_upperz (required)z-{short_flag}, r   )Z
short_flagNonezOptional[{}]r   r   zDefault: {}r\   c                 s   s   | ]}|r|V  qd S r   r   r   r   r   r   r     s     z"_CreateFlagItem.<locals>.<genexpr>rc   )r3   r4   r   rq   rB   r	   r   r   r   _GetArgDefaultr5   r   r#   ro   )rL   rM   r'   rH   rb   rI   r   rF   Zflag_string_templater   Zarg_defaultr9   r   r   r   rJ     s8    




rJ   c                 C   sX   | |j krT|j |  }z"tjdd dkr0|jW S |jW S  tk
rR   t| Y S X dS )a|  Returns a string describing the type of an argument.

  Args:
    arg: The name of the argument.
    spec: An instance of fire.inspectutils.FullArgSpec, containing type and
     default information about the arguments to a callable.
  Returns:
    A string to be used in constructing the help screen for the function, the
    empty string if the argument type is not available.
  r      )   r   r   )annotationssysversion_info__qualname____name__AttributeErrorrepr)r[   r'   r   r   r   r   r     s    

r   c                 C   s`   t |j}|j| d }t||jD ]\}}|| kr&t|  S q&| |jkr\t|j|  S dS )a  Returns a string describing a flag's default value.

  Args:
    flag: The name of the flag.
    spec: An instance of fire.inspectutils.FullArgSpec, containing type and
     default information about the arguments to a callable.
  Returns:
    A string to be used in constructing the help screen for the function, the
    empty string if the flag does not have a default or the default is not
    available.
  Nr   )r5   rg   rf   zipr   rK   )rL   r'   num_defaultsrs   r[   defaultr   r   r   r   '  s    

r   r   c                 C   s   |s| S dj | t||dS )Nz{name}
{description})ra   rF   )rB   r	   r   )ra   rF   rd   r   r   r   ro   >  s    
ro   c                 C   s8   |j r4|j D ]&}|j| d|  d|  fkr|j  S qd S )N*z**)rf   ra   rF   )ra   rM   Zarg_in_docstringr   r   r   rq   F  s
    
rq   c                 C   s   g }|   D ]l\}}t|}|}|d}|rBt|sB|j}n"t|r`t|tt	 t}nd}t
||}|| q| j t| j |fS )z>Creates a usage details section for the provided action group.rM   N)GetItemsr
   r   rh   r   r2   r8   r6   r3   r4   ro   r>   r   r   r{   ra   )r   Zitem_stringsra   r   r%   rw   rM   r8   r   r   r   ry   N  s*    


  
ry   c           	      C   s   g }|  D ]n\}}~t| jj}d}d|kr`|d }|jr`|jD ]}|j|krDt||j}qD|dkrpt	|}|
| qdtd|fS )zDCreates a section tuple for the values section of the usage details.NrM   ZVALUESZVALUE)r   r
   r   	__class____init__rf   ra   ro   rF   r   r>   r{   )	r$   r   Zvalue_item_stringsZ
value_namer   Z	init_infoZ
value_itemZinit_docstring_infoZarg_infor   r   r   rz   c  s    

rz   c                 C   s,   t djtt| ddd| ddS )Nz{name} is one of the following:r`   r\   r   rR   rc   )ro   rB   r	   r   r   r#   )ra   choicesr   r   r   r{   v  s    r{   c                 C   s   d}|r|  }| }nd}d}|s*d}|}t| }t| }t| |d}	t|	}
g }|
rl|t	|
 t
|	}t| rt||}|r|d| n|r||j |t| |r|dd| 7 }||rdnd d	 }|j|d||d
S )aD  Returns usage text for the given component.

  Args:
    component: The component to determine the usage text for.
    trace: The Fire trace object containing all metadata of current execution.
    verbose: Whether to display the usage text in verbose mode.

  Returns:
    String suitable for display in an error screen.
  zoUsage: {continued_command}
{availability_lines}
For detailed information on this command, run:
  {help_command}NFr   r   r   r;   z -- z--help)continued_commandavailability_lineshelp_command)r   ZNeedsSeparatingHyphenHyphenr
   r   r   r   r   r=   r>   _GetPossibleActionsUsageString_UsageAvailabilityLinesr    _GetCallableUsageItemsr#   rA   rp   _GetCallableAvailabilityLinesrB   )r$   r   r   Zoutput_templater   Zneeds_separating_hyphen_hyphenr   r'   r(   r&   rC   rD   r   Zcallable_itemsr   r   r   r   	UsageText~  sJ    




r   c                 C   s   | rdj d| dS d S )Nz<{actions}>|)actions)rB   r#   r   r   r   r   r     s    r   c                 C   s:   g }| D ],}|j rtdj|jd|jd}|| q|S )Nzavailable {plural}:)r   )headeritems)rx   _CreateAvailabilityLinerB   r   r|   r>   )r&   r   r   Zavailability_liner   r   r   r     s    r   c                 C   s   | j dt| j t| j  }| j t| j t| j d }|tj}|s\dd |D }ndd |D }|sz| jsz| jr|d | j	r|dj
| j	 d |S )zBA list of elements that comprise the usage summary for a callable.Nc                 S   s   g | ]}d j || dqS )z--{arg}={upper})r[   r   )rB   r   rZ   r   r   r   rQ     s   z*_GetCallableUsageItems.<locals>.<listcomp>c                 S   s   g | ]}|  qS r   )r   rZ   r   r   r   rQ     s     r   r   r   )rf   r5   rg   rh   r   ri   rl   rm   r>   rj   rB   r   )r'   r(   rr   rs   rt   r   r   r   r   r     s    
r   c                    s    fddj D S )Nc                 3   s    | ]} |j kkr|V  qd S r   )rK   r]   rH   r'   r   r   r     s    z(_KeywordOnlyArguments.<locals>.<genexpr>)rl   )r'   rH   r   r   r   _KeywordOnlyArguments  s    r   c                 C   s   | j t| j t| j d }dd t|t| ddD }dd t| ddD }g }|rn|td|d	d
 |r|td|d	d
 | jr|s|rdnd}|t|g d	d
 |S )zHThe list of availability lines for a callable for use in a usage string.Nc                 S   s   g | ]}d | qS z--r   r]   r   r   r   rQ     s     z1_GetCallableAvailabilityLines.<locals>.<listcomp>F)rH   c                 S   s   g | ]}d | qS r   r   r]   r   r   r   rQ     s    Tzoptional flags:r   )r   r   header_indentzrequired flags:zadditional flags are acceptedzflags are accepted)	rf   r5   rg   	itertoolschainr   r>   r   rm   )r'   rs   Zoptional_flagsZrequired_flagsr   Zadditional_flagsr   r   r   r     sD    
 

r      c           	      C   sP   || }d tj||d}tj||d}tj| |d}||t|d   d S )Nr\   )width)spaces)r#   r	   ZWrappedJoinr   r5   )	r   r   r   Zitems_indentZline_lengthZitems_widthZ
items_textZindented_items_textZindented_headerr   r   r   r     s
    r   c                   @   s*   e Zd ZdZdd Zd	ddZdd ZdS )
r   z$A group of actions of the same kind.c                 C   s   || _ || _g | _g | _d S r   )ra   r   r|   rx   )selfra   r   r   r   r   r     s    zActionGroup.__init__Nc                 C   s   | j | | j| d S r   )r|   r>   rx   )r   ra   r   r   r   r   r      s    zActionGroup.Addc                 C   s   t | j| jS r   )r   r|   rx   )r   r   r   r   r   $  s    zActionGroup.GetItems)N)r   
__module__r   __doc__r   r   r   r   r   r   r   r     s   
r   )NF)NF)N)F)NT)FNF)r   )NF)T)5r   
__future__r   r   r   rU   r   r   Zfirer   r   r   r   r	   r
   r   r3   r4   r   r,   r   r   r   rN   rX   r!   r"   r1   rE   r@   r=   r?   r   r0   r   rY   rJ   r   r   ro   rq   ry   rz   r{   r   r   r   r   r   r   r   objectr   r   r   r   r   <module>   sp   
.
 
c1	

	!    
?

A
  

