o
    iX'                     @  sJ  d dl mZ d dlZd dlZd dlZd dlmZmZmZmZm	Z	m
Z
 d dlmZ d dlZerFd dlZd dlmZmZ d dlmZ d dlmZ d9ddZd:ddZd;ddZd<ddZd=ddZd>d!d"Zd?d$d%Zd@d'd(ZdAd,d-ZdBd2d3Zzd d4lmZ W eZdS  ey   d dlZd dl Z d dl!Z!ddd5d6d7d8ZY eZdS w )C    )annotationsN)TYPE_CHECKINGAny	AwaitableCallableOptionalType)	TypeGuard)InputTOutputT)ServiceT)OperationHandlerobjr   return$Optional[nexusrpc.ServiceDefinition]c                 C  sP   t | tr| jd}n	t| di d}|r&t |tjs&td| j d|S )zIReturn the :py:class:`nexusrpc.ServiceDefinition` for the object, or None__nexus_service____dict__zService definition zC has a __nexus_service__ attribute that is not a ServiceDefinition.)	
isinstancetyper   getgetattrnexusrpcServiceDefinition
ValueError__name__)r   defn r   T/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/nexusrpc/_util.pyget_service_definition   s   
r   clsType[ServiceT]service_definitionnexusrpc.ServiceDefinitionNonec                 C  s4   t | tstd|  dt|  dt| d| dS )z?Set the :py:class:`nexusrpc.ServiceDefinition` for this object.z	Expected z to be a class, but is .r   N)r   r   	TypeErrorsetattr)r   r!   r   r   r   set_service_definition$   s   
r'   Optional[nexusrpc.Operation]c                 C  s   t | ddS )zReturn the :py:class:`nexusrpc.Operation` for the object, or None

    ``obj`` should be a decorated operation start method.
    __nexus_operation__N)r   r   r   r   r   get_operation_definition-   s   r+   operation_definitionnexusrpc.Operationc                 C     t | d| dS )znSet the :py:class:`nexusrpc.Operation` for this object.

    ``obj`` should be an operation start method.
    r)   Nr&   )r   r,   r   r   r   set_operation_definition7      r0   rtuple[Optional[Callable[[Any], OperationHandler[InputT, OutputT]]], Optional[nexusrpc.Operation[InputT, OutputT]]]c                 C  sB   t | }|r	| }nt| dd }rt |}t|tjsdS ||fS )zReturn the :py:class:`Operation` for the object along with the factory function.

    ``obj`` should be a decorated operation start method.
    __nexus_operation_factory__N)NN)r+   r   r   r   	Operation)r   op_defnfactoryr   r   r   get_operation_factoryB   s   
r7   operation_factory2Callable[[Any], OperationHandler[InputT, OutputT]]c                 C  r.   )ztSet the :py:class:`OperationHandler` factory for this object.

    ``obj`` should be an operation start method.
    r3   Nr/   )r   r8   r   r   r   set_operation_factoryW   r1   r:   (TypeGuard[Callable[..., Awaitable[Any]]]c                 C  sB   t | tjr| j} t | tjst| p t| o tt| ddS )zn
    Return True if `obj` is an async callable.

    Supports partials of async callable class instances.
    __call__N)r   	functoolspartialfuncinspectiscoroutinefunctioncallabler   r*   r   r   r   is_async_callablei   s   
rC   TypeGuard[Callable[..., Any]]c                 C  s:   t | tjr| j} t | tjst| pt| ot| dS )z-
    Return True if `obj` is a callable.
    r<   )r   r=   r>   r?   r@   
isfunctionrB   hasattrr*   r   r   r   is_callablew   s   rG   fnCallable[..., Any]strc                 C  sB   t | dd }|st| rt| dr| jj}|std|  d|S )Nr   r<   z,Could not determine callable name: expected z' to be a function or callable instance.)r   rB   rF   	__class__r   r%   )rH   method_namer   r   r   get_callable_name   s   rM   type1	Type[Any]type2boolc                 C  s    | |krdS t | t|p|S )NT)
issubclasstyping
get_origin)rN   rP   r   r   r   
is_subtype   s   rU   )get_annotationsF)globalslocalseval_strc                  s  t | trEt| dd}|r!t|dr!|dd}t |tjr d}nd}d}t| dd}|r<tj|d}|r<t|dd}t	t
| }	| }
n2t | tjr[t| dd}t| d}d}	d}
nt| rpt| dd}t| dd}d}	| }
nt| d|du r}i S t |t	st| d|si S |st	|S |
dur	 t|
d
r|
j}
qt |
tjr|
j}
q	 t|
dr|
j} du r| du r|	pi t| dd }rdd |D B  fdd| D }|S )aY  Compute the annotations dict for an object.

        obj may be a callable, class, or module.
        Passing in an object of any other type raises TypeError.

        Returns a dict.  get_annotations() returns a new dict every time
        it's called; calling it twice on the same object will return two
        different but equivalent dicts.

        This function handles several details for you:

        * If eval_str is true, values of type str will
            be un-stringized using eval().  This is intended
            for use with stringized annotations
            ("from __future__ import annotations").
        * If obj doesn't have an annotations dict, returns an
            empty dict.  (Functions and methods always have an
            annotations dict; classes, modules, and other types of
            callables may not.)
        * Ignores inherited annotations on classes.  If a class
            doesn't have its own annotations dict, returns an empty dict.
        * All accesses to object members and dict values are done
            using getattr() and dict.get() for safety.
        * Always, always, always returns a freshly-created dict.

        eval_str controls whether or not values of type str are replaced
        with the result of calling eval() on those values:

        * If eval_str is true, eval() is called on values of type str.
        * If eval_str is false (the default), values of type str are unchanged.

        globals and locals are passed in to eval(); see the documentation
        for eval() for more information.  If either globals or locals is
        None, this function may replace that value with a context-specific
        default, contingent on type(obj):

        * If obj is a module, globals defaults to obj.__dict__.
        * If obj is a class, globals defaults to
            sys.modules[obj.__module__].__dict__ and locals
            defaults to the obj class namespace.
        * If obj is a callable, globals defaults to obj.__globals__,
            although if obj is a wrapped function (using
            functools.update_wrapper()) it is first unwrapped.
        r   Nr   __annotations__
__module____globals__z% is not a module, class, or callable.z+.__annotations__ is neither a dict nor NoneT__wrapped____type_params__r   c                 S  s   i | ]}|j |qS r   )r   ).0paramr   r   r   
<dictcomp>  s    z#get_annotations.<locals>.<dictcomp>c                   s,   i | ]\}}|t |ts|nt| qS r   )r   rJ   eval)r_   keyvaluerW   rX   r   r   ra     s    )r   r   r   rF   r   typesGetSetDescriptorTypesysmodulesdictvars
ModuleTyperB   r%   r   r]   r=   r>   r?   r\   items)r   rW   rX   rY   obj_dictannobj_globalsmodule_namemodule
obj_localsunwraptype_paramsreturn_valuer   re   r   rV      sp   
-



rV   )r   r   r   r   )r   r    r!   r"   r   r#   )r   r   r   r(   )r   r   r,   r-   r   r#   )r   r   r   r2   )r   r   r8   r9   r   r#   )r   r   r   r;   )r   r   r   rD   )rH   rI   r   rJ   )rN   rO   rP   rO   r   rQ   )"
__future__r   r=   r@   rS   r   r   r   r   r   r   typing_extensionsr	   r   r
   r   nexusrpc._commonr   #nexusrpc.handler._operation_handlerr   r   r'   r+   r0   r7   r:   rC   rG   rM   rU   rV   ImportErrorrh   rf   r   r   r   r   <module>   sF     


	






	
  z 