CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
dbfs.cpython-310.pyc242 linesDownload Raw Back to __pycache__
1o

2��YiY?�@s�ddlmZddlZddlZddlZddlmZmZddlm	Z	ddl3mZddlm
Z
Gdd�de�ZGd	d4�d5e�ZGdd�de
�ZdS)
�)�annotationsN)�HTTPAdapter�Retry)�override)�AbstractFileSystem)�AbstractBufferedFilecs"eZdZdZd�fdd�	Z�ZS)�DatabricksExceptionz<6    Helper class for exceptions raised in this module.7    Ncs"t��|�||_||_||_dS)z Create a new DatabricksExceptionN)�super�__init__�8error_code�message�details)�selfrrr
��	__class__���E:\DocsHouse\542 percep lab latest\PerceptionLab\PerceptionLab_Portable\python_embed\Lib\site-packages\fsspec/implementations/dbfs.pyr9s10zDatabricksException.__init__�N)�__name__�11__module__�__qualname__�__doc__r12�
__classcell__rrrrrsrcs�eZdZdZ�fdd�Zed%dd��Zd&d	d13�Zd&dd�Zd&d
d�Z	d'dd�Z14	d(dd�Zd)dd�Zdd�Z
d&dd�Zdd�Zdd �Zd!d"�Zd*�fd#d$�	Z�ZS)+�DatabricksFileSystemz�15    Get access to the Databricks filesystem implementation over HTTP.16    Can be used inside and outside of a databricks cluster.17    csp||_||_t��|_tddgd�d�|_|j�dt|jd��|jj	�18dd|j��i�t�jd19i|��d	S)a�20        Create a new DatabricksFileSystem.21 22        Parameters23        ----------24        instance: str25            The instance URL of the databricks cluster.26            For example for an Azure databricks cluster, this27            has the form adb-<some-number>.<two digits>.azuredatabricks.net.28        token: str29            Your personal token. Find out more30            here: https://docs.databricks.com/dev-tools/api/latest/authentication.html3132g�������?)i�i�i�i�i�i�)�total�backoff_factor�status_forcelist�https://)�max_retries�
AuthorizationzBearer Nr)
�instance�token�requests�Session�sessionr�retries�mountr�headers�updater	r33)rr!r"�kwargsrrrr34"s35�zDatabricksFileSystem.__init__�return�!list[dict[str, str | int]] | NonecCst|j�|�d�d�|�|�}||jvr8|j|D]}|d|�d�kr3|ddkr/|gSgSqt|��dS)z�Check cache for listing36 37        Returns listing, if found (may be empty list for a directory that38        exists but contains nothing), None if not in cache.39        �/N�name�type�	directory)�dircache�pop�rstrip�_parent�FileNotFoundError)r�path�parent�entryrrr�_ls_from_cache>s404142��z#DatabricksFileSystem._ls_from_cacheTc43Ks�z|�|�}Wnty|j�|�|�d�d}Ynw|sVz|jddd|id�}WntyC}z|jdkr>t|j�|��d}~ww|�	dg�}dd	�|D�}||j|<|rZ|Sd44d	�|D�S)a"45        List the contents of the given path.46 47        Parameters48        ----------49        path: str50            Absolute path51        detail: bool52            Return not only the list of filenames,53            but also additional information on file sizes54            and types.55        N�get�listr6��method�endpoint�json�RESOURCE_DOES_NOT_EXIST�filescSs,g|]}|d|dr
dnd|dd��qS)r6�is_dirr0�file�	file_size)r.r/�sizer��.0�orrr�56<listcomp>qs���z+DatabricksFileSystem.ls.<locals>.<listcomp>cSsg|]}|d�qS)r.rrFrrrrI}s)57r9r5r1r2r4�_send_to_apirrrr:)rr6�detailr*�out�r�erArrr�lsPs2
�5859�60���61zDatabricksFileSystem.lsc62Cs�|s-z|jddd|id�td|�d���ty,}z|jdkr"	WYd}~nd}~wwz|jd	d63d|id�WntyQ}z|jdkrLt|j�|��d}~ww|�|�|��dS)aE64        Create a given absolute path and all of its parents.65 66        Parameters67        ----------68        path: str69            Absolute path to create70        exist_ok: bool71            If false, checks if the folder72            exists before creating it (and raises an73            Exception if this is the case)74        r:z75get-statusr6r<zPath z already existsr@N�post�mkdirs�RESOURCE_ALREADY_EXISTS)rJ�FileExistsErrorrrr�invalidate_cacher4)rr6�exist_okrNrrr�makedirss(
76�77��78��zDatabricksFileSystem.makedirscKs|st�|j|fi|��dS)a!79        Create a given absolute path and all of its parents.80 81        Parameters82        ----------83        path: str84            Absolute path to create85        create_parents: bool86            Whether to create all parents or not.87            "False" is not implemented so far.88        N)�NotImplementedErrorrQ)rr6Zcreate_parentsr*rrr�mkdir�szDatabricksFileSystem.mkdirFc89Kszz
|jdd||d�d�Wn%ty2}z|jdkr"|j||d��|jdkr-t|j�|��d}~ww|�|�|��dS)z�90        Remove the file or folder at the given absolute path.91 92        Parameters93        ----------94        path: str95            Absolute path what to remove96        recursive: bool97            Recursively delete all files in a folder.98        rP�delete)r6�	recursiver<ZPARTIAL_DELETEZIO_ERRORN)rJrr�rm�OSErrorrrTr4)rr6rZr*rNrrrr[�s 99�100101���102zDatabricksFileSystem.rmNc103Ks�|rt�|rt�z
|jdd||d�d�Wn#ty8}z|jdkr(t|j�|�|jdkr3t|j�|��d}~ww|�|�|��|�|�|��dS)a�104        Move a source to a destination path.105 106        A note from the original [databricks API manual]107        (https://docs.databricks.com/dev-tools/api/latest/dbfs.html#move).108 109        When moving a large number of files the API call will time out after110        approximately 60s, potentially resulting in partially moved data.111        Therefore, for operations that move more than 10k files, we strongly112        discourage using the DBFS REST API.113 114        Parameters115        ----------116        source_path: str117            From where to move (absolute path)118        destination_path: str119            To where to move (absolute path)120        recursive: bool121            Not implemented to far.122        maxdepth:123            Not implemented to far.124        rP�move)�source_path�destination_pathr<r@rRN)	rWrJrrr5rrSrTr4)rr^r_rZZmaxdepthr*rNrrr�mv�s(125�126127��zDatabricksFileSystem.mv�rb�defaultcKst||f||d�|��S)z�128        Overwrite the base class method to make sure to create a DBFile.129        All arguments are copied from the base method.130 131        Only the default blocksize is allowed.132        )�mode�133block_size)�DatabricksFile)rr6rcrdr*rrr�_open�szDatabricksFileSystem._openc	Cs�|dkr	|jj}n|dkr|jj}ntd|����tj�d|j�d�|�}|||d�}z	|��W|��St	j134y[}zz|j��}Wn135t
yN|d�wtdi|��|�d}~ww)	a�136        Send the given json to the DBFS API137        using a get or post request (specified by the argument `method`).138 139        Parameters140        ----------141        method: str142            Which http method to use for communication; "get" or "post".143        endpoint: str144            Where to send the request to (last part of the API URL)145        json: dict146            Dictionary of information to send147        rPr:zDo not understand method rz/api/2.0/dbfs/)r?Nr)r%rPr:�148ValueError�urllib�parse�urljoinr!�raise_for_statusr#�	HTTPError�responser?�	Exceptionr)	rr=r>r?Zsession_call�urlrMrNZexception_jsonrrrrJs&149150151����z!DatabricksFileSystem._send_to_apic152CsRz|jdd||d�d�}|dWSty(}z|jdkr#t|j�|��d}~ww)a{153        Internal function to create a handle, which can be used to154        write blocks of a file to DBFS.155        A handle has a unique identifier which needs to be passed156        whenever written during this transaction.157        The handle is active for 10 minutes - after that a new158        write transaction needs to be created.159        Make sure to close the handle after you are finished.160 161        Parameters162        ----------163        path: str164            Absolute path for this file.165        overwrite: bool166            If a file already exist at this location, either overwrite167            it or raise an exception.168        rP�create)r6�	overwriter<�handlerRN)rJrrrSr)rr6rqrMrNrrr�_create_handle.s�169170��z#DatabricksFileSystem._create_handlec171CsLz
|jddd|id�WdSty%}z|jdkr t|j�|��d}~ww)z�172        Close a handle, which was opened by :func:`_create_handle`.173 174        Parameters175        ----------176        handle: str177            Which handle to close.178        rP�closerrr<r@N)rJrrr5r)rrrrNrrr�
_close_handleMs	179��z"DatabricksFileSystem._close_handlec180Csrt�|���}z|jdd||d�d�WdSty8}z|jdkr(t|j�|�|jdkr3t|j�|��d}~ww)a�181        Upload data to an already opened file handle182        (opened by :func:`_create_handle`).183        The maximal allowed data size is 1MB after184        conversion to base64.185        Remember to close the handle when you are finished.186 187        Parameters188        ----------189        handle: str190            Which handle to upload data to.191        data: bytes192            Block of data to add to the handle.193        rPz	add-block�rr�datar<r@ZMAX_BLOCK_SIZE_EXCEEDEDN)	�base64�	b64encode�decoderJrrr5rrg)rrrrwrNrrr�	_add_data^s�194195��zDatabricksFileSystem._add_datac196Cstz|jdd||||d�d�}t�|d�WSty9}z|jdkr)t|j�|�|jdvr4t|j�|��d}~ww)	a�197        Download data in bytes from a given absolute path in a block198        from [start, start+length].199        The maximum number of allowed bytes to read is 1MB.200 201        Parameters202        ----------203        path: str204            Absolute path to download data from205        start: int206            Start position of the block207        end: int208            End position of the block209        r:�read)r6�offset�lengthr<rwr@)ZINVALID_PARAMETER_VALUEZMAX_READ_SIZE_EXCEEDEDN)rJrx�	b64decoderrr5rrg)rr6�start�endrMrNrrr�	_get_data|s�210211��zDatabricksFileSystem._get_datacs2|dur212|j��n|j�|d�t��|�dSr)r1�clearr2r	rT)rr6rrrrT�sz%DatabricksFileSystem.invalidate_cache)r+r,)T�F)FN)rarbr)rrrrr213rr9rOrVrXr[r`rfrJrsrur{r�rTrrrrrrs$214215/216!217218219.	220)rcsTeZdZdZdZ					d�fdd	�	Zd221d�Zdd
d�Zdd�Zddd�Z	�Z222S)rezH223    Helper class for files referenced in the DatabricksFileSystem.224    irarbT�	readaheadNc	sX|dus|dkr|j}||jksJd|����t�j||f|||||p$id�|��dS)zr225        Create a new instance of the DatabricksFile.226 227        The blocksize needs to be the default one.228        Nrbz,Only the default block size is allowed, not )rcrd�229autocommit�230cache_type�
cache_options)�DEFAULT_BLOCK_SIZEr	r231)	r�fsr6rcrdr�r�r�r*rrrr232�s"���233�zDatabricksFile.__init__cCs|j�|j�|_dS)z(Internal function to start a file uploadN)r�rsr6rr)rrrr�_initiate_upload�szDatabricksFile._initiate_uploadFcsj|j�d�|j����fdd�|�t���D�}|D]}|jj|j|d�q|r3|jj|jd�dSdS)z<Internal function to add a chunk of data to a started uploadrcsg|]234\}}�||��qSrr)rGr�r��rwrrrI�s�z0DatabricksFile._upload_chunk.<locals>.<listcomp>rv)rrTN)	�buffer�seek�getvalue�_to_sized_blocks�lenr�r{rrru)r�finalZdata_chunks�235data_chunkrr�r�
_upload_chunk�s236237��zDatabricksFile._upload_chunkcCs>d}||}|�||�D]\}}||jj|j||d�7}q|S)z-Internal function to download a block of data�)r6r�r�)r�r�r�r6)rr�r�Z
return_bufferr~Zchunk_startZ	chunk_endrrr�_fetch_range�s238�zDatabricksFile._fetch_rangerccs@�||}t|||j�D]}|}t|||j�}||fVqdS)zGHelper function to split a range from 0 to total_length into blocksizesN)�range�	blocksize�min)rr~r�r�r�Z239data_startZdata_endrrrr��s��zDatabricksFile._to_sized_blocks)rarbTr�Nr�)r)rrrrr�r240r�r�r�r�rrrrrre�s�"241re)�242__future__rrxrhr#Zrequests.adaptersrrZtyping_extensionsrZfsspecrZfsspec.specrrnrrrerrrr�<module>s