
    |h(                     `    d Z ddlZddlmZ dZdZdZdZdZd	Z	d
Z
 G d d      Z G d d      Zy)aw  This module has classes for tracing the execution of a Fire execution.

A FireTrace consists of a sequence of FireTraceElement objects. Each element
represents an action taken by Fire during a single Fire execution. An action may
be instantiating a class, calling a routine, or accessing a property.

Each action consumes args and results in a new component. The final component
is serialized to stdout by Fire as well as returned by the Fire method. If
a Fire usage error occurs, such as insufficient arguments being provided to call
a function, then that error will be captured in the trace and the final
component will be None.
    N)inspectutilszInitial componentzInstantiated classzCalled routinezCalled callablezAccessed propertyzGenerated completion scriptzEntered interactive modec                   x    e Zd ZdZ	 	 ddZd Zd Zd Zd Ze	fdZ
d	 Zd
 Zd Zd Zd ZddZd Zd ZddZy)	FireTracea4  A FireTrace represents the steps taken during a single Fire execution.

  A FireTrace consists of a sequence of FireTraceElement objects. Each element
  represents an action taken by Fire during a single Fire execution. An action
  may be instantiating a class, calling a routine, or accessing a property.
  Nc                 |    t        |t              }|| _        || _        |g| _        || _        || _        || _        y N)	componentaction)FireTraceElementINITIAL_COMPONENTname	separatorelementsverbose	show_help
show_trace)selfinitial_componentr   r   r   r   r   initial_trace_elements           I/var/www/html/test/engine/venv/lib/python3.12/site-packages/fire/trace.py__init__zFireTrace.__init__1   sB    ,# 
 DIDN*+DMDLDN DO    c                 6    | j                         j                  S )z9Returns the component from the last element of the trace.)GetLastHealthyElementr   r   s    r   	GetResultzFireTrace.GetResult?   s     %%'111r   c                 |    t        | j                        D ]  }|j                         r|c S  | j                  d   S )zReturns the last element of the trace that is not an error.

    This element will contain the final component indicated by the trace.

    Returns:
      The last element of the trace that is not an error.
    r   )reversedr   HasErrorr   elements     r   r   zFireTrace.GetLastHealthyElementE   s>     DMM*  ==r   c                 <    | j                   d   j                         S )zBReturns whether the Fire execution encountered a Fire usage error.)r   r   r   s    r   r   zFireTrace.HasErrorR   s    ==%%''r   c                 d    t        |t        ||||      }| j                  j                  |       y )N)r   r	   targetargsfilenamelineno)r
   ACCESSED_PROPERTYr   append)r   r   r$   r%   r&   r'   r    s          r   AddAccessedPropertyzFireTrace.AddAccessedPropertyV   s2     G 	MM!r   c           	      ^    t        |||||||      }| j                  j                  |       y)aQ  Adds an element to the trace indicating that a component was called.

    Also applies to instantiating a class.

    Args:
      component: The result of calling the callable.
      target: The name of the callable.
      args: The args consumed in order to call this callable.
      filename: The file in which the callable is defined, or None if N/A.
      lineno: The line number on which the callable is defined, or None if N/A.
      capacity: (bool) Whether the callable could have accepted additional args.
      action: The value to include as the action in the FireTraceElement.
    )r   r	   r$   r%   r&   r'   capacityNr
   r   r)   )	r   r   r$   r%   r&   r'   r,   r	   r    s	            r   AddCalledComponentzFireTrace.AddCalledComponenta   s7     G 	MM!r   c                 \    t        |t              }| j                  j                  |       y r   )r
   COMPLETION_SCRIPTr   r)   )r   scriptr    s      r   AddCompletionScriptzFireTrace.AddCompletionScript{   s&     G 	MM!r   c                 Z    t        t              }| j                  j                  |       y )N)r	   )r
   INTERACTIVE_MODEr   r)   r   s     r   AddInteractiveModezFireTrace.AddInteractiveMode   s    &67GMM!r   c                 T    t        ||      }| j                  j                  |       y )N)errorr%   r-   )r   r7   r%   r    s       r   AddErrorzFireTrace.AddError   s     U6GMM!r   c                 >    | j                   d   j                          y)a  Marks that the most recent element of the trace used  a separator.

    A separator is an argument you can pass to a Fire CLI to separate args left
    of the separator from args right of the separator.

    Here's an example to demonstrate the separator. Let's say you have a
    function that takes a variable number of args, and you want to call that
    function, and then upper case the result. Here's how to do it:

    # in Python
    def display(arg1, arg2='!'):
      return arg1 + arg2

    # from Bash (the default separator is the hyphen -)
    display hello   # hello!
    display hello upper # helloupper
    display hello - upper # HELLO!

    Note how the separator caused the display function to be called with the
    default value for arg2.
    r"   N)r   AddSeparatorr   s    r   r:   zFireTrace.AddSeparator   s    , 	MM"""$r   c                     |j                  d      rGd|v rC|j                  dd      \  }}t        j                  |      dz   t        j                  |      z   S t        j                  |      S )Nz--=   )
startswithsplitshlexquote)r   argprefixvalues       r   _QuotezFireTrace._Quote   sY    
~~ds
iiQ'mfe[[ 3&U);;;;;sr   c                     g } j                   r|j                   j                           j                  D ]i  }|j                         r|j                  r|j                  |j                         |j                         sL|sO|j                   j                         k  j                         r|r|j                   j                         dj                   fd|D              S )zReturns the command representing the trace up to this point.

    Args:
      include_separators: Whether or not to include separators in the command.

    Returns:
      A string representing a Fire CLI command that would produce this trace.
     c              3   @   K   | ]  }j                  |        y wN)rE   ).0rB   r   s     r   	<genexpr>z'FireTrace.GetCommand.<locals>.<genexpr>   s     5DKK$5s   )
r   r)   r   r   r%   extendHasSeparatorr   NeedsSeparatorjoin)r   include_separatorsr%   r    s   `   r   
GetCommandzFireTrace.GetCommand   s     Dyy
kk$))== $					GLL!				$6DNN#$ !3
kk$..!885555r   c                 h    | j                         }|j                         xr |j                          S )a  Returns whether a separator should be added to the command.

    If the command is a function call, then adding an additional argument to the
    command sometimes would add an extra arg to the function call, and sometimes
    would add an arg acting on the result of the function call.

    This function tells us whether we should add a separator to the command
    before adding additional arguments in order to make sure the arg is applied
    to the result of the function call, and not the function call itself.

    Returns:
      Whether a separator should be added to the command if order to keep the
      component referred to by the command the same when adding additional args.
    )r   HasCapacityrM   r   s     r   rN   zFireTrace.NeedsSeparator   s2     ((*G ?)=)=)?%??r   c                     g }t        | j                        D ]   \  }}|dz    d| }|j                  |       " dj                  |      S )Nr=   z. 
)	enumerater   r)   rO   )r   linesindexr    lines        r   __str__zFireTrace.__str__   sT    E#DMM2 w	{"WI&dll4 99Ur   c                     | j                         }|j                  }t        j                  |      }|j                  duxs ||j
                  v xs ||j                  v S )a  Returns whether a the trace need '--' before '--help'.

    '--' is needed when the component takes keyword arguments, when the value of
    flag matches one of the argument of the component, or the component takes in
    keyword-only arguments(e.g. argument with default value).

    Args:
      flag: the flag available for the trace

    Returns:
      True for needed '--', False otherwise.

    N)r   r   r   GetFullArgSpecvarkwr%   
kwonlyargs)r   flagr    r   specs        r   NeedsSeparatingHyphenHyphenz%FireTrace.NeedsSeparatingHyphenHyphen   s`     ((*G!!I&&y1DJJd" 'tyy 't&(r   )N-FFF)T)help)__name__
__module____qualname____doc__r   r   r   r   r*   CALLED_CALLABLEr.   r2   r5   r8   r:   rE   rQ   rN   rZ   ra    r   r   r   r   )   sb     KP+0!2(	" +:"4"""%064@$(r   r   c                   L    e Zd ZdZ	 	 	 	 	 	 	 	 d
dZd Zd Zd Zd Zd Z	d	 Z
y)r
   zA FireTraceElement represents a single step taken by a Fire execution.

  Examples of a FireTraceElement are the instantiation of a class or the
  accessing of an object member.
  Nc	                     || _         || _        || _        || _        || _        || _        || _        d| _        || _        y)aZ  Instantiates a FireTraceElement.

    Args:
      component: The result of this element of the trace.
      action: The type of action (e.g. instantiating a class) taking place.
      target: (string) The name of the component being acted upon.
      args: The args consumed by the represented action.
      filename: The file in which the action is defined, or None if N/A.
      lineno: The line number on which the action is defined, or None if N/A.
      error: The error represented by the action, or None if N/A.
      capacity: (bool) Whether the action could have accepted additional args.
    FN)	r   _action_targetr%   	_filename_lineno_error
_separator	_capacity)	r   r   r	   r$   r%   r&   r'   r7   r,   s	            r   r   zFireTraceElement.__init__   sC    * DNDLDLDIDNDLDKDODNr   c                     | j                   d uS rI   )rp   r   s    r   r   zFireTraceElement.HasError  s    ;;d""r   c                     | j                   S rI   )rr   r   s    r   rS   zFireTraceElement.HasCapacity  s    >>r   c                     | j                   S rI   rq   r   s    r   rM   zFireTraceElement.HasSeparator  s    ??r   c                     d| _         y )NTrv   r   s    r   r:   zFireTraceElement.AddSeparator   s	    DOr   c                 Z    dj                  d | j                  j                  D              S )NrG   c              3   2   K   | ]  }t        |        y wrI   )str)rJ   rB   s     r   rK   z.FireTraceElement.ErrorAsStr.<locals>.<genexpr>$  s     9CH9s   )rO   rp   r%   r   s    r   
ErrorAsStrzFireTraceElement.ErrorAsStr#  s!    889(8(8999r   c                    | j                         r| j                         S | j                  }| j                  |d| j                   dz  }| j                  3| j                  }| j
                  |d| j
                   z  }|d| dz  }|S )Nz "":z ())r   r{   rl   rm   rn   ro   )r   stringpaths      r   rZ   zFireTraceElement.__str__&  s    }}__ ||f		!Bt||nA&&		#~~<<#
Adll^$
$$BtfA,mr   )NNNNNNNN)rd   re   rf   rg   r   r   rS   rM   r:   r{   rZ   ri   r   r   r
   r
      sD     >#:r   r
   )rg   r@   firer   r   INSTANTIATED_CLASSCALLED_ROUTINErh   r(   r0   r4   r   r
   ri   r   r   <module>r      sS     ' ) !#' 1 - E( E(PC Cr   