
    Xf=                        d dl 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
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   )Transitioner	Automaton)preserveNameArgSpecargsvarargsvarkwdefaults
kwonlyargskwonlydefaultsannotationsc                 f   t          |           }t          t          |j                  |j        |j        |j        r|j        ndt          |j                  |j        r&t          |j        	                                          ndt          |j
        	                                                    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   r   r   r   r   itemsr   )funcspecs     E/var/www/html/env/lib/python3.11/site-packages/automat/_methodical.py_getArgSpecr      s     tD49j"&-7R)) "+E$%++--...(*$*002233       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    ,   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>.gI   s    q}}}}r   r   )r&   r'   s   ` r   _keywords_onlyr(   ?   s3     1XX    XHr   T)frozenc                       e Zd ZdZ ej        d          Z ej                    Z ej        d          Zdde	fdZ
d ZdS )MethodicalStatez-
    A state for a L{MethodicalMachine}.
    FreprNc                    || }|g }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.
        Nzdmethod {input} signature {inputSignature} does not match output {output} signature {outputSignature})inputoutputinputSignatureoutputSignature)
r    argSpecissubset	TypeErrorformatmethod__name__r   machine_oneTransition)r$   r/   enteroutputs	collector	inputArgsr0   
outputArgss           r   uponzMethodicalState.uponX   s    & =E?G //	 	 	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#   r7   r8   r$   s    r   _namezMethodicalState._name~       {##r   )r8   
__module____qualname____doc__attribr9   r7   
serializedlistr@   rD   r   r   r   r+   r+   O   s          dg5!!!GTWYYFe$$$J $dd $L $L $L $LL$ $ $ $ $r   r+   c                 x    t          | |d          }|&t          ||j                  }t          | ||           |S )z
    Get a L{Transitioner}
    N)getattrr   initialStatesetattr)oselfsymbol	automatontransitioners       r   _transitionerFromInstancerU      sM     5&$//L#"
 
 	v|,,,r   c                      d S r#   r   r   r   r   _emptyrW      s    Dr   c                      dS )	docstringNr   r   r   r   
_docstringrZ      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_coderW   rZ   
ValueError)inst	attributer&   s      r   assertNoCodera      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   )rd   re   rf   passed_arg_namess      r   
<dictcomp>z_filterArgs.<locals>.<dictcomp>   s*    JJJDAq9I0I0I1a0I0I0Ir   c                 $    i | ]\  }}|v 	||S r   r   )rd   re   rf   all_accepted_namess      r   rl   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	inputSpecrg   
named_argsreturn_argsnamevaluer   full_kwargsreturn_kwargsrn   rk   s      `       @@r   _filterArgsry      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}.
    Fr,   )	validator)defaultr-   initr-   c                 *    t          | j                  S r#   r   r7   rC   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#   )r7   _state
transition
collectorsrD   ry   r3   append)r   rq   previousStater<   	outTracerr=   valuesr0   akrv   rQ   r$   rT   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   )rU   rR   rS   r
   r7   r   )r$   rQ   typer   rT   s   ``  @r   __get__zMethodicalInput.__get__   sw     115A A	dk	"	"	t{			% 	% 	% 	% 	% 	% 
	 
#	"	% r   c                     | j         j        S r#   rB   rC   s    r   rD   zMethodicalInput._name   rE   r   r#   )r8   rF   rG   rH   rI   rJ   rS   ra   r7   rR   Factorydictr   r3   r   r   r   rD   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}.
    Fr,   r   c                 *    t          | j                  S r#   r   rC   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.)clsr7   )AttributeErrorr6   r8   r7   r$   rQ   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.
        )r7   )r$   rQ   r   rq   s       r   __call__zMethodicalOutput.__call__  s&     t{5242226222r   c                     | j         j        S r#   rB   rC   s    r   rD   zMethodicalOutput._name  rE   r   r#   )r8   rF   rG   rH   rI   rJ   r9   r7   r3   r   r   r   r   rD   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 )MethodicalTracerFr,   Nc                 H    t          || j        | j                  fd}|S )Nc                 2                         |            d S r#   )setTrace)tracerrT   s    r   r   z*MethodicalTracer.__get__.<locals>.setTrace'  s    !!&)))))r   )rU   rR   rS   )r$   rQ   r   r   rT   s       @r   r   zMethodicalTracer.__get__$  s=    015A A	* 	* 	* 	* 	*r   r#   )r8   rF   rG   rI   rJ   rS   rR   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   .  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   _symbolrC   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__B  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)r9   r7   rK   )r+   r   rO   )stateMethodstateinitialr$   rK   s     r   	decoratorz*MethodicalMachine.state.<locals>.decoratord  s8    #D+6/9; ; ;E  5/4,Lr   r   )r$   r   terminalrK   r   s   `` ` r   r   zMethodicalMachine.stateN  s0    ,	 	 	 	 	 	 	 r   c                       fd}|S )zM
        Declare an input.

        This is a decorator for methods.
        c                 <    t          j        | j                  S )N)rS   r7   rR   )r}   r   r   )inputMethodr$   s    r   r   z*MethodicalMachine.input.<locals>.decoratoru  s&    "T_*5*.,8 8 8 8r   r   r$   r   s   ` r   r/   zMethodicalMachine.inputn  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)r9   r7   )r   )outputMethodr$   s    r   r   z+MethodicalMachine.output.<locals>.decorator  s    #DFFFFr   r   r   s   ` r   r0   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outputTokensr=   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#   )rU   r   r   r   rK   )rQ   rT   	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   statesrK   rU   r   r   )	rQ   r   rq   r   mapping	eachStaterT   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   rC   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#   rB   )r   s    r   <lambda>z-MethodicalMachine.asDigraph.<locals>.<lambda>      (= r   c                     | j         j        S r#   rB   )r/   s    r   r   z-MethodicalMachine.asDigraph.<locals>.<lambda>  r   r   c                     | j         j        S r#   rB   )r0   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)r8   rF   rG   rH   r   r   r(   r   r/   r0   r:   r   r   propertyr   r   r   r   r   r   r   6  s        
     	 	 	 	 ,1   ^> 
 
 ^
   ^6 6 60   ^   ^$ ? ? X?
 
 
 
 
r   r   )!collections	functoolsr   	itertoolsr   inspectr   r   rI   _corer   r	   _introspectionr
   
namedtupler   r   r    r(   sobjectr+   rU   rW   rZ   ra   ry   r}   r   r   r   r   r   r   r   r   <module>r      s                   1 1 1 1 1 1  * * * * * * * * ( ( ( ( ( ( !+
  -N -N -N O O
  0  &    t/$ /$ /$ /$ /$f /$ /$ /$d  	 	 	  8 8 8"%& %& %&P 5u($ ($ ($ ($ ($f ($ ($ ($V t $  $  $  $  $v  $  $  $D 5u
 
 
 
 
v 
 
 
 %''+ + +b
 b
 b
 b
 b
 b
 b
 b
 b
 b
r   