o
    !wiV                     @  s  d dl mZ d dlZd dlZd dlmZ d dlZd dlZd dlZddl	m
Z ddl	mZ ddl	mZ ddl	mZ dd	l	mZ dd
lT d dlmZ g dZG dd deZzd dlmZ d dlmZ e jejejf7  _W n e yv   Y n e!y   d dl"Z#e#$de d Y nw ej%rej&e'ej(f Z)dd Z*dd Z+dEddZ,	dFdGddZ-da.dd  Z/d!d" Z0dddde/  d#dHd-d.Z1d/d0 Z2d1d2 Z3d3d4 Z4e5e4 e*d5d6d7 Z6e*d8d9d: Z7e*d;d<d= Z8d>d? Z9d@dA Z:e;dBG dCdD dDe<Z=ee_dS )I    )annotationsN)Path   )types)
_classpath)_jcustomizer)_jinit)_pykeywords)*)util)isJVMStartedstartJVMshutdownJVMgetDefaultJVMPathgetJVMVersionisThreadAttachedToJVMattachThreadToJVMdetachThreadFromJVMsynchronizedJVMNotFoundExceptionJVMNotSupportedExceptionJVMNotRunningc                   @  s   e Zd ZdS )r   N)__name__
__module____qualname__ r   r   H/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/jpype/_core.pyr   1   s    r   )__version__z,provided Jedi seems out of date. Version is .c                    sZ   fddt  d tr fdd}|S  d }|j_|j_d_|_d_S )zr Marks a function a deprecated when used as decorator.

    Be sure to start python with -Wd to see warnings.
    c                    sF   dd l } js|j j jj jjf tdd d _ j| i |S )Nr      )category
stacklevelT)warnings_warnedwarn_warning_realr   r   DeprecationWarning)argskwargsr"   )func2r   r   r*   K   s   zdeprecated.<locals>.func2r   c                   s0   | j _ | j_d_| _d d _S )NFz$%s.%s is deprecated, use {0} insteadr   )r   __doc__r#   r&   formatr%   )funcr(   r*   r   r   decorateT   s   zdeprecated.<locals>.decorateFz%s.%s is deprecated)
isinstancestrr   r+   r#   r&   r%   )r(   r/   r-   r   r.   r   
deprecatedF   s   r2   c                   C     t  S )z& True if the JVM is currently running.)_jpype	isStartedr   r   r   r   r   g   s   r   Fc                 C  sb   t | D ]*\}}t|tsq|d| \}}}|r.|s| |= |dur*||  S |  S qg S )zA Get an option and remove it from the current jvm arguments list z%s=N)	enumerater0   r1   	partitionsplit)r(   varsepkeepiv_valuer   r   r   
_getOptionp   s   
r@   	classpath;typing.Union[typing.Sequence[_PathOrStr], _PathOrStr, None]returntyping.List[str]c                 C  s   g }t | ttjfr| f} zt| } W n ty   tdw | D ]=}zt|}W n ty; } ztd|d}~ww t |trEtd|drYddl	}|
|	|d  q!|| q!|S )zY
    Return a classpath which represents the given tuple of classpath specifications
    zUnknown class path elementz/Classpath elements must be strings or Path-likeNr
   r   z.jar)r0   r1   osPathLiketuple	TypeErrorfspathbytesendswithglobextendappend)rA   outelementptherrrL   r   r   r   _expandClassPath   s4   

rS   c                   C  s   t ttdtjjS )Nps1)boolgetattrsysflagsinteractiver   r   r   r   rY         rY   c                  C  s   g } dD ]}t |}|r| r| | qt jdkr;t jdt jdddddfD ]}|r9| r9| | q,n| g d	 t	t 
 }| D ])}td
||f }z|  |  W |  S  tys } zW Y d }~qJd }~ww td)N)TMPDIRTEMPTMPntz~\AppData\Local\Tempz%SYSTEMROOT%\Tempzc:\tempzc:\tmpz\tempz\tmp)z/tmpz/var/tmpz/usr/tmpz%s/%szUnable to find non-ansii path)rE   getenvisasciirN   namepath
expanduser
expandvarsrM   r1   getpidr   touchunlink	ExceptionSystemError)dirlistenvnamedirnamera   dpexr   r   r   	_findTemp   s8   






rp   )jvmpathrA   ignoreUnrecognizedconvertStrings	interruptjvmargsr1   rq   typing.Optional[_PathOrStr]rr   rU   rs   rt   Nonec              
     s  t  rtdtrtdt|}|r8|d du s(t|d tr8|d ds8| r.td|d } |dd }| s>t	 } nt
| } g }t|dtj}|rY|durVtd	|}n|du rat }t
jt
jt
jtd
}	t
j|	s|td|	 t|ddd}
t|}||	 ttt|}tj|}d}| s|
rtd|	 sddl}ddl}|jt d\}}| std| |	| |}	|}t
!| ddl"m#} |dd|	 d|| dg7 }n|d| g7 }zKddl$  fddt% D } fdd|D }t &| t'|| |||| t(  t)||D ]\}}z	 *|| W q  j+y5   Y qw W dS  tyo } z*t|}d|v rjddl,}|-d|}|rjt.|/dd }t|  d| | d}~ww )a  
    Starts a Java Virtual Machine.  Without options it will start
    the JVM with the default classpath and jvmpath.

    The default classpath is determined by ``jpype.getClassPath()``.
    The default jvmpath is determined by ``jpype.getDefaultJVMPath()``.

    Parameters:
     *jvmargs (Optional, str[]): Arguments to give to the JVM.
        The first argument may be the path to the JVM.

    Keyword Arguments:
      jvmpath (str, PathLike):  Path to the jvm library file,
        Typically one of (``libjvm.so``, ``jvm.dll``, ...)
        Using None will apply the default jvmpath.
      classpath (str, PathLike, [str, PathLike]): Set the classpath for the JVM.
        This will override any classpath supplied in the arguments
        list. A value of None will give no classpath to JVM.
      ignoreUnrecognized (bool): Option to ignore
        invalid JVM arguments. Default is False.
      convertStrings (bool): Option to force Java strings to
        cast to Python strings. This option is to support legacy code
        for which conversion of Python strings was the default. This
        will globally change the behavior of all calls using
        strings, and a value of True is NOT recommended for newly
        developed code.
      interrupt (bool): Option to install ^C signal handlers.
        If True then ^C will stop the process, else ^C will
        transfer control to Python rather than halting.  If
        not specified will be False if Python is started as
        an interactive shell.

    Raises:
      OSError: if the JVM cannot be started or is already running.
      TypeError: if a keyword argument conflicts with the positional arguments.

     zJVM is already startedzJVM cannot be restartedr   N-zjvmpath specified twicer   z-Djava.class.pathzclasspath specified twicezorg.jpype.jarz0Unable to find org.jpype.jar support library at z-Djava.system.class.loaderT)r;   zQsystem classloader cannot be specified with non ascii characters in the classpath)dirz$Unable to find ascii temp directory.)quotez5-Djava.system.class.loader=org.jpype.JPypeClassLoaderz-Djava.class.path=%sz-Djpype.class.path=%sz-Xshare:offc                   s(   g | ]}| d r|dkrt |qS )LC_LC_ALL)
startswithrV   .0r<   localer   r   
<listcomp>`  s    zstartJVM.<locals>.<listcomp>c                   s   g | ]}  |qS r   )	getlocaler~   r   r   r   r   c  s    UnsupportedClassVersionz([0-9]+)\.[0-9]+,   z$ is older than required Java version)0r4   r5   OSError_JVM_startedlistr0   r1   r}   rH   r   rE   rI   r@   r   _SEPgetClassPathrb   joinrl   __file__existsRuntimeErrorrS   rN   filterlenr`   
ValueErrortempfileshutilmkstemprp   copyfilecloseurllib.parserz   r   ry   startuprG   initializeResourceszip	setlocaleErrorresearchintgroup)rq   rA   rr   rs   rt   ru   jvm_argsextra_jvm_argsold_classpathsupport_libsystem_class_loaderjava_class_pathtmpr   r   fdrb   rz   
categoriespriorr<   jro   sourcer   matchversionr   r   r   r      s   2(





r   c                   C  s6  d t _t dt _t dt _t dt _t dt _t dt _t dt _t dt _t dt _	t d	t _
t d
t _t dt _t dt _t dt _t dt _t jt j_t jt j_t jjtj_t j	jtj_t j
jtj_t jjtj_t jjtj_t jjtj_t jjtj_t jjtj_tdtj_tdtj_tdtj_tdtj_tdtj_tdtj_tdtj_tdtj_i t _ t jt j t!< t jt j t"< t jt j t#< t jt j t$< t jt j t%< t jt j t&< t jt j t j'< t jt j tj< t j	t j tj< t j
t j tj< t jt j tj< t jt j tj< t jt j tj< t jt j tj< t jt j tj< t jt j t%d < t jt j t j< i t _(tjt j(t!< tjt j(t"< tjt j(t#< t jt j(t$< t jt j(t%< t jt j(t&< t jt j(t j< t jt j(t j< t jt j(t j< t)*  t d+t,t-j. t d/ t _0t j01 t _2da3d S )Nzjava.lang.Objectzjava.lang.Throwablezjava.lang.Exceptionzjava.lang.Classzjava.lang.Stringzjava.lang.RuntimeExceptionzjava.lang.Booleanzjava.lang.Bytezjava.lang.Characterzjava.lang.Shortzjava.lang.Integerzjava.lang.Longzjava.lang.Floatzjava.lang.Doublebooleanbytecharshortr   longfloatdoublezorg.jpype.JPypeKeywordsorg.jpype.JPypeContextT)4r4   _java_lang_ClassJClass_java_lang_Object_java_lang_Throwable_java_lang_Exception_java_lang_String_java_lang_RuntimeException_java_lang_Boolean_java_lang_Byte_java_lang_Character_java_lang_Short_java_lang_Integer_java_lang_Long_java_lang_Float_java_lang_DoubleJStringclass_JObjectTYPE_jtypesJBooleanJByteJCharJShortJIntJLongJFloatJDoubler   getClassHints_hints_object_classesrU   r   r   r1   typeobject_JClass_type_classesr   runJVMInitializerssetKeywordsr   r	   	_KEYWORDSgetInstanceJPypeContextgetClassLoaderJPypeClassLoaderr   r   r   r   r   r   {  s   


r   c                  C  sP   ddl } ddl}|  |  urtdt r|jjtj	_
t|jjd dS )z Shuts down the JVM.

    This method shuts down the JVM and disables access to existing
    Java objects. Due to limitations in the JPype, it is not possible to
    restart the JVM after being terminated.
    r   Nz(Shutdown must be called from main threadF)	threadingjpype.configcurrent_threadmain_threadr   r4   r5   configfree_resourcesr   freeResourcesshutdowndestroy_jvm)r   jpyper   r   r   r     s   r   c                  C  sB   zdd l } | jjrt| jjd W d S W d S  ty    Y d S w )Nr   F)r   r   onexitr4   r   r   r   )r   r   r   r   _JTerminate  s   r   zjava.lang.Thread.isAttachedc                   C  r3   )a   Checks if a thread is attached to the JVM.

    Python automatically attaches threads when a Java method is called.
    This creates a resource in Java for the Python thread. This method
    can be used to check if a Python thread is currently attached so that
    it can be disconnected prior to thread termination to prevent leaks.

    Returns:
      True if the thread is attached to the JVM, False if the thread is
      not attached or the JVM is not running.
    )r4   r   r   r   r   r   r     s   r   zjava.lang.Thread.attachc                   C     t   dS )a0   Attaches a thread to the JVM.

    The function manually connects a thread to the JVM to allow access to
    Java objects and methods. JPype automatically attaches when a Java
    resource is used, so a call to this is usually not needed.

    Raises:
      RuntimeError: If the JVM is not running.
    N)r4   r   r   r   r   r   r     s   r   zjava.lang.Thread.detachc                   C  r   )a   Detaches a thread from the JVM.

    This function detaches the thread and frees the associated resource in
    the JVM. For codes making heavy use of threading this should be used
    to prevent resource leaks. The thread can be reattached, so there
    is no harm in detaching early or more than once. This method cannot fail
    and there is no harm in calling it when the JVM is not running.
    N)r4   r   r   r   r   r   r     s   
r   c                 C  s
   t | S )a&   Creates a resource lock for a Java object.

    Produces a monitor object. During the lifespan of the monitor Java
    will not be able to acquire a thread lock on the object. This will
    prevent multiple threads from modifying a shared resource.

    This should always be used as part of a Python ``with`` startment.

    Arguments:
        obj: A valid Java object shared by multiple threads.

    Example:

    .. code-block:: python

      with synchronized(obj):
         # modify obj values

      # lock is freed when with block ends

    )r4   	_JMonitor)objr   r   r   r     s   
r   c                  C  sf   t  sdS ddl} t d}|j  }|s| }| dt	|
d}tdd |d	D S )
a	   Get the JVM version if the JVM is started.

    This function can be used to determine the version of the JVM. It is
    useful to help determine why a Jar has failed to load.

    Returns:
      A typle with the (major, minor, revison) of the JVM if running.
    )r   r   r   r   Njava.lang.Runtimez	([0-9.]+)r   c                 S  s   g | ]}t |qS r   )r   r~   r   r   r   r   J  s    z!getJVMVersion.<locals>.<listcomp>r   )r4   r5   r   r   r   
getPackagegetImplementationVersionr   r   r1   r   rG   r8   )r   runtimer   r   r   r   r   6  s   	
r   r   c                   @  s   e Zd Zdd Zdd ZdS )	_JRuntimec                 C     t d |S Nr   )r4   r   r   addShutdownHookselfthreadr   r   r   r   P  rZ   z_JRuntime.addShutdownHookc                 C  r   r   )r4   r   r   removeShutdownHookr   r   r   r   r   S  rZ   z_JRuntime.removeShutdownHookN)r   r   r   r   r   r   r   r   r   r   M  s    r   )NF)N)rA   rB   rC   rD   )ru   r1   rq   rv   rA   rB   rr   rU   rs   rU   rt   rU   rC   rw   )>
__future__r   atexitrE   pathlibr   rW   typingr4    r   r   r   r   r   r	   
_jvmfinder	importlibr   _util__all__r   r   jedir   _jedi_versionjedi.accessaccess_jedi_accessALLOWED_DESCRIPTOR_ACCESS_JMethod_JFieldModuleNotFoundErrorAttributeErrorr"   _wr$   TYPE_CHECKINGUnionr1   rF   
_PathOrStrr2   r   r@   rS   r   rY   rp   r   r   r   r   registerr   r   r   r   r   JImplementationForr   r   r   r   r   r   <module>   st   !
	$ 6W





	