
    dh;D                       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/e1d0<   d<d4Z2 ed()           G d5 d6e#                      Z3 e            Z4d7 Z5 G d8 d9e#          Z6dS )=    )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                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     c/home/jenkins/workspace/simtester-sanitize/venv/lib/python3.11/site-packages/automat/_methodical.py_getArgSpecr(   "   s     tD49j"&-7R))262EME$%++--...2$*002233
 
 
 
    c                    t          | j        | j        z   | j        rdndz   | j        rdndz   t          d | j        D                       z             S )a  
    Get the name of all arguments defined in a function signature.

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

    Return type annotations are omitted, since we don't constrain input methods
    to have the same return type as output methods, nor output methods to have
    the same output type.

    :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**kwargsc              3  2   K   | ]}|d          dk    |V  dS )r   returnNr!   ).0as     r'   	<genexpr>z_getArgNames.<locals>.<genexpr>L   s0      @@aqtx/?/?/?/?/?/?@@r)   )setr   r   r   r   r#   r   )r&   s    r'   _getArgNamesr1   9   sr     	
/	-::2	/ !J.==B	0 @@4+@@@
@
@		A  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>.g[   s    q}}}}r)   r   )r7   r8   s   ` r'   _keywords_onlyr9   P   s5     1XX    X 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d
S )MethodicalStatez-
    A state for a L{MethodicalMachine}.
    FreprMethodicalMachinemachineCallable[..., Any]methodbool
serializedNinputMethodicalInputenterMethodicalState | Noneoutputs!Iterable[MethodicalOutput] | None	collectorCallable[[Iterable[T]], object]r,   Nonec                   || }|g }t          |j                  }|D ]}t          |j                  }|                    |          s^t          d                    |j        j        |j        j        t          |j                  t          |j                                      | j        	                    | ||||           dS )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})rE   outputinputSignatureoutputSignature)
r1   argSpecissubset	TypeErrorformatrB   __name__r"   r@   _oneTransition)r5   rE   rG   rI   rK   	inputArgsrO   
outputArgss           r'   uponzMethodicalState.uponl   s    8 =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)   strc                    | j         j        S r4   rB   rV   r5   s    r'   _namezMethodicalState._name       {##r)   )
rE   rF   rG   rH   rI   rJ   rK   rL   r,   rM   r,   r[   )rV   
__module____qualname____doc__r   r@   __annotations__rB   rD   listrZ   r_   r!   r)   r'   r<   r<   b   s           "'E!2!2!2G2222!&F((((u%(((J((((
 )-5959.L .L .L .L .L`$ $ $ $ $ $r)   r<   oselfobjectsymbolr[   	automaton=Automaton[MethodicalState, MethodicalInput, MethodicalOutput]r,   @Transitioner[MethodicalState, MethodicalInput, MethodicalOutput]c                x    t          | |d          }|&t          ||j                  }t          | ||           |S )z
    Get a L{Transitioner}
    N)getattrr   initialStatesetattr)rg   ri   rj   transitioners       r'   _transitionerFromInstancerr      sM     5&$//L#"
 
 	v|,,,r)   c                     d S r4   r!   r!   r)   r'   _emptyrt      s    Dr)   c                     dS )	docstringNr!   r!   r)   r'   
_docstringrw      s      r)   r7   rA   rM   c                    | j         j        t          j         j        t          j         j        fvrt	          d          d S )Nzfunction body must be empty)__code__co_codert   rw   
ValueError)r7   s    r'   assertNoCoder|      s<     	z&/"9:;N;V!WWW6777 XWr)   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   )r-   nv
outputSpecs      r'   
<listcomp>z_filterArgs.<locals>.<listcomp>   s)    HHHTQ1
3G3Gq3G3G3Gr)   c                $    i | ]\  }}|v	||S r!   r!   )r-   r   r   passed_arg_namess      r'   
<dictcomp>z_filterArgs.<locals>.<dictcomp>   s*    JJJDAq9I0I0I1a0I0I0Ir)   c                $    i | ]\  }}|v 	||S r!   r!   )r-   r   r   all_accepted_namess      r'   r   z_filterArgs.<locals>.<dictcomp>   s1     
 
 
QA9K4K4KAq4K4K4Kr)   )	r#   zipr   r   r   updater   r   r$   )r   kwargs	inputSpecr   
named_argsreturn_argsnamevaluer   full_kwargsreturn_kwargsr   r   s      `       @@r'   _filterArgsr      sP    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 
# (_QRR0:3HH
 
 
 
(..00
 
 
 %%r)   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dS )rF   z.
    An input for a L{MethodicalMachine}.
    Fr=   rk   rj   rA   rB   r[   ri   )default_factoryr>   z1dict[MethodicalState, Callable[[Iterable[T]], R]]
collectors)initr>   r   rR   r,   rM   c                `    t          | j                  | _        t          | j                   d S r4   )r(   rB   rR   r|   r^   s    r'   __post_init__zMethodicalInput.__post_init__  s)    "4;//T[!!!!!r)   Nrg   rh   typec                     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.
        r   rh   r   r,   c                 <    j         g| R i | j        }                              \  }}j        |         }g }|D ]O}| ||           t	          | |j        |j                  \  }}	 |g|R i |	}
|                    |
           P ||          S r4   )rB   _state
transitionr   r   rR   append)r   r   previousStaterI   	outTracerrK   valuesrO   r.   kr   rg   r5   rq   s              r'   doInputz(MethodicalInput.__get__.<locals>.doInput  s     DK///////(/M#/#:#:4#@#@ Wi6IF! % %(If%%%"4v~NN1u.q...A..e$$$$9V$$$r)   )r   rh   r   rh   r,   rh   )rr   ri   rj   r   rB   r   )r5   rg   r   r   rq   s   ``  @r'   __get__zMethodicalInput.__get__  su     1T^TT	dk	"	"	t{			% 	% 	% 	% 	% 	% 	% 
	 
#	"	% r)   c                    | j         j        S r4   r]   r^   s    r'   r_   zMethodicalInput._name&  r`   r)   r,   rM   r4   )rg   rh   r   rM   r,   rh   ra   )rV   rb   rc   rd   r   rj   re   rB   ri   dictr   rR   r   r   r_   r!   r)   r'   rF   rF      s           PUuP P PI     "'F((((%U###F####DIE5E E EJ     u%e444G4444" " " "    4$ $ $ $ $ $r)   rF   c                      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dS )MethodicalOutputz/
    An output for a L{MethodicalMachine}.
    Fr=   r?   r@   rA   rB   )r   r>   comparer   rR   r,   rM   c                >    t          | j                  | j        d<   d S )NrR   )r(   rB   __dict__r^   s    r'   r   zMethodicalOutput.__post_init__4  s    #.t{#;#;i   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.)clsrB   )AttributeErrorrU   rV   rB   r5   rg   r   s      r'   r   zMethodicalOutput.__get__7  s@     DDJFM$+*> EK E E
 
 	
r)   c                $     | j         |g|R i |S )z-
        Call the underlying method.
        )rB   )r5   rg   r   r   s       r'   __call__zMethodicalOutput.__call__B  s&     t{5242226222r)   r[   c                    | j         j        S r4   r]   r^   s    r'   r_   zMethodicalOutput._nameH  r`   r)   r   r4   ra   )rV   rb   rc   rd   r   r@   re   rR   r   r   r   r_   r!   r)   r'   r   r   *  s           "'E!2!2!2G2222u%eUCCCGCCCC< < < <	
 	
 	
 	
3 3 3$ $ $ $ $ $r)   r   z4Callable[[str, str, str], StringOutputTracer | None]r   StringTracerwrappedStringTracer | NoneATracer[MethodicalState, MethodicalInput, MethodicalOutput] | Nonec                      d S d	 fd}|S )
Nstater<   rE   rF   rO   r,   %OutputTracer[MethodicalOutput] | Nonec                     |                                  |                                 |                                           fdS d S )Nc                >     |                                            S r4   r_   )outresults    r'   <lambda>z,wrapTracer.<locals>.tracer.<locals>.<lambda>]  s    vvciikk22 r)   r   )r   rE   rO   r   r   s      @r'   tracerzwrapTracer.<locals>.tracerV  sK    
 v||~~FF22222tr)   )r   r<   rE   rF   rO   r<   r,   r   r!   )r   r   s   ` r'   
wrapTracerr   P  s4     t      Mr)   c                  `    e Zd ZU  ed          Zded<    ed          Zded<   	 dddZdS )MethodicalTracerFr=   rk   rj   r[   ri   Nrg   rh   r   r,   Callable[[StringTracer], None]c                J    t          || j        | j                  dfd}|S )Nr   r   r,   rM   c                L                         t          |                      d S r4   )setTracer   )r   rq   s    r'   r   z*MethodicalTracer.__get__.<locals>.setTraceo  s%    !!*V"4"455555r)   )r   r   r,   rM   )rr   ri   rj   )r5   rg   r   r   rq   s       @r'   r   zMethodicalTracer.__get__j  s?     1T^TT	6 	6 	6 	6 	6 	6 r)   r4   )rg   rh   r   rh   r,   r   )rV   rb   rc   r   rj   re   ri   r   r!   r)   r'   r   r   c  s         OTuP P PI     %U###F#### -1      r)   r   c                 J    dt          t          t                              z   S )z,
    Create a unique Python identifier.
    _symbol_)r[   nextcounterr!   r)   r'   gensymr   x  s     DMM****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dS )r?   ze
    A L{MethodicalMachine} is an interface to an L{Automaton} that uses methods
    on a class.
    c                `    t                      | _        i | _        t                      | _        d S r4   )r   
_automaton	_reducersr   _symbolr^   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__  s      !QRRRr)   FinitialrC   terminalrD   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                H    t          |           }r|j        _        |S )N)r@   rB   rD   )r<   r   ro   )stateMethodr   r   r5   rD   s     r'   	decoratorz*MethodicalMachine.state.<locals>.decorator  s7    #[Z  E  5/4,Lr)   r!   )r5   r   r   rD   r   s   `` ` r'   r   zMethodicalMachine.state  s0    .	 	 	 	 	 	 	 r)   c                      fd}|S )zM
        Declare an input.

        This is a decorator for methods.
        c                <    t          j        | j                  S )N)rj   rB   ri   )rF   r   r   )inputMethodr5   s    r'   r   z*MethodicalMachine.input.<locals>.decorator  s%    "/+dl   r)   r!   r5   r   s   ` r'   rE   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@   rB   )r   )outputMethodr5   s    r'   r   z+MethodicalMachine.output.<locals>.decorator  s    #DFFFFr)   r!   r   s   ` r'   rO   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   )r5   
startState
inputTokenendStateoutputTokensrK   s         r'   rW   z MethodicalMachine._oneTransition  sD    $ 	%%
HeL.A.A	
 	
 	
 -6
j)))r)   c                      fd}|S ) c                @     t                      fd            }|S )Nc                f    t          | j        j                  } | |j        j                  S r4   )rr   r   r   r   rD   )rg   rq   	decorateer5   s     r'   	serializezBMethodicalMachine.serializer.<locals>.decorator.<locals>.serialize  s7    84<    !y(;(FGGGr)   r   )r   r   r5   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 r4   )r   statesrD   rr   r   r   )	rg   r   r   r   mapping	eachStaterq   r   r5   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   r5   s   ` r'   r   z1MethodicalMachine.unserializer.<locals>.decorator  s=    9	 	 	 	 	 	 r)   r!   r   s   ` r'   unserializerzMethodicalMachine.unserializer  s$    	 	 	 	 	 r)   r,   r   c                6    t          | j        | j                  S r4   )r   r   r   r^   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 r4   r]   )r   s    r'   r   z-MethodicalMachine.asDigraph.<locals>.<lambda>"      (= r)   c                    | j         j        S r4   r]   )rE   s    r'   r   z-MethodicalMachine.asDigraph.<locals>.<lambda>#  r   r)   c                    | j         j        S r4   r]   )rO   s    r'   r   z-MethodicalMachine.asDigraph.<locals>.<lambda>$  s    &-*@ r)   )stateAsStringinputAsStringoutputAsString)
_visualizer   r   )r5   r   s     r'   	asDigraphzMethodicalMachine.asDigraph  sF     	,+++++{O====@@	
 
 
 	
r)   r4   )FFN)r   rC   r   rC   rD   r   )r,   r   )rV   rb   rc   rd   r   r   r9   r   rE   rO   rW   r   r   propertyr   r  r!   r)   r'   r?   r?     s        
     
    TX    ^@   ^   ^6 6 6.   ^   ^( ? ? ? X?
 
 
 
 
r)   r?   )rg   rh   ri   r[   rj   rk   r,   rl   )r7   rA   r,   rM   )r   r   r,   r   )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(   r1   r9   rh   r<   rr   rt   rw   r|   r   r   r   rF   r   r[   StringOutputTracerr   re   r   r   r   r   r?   r!   r)   r'   <module>r     s|   " " " " " " "     



 ( ( ( ( ( ( ( (       1 1 1 1 1 1       = = = = = = = = = = = = = =g+++++++       @ @ @ @ @ @ @ @ @ @ @ @ ( ( ( ( ( (
 +
      .  .  $ $:$ :$ :$ :$ :$f :$ :$ :$z   $	 	 	  8 8 8 8 && && &&R GCLLGCLL e/$ /$ /$ /$ /$f /$ /$ /$d $$ $ $ $ $v $ $ $B seTk* P P P P P   & e    v   " %''+ + +f
 f
 f
 f
 f
 f
 f
 f
 f
 f
r)   