o
    !wi@"                     @   st   d dl Z ddgZdd Zdd Zdd	 Zd
d Zdd ZddddZdd ZG dd de	Z
G dd de jZdS )    NJProxyJImplementsc                 C   sZ   | D ](}|j  D ] }| d@ dkrq	t| |vr)td|j  | f q	qd S )Ni   r   z6Interface '%s' requires method '%s' to be implemented.)class_
getMethodsgetModifiersstrgetNameNotImplementedError)
interfaces	overrides	interfacemethod r   J/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/jpype/_jproxy.py_checkInterfaceOverrides   s   r   c              	   C   sJ   i }| j  D ]\}}zt|d}||f||< W q ty"   Y qw |S )N__joverride__)__dict__itemsobject__getattribute__AttributeError)clsr   kvattrr   r   r   _classOverrides%   s   r   c                 C   s   t |}t| }t|| |S N)_convertInterfacesr   r   )r   intf
actualIntfr   r   r   r   _prepareInterfaces1   s   
r    c                    sH   t  tstdt   fdd}d|i}td j  tjf|S )z (internal) Create a proxy from a Python class with
    @JOverride notation on methods evaluated at first
    instantiation.
    )JImplements only applies to types, not %sc                    s6   t | dd }|d u rt }|| _tj| d d |S )N__jpype_interfaces__)getattrr    r"   _jpype_JProxy__new__)tpargskwargsr   r   r   r   r   newB   s
   
z"_createJProxyDeferred.<locals>.newr&   proxy.%s)
isinstancetype	TypeError__name__r$   r%   r   r   r+   membersr   r*   r   _createJProxyDeferred9   s   

	r3   c                    sP   t | tstdt|  t| |  fdd}d|i}td| j | tjf|S )zu (internal) Create a proxy from a Python class with
    @JOverride notation on methods evaluated at declaration.
    r!   c                    s.   t j| d d  }| j|g|R i | |S r   )r$   r%   r&   __init__)r'   r(   r)   selfr   r   r   r+   Z   s   z_createJProxy.<locals>.newr&   r,   )r-   r.   r/   r    r0   r$   r%   r1   r   r6   r   _createJProxyP   s   


r7   F)deferredc                    s(   | r fdd}|S  fdd}|S )a   Annotation for creating a new proxy that implements one or more
    Java interfaces.

    This annotation is placed on an ordinary Python class.  The annotation
    requires a list of interfaces.  It must implement all of the java
    methods for each of the interfaces.  Each implemented method
    should have a @JOverride annotation.  The JVM must be running in
    order to validate the class.

    Args:
      interfaces (str*,JClass*): Strings or JClasses for each Java interface
        this proxy is to implement.

    Kwargs:
      deferred (bool):
        Whether to defer validation of the interfaces and overrides until
        the first instance instantiation (True) or validate at declaration
        (False). Deferred validation allows a proxy class to be declared prior
        to starting the JVM.  Validation only occurs once per proxy class,
        thus there is no performance penalty.  Default False.

    Example:

      .. code-block:: python

          @JImplement("java.lang.Runnable")
          class MyImpl(object):
             @JOverride
             def run(self, arg):
               pass

          @JImplement("org.my.Interface1", "org.my.Interface2")
          class MyImpl(object):
             @JOverride
             def method(self, arg):
               pass

    c                       t | g R i S r   )r3   r   r
   r)   r   r   JProxyCreator      z"JImplements.<locals>.JProxyCreatorc                    r9   r   )r7   r:   r;   r   r   r<      r=   r   )r8   r
   r)   r<   r   r;   r   r   d   s
   'c                 C   s   g }| D ]#}t |tjr|| qt |tst|ds"|| q|| qt }|D ]}t |tr=|t| q-|| q-|sIt	d|D ]}t |tjs\t	dt
|j t|tjsit	d|j qKt|S )zf (internal) Convert a list of interface names into
    a list of interfaces suitable for a proxy.
    __iter__z-At least one Java interface must be specifiedz'%s' is not a Java interface)r-   r$   JClassappendr   hasattrextendsetaddr/   r.   r0   
issubclass
JInterfacetuple)r   intflistitemr   r   r   r   r   r      s.   
r   c                   @   s   e Zd Zdd Zdd ZdS )
_JFromDictc                 C   s
   || _ d S r   )dict)r5   rK   r   r   r   r4      s   
z_JFromDict.__init__c                 C   s,   z	t | d| W S  ty   Y tdw )NrK   zattribute not found)r   r   KeyErrorr   )r5   namer   r   r   r      s   z_JFromDict.__getattribute__N)r0   
__module____qualname__r4   r   r   r   r   r   rJ      s    rJ   c                   @   s&   e Zd ZdZdddZedd ZdS )	r   a   Define a proxy for a Java interface.

    This is an older style JPype proxy interface that uses either a
    dictionary or an object instance to implement methods defined
    in Java.  The Python object can be held by Java and its lifespan
    will continue as long as java holds a reference to the object
    instance.  New code should use ``@JImplements`` annotation as
    it will support improved type safety and error handling.

    Name lookups can either made using a dictionary or an object
    instance.  One of these two options must be specified.

    Args:
        intf: either a single interface or a list of java interfaces.
            The interfaces can either be defined by strings or
            JClass instance.  Only interfaces may be used in a
            proxy,
        dict (dict[string, callable], optional): specifies a dictionary
            containing the methods to be called when executing the
            Java interface methods.
        inst (object, optional): specifies an object with methods
            whose names matches the Java interfaces methods.
        convert (bool, optional): if True the proxy is unwrapped
            to a Python object.
    NFc                 C   sJ   t |g}|d urt|t|||S |d ur!tj| ||||S td)Nz a dict or inst must be specified)r   r$   r%   rJ   r&   r/   )r   r   rK   instconvertr   r   r   r   r&      s   
zJProxy.__new__c                 C   s   t | tjs| S | jS r   )r-   r$   r%   __javainst__)objr   r   r   unwrap   s   zJProxy.unwrap)NNF)r0   rN   rO   __doc__r&   staticmethodrT   r   r   r   r   r      s
    
)r$   __all__r   r   r    r3   r7   r   r   r   rJ   r%   r   r   r   r   r   <module>   s   0&