o
    Ug[C                     @  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   _/var/www/html/Testing_prj/Navya-Bakers/venv/lib/python3.10/site-packages/automat/_methodical.py_getArgSpec"   s   	r&   c                 C  s2   t | j| j | jrdnd | jrdnd | j 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$   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%   gW   s   z_keywords_only.<locals>.gr   )r-   r.   r   r,   r%   _keywords_onlyL   s   r/   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]return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formatr7   __name__r    r5   _oneTransition)r*   r:   r<   r>   r@   	inputArgsrD   
outputArgsr   r   r%   uponh   s&   


zMethodicalState.uponstrc                 C     | j jS r)   r7   rK   r*   r   r   r%   _name      zMethodicalState._name)
r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rB   rP   )rK   
__module____qualname____doc__r   r5   __annotations__r7   r9   listrO   rT   r   r   r   r%   r1   ^   s   
 0r1   oselfobjectsymbolrP   	automaton=Automaton[MethodicalState, MethodicalInput, MethodicalOutput]rB   @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\   r^   r_   transitionerr   r   r%   _transitionerFromInstance   s   rf   c                   C  s   d S r)   r   r   r   r   r%   _empty   s   rg   c                   C  s   dS )	docstringNr   r   r   r   r%   
_docstring   s    ri   r-   r6   rC   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_coderg   ri   
ValueErrorr,   r   r   r%   assertNoCode   s   rm   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   .0nv)
outputSpecr   r%   
<listcomp>       z_filterArgs.<locals>.<listcomp>c                   s   i | ]\}}| vr||qS r   r   rn   )passed_arg_namesr   r%   
<dictcomp>   rt   z_filterArgs.<locals>.<dictcomp>c                   s   i | ]\}}| v r||qS r   r   rn   )all_accepted_namesr   r%   rw      s    )	r!   zipr   r   r   updater   r   r"   )r   kwargs	inputSpecrr   
named_argsreturn_argsnamevaluer   full_kwargsreturn_kwargsr   )rx   rr   rv   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}.
    Fr2   r`   r_   r6   r7   rP   r^   )default_factoryr3   z1dict[MethodicalState, Callable[[Iterable[T]], R]]
collectors)initr3   r   rG   rB   rC   c                 C  s   t | j| _t| j d S r)   )r&   r7   rG   rm   rS   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{   rB   c                    s   j  g| R i | j}\}}j| }g }|D ]%}|d ur)|| t| |j|j\}}	| g|R i |	}
||
 q||S r)   )r7   _state
transitionr   r   rG   append)r   r{   previousStater>   	outTracerr@   valuesrD   akr   r\   r*   re   r   r%   doInput  s   
z(MethodicalInput.__get__.<locals>.doInputN)r   r]   r{   r]   rB   r]   )rf   r^   r_   r   r7   r   )r*   r\   r   r   r   r   r%   __get__  s
   zMethodicalInput.__get__c                 C  rQ   r)   rR   rS   r   r   r%   rT   "  rU   zMethodicalInput._namerB   rC   r)   )r\   r]   r   rC   rB   r]   rV   )rK   rW   rX   rY   r   r_   rZ   r7   r^   dictr   rG   r   r   rT   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}.
    Fr2   r4   r5   r6   r7   )r   r3   comparer   rG   rB   rC   c                 C  s   t | j| jd< d S )NrG   )r&   r7   __dict__rS   r   r   r%   r   0  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.)clsr7   )AttributeErrorrJ   rK   r7   r*   r\   r   r   r   r%   r   3  s   
zMethodicalOutput.__get__c                 O  s   | j |g|R i |S )z-
        Call the underlying method.
        )r7   )r*   r\   r   r{   r   r   r%   __call__>  s   zMethodicalOutput.__call__rP   c                 C  rQ   r)   rR   rS   r   r   r%   rT   D  rU   zMethodicalOutput._namer   r)   rV   )rK   rW   rX   rY   r   r5   rZ   rG   r   r   r   rT   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stater1   r:   r;   rD   rB   %OutputTracer[MethodicalOutput] | Nonec                   s0   |   |  |    d ur fddS d S )Nc                   s    |   S r)   rT   )outresultr   r%   <lambda>Y  s    z,wrapTracer.<locals>.tracer.<locals>.<lambda>r   )r   r:   rD   r   r   r%   tracerR  s   zwrapTracer.<locals>.tracer)r   r1   r:   r;   rD   r1   rB   r   r   )r   r   r   r   r%   
wrapTracerL  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 )MethodicalTracerFr2   r`   r_   rP   r^   Nr\   r]   r   rB   Callable[[StringTracer], None]c                   s"   t || j| j d fdd}|S )Nr   r   rB   rC   c                   s     t|  d S r)   )setTracer   )r   re   r   r%   r   k  s   z*MethodicalTracer.__get__.<locals>.setTrace)r   r   rB   rC   )rf   r^   r_   )r*   r\   r   r   r   r   r%   r   f  s   zMethodicalTracer.__get__r)   )r\   r]   r   r]   rB   r   )rK   rW   rX   r   r_   rZ   r^   r   r   r   r   r%   r   _  s   
 r   c                   C  s   dt tt S )z,
    Create a unique Python identifier.
    _symbol_)rP   nextcounterr   r   r   r%   gensymt  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 )#r4   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   _symbolrS   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initialr8   terminalr9   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)r5   r7   r9   )r1   r   rc   )stateMethodr   r   r*   r9   r   r%   	decorator  s   z*MethodicalMachine.state.<locals>.decoratorr   )r*   r   r   r9   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)r_   r7   r^   )r;   r   r   )inputMethodrS   r   r%   r     s   
z*MethodicalMachine.input.<locals>.decoratorr   r*   r   r   rS   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)r5   r7   )r   )outputMethodrS   r   r%   r     s   z+MethodicalMachine.output.<locals>.decoratorr   r   r   rS   r%   rD     s   zMethodicalMachine.outputc                 C  s$   | j |||t| ||j|< dS )z.
        See L{MethodicalState.upon}.
        N)r   addTransitionr!   r   )r*   
startState
inputTokenendStateoutputTokensr@   r   r   r%   rL     s   z MethodicalMachine._oneTransitionc                   r   ) c                      t   fdd}|S )Nc                   s   t | jj} | |jjS r)   )rf   r   r   r   r9   )r\   re   	decorateer*   r   r%   	serialize  s   
zBMethodicalMachine.serializer.<locals>.decorator.<locals>.serializer   )r   r   rS   r   r%   r     s   z/MethodicalMachine.serializer.<locals>.decoratorr   r   r   rS   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   statesr9   rf   r   r   )r\   r   r{   r   mapping	eachStatere   r   r   r%   unserialize  s   

zFMethodicalMachine.unserializer.<locals>.decorator.<locals>.unserializer   )r   r   rS   r   r%   r     s   z1MethodicalMachine.unserializer.<locals>.decoratorr   r   r   rS   r%   unserializer  s   zMethodicalMachine.unserializerrB   r   c                 C  s   t | j| jS r)   )r   r   r   rS   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  rQ   r)   rR   )r   r   r   r%   r         z-MethodicalMachine.asDigraph.<locals>.<lambda>c                 S  rQ   r)   rR   )r:   r   r   r%   r     r   c                 S  rQ   r)   rR   )rD   r   r   r%   r      r   )stateAsStringinputAsStringoutputAsString)
_visualizer   r   )r*   r   r   r   r%   	asDigraph  s   
zMethodicalMachine.asDigraphr)   )FFN)r   r8   r   r8   r9   r   )rB   r   )rK   rW   rX   rY   r   r   r/   r   r:   rD   rL   r   r   propertyr   r   r   r   r   r%   r4   {  s&    

 



r4   )r\   r]   r^   rP   r_   r`   rB   ra   )r-   r6   rB   rC   )r   r   rB   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(   r/   r]   r1   rf   rg   ri   rm   r   r   r   r;   r   rP   StringOutputTracerr   rZ   r   r   r   r   r4   r   r   r   r%   <module>   sR   

=
)2!
