
    hj*                         d dl mZ d dlmZ d dlmZ d Zd Zd Z G d d      Z	 G d	 d
e	      Z
 G d de	      Zg dZy)    )Path)	HTTPError)urlretrievec                     dd l }d}|j                         }|j                  || dddd      }t        |      }|r| |d}|j                  ||d      }t	        ||       y )	Nr   zhttps://docs.google.com/uctdownload)idconfirmexportT)paramsstream)r	   r
   )requestsSessiongetget_confirm_tokensave_response_content)r	   destinationr   URLsessionresponsetokenr   s           G/DATA/.local/lib/python3.12/site-packages/trame_common/assets/remote.pydownload_file_from_google_driver      sx    
&C G{{ 

   H h'Eu-;;s6$;?(K0    c                 r    | j                   j                         D ]  \  }}|j                  d      s|c S  y )Ndownload_warning)cookiesitems
startswith)r   keyvalues      r   r   r      s6    &&,,.
U>>,-L / r   c                     d}t        |      j                  d      5 }| j                  |      D ]  }|s|j                  |        	 d d d        y # 1 sw Y   y xY w)Ni   wb)r   openiter_contentwrite)r   r   
CHUNK_SIZEfchunks        r   r   r   &   sK    J	k				%**:6E 7 
&	%	%s   AAAc                   >    e Zd ZdZddZed        Zd Zed        Zy)AbstractRemoteFilez
    AbstractRemoteFile provide infrastructure for RemoteFile where
    only the method fetch() needs to be defined for a concreate implementation.
    Nc                    t        j                         j                         | _        |Qt        |      j                         }|| _        |j	                         r!|j                         r|j                  | _        t        |      | _        | j                  j                         s| j                  |z  | _        | j                  j                  }|j                  dd       y )NT)parentsexist_ok)
r   cwdresolve_baseexistsis_fileparent
_file_pathis_absolutemkdir)self
local_path
local_base
parent_dirs       r   __init__zAbstractRemoteFile.__init__5   s    XXZ'')
!j)113J#DJ  "z'9'9';'..
 z***,"jj:5DO __++
5r   c                 6    | j                   j                         S )z?Return true if the file is available locally on the File System)r5   r2   r8   s    r   localzAbstractRemoteFile.localG   s     %%''r   c                      y)zCPerform the action needed to fetch the content and store it locallyN r>   s    r   fetchzAbstractRemoteFile.fetchL   s    r   c                 d    | j                   s| j                          t        | j                        S )z!Return the actual local file path)r?   rB   strr5   r>   s    r   pathzAbstractRemoteFile.pathO   s#     zzJJL4??##r   )NN)	__name__
__module____qualname____doc__r<   propertyr?   rB   rE   rA   r   r   r+   r+   /   s:    
6$ ( (R $ $r   r+   c                   0     e Zd ZdZ	 	 	 d fd	Zd Z xZS )GoogleDriveFilezX
    Helper file to manage caching and retrieving of file available on Google Drive
    c                 4    t         |   ||       || _        y)a+  
        Provide the information regarding where the file should be located
        and where to fetch it if missing.

        :param local_path: relative or absolute path
        :param google_id: Resource ID from google
        :param local_base: Absolute path when local_path is relative
        N)superr<   _gid)r8   r9   	google_idr:   	__class__s       r   r<   zGoogleDriveFile.__init__]   s     	Z0	r   c           	         	 t        d| j                   d| j                          t        | j                  | j                         y # t        $ r8}t        t        d| j                   d|j                                Y d }~y d }~ww xY wNzDownloading:
 - z
 - to zFailed to download z. )printrO   r5   r   r   RuntimeErrorreasonr8   es     r   rB   zGoogleDriveFile.fetchn   sq    	O%dii[8IJK+DIItG 	O,!4TYYKr!((LMNN	Os   AA 	B	.BB	NNNrF   rG   rH   rI   r<   rB   __classcell__rQ   s   @r   rL   rL   X   s     	"Or   rL   c                   0     e Zd ZdZ	 	 	 d fd	Zd Z xZS )HttpFilezX
    Helper file to manage caching and retrieving of file available on HTTP servers
    c                 4    t         |   ||       || _        y)a:  
        Provide the information regarding where the file should be located
        and where to fetch it if missing.

        :param local_path: relative or absolute path
        :param remote_url: http(s):// url to fetch the file from
        :param local_base: Absolute path when local_path is relative
        N)rN   r<   _url)r8   r9   
remote_urlr:   rQ   s       r   r<   zHttpFile.__init__{   s     	Z0	r   c           	      *   	 t        d| j                   d| j                          t        | j                  t	        | j                               y # t
        $ r8}t        t        d| j                   d|j                                Y d }~y d }~ww xY wrS   )rT   r`   r5   r   rD   r   rU   rV   rW   s     r   rB   zHttpFile.fetch   sv    	O%dii[8IJK		3t#78 	O,!4TYYKr!((LMNN	Os   AA 	B.BBrY   rZ   r\   s   @r   r^   r^   v   s     	"Or   r^   )r+   rL   r^   N)pathlibr   urllib.errorr   urllib.requestr   r   r   r   r+   rL   r^   __all__rA   r   r   <module>rg      sM     " &10&$ &$RO( O<O! O<r   