o
    >h;D                     @  s  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 rCd 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dg dZdd Z dd Z!dd Z"eddG dd de#Z$dDd!d"Z%d#d$ Z&d%d& Z'dEd*d+Z(d,d- Z)ed.Z*ed/Z+ed0d1G d2d3 d3e#Z,eddG d4d5 d5e#Z-ee.gdf Z/d6Z0d7e1d8< dFd<d=Z2ed0d1G d>d? d?e#Z3e Z4d@dA Z5G dBdC dCe#Z6dS )G    )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              
   C  sX   t | }tt|j|j|j|jr|jndt|j|jr"t|j	 ndt|j
	 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   r   r   r   r   itemsr   )funcspecr   r   s/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/automat/_methodical.py_getArgSpec"   s   	r&   c                 C  s@   t | j| j | jrdnd | jrdnd tdd | jD  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                 s  s     | ]}|d  dkr|V  qdS )r   returnNr   ).0ar   r   r%   	<genexpr>L   s    z_getArgNames.<locals>.<genexpr>)setr   r   r   r   r!   r   )r$   r   r   r%   _getArgNames9   s   r,   c                   s   t   fd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                   s    | fi |S Nr   )selfkwfr   r%   g[   s   z_keywords_only.<locals>.gr   )r1   r2   r   r0   r%   _keywords_onlyP   s   r3   T)frozenc                   @  s`   e Zd ZU dZeddZded< e Zded< eddZded	< d
d
e	fdddZ
d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                 C  s   |du r| }|du rg }t |j}|D ]!}t |j}||s4tdj|jj|jjt|jt|jdq| 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})r>   outputinputSignatureoutputSignature)
r,   argSpecissubset	TypeErrorformatr;   __name__r    r9   _oneTransition)r.   r>   r@   rB   rD   	inputArgsrG   
outputArgsr   r   r%   uponl   s&   


zMethodicalState.uponstrc                 C     | j jS r-   r;   rN   r.   r   r   r%   _name      zMethodicalState._name)
r>   r?   r@   rA   rB   rC   rD   rE   r'   rF   r'   rS   )rN   
__module____qualname____doc__r   r9   __annotations__r;   r=   listrR   rW   r   r   r   r%   r5   b   s   
 0r5   oselfobjectsymbolrS   	automaton=Automaton[MethodicalState, MethodicalInput, MethodicalOutput]r'   @Transitioner[MethodicalState, MethodicalInput, MethodicalOutput]c                 C  s0   t | |d}|du rt||j}t| || |S )z
    Get a L{Transitioner}
    N)getattrr   initialStatesetattr)r_   ra   rb   transitionerr   r   r%   _transitionerFromInstance   s   ri   c                   C  s   d S r-   r   r   r   r   r%   _empty   s   rj   c                   C  s   dS )	docstringNr   r   r   r   r%   
_docstring   s    rl   r1   r:   rF   c                 C  s$   | j jtj jtj jfvrtdd S )Nzfunction body must be empty)__code__co_coderj   rl   
ValueErrorr0   r   r   r%   assertNoCode   s   rp   c                   s   t t|jdd | }jr| }n	fdd|D }t ||D ]
\}}||f7 q!t|jddd |jddd }fdd|D }	|	| jrT|	}
||
fS jdd j   fd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                   s   g | ]\}}| j v r|qS r   )r   r(   nv)
outputSpecr   r%   
<listcomp>       z_filterArgs.<locals>.<listcomp>c                   s   i | ]\}}| vr||qS r   r   rq   )passed_arg_namesr   r%   
<dictcomp>   rv   z_filterArgs.<locals>.<dictcomp>c                   s   i | ]\}}| v r||qS r   r   rq   )all_accepted_namesr   r%   ry      s    )	r!   zipr   r   r   updater   r   r"   )r   kwargs	inputSpecrt   
named_argsreturn_argsnamevaluer   full_kwargsreturn_kwargsr   )rz   rt   rx   r%   _filterArgs   s$   "

r   TRF)eqc                   @  s   e Zd ZU dZeddZded< e Zded< eddZded	< ee	dd
Z
ded< edddZded< dddZddddZdddZdS ) r?   z.
    An input for a L{MethodicalMachine}.
    Fr6   rc   rb   r:   r;   rS   ra   )default_factoryr7   z1dict[MethodicalState, Callable[[Iterable[T]], R]]
collectors)initr7   r   rJ   r'   rF   c                 C  s   t | j| _t| j d S r-   )r&   r;   rJ   rp   rV   r   r   r%   __post_init__  s   zMethodicalInput.__post_init__Nr_   r`   typec                   s:   t  jjtjtjd fd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   r`   r}   r'   c                    s   j  g| R i | j}\}}j| }g }|D ]%}|d ur)|| t| |j|j\}}	| g|R i |	}
||
 q||S r-   )r;   _state
transitionr   r   rJ   append)r   r}   previousStaterB   	outTracerrD   valuesrG   r)   kr   r_   r.   rh   r   r%   doInput  s   
z(MethodicalInput.__get__.<locals>.doInputN)r   r`   r}   r`   r'   r`   )ri   ra   rb   r   r;   r   )r.   r_   r   r   r   r   r%   __get__  s
   zMethodicalInput.__get__c                 C  rT   r-   rU   rV   r   r   r%   rW   &  rX   zMethodicalInput._namer'   rF   r-   )r_   r`   r   rF   r'   r`   rY   )rN   rZ   r[   r\   r   rb   r]   r;   ra   dictr   rJ   r   r   rW   r   r   r   r%   r?      s   
 
r?   c                   @  sh   e Zd ZU dZeddZded< ded< eddddZd	ed
< dddZdddZ	dd Z
dddZdS )MethodicalOutputz/
    An output for a L{MethodicalMachine}.
    Fr6   r8   r9   r:   r;   )r   r7   comparer   rJ   r'   rF   c                 C  s   t | j| jd< d S )NrJ   )r&   r;   __dict__rV   r   r   r%   r   4  s   zMethodicalOutput.__post_init__Nc                 C  s   t dj|j| jjd)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;   )AttributeErrorrM   rN   r;   r.   r_   r   r   r   r%   r   7  s   
zMethodicalOutput.__get__c                 O  s   | j |g|R i |S )z-
        Call the underlying method.
        )r;   )r.   r_   r   r}   r   r   r%   __call__B  s   zMethodicalOutput.__call__rS   c                 C  rT   r-   rU   rV   r   r   r%   rW   H  rX   zMethodicalOutput._namer   r-   rY   )rN   rZ   r[   r\   r   r9   r]   rJ   r   r   r   rW   r   r   r   r%   r   *  s   
 

r   z4Callable[[str, str, str], StringOutputTracer | None]r   StringTracerwrappedStringTracer | NoneATracer[MethodicalState, MethodicalInput, MethodicalOutput] | Nonec                   s    d u rd S d
 fdd	}|S )Nstater5   r>   r?   rG   r'   %OutputTracer[MethodicalOutput] | Nonec                   s0   |   |  |    d ur fddS d S )Nc                   s    |   S r-   rW   )outresultr   r%   <lambda>]  s    z,wrapTracer.<locals>.tracer.<locals>.<lambda>r   )r   r>   rG   r   r   r%   tracerV  s   zwrapTracer.<locals>.tracer)r   r5   r>   r?   rG   r5   r'   r   r   )r   r   r   r   r%   
wrapTracerP  s   
r   c                   @  s@   e Zd ZU eddZded< eddZded< 	ddddZdS )MethodicalTracerFr6   rc   rb   rS   ra   Nr_   r`   r   r'   Callable[[StringTracer], None]c                   s"   t || j| j d fdd}|S )Nr   r   r'   rF   c                   s     t|  d S r-   )setTracer   )r   rh   r   r%   r   o  s   z*MethodicalTracer.__get__.<locals>.setTrace)r   r   r'   rF   )ri   ra   rb   )r.   r_   r   r   r   r   r%   r   j  s   zMethodicalTracer.__get__r-   )r_   r`   r   r`   r'   r   )rN   rZ   r[   r   rb   r]   ra   r   r   r   r   r%   r   c  s   
 r   c                   C  s   dt tt S )z,
    Create a unique Python identifier.
    _symbol_)rS   nextcounterr   r   r   r%   gensymx  s   r   c                   @  s   e Zd ZdZdd ZdddZe	d d!ddZedd Zedd Z	dd Z
edd Zedd Zed"ddZdd ZdS )#r8   ze
    A L{MethodicalMachine} is an interface to an L{Automaton} that uses methods
    on a class.
    c                 C  s   t  | _i | _t | _d S r-   )r   
_automaton	_reducersr   _symbolrV   r   r   r%   __init__  s   zMethodicalMachine.__init__Nc                 C  s   |dur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   r   r   r%   r     s   zMethodicalMachine.__get__Finitialr<   terminalr=   r   c                   s    fd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                   s   t | d} r|j_|S )N)r9   r;   r=   )r5   r   rf   )stateMethodr   r   r.   r=   r   r%   	decorator  s   z*MethodicalMachine.state.<locals>.decoratorr   )r.   r   r   r=   r   r   r   r%   r     s   zMethodicalMachine.statec                       fdd}|S )zM
        Declare an input.

        This is a decorator for methods.
        c                   s   t  j|  jdS )N)rb   r;   ra   )r?   r   r   )inputMethodrV   r   r%   r     s   
z*MethodicalMachine.input.<locals>.decoratorr   r.   r   r   rV   r%   r>     s   zMethodicalMachine.inputc                   r   )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                   s   t  | dS )N)r9   r;   )r   )outputMethodrV   r   r%   r     s   z+MethodicalMachine.output.<locals>.decoratorr   r   r   rV   r%   rG     s   zMethodicalMachine.outputc                 C  s$   | j |||t| ||j|< dS )z.
        See L{MethodicalState.upon}.
        N)r   addTransitionr!   r   )r.   
startState
inputTokenendStateoutputTokensrD   r   r   r%   rO     s   z MethodicalMachine._oneTransitionc                   r   ) c                      t   fdd}|S )Nc                   s   t | jj} | |jjS r-   )ri   r   r   r   r=   )r_   rh   	decorateer.   r   r%   	serialize  s   
zBMethodicalMachine.serializer.<locals>.decorator.<locals>.serializer   )r   r   rV   r   r%   r     s   z/MethodicalMachine.serializer.<locals>.decoratorr   r   r   rV   r%   
serializer  s   
zMethodicalMachine.serializerc                   r   )r   c                   r   )Nc                   sR    | g|R i |}i }j  D ]}|||j< qt| jj }|| |_d S r-   )r   statesr=   ri   r   r   )r_   r   r}   r   mapping	eachStaterh   r   r   r%   unserialize   s   

zFMethodicalMachine.unserializer.<locals>.decorator.<locals>.unserializer   )r   r   rV   r   r%   r     s   z1MethodicalMachine.unserializer.<locals>.decoratorr   r   r   rV   r%   unserializer  s   zMethodicalMachine.unserializerr'   r   c                 C  s   t | j| jS r-   )r   r   r   rV   r   r   r%   	_setTrace  s   zMethodicalMachine._setTracec                 C  s*   ddl m} || jdd dd 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                 S  rT   r-   rU   )r   r   r   r%   r   "      z-MethodicalMachine.asDigraph.<locals>.<lambda>c                 S  rT   r-   rU   )r>   r   r   r%   r   #  r   c                 S  rT   r-   rU   )rG   r   r   r%   r   $  r   )stateAsStringinputAsStringoutputAsString)
_visualizer   r   )r.   r   r   r   r%   	asDigraph  s   
zMethodicalMachine.asDigraphr-   )FFN)r   r<   r   r<   r=   r   )r'   r   )rN   rZ   r[   r\   r   r   r3   r   r>   rG   rO   r   r   propertyr   r   r   r   r   r%   r8     s&    

 



r8   )r_   r`   ra   rS   rb   rc   r'   rd   )r1   r:   r'   rF   )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&   r,   r3   r`   r5   ri   rj   rl   rp   r   r   r   r?   r   rS   StringOutputTracerr   r]   r   r   r   r   r8   r   r   r   r%   <module>   sR   

=
)2!
