o
    poi                     @   s   d dl Z d dlZd dlZd dlZd dlZdededefddZdee dedee fd	d
Zdededee	eef  ddfddZ
				ddededededdf
ddZdS )    Nfile_urlfolderreturnc                 C   s   t j| }t j||}t j|rtd| d|   tj| dd}t	|d}|
|j W d   |S 1 s<w   Y  |S )z0Download a file from URL to a particular folder.zgiven file "z." already exists and will be overwritten with 
   )timeoutwbN)ospathbasenamejoinisfileloggingwarningrequestsgetopenwritecontent)r   r   fname	file_pathrqoutfile r   V/home/ubuntu/.local/lib/python3.10/site-packages/lightning_utilities/docs/retriever.py_download_file   s   
r   
list_filespatternc              	   C   sZ   g }| D ]&}t |dd}| }W d   n1 sw   Y  t||}||7 }q|S )zSearch for all occurrences of specific pattern in a collection of files.

    Args:
        list_files: list of files to be scanned
        pattern: pattern for search, reg. expression

    UTF-8encodingN)r   readrefindall)r   r   	collectedr   fopembodyfoundr   r   r   _search_all_occurrences   s   

r'   r   docs_folderpairs_url_pathc                 C   s   t j| |d}tdd |t jjD }t| dd}| }W d   n1 s-w   Y  |D ]\}}|rJdg| }	t jj	g |	|R  }|||}q4t| ddd}
|

| W d   dS 1 siw   Y  dS )	zReplace all URL with local files in a given file.

    Args:
        file_path: file for replacement
        docs_folder: the location of docs related to the project root
        pairs_url_path: pairs of URL and local file path to be swapped

     c                 S   s   g | ]}|r|qS r   r   ).0pr   r   r   
<listcomp>6   s    z._replace_remote_with_local.<locals>.<listcomp>r   r   Nz..w)r   r	   dirnamereplacelensplitsepr   r    r   r   )r   r(   r)   	relt_pathdepthfopenr%   urlfpathpath_upfwr   r   r   _replace_remote_with_local*   s   


"r;   docs/sourcefetched-s3-assets*.rst5https?://[-a-zA-Z0-9_]+\.s3\.[-a-zA-Z0-9()_\\+.\\/=]+assets_folderfile_patternretrieve_patternc              	   C   s   t j tj| d|dd}|std|  d| d dS t||d}|s2td	|  d
|  dS tj| |}tj|dd g }t	t
|D ]%\}}	td| dt| d|	  t|	|}
||	tj||
f qH|D ]}t|| | qpdS )a  Search all URL in docs, download these files locally and replace online with local version.

    Args:
        docs_folder: the location of docs related to the project root
        assets_folder: a folder inside ``docs_folder`` to be created and saving online assets
        file_pattern: what kind of files shall be scanned
        retrieve_pattern: pattern for reg. expression to search URL/S3 resources

    z**T)	recursivez no files were listed in folder "z" and pattern ""N)r   z"no resources/assets were match in z for )exist_okz >> downloading (/z): )globr   r	   r   r   r   r'   infomakedirs	enumeratesetr1   r   appendr;   )r(   r@   rA   rB   r   urlstarget_folderpairs_url_fileir7   r   r8   r   r   r   fetch_external_assetsB   s$    
rQ   )r<   r=   r>   r?   )rG   r   r   r!   r   strr   listr'   tupler;   rQ   r   r   r   r   <module>   s.   &