a
    d                     @   s   d dl Z d dlZd dlZd dlZd dlmZmZ d dlZeeedddZ	ee eee dddZ
deeeeeef  edd
ddZdeeeeddddZdS )    N)ListTuple)file_urlfolderreturnc                 C   s   t j| }t j||}t j|r<td| d|   t| }t	|d}|
|j W d   n1 sr0    Y  |S )z0Download a file from URL to a particular folder.zgiven file "z." already exists and will be overwritten with wbN)ospathbasenamejoinisfileloggingwarningrequestsgetopenwritecontent)r   r   fname	file_pathZrqoutfile r   k/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/lightning_utilities/docs/retriever.py_download_file   s    
*r   )
list_filespatternr   c              	   C   sZ   g }| D ]L}t |dd}| }W d   n1 s60    Y  t||}||7 }q|S )zSearch for all occurrences of specific patter 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   Z	collectedr   fobodyfoundr   r   r   _search_all_occurrences   s    &
r%      )r   docs_folderpairs_url_path
base_depthr   c                 C   s   t j| |d}tdd |t jjD }t| dd}| }W d   n1 sZ0    Y  |D ]8\}}	|rdg| }
t jj	g |
|	R  }	|||	}qht| ddd}|
| W d   n1 s0    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>7       z._replace_remote_with_local.<locals>.<listcomp>r   r   Nz..w)r   r	   dirnamereplacelensplitsepr   r   r   r   )r   r'   r(   r)   Z	relt_pathdepthr"   r#   urlfpathZpath_upfwr   r   r   _replace_remote_with_local*   s    &
r9   docs/sourcefetched-s3-assets*.rst5https?://[-a-zA-Z0-9_]+\.s3\.[-a-zA-Z0-9()_\\+.\\/=]+)r'   assets_folderfile_patternretrieve_patternr   c              	   C   s   t j tj| d|dd}|s:td|  d| d dS t||d}|sdtd	|  d
|  dS tj| |}tj|dd g }t	t
|D ]J\}}	td| dt| d|	  t|	|}
||	tj||
f q|D ]}t|| | qd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: patter 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setr2   r   appendr9   )r'   r>   r?   r@   r   urlsZtarget_folderZpairs_url_fileir6   r   r7   r   r   r   fetch_external_assetsC   s"     
rM   )r&   )r:   r;   r<   r=   )rE   r   r   r    typingr   r   r   strr   r%   intr9   rM   r   r   r   r   <module>   s.        