
    5i"                        d Z ddlmZmZmZ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ZddlmZ d	d
lmZ d	dlmZ d	dlmZ  e e       d      ZdZdZej6                  rdZdZdZd Zd Zd Z  G d de      Z!y)zc
DXDatabase Handler
**************

This remote database handler is a Python database-like object.
    )print_functionunicode_literalsdivisionabsolute_importN)BytesIO)Lock)	cpu_count   )DXDataObject   )object_exists_in_project)logger)
basestring   i   i   i   i  @ <   c                     | j                         D ]F  \  }}t        |t              st        d|d      t        |t              r6t        d|d|d       | S )NzExpected key z of headers to be a stringzExpected value z! of headers (associated with key z) to be a string)items
isinstancer   
ValueError)headerskeyvalues      z/home/marpiech/ifpan-abm-pgxpred/analysis/marpiech-gwas-test/venv/lib/python3.12/site-packages/dxpy/bindings/dxdatabase.py_validate_headersr   2   s[    mmo -
U#z*CQRR%, %s, - -	- N    c                    | }t        d      }t        |dz        }t        |dz        }t        |dz        }||k  rdj                  ||dk7  rd      S d      S ||cxk  r|k  rn ndj                  ||z        S ||cxk  r|k  rn ndj                  ||z        S ||cxk  r|k  rn ndj                  ||z        S ||k  rd	j                  ||z        S y
)z'Returns the file size in readable form.i   z{0} {1}r
   bytesbytez{0:.2f} KiBz{0:.2f} MiBz{0:.2f} GiBz{0:.2f} TiBN)floatformat)	num_bytesBKBMBGBTBs         r   _readable_part_sizer'   <   s    A	tB	rDy	B	rDy	B	rDy	B2va1f7AA&AA	q2##AbD))	q2##AbD))	q2##AbD))	q##AbD)) 
r   c                 .    t        j                  |        y )N)r   debug)msgs    r   do_debugr+   O   s    
LLr   c                       e Zd ZdZdZ eej                  j                        Z	e
Zej                  j                  e      ZdZddeddfdZd	dZy)

DXDatabasezRemote database object handler.

    :param dxid: Object ID
    :type dxid: string
    :param project: Project ID
    :type project: string

    database)max_workersNO_PROJECT_HINTNFc                     t        j                  | ||       t               | _        || _        || _        || _        d\  | _        | _        | _	        t               | _        d\  | _        | _        t               | _        y)a>  
        :param dxid: Object ID
        :type dxid: string
        :param project: Project ID
        :type project: string
        :param read_buffer_size: size of read buffer in bytes
        :type read_buffer_size: int
        :param expected_file_size: size of data that will be written, if
            known
        :type expected_file_size: int
        :param file_is_mmapd: True if input file is mmap'd (if so, the
            write buffer size will be constrained to be a multiple of
            the allocation granularity)
        :type file_is_mmapd: bool
        )dxidprojectNNN)NNN)r   __init__r   	_read_buf_read_bufsize_expected_file_size_file_is_mmapd_download_url_download_url_headers_download_url_expiresr   _url_download_mutex_request_iterator_response_iteratorset_http_threadpool_futures)selfr2   r3   read_buffer_sizeexpected_file_sizefile_is_mmapds         r   r5   zDXDatabase.__init__e   sv    $ 	dw? -#5 + VfRD68R $(6 :D7 7(+%r   c                    i }|||d<   |@dt         j                  vr.| j                         }|rt        | j	                         |      r|}||t
        j                  ur||d<   dt         j                  v rLt        t         j                  d   d      5 }|$|t
        j                  k7  r|j                  |       ddd       | j                  5  | j                  !| j                  t        j                         k  rd|vr	t        |d<   t        dj                  |             t!        j"                  j$                  | j&                  |fi |}t        d	j                  |             |d
   | _
        t)        |j+                  di             | _        d| _        | j                  }	t/        j.                  | j,                        }
ddd       |	|
fS # 1 sw Y   xY w# 1 sw Y   	
fS xY w)a  
        :param filename: desired filename of the local downloaded file
        :type filename: str
        :param project: ID of a project containing the file (the download URL
            will be associated with this project, and this may affect which
            billing account is billed for this download).
            If no project is specified, an attempt will be made to verify if the database is
            in the project from the DXDatabase handler (as specified by the user or
            the current project stored in dxpy.WORKSPACE_ID). Otherwise, no hint is supplied.
            This fall back behavior does not happen inside a job environment.
        :type project: str
        :returns: download URL and dict containing HTTP headers to be supplied
            with the request
        :rtype: tuple (str, dict)
        :raises: :exc:`~dxpy.exceptions.ResourceNotFound` if a project context was
            given and the database was not found in that project context.
        :raises: :exc:`~dxpy.exceptions.ResourceNotFound` if no project context was
            given and the database was not found in any projects.

        Obtains a URL that can be used to directly download files associated
        with the specified database.

        Nfilename	DX_JOB_IDprojectContext_DX_DUMP_BILLED_PROJECTwtimeoutz5dxdatabase get_download_url - download_file args = {}z5dxdatabase get_download_url - download_file resp = {}urlr   l    L" )osenvironget_proj_idr   get_idr-   r0   openwriter=   r:   r<   timeFILE_REQUEST_TIMEOUTr+   r    dxpyapidatabase_download_file_dxidr   getr;   copy)rB   rG   src_filenamer3   kwargsargsproject_from_handlerfdrespretval_download_urlretval_download_url_headerss              r   get_download_urlzDXDatabase.get_download_url   s   2 #+D ?{"**<#'#3#3#5 #(@Pd(e.7*2L2L#L%,D!" %

2bjj!:;SA &R&7j6P6P+PHHW%& %% 	P!!)T-G-G$))+-U F*(<F9%PWWX\]^xx66tzz4R6RPWWX\]^%)%["->txx	SU?V-W*-8* #'"4"4*.))D4N4N*O'3	P6 #$????& &	P6 #$???s   'GC6GGG*r4   )__name__
__module____qualname____doc___classstaticmethodrV   rW   file_describe	_describeDXFILE_HTTP_THREADS_http_threadpool_sizeutilsget_futures_threadpool_http_threadpoolr0   DEFAULT_BUFFER_SIZEr5   rd    r   r   r-   r-   R   s`     FTXX334I/zz88EZ8['O $AT$(!.HQ@r   r-   )"rh   
__future__r   r   r   r   rN   r[   rT   ior   	threadingr   multiprocessingr	   rV    r   utils.resolverr   r   compatr   minrm   MIN_BUFFER_SIZErr   JOB_IDMD5_READ_CHUNK_SIZErU   r   r'   r+   r-   rs   r   r   <module>r      s   " S R     %   5   )+q) " ;; '!  *&H@ H@r   