
    '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dS )    )Path)	HTTPError)urlretrievec                     dd l }d}|                                }|                    || dddd          }t          |          }|r| |d}|                    ||d          }t	          ||           d S )	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           T/DATA/AppData/hermes/venv/lib/python3.11/site-packages/trame_common/assets/remote.pydownload_file_from_google_driver      s    OOO
&C  G{{ 
 

    H h''E @u--;;s6$;??(K00000    c                 v    | j                                         D ]\  }}|                    d          r|c S d S )Ndownload_warning)cookiesitems
startswith)r   keyvalues      r   r   r      sN    &,,..  
U>>,-- 	LLL	 4r   c                     d}t          |                              d          5 }|                     |          D ]}|r|                    |           	 d d d            d S # 1 swxY w Y   d S )Ni   wb)r   openiter_contentwrite)r   r   
CHUNK_SIZEfchunks        r   r   r   &   s    J	k					%	% **:66 	 	E 	                 s   0A##A'*A'c                   L    e Zd ZdZddZed             Zd Zed             ZdS )AbstractRemoteFilez
    AbstractRemoteFile provide infrastructure for RemoteFile where
    only the method fetch() needs to be defined for a concreate implementation.
    Nc                    t          j                                                    | _        |\t          |                                          }|| _        |                                r |                                r|j        | _        t          |          | _        | j                                        s| j        |z  | _        | j        j        }|	                    dd           d S )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    XZZ''))
!j))1133J#DJ  "" /z'9'9';'; /'.
 z****,, 	6"j:5DO _+
55555r   c                 4    | 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                     dS )zCPerform the action needed to fetch the content and store it locallyN r>   s    r   fetchzAbstractRemoteFile.fetchL   s      r   c                 `    | j         s|                                  t          | j                  S )z!Return the actual local file path)r?   rB   strr5   r>   s    r   pathzAbstractRemoteFile.pathO   s+     z 	JJLLL4?###r   )NN)	__name__
__module____qualname____doc__r<   propertyr?   rB   rE   rA   r   r   r+   r+   /   sz         
6 6 6 6$ ( ( X(R R R $ $ X$ $ $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
    Nc                 Z    t                                          ||           || _        dS )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00			r   c           	         	 t          d| j         d| j                    t          | j        | j                   d S # t          $ r7}t          t          d| j         d|j                              Y d }~d S 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   s    	OJdiJJJJKKK+DItGGGGG 	O 	O 	O,LTYLL!(LLMMNNNNNNNNN	Os   9= 
A>,A99A>NNNrF   rG   rH   rI   r<   rB   __classcell__rQ   s   @r   rL   rL   X   sh          	     "O O O O O O 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
    Nc                 Z    t                                          ||           || _        dS )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00			r   c           	         	 t          d| j         d| j                    t          | j        t	          | j                             d S # t
          $ r7}t          t          d| j         d|j                              Y d }~d S d }~ww xY wrS   )rT   r`   r5   r   rD   r   rU   rV   rW   s     r   rB   zHttpFile.fetch   s    	OJdiJJJJKKK	3t#7#788888 	O 	O 	O,LTYLL!(LLMMNNNNNNNNN	Os   AA
 

B,BBrY   rZ   r\   s   @r   r^   r^   v   sh          	     "O O O O O O Or   r^   )r+   rL   r^   N)pathlibr   urllib.errorr   urllib.requestr   r   r   r   r+   rL   r^   __all__rA   r   r   <module>rg      s         " " " " " " & & & & & &1 1 10    &$ &$ &$ &$ &$ &$ &$ &$RO O O O O( O O O<O O O O O! O O O<  r   