o
    ÉÙ¾i  ã                   @   s  d Z ddlmZ ddlZddlZddlZddlZddlm	Z	 ej
d dkr5efZeeejejfZdd„ ZnefZeeeejejfZd	d„ ZG d
d„ deƒZG dd„ deƒZdd„ Ze	jZeƒ \ZZej dd¡Zeej dd¡ƒZdd„ Z dd„ Z!dd„ Z"dd„ Z#dS )z$ctypes library and helper functions é    )Úabsolute_importNé   )Úlibinfoé   c                 C   s
   |   d¡S )Núutf-8)Údecode©Úx© r
   úD/home/ubuntu/.local/lib/python3.10/site-packages/decord/_ffi/base.pyÚ<lambda>   s   
 r   c                 C   s   | S )Nr
   r   r
   r
   r   r      s    c                   @   ó   e Zd ZdZdS )ÚDECORDErrorzError thrown by DECORD functionN©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r
   r
   r   r      ó    r   c                   @   r   )ÚDECORDLimitReachedErrorz-Limit Reached Error thrown by DECORD functionNr   r
   r
   r
   r   r      r   r   c                  C   s\   t  ¡ } tjd  tjtj | d ¡ 7  < t | d tj	¡}tj
|j_|tj | d ¡fS )z'Load libary by searching possible path.ÚPATHr   )r   Úfind_lib_pathÚosÚenvironÚpathsepÚpathÚdirnameÚctypesÚCDLLÚRTLD_GLOBALÚc_char_pÚDECORDGetLastErrorÚrestypeÚbasename)Úlib_pathÚlibr
   r
   r   Ú	_load_lib#   s
   $
r&   Ú
DECORD_FFIÚautoÚDECORD_ENABLE_STACK_TRACEÚ0c                 C   sh   | dkr2t t ¡ ƒ}tsd|v r| d¡d  ¡ }d|v r.d|v r*| d¡d  ¡ }t|ƒ‚t|ƒ‚dS )zàCheck the return value of C API call

    This function will raise exception when error occurs.
    Wrap every API call with this function

    Parameters
    ----------
    ret : int
        return value from API calls
    r   zStack tracezrecovered from nearest framesN)Úpy_strÚ_LIBr!   Ú_ENABLE_STACK_TRACEÚsplitÚstripr   r   )ÚretÚerr_strr
   r
   r   Ú
check_call:   s   ÷r2   c                 C   s   t  |  d¡¡S )zÚCreate ctypes char * from a python string
    Parameters
    ----------
    string : string type
        python string

    Returns
    -------
    str : c_char_p
        A char pointer that can be passed to C API
    r   )r   r    Úencode)Ústringr
   r
   r   Úc_strQ   s   r5   c                 C   s   | t |ƒ |Ž S )a  Create ctypes array from a python array

    Parameters
    ----------
    ctype : ctypes data type
        data type of the array we want to convert to

    values : tuple or list
        data content

    Returns
    -------
    out : ctypes array
        Created ctypes array
    )Úlen)ÚctypeÚvaluesr
   r
   r   Úc_array`   s   r9   c                 C   s   ddl }| | |¡S )zÅA wrapper call of decorator package, differs to call time

    Parameters
    ----------
    func : function
        The original function

    fwrapped : function
        The wrapped function
    r   N)Ú	decoratorÚdecorate)ÚfuncÚfwrappedr:   r
   r
   r   r;   s   s   r;   )$r   Ú
__future__r   Úsysr   r   ÚnumpyÚnpÚ r   Úversion_infoÚstrÚstring_typesÚfloatÚintÚfloat32Úint32Únumeric_typesr+   Ú
basestringÚlongÚ	Exceptionr   r   r&   Ú__version__r,   Ú	_LIB_NAMEr   ÚgetÚ	_FFI_MODEr-   r2   r5   r9   r;   r
   r
   r
   r   Ú<module>   s2   


