
    b_:>                     <   d dl Z d dlmZ d dlmZ 	 d dlmZ n# e$ r	 d dlm	Z Y nw xY wd dl
Z
d dlZddlmZmZ ddlmZ  e j        d	g d
          Zd Zd Zd Z e
j        d           G d de                      Zd Zd Zd Zd Zd Z e
j        dd           G d de                      Z e
j        d           G d de                      Z e
j        dd           G d de                      Z  e            Z!d Z" G d  d!e          Z#dS )"    Nwraps)count)getfullargspec)
getargspec   )Transitioner	Automaton)preserveNameArgSpecargsvarargsvarkwdefaults
kwonlyargskwonlydefaultsannotationsc                    t          |           }t          t          |j                  |j        t
          j        r|j        n|j        |j	        r|j	        ndt
          j        rt          |j
                  ndt
          j        r/|j        r&t          |j                                                  ndndt
          j        r&t          |j                                                  nd          S )z
    Normalize inspect.ArgSpec across python versions
    and convert mutable attributes to immutable types.

    :param Callable func: A function.
    :return: The function's ArgSpec.
    :rtype: ArgSpec
     r   )getArgsSpecr   tupler   r   sixPY3r   keywordsr   r   r   itemsr   )funcspecs     3lib/python3.11/site-packages/automat/_methodical.py_getArgSpecr       s     tD49G6djj"&-7R-0W<5)))" W"+E$%++--...(*7:wFE$*0022333B       c                 z    t          | j        | j        z   | j        rdndz   | j        rdndz   | j        z             S )a0  
    Get the name of all arguments defined in a function signature.

    The name of * and ** arguments is normalized to "*args" and "**kwargs".

    :param ArgSpec spec: A function to interrogate for a signature.
    :return: The set of all argument names in `func`s signature.
    :rtype: Set[str]
    )z*argsr   )z**kwargs)setr   r   r   r   r   )r   s    r   _getArgNamesr$   2   sZ     	
/	-::2	/ !J.==B	0 
		  r!   c                 <     t                      fd            }|S )a  
    Decorate a function so all its arguments must be passed by keyword.

    A useful utility for decorators that take arguments so that they don't
    accidentally get passed the thing they're decorating as their first
    argument.

    Only works for methods right now.
    c                      | fi |S Nr   )selfkwfs     r   gz_keywords_only.<locals>.gO   s    q}}}}r!   r   )r*   r+   s   ` r   _keywords_onlyr,   E   s3     1XX    XHr!   T)frozenc                       e Zd ZdZ ej        d          Z ej                    Z ej        d          Ze	fdZ
d ZdS )MethodicalStatez-
    A state for a L{MethodicalMachine}.
    Freprc                    t          |j                  }|D ]}t          |j                  }|                    |          s^t          d                    |j        j        |j        j        t          |j                  t          |j                                      | j        	                    | ||||           dS )ac  
        Declare a state transition within the :class:`automat.MethodicalMachine`
        associated with this :class:`automat.MethodicalState`:
        upon the receipt of the `input`, enter the `state`,
        emitting each output in `outputs`.

        :param MethodicalInput input: The input triggering a state transition.
        :param MethodicalState enter: The resulting state.
        :param Iterable[MethodicalOutput] outputs: The outputs to be triggered
            as a result of the declared state transition.
        :param Callable collector: The function to be used when collecting
            output return values.

        :raises TypeError: if any of the `outputs` signatures do not match
            the `inputs` signature.
        :raises ValueError: if the state transition from `self` via `input`
            has already been defined.
        zdmethod {input} signature {inputSignature} does not match output {output} signature {outputSignature})inputoutputinputSignatureoutputSignatureN)
r$   argSpecissubset	TypeErrorformatmethod__name__r   machine_oneTransition)r(   r3   enteroutputs	collector	inputArgsr4   
outputArgss           r   uponzMethodicalState.upon^   s    & !//	 	 	F%fn55J&&y11 	228&#l3%}5'25<'@'@(3FM(B(B	 39 3 3  	 	##D%KKKKKr!   c                     | j         j        S r'   r;   r<   r(   s    r   _namezMethodicalState._name       {##r!   N)r<   
__module____qualname____doc__attribr=   r;   
serializedlistrD   rH   r   r!   r   r/   r/   U   s          dg5!!!GTWYYFe$$$J48  L  L  L  LD$ $ $ $ $r!   r/   c                 x    t          | |d          }|&t          ||j                  }t          | ||           |S )z
    Get a L{Transitioner}
    N)getattrr	   initialStatesetattr)oselfsymbol	automatontransitioners       r   _transitionerFromInstancerY      sM     5&$//L#"
 
 	v|,,,r!   c                      d S r'   r   r   r!   r   _emptyr[      s    Dr!   c                      dS )	docstringNr   r   r!   r   
_docstringr^      s      r!   c                     |j         j        t          j         j        t          j         j        fvrt	          d          d S )Nzfunction body must be empty)__code__co_coder[   r^   
ValueError)inst	attributer*   s      r   assertNoCodere      sD     	z&/"9","5"="? ? ?6777? ?r!   c                    t          t          |j        dd         |                     }j        r| }nfd|D             }t          |          |D ]\  }}||fz  t          |j        ddd         |j        ddd                   }fd|D             }	|	                    |           j        r|	}
n7j        dd         j        z   fd|	                                D             }
||
fS )a  
    Filter out arguments that were passed to input that output won't accept.

    :param tuple args: The *args that input received.
    :param dict kwargs: The **kwargs that input received.
    :param ArgSpec inputSpec: The input's arg spec.
    :param ArgSpec outputSpec: The output's arg spec.
    :return: The args and kwargs that output will accept.
    :rtype: Tuple[tuple, dict]
    r   Nc                 ,    g | ]\  }}|j         v |S r   )r   ).0nv
outputSpecs      r   
<listcomp>z_filterArgs.<locals>.<listcomp>   s)    HHHTQ1
3G3Gq3G3G3Gr!   c                 $    i | ]\  }}|v	||S r   r   )rh   ri   rj   passed_arg_namess      r   
<dictcomp>z_filterArgs.<locals>.<dictcomp>   s*    JJJDAq9I0I0I1a0I0I0Ir!   c                 $    i | ]\  }}|v 	||S r   r   )rh   ri   rj   all_accepted_namess      r   rp   z_filterArgs.<locals>.<dictcomp>   s4     5 5 5$!Q!333 A333r!   )	r   zipr   r   r   updater   r   r   )r   kwargs	inputSpecrk   
named_argsreturn_argsnamevaluer   full_kwargsreturn_kwargsrr   ro   s      `       @@r   _filterArgsr}      sO    s9>!""-t4455J I IHHHZHHH V}}! * *eT5M)9>$$B$');DDbD)ABBHJJJJHJJJKv 5# (_QRR0:3HH5 5 5 5+*;*;*=*= 5 5 5 %%r!   F)eqhashc                   
   e Zd ZdZ ej        d          Z ej        e          Z ej        d          Z	 ej         ej
        e          d          Z ej        dd          Zej        d             Zdd	Zd
 ZdS )MethodicalInputz.
    An input for a L{MethodicalMachine}.
    Fr0   )	validator)defaultr1   initr1   c                 *    t          | j                  S r'   r    r;   rG   s    r   _buildArgSpeczMethodicalInput._buildArgSpec       4;'''r!   Nc                      t           j         j                  t           j                  t           j                   fd                        }|S )z
        Return a function that takes no arguments and returns values returned
        by output functions produced by the given L{MethodicalInput} in
        C{oself}'s current state.
        c                  `    j         g| R i | j        }                              \  }}j        |         }g }|D ]a}|r ||                                           t          | |j        |j                  \  }}	 |g|R i |	}
|                    |
           b ||          S r'   )r;   _state
transition
collectorsrH   r}   r7   append)r   ru   previousStater@   	outTracerrA   valuesr4   akrz   rU   r(   rX   s              r   doInputz(MethodicalInput.__get__.<locals>.doInput   s     DK///////(/M#/#:#:4#@#@ Wi6IF! % % .Ifllnn---"4v~NN1u.q...A..e$$$$9V$$$r!   )rY   rV   rW   r   r;   r   )r(   rU   typer   rX   s   ``  @r   __get__zMethodicalInput.__get__   sw     115A A	dk	"	"	t{			% 	% 	% 	% 	% 	% 
	 
#	"	% r!   c                     | j         j        S r'   rF   rG   s    r   rH   zMethodicalInput._name   rI   r!   r'   )r<   rJ   rK   rL   rM   rN   rW   re   r;   rV   Factorydictr   r7   r   r   r   rH   r   r!   r   r   r      s          U###ITW|,,,FTW%   Fd!3!3%@@@Jdg5u---G_( ( _(   2$ $ $ $ $r!   r   c                       e Zd ZdZ ej        d          Z ej                    Z ej        dd          Zej	        d             Z
d
dZd Zd	 ZdS )MethodicalOutputz/
    An output for a L{MethodicalMachine}.
    Fr0   r   c                 *    t          | j                  S r'   r   rG   s    r   r   zMethodicalOutput._buildArgSpec  r   r!   Nc                 h    t          d                    |j        | j        j                            )zX
        Outputs are private, so raise an exception when we attempt to get one.
        zf{cls}.{method} is a state-machine output method; to produce this output, call an input method instead.)clsr;   )AttributeErrorr:   r<   r;   r(   rU   r   s      r   r   zMethodicalOutput.__get__
  sA     DDJFM{+ EK E E
 
 	
r!   c                 $     | j         |g|R i |S )z-
        Call the underlying method.
        )r;   )r(   rU   r   ru   s       r   __call__zMethodicalOutput.__call__  s&     t{5242226222r!   c                     | j         j        S r'   rF   rG   s    r   rH   zMethodicalOutput._name  rI   r!   r'   )r<   rJ   rK   rL   rM   rN   r=   r;   r7   r   r   r   r   rH   r   r!   r   r   r      s          dg5!!!GTWYYFdg5u---G_( ( _(

 

 

 

3 3 3$ $ $ $ $r!   r   c                   Z    e Zd Z ej        d          Z ej        d          ZddZdS )MethodicalTracerFr0   Nc                 H    t          || j        | j                  fd}|S )Nc                 2                         |            d S r'   )setTrace)tracerrX   s    r   r   z*MethodicalTracer.__get__.<locals>.setTrace)  s    !!&)))))r!   )rY   rV   rW   )r(   rU   r   r   rX   s       @r   r   zMethodicalTracer.__get__&  s=    015A A	* 	* 	* 	* 	*r!   r'   )r<   rJ   rK   rM   rN   rW   rV   r   r   r!   r   r   r      sP        U###ITW%   F     r!   r   c                  J    dt          t          t                              z   S )z,
    Create a unique Python identifier.
    _symbol_)strnextcounterr   r!   r   gensymr   0  s     DMM****r!   c                       e Zd ZdZd ZddZe	 	 dd            Zed             Zed             Z	d	 Z
ed
             Zed             Zed             Zd ZdS )MethodicalMachinezj
    A :class:`MethodicalMachine` is an interface to an `Automaton`
    that uses methods on a class.
    c                 `    t                      | _        i | _        t                      | _        d S r'   )r
   
_automaton	_reducersr   _symbolrG   s    r   __init__zMethodicalMachine.__init__>  s#    #++xxr!   Nc                 (    |t          d          | S )z
        L{MethodicalMachine} is an implementation detail for setting up
        class-level state; applications should never need to access it on an
        instance.
        Nz.MethodicalMachine is an implementation detail.)r   r   s      r   r   zMethodicalMachine.__get__D  s'      @B B Br!   Fc                       fd}|S )a  
        Declare a state, possibly an initial state or a terminal state.

        This is a decorator for methods, but it will modify the method so as
        not to be callable any more.

        :param bool initial: is this state the initial state?
            Only one state on this :class:`automat.MethodicalMachine`
            may be an initial state; more than one is an error.

        :param bool terminal: Is this state a terminal state?
            i.e. a state that the machine can end up in?
            (This is purely informational at this point.)

        :param Hashable serialized: a serializable value
            to be used to represent this state to external systems.
            This value should be hashable;
            :py:func:`unicode` is a good type to use.
        c                 H    t          |           }r|j        _        |S )N)r=   r;   rO   )r/   r   rS   )stateMethodstateinitialr(   rO   s     r   	decoratorz*MethodicalMachine.state.<locals>.decoratorf  s8    #D+6/9; ; ;E  5/4,Lr!   r   )r(   r   terminalrO   r   s   `` ` r   r   zMethodicalMachine.stateP  s0    ,	 	 	 	 	 	 	 r!   c                       fd}|S )zM
        Declare an input.

        This is a decorator for methods.
        c                 <    t          j        | j                  S )N)rW   r;   rV   )r   r   r   )inputMethodr(   s    r   r   z*MethodicalMachine.input.<locals>.decoratorw  s&    "T_*5*.,8 8 8 8r!   r   r(   r   s   ` r   r3   zMethodicalMachine.inputp  s$    	8 	8 	8 	8 	8 r!   c                       fd}|S )z
        Declare an output.

        This is a decorator for methods.

        This method will be called when the state machine transitions to this
        state as specified in the decorated `output` method.
        c                 &    t          |           S )N)r=   r;   )r   )outputMethodr(   s    r   r   z+MethodicalMachine.output.<locals>.decorator  s    #DFFFFr!   r   r   s   ` r   r4   zMethodicalMachine.output~  s'    	G 	G 	G 	G 	Gr!   c                 n    | j                             |||t          |                     ||j        |<   dS )z.
        See L{MethodicalState.upon}.
        N)r   addTransitionr   r   )r(   
startState
inputTokenendStateoutputTokensrA   s         r   r>   z MethodicalMachine._oneTransition  sB    & 	%%j*h&+L&9&9	; 	; 	;,5
j)))r!   c                       fd}|S )


        c                 @     t                      fd            }|S )Nc                 f    t          | j        j                  } | |j        j                  S r'   )rY   r   r   r   rO   )rU   rX   	decorateer(   s     r   	serializezBMethodicalMachine.serializer.<locals>.decorator.<locals>.serialize  s7    89= J  J y(;(FGGGr!   r   )r   r   r(   s   ` r   r   z/MethodicalMachine.serializer.<locals>.decorator  sC    9H H H H H H r!   r   r   s   ` r   
serializerzMethodicalMachine.serializer  s$    
	 	 	 	 	 r!   c                       fd}|S )r   c                 @     t                      fd            }|S )Nc                      | g|R i |}i }j                                         D ]}|||j        <   t          | j        j                   }||         |_        d S r'   )r   statesrO   rY   r   r   )	rU   r   ru   r   mapping	eachStaterX   r   r(   s	          r   unserializezFMethodicalMachine.unserializer.<locals>.decorator.<locals>.unserialize  s    !	%9$999&99!%!7!7!9!9 > >I4=GI01184< :  :&-en#tr!   r   )r   r   r(   s   ` r   r   z1MethodicalMachine.unserializer.<locals>.decorator  s=    9      r!   r   r   s   ` r   unserializerzMethodicalMachine.unserializer  s$    
	 	 	 	 	 r!   c                 6    t          | j        | j                  S r'   )r   r   r   rG   s    r   	_setTracezMethodicalMachine._setTrace  s    >>>r!   c                 <    ddl m}  || j        d d d           S )a  
        Generate a L{graphviz.Digraph} that represents this machine's
        states and transitions.

        @return: L{graphviz.Digraph} object; for more information, please
            see the documentation for
            U{graphviz<https://graphviz.readthedocs.io/>}

        r   )makeDigraphc                     | j         j        S r'   rF   )r   s    r   <lambda>z-MethodicalMachine.asDigraph.<locals>.<lambda>      (= r!   c                     | j         j        S r'   rF   )r3   s    r   r   z-MethodicalMachine.asDigraph.<locals>.<lambda>  r   r!   c                     | j         j        S r'   rF   )r4   s    r   r   z-MethodicalMachine.asDigraph.<locals>.<lambda>  s    &-*@ r!   )stateAsStringinputAsStringoutputAsString)
_visualizer   r   )r(   r   s     r   	asDigraphzMethodicalMachine.asDigraph  sF     	,+++++{O====@@	
 
 
 	
r!   r'   )FFN)r<   rJ   rK   rL   r   r   r,   r   r3   r4   r>   r   r   propertyr   r   r   r!   r   r   r   8  s        
     	 	 	 	 ,1   ^> 
 
 ^
   ^6 6 60   ^   ^$ ? ? X?
 
 
 
 
r!   r   )$collections	functoolsr   	itertoolsr   inspectr   r   ImportErrorr   rM   r   _corer	   r
   _introspectionr   
namedtupler   r    r$   r,   sobjectr/   rY   r[   r^   re   r}   r   r   r   r   r   r   r   r!   r   <module>r      s                  25555555 2 2 2111111112  



 * * * * * * * * ( ( ( ( ( ( !+
  -N -N -N O O
  0  &    t+$ +$ +$ +$ +$f +$ +$ +$\  	 	 	  8 8 8"%& %& %&P 5u($ ($ ($ ($ ($f ($ ($ ($V t $  $  $  $  $v  $  $  $D 5u
 
 
 
 
v 
 
 
 %''+ + +b
 b
 b
 b
 b
 b
 b
 b
 b
 b
s    ''