
    If[C                    l   U d dl mZ d dlZd dlZd dlmZmZ d dlmZ d dl	m
Z d dlmZ d dlmZmZmZmZmZ ej(                  dk  rd d	lmZ nd d	lmZ d
dlmZmZmZmZ d
dlmZ  ej<                  dg d      Zd Z d Z!d Z" ed       G d de#             Z$	 	 	 	 	 	 	 	 d,dZ%d Z&d Z'd-dZ(d Z) ed      Z* ed      Z+ ed       G d d e#             Z, ed       G d! d"e#             Z-ee.gdf   Z/d#Z0d$e1d%<   	 	 	 	 d.d&Z2 ed       G d' d(e#             Z3 e       Z4d) Z5 G d* d+e#      Z6y)/    )annotationsN)	dataclassfieldwraps)getfullargspec)count)AnyCallableHashableIterableTypeVar)   
   )	TypeAlias   )	AutomatonOutputTracerTracerTransitioner)preserveNameArgSpecargsvarargsvarkwdefaults
kwonlyargskwonlydefaultsr   c                   t        |       }t        t        |j                        |j                  |j
                  |j                  r|j                  ndt        |j                        |j                  r#t        |j                  j                               ndt        |j                  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     N/var/www/cs2snipe.com/venv/lib/python3.12/site-packages/automat/_methodical.py_getArgSpecr(   "   s     tD499jj"&--R)262E2EE$%%++-.2$**0023
 
    c                    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,   9   sV     		
//	:2	/ !JJ=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>.gW   s    }}r)   r   )r2   r3   s   ` r'   _keywords_onlyr4   L   s"     1X  Hr)   T)frozenc                      e Zd ZU dZ ed      Zded<    e       Zded<    ed      Zded	<   d
d
e	f	 	 	 	 	 	 	 	 	 ddZ
ddZy
)MethodicalStatez-
    A state for a L{MethodicalMachine}.
    FreprMethodicalMachinemachineCallable[..., Any]methodbool
serializedNc                   || }|g }t        |j                        }|D ]  }t        |j                        }|j                  |      r*t        dj	                  |j
                  j                  |j
                  j                  t        |j
                        t        |j
                                     | j                  j                  | ||||       y)a  
        Declare a state transition within the L{MethodicalMachine} associated
        with this L{MethodicalState}: upon the receipt of the `input`, enter
        the `state`, emitting each output in `outputs`.

        @param input: The input triggering a state transition.

        @param enter: The resulting state.

        @param outputs: The outputs to be triggered as a result of the declared
            state transition.

        @param 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formatr=   __name__r"   r;   _oneTransition)r0   rA   enteroutputs	collector	inputArgsrB   
outputArgss           r'   uponzMethodicalState.uponh   s    8 =E?G /	 	F%fnn5J&&y1228&#ll33%}}55'25<<'@(3FMM(B	 39 3	 		 	##D%Kr)   c                .    | j                   j                  S r/   r=   rI   r0   s    r'   _namezMethodicalState._name       {{###r)   )
rA   MethodicalInputrK   zMethodicalState | NonerL   z!Iterable[MethodicalOutput] | NonerM   zCallable[[Iterable[T]], object]returnNonerW   str)rI   
__module____qualname____doc__r   r;   __annotations__r=   r?   listrP   rT   r!   r)   r'   r7   r7   ^   s     "'E!2G2!&F(%(J(
 )-5959.L.L &.L 3	.L
 3.L 
.L`$r)   r7   c                j    t        | |d      }|#t        ||j                        }t        | ||       |S )z
    Get a L{Transitioner}
    N)getattrr   initialStatesetattr)oselfsymbol	automatontransitioners       r'   _transitionerFromInstancerh      sB     5&$/L#""
 	v|,r)   c                      y r/   r!   r!   r)   r'   _emptyrj      s    r)   c                      y)	docstringNr!   r!   r)   r'   
_docstringrm      s    r)   c                    | j                   j                  t        j                   j                  t        j                   j                  fvrt	        d      y )Nzfunction body must be empty)__code__co_coderj   rm   
ValueError)r2   s    r'   assertNoCoderr      sC     	zz&//"9"9:;N;N;V;V!WW677 Xr)   c                h   t        t        |j                  dd |             }|j                  r| }n#|D cg c]  \  }}||j                  v s| }}}t        |      }|D ]  \  }	}
||	|
fz  } t        |j                  ddd   |j                  ddd         }|D ci c]  \  }}||vs|| }}}|j                  |       |j                  r|}||fS |j                  dd |j                  z   }|j                         D ci c]  \  }}||v s|| }}}||fS c c}}w c c}}w c c}}w )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   N)	r#   zipr   r   r   updater   r   r$   )r   kwargs	inputSpec
outputSpec
named_argsreturn_argsnvpassed_arg_namesnamevaluer   full_kwargsreturn_kwargsall_accepted_namess                  r'   _filterArgsr      sc    s9>>!"-t45J &0HTQ1
3GqHH V}! *eT5M)*9>>$B$'););DbD)ABH$,JDAq9I0I1a4JKJv# %% (__QR0:3H3HH(..0
QA9K4KAqD
 
 %%) I K
s#   D"D""D(/D(	D.D.TRF)eqc                      e Zd ZU dZ ed      Zded<    e       Zded<    ed      Zded	<    ee	d
      Z
ded<    edd      Zded<   ddZdddZddZy)rV   z.
    An input for a L{MethodicalMachine}.
    Fr8   =Automaton[MethodicalState, MethodicalInput, MethodicalOutput]rf   r<   r=   rZ   re   )default_factoryr9   z1dict[MethodicalState, Callable[[Iterable[T]], R]]
collectors)initr9   r   rE   c                b    t        | j                        | _        t        | j                         y r/   )r(   r=   rE   rr   rS   s    r'   __post_init__zMethodicalInput.__post_init__  s    "4;;/T[[!r)   Nc                     t         j                   j                        t         j                        t         j                        d 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                 H    j                   g| i | j                  }j                        \  }}j                  |   }g }|D ]N  }| ||       t	        | |j
                  |j
                        \  }}	 |g|i |	}
|j                  |
       P  ||      S r/   )r=   _state
transitionr   r   rE   append)r   rw   previousStaterL   	outTracerrM   valuesrB   akr   rd   r0   rg   s              r'   doInputz(MethodicalInput.__get__.<locals>.doInput  s     DKK///(//M#/#:#:4#@ Wi6IF! %(f%"4v~~N1u.q.A.e$% V$$r)   )r   objectrw   r   rW   r   )rh   re   rf   r   r=   r   )r0   rd   typer   rg   s   ``  @r'   __get__zMethodicalInput.__get__  sN     1T^^T	dkk	"	t{{		% 
 
#	% r)   c                .    | j                   j                  S r/   rR   rS   s    r'   rT   zMethodicalInput._name"  rU   r)   rW   rX   r/   )rd   r   r   rX   rW   r   rY   )rI   r[   r\   r]   r   rf   r^   r=   re   dictr   rE   r   r   rT   r!   r)   r'   rV   rV      sy     PUPIL  "'F(U#FC#DI5EJA  %e4GW4"4$r)   rV   c                  v    e Zd ZU dZ ed      Zded<   ded<    eddd      Zd	ed
<   ddZddZ	d Z
ddZy)MethodicalOutputz/
    An output for a L{MethodicalMachine}.
    Fr8   r:   r;   r<   r=   )r   r9   comparer   rE   c                H    t        | j                        | j                  d<   y )NrE   )r(   r=   __dict__rS   s    r'   r   zMethodicalOutput.__post_init__0  s    #.t{{#;i r)   Nc                v    t        dj                  |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=   )AttributeErrorrH   rI   r=   r0   rd   r   s      r'   r   zMethodicalOutput.__get__3  s<     DDJFMM$++*>*> EK E
 	
r)   c                .     | j                   |g|i |S )z-
        Call the underlying method.
        )r=   )r0   rd   r   rw   s       r'   __call__zMethodicalOutput.__call__>  s     t{{5242622r)   c                .    | j                   j                  S r/   rR   rS   s    r'   rT   zMethodicalOutput._nameD  rU   r)   r   r/   rY   )rI   r[   r\   r]   r   r;   r^   rE   r   r   r   rT   r!   r)   r'   r   r   &  sF     "'E!2G2%eUCGWC<	
3$r)   r   z4Callable[[str, str, str], StringOutputTracer | None]r   StringTracerc                *      y 	 	 	 	 	 	 	 	 d fd}|S )Nc                ~     | j                         |j                         |j                               fdS y )Nc                0     | j                               S r/   rT   )outresults    r'   <lambda>z,wrapTracer.<locals>.tracer.<locals>.<lambda>Y  s    vciik2 r)   r   )staterA   rB   r   wrappeds      @r'   tracerzwrapTracer.<locals>.tracerR  s4    
 v||~F22r)   )r   r7   rA   rV   rB   r7   rW   z%OutputTracer[MethodicalOutput] | Noner!   )r   r   s   ` r'   
wrapTracerr   L  s=        
/	 Mr)   c                  \    e Zd ZU  ed      Zded<    ed      Zded<   	 d		 	 	 	 	 d
dZy)MethodicalTracerFr8   r   rf   rZ   re   Nc                V    t        || j                  | j                        dfd}|S )Nc                :    j                  t        |              y r/   )setTracer   )r   rg   s    r'   r   z*MethodicalTracer.__get__.<locals>.setTracek  s    !!*V"45r)   )r   StringTracer | NonerW   rX   )rh   re   rf   )r0   rd   r   r   rg   s       @r'   r   zMethodicalTracer.__get__f  s'     1T^^T	6 r)   r/   )rd   r   r   r   rW   zCallable[[StringTracer], None])rI   r[   r\   r   rf   r^   re   r   r!   r)   r'   r   r   _  sK    OTPIL  U#FC# -1#)	'r)   r   c                 8    dt        t        t                    z   S )z,
    Create a unique Python identifier.
    _symbol_)rZ   nextcounterr!   r)   r'   gensymr   t  s     DM***r)   c                      e Zd ZdZd ZddZe	 d	 	 	 	 	 dd       Zed        Zed        Z	d Z
ed	        Zed
        Zedd       Zd Zy)r:   ze
    A L{MethodicalMachine} is an interface to an L{Automaton} that uses methods
    on a class.
    c                N    t               | _        i | _        t               | _        y r/   )r   
_automaton	_reducersr   _symbolrS   s    r'   __init__zMethodicalMachine.__init__  s    #+x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.
        z.MethodicalMachine is an implementation detail.)r   r   s      r'   r   zMethodicalMachine.__get__  s      !QRRr)   c                      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 initial: is this state the initial state?  Only one state on
            this L{automat.MethodicalMachine} may be an initial state; more
            than one is an error.

        @param 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 serialized: a serializable value to be used to represent this
            state to external systems.  This value should be hashable; L{str}
            is a good type to use.
        c                J    t        |       }r|j                  _        |S )N)r;   r=   r?   )r7   r   rb   )stateMethodr   initialr0   r?   s     r'   	decoratorz*MethodicalMachine.state.<locals>.decorator  s*    #[ZE /4,Lr)   r!   )r0   r   terminalr?   r   s   `` ` r'   r   zMethodicalMachine.state  s    .	 r)   c                      fd}|S )zM
        Declare an input.

        This is a decorator for methods.
        c                H    t        j                  | j                        S )N)rf   r=   re   )rV   r   r   )inputMethodr0   s    r'   r   z*MethodicalMachine.input.<locals>.decorator  s    "//+dll r)   r!   r0   r   s   ` r'   rA   zMethodicalMachine.input  s    	
 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   )outputMethodr0   s    r'   r   z+MethodicalMachine.output.<locals>.decorator  s    #DFFr)   r!   r   s   ` r'   rB   zMethodicalMachine.output  s    	G r)   c                p    | j                   j                  |||t        |             ||j                  |<   y)z.
        See L{MethodicalState.upon}.
        N)r   addTransitionr#   r   )r0   
startState
inputTokenendStateoutputTokensrM   s         r'   rJ   z MethodicalMachine._oneTransition  s6    $ 	%%
HeL.A	
 -6
j)r)   c                      fd}|S ) c                2     t                fd       }|S )Nc                    t        | j                  j                        } | |j                  j                        S r/   )rh   r   r   r   r?   )rd   rg   	decorateer0   s     r'   	serializezBMethodicalMachine.serializer.<locals>.decorator.<locals>.serialize  s7    84<<  !(;(;(F(FGGr)   r   )r   r   r0   s   ` r'   r   z/MethodicalMachine.serializer.<locals>.decorator  s$    9H H r)   r!   r   s   ` r'   
serializerzMethodicalMachine.serializer  s    	 r)   c                      fd}|S )r   c                2     t                fd       }|S )Nc                     | g|i |}i }j                   j                         D ]  }|||j                  <    t        | j                  j                         }||   |_        y r/   )r   statesr?   rh   r   r   )	rd   r   rw   r   mapping	eachStaterg   r   r0   s	          r'   unserializezFMethodicalMachine.unserializer.<locals>.decorator.<locals>.unserialize  sr    !%9$9&9!%!7!7!9 >I4=GI001>84<<  '.en#r)   r   )r   r   r0   s   ` r'   r   z1MethodicalMachine.unserializer.<locals>.decorator  s"    9	 	 r)   r!   r   s   ` r'   unserializerzMethodicalMachine.unserializer  s    	 r)   c                B    t        | j                  | j                        S r/   )r   r   r   rS   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/   rR   )r   s    r'   r   z-MethodicalMachine.asDigraph.<locals>.<lambda>      (=(= r)   c                .    | j                   j                  S r/   rR   )rA   s    r'   r   z-MethodicalMachine.asDigraph.<locals>.<lambda>  r   r)   c                .    | j                   j                  S r/   rR   )rB   s    r'   r   z-MethodicalMachine.asDigraph.<locals>.<lambda>   s    &--*@*@ r)   )stateAsStringinputAsStringoutputAsString)
_visualizer   r   )r0   r   s     r'   	asDigraphzMethodicalMachine.asDigraph  s$     	,OO==@	
 	
r)   r/   )FFN)r   r>   r   r>   r?   r   )rW   r   )rI   r[   r\   r]   r   r   r4   r   rA   rB   rJ   r   r   propertyr   r   r!   r)   r'   r:   r:   {  s    
 
 TX/3IQ @    6.    ( ? ?
r)   r:   )rd   r   re   rZ   rf   r   rW   z@Transitioner[MethodicalState, MethodicalInput, MethodicalOutput])r2   r<   rW   rX   )r   r   rW   zATracer[MethodicalState, MethodicalInput, MethodicalOutput] | None)7
__future__r   collectionssysdataclassesr   r   	functoolsr   inspectr   r"   	itertoolsr	   typingr
   r   r   r   r   version_infotyping_extensionsr   _corer   r   r   r   _introspectionr   
namedtupler   r(   r,   r4   r   r7   rh   rj   rm   rr   r   r   r   rV   r   rZ   StringOutputTracerr   r^   r   r   r   r   r:   r!   r)   r'   <module>r     s   "  
 (  1  = =g+  @ @ (
 +
 
 .&$ $:$f :$ :$z M F	$	8 &&R CLCL e/$f /$ /$d $$v $ $B seTk* Pi P F& ev  " '+f
 f
r)   