
    i{                         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Zdee   dedee   fdZd	ed
edee	eef      ddfdZ
	 	 	 	 dd
ededededdf
dZy)    Nfile_urlfolderreturnc                    t         j                  j                  |       }t         j                  j                  ||      }t         j                  j	                  |      rt        j                  d| d|         t        j                  | d      }t        |d      5 }|j                  |j                         ddd       |S # 1 sw Y   |S xY w)a*  Download a file from a URL into the given folder.

    If a file with the same name already exists, it will be overwritten.
    Returns the basename of the downloaded file. Network-related exceptions from
    ``requests.get`` (e.g., timeouts or connection errors) may propagate to the caller.

    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outfiles         w/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/lightning_utilities/docs/retriever.py_download_filer      s     GGX&EVU+I	ww~~i ,yk1_`h_ijk	h	+B	i	'bjj! 
L 
Ls   CC
list_filespatternc                     g }| D ]C  }t        |d      5 }|j                         }ddd       t        j                  |      }||z  }E |S # 1 sw Y   (xY w)aA  Search for all occurrences of a regular-expression pattern across files.

    Args:
        list_files: The list of file paths to scan.
        pattern: A regular-expression pattern to search for in each file.

    Returns:
        A list with all matches found across the provided files (order preserved per file).

    UTF-8encodingN)r   readrefindall)r   r   	collectedr   fopembodyfounds          r   _search_all_occurrencesr)      sZ     I	)g.%::<D /

7D)U		  
 	 /.s   AA	r   docs_folderpairs_url_pathc                 H   t         j                  j                  |       j                  |d      }t	        |j                  t         j                  j                        D cg c]  }|s|	 c}      }t        | d      5 }|j                         }ddd       |D ]@  \  }}	|r'dg|z  }
t        j                  j                  g |
|	 }	j                  ||	      }B t        | dd      5 }|j                         ddd       yc c}w # 1 sw Y   |xY w# 1 sw Y   yxY w)aC  Replace all matching remote URLs with local file paths in a given file.

    Args:
        file_path: The file in which replacements should be performed.
        docs_folder: The documentation root folder (used to compute relative paths).
        pairs_url_path: Pairs of (remote_url, local_relative_path) to replace.

     r   r    Nz..w)r
   r   dirnamereplacelensplitsepr   r"   r   r   )r   r*   r+   	relt_pathpdepthfopenr'   urlfpathpath_upfws               r   _replace_remote_with_localr<   3   s     	*22;CIIOOBGGKK8>8qA8>?E	i'	*ezz| 
+$
UfunGGGLL1'151E||C'	 %
 
iw	/2
 
0	/ ?	*	* 
0	/s$    D(D D,DDD!assets_folderfile_patternretrieve_patternc           	         t        j                   t        j                  j                  | d|      d      }|st	        j
                  d|  d| d       yt        ||      }|st	        j                  d	|  d
|        yt        j                  j                  | |      }t        j                  |d       g }t        t        |            D ]i  \  }}	t	        j                  d| dt        |       d|	        t        |	|      }
|j                  |	t        j                  j                  ||
      f       k |D ]  }t        || |        y)a  Find S3 (or HTTP) asset URLs in docs, download them locally, and rewrite references to local paths.

    Args:
        docs_folder: The documentation root relative to the project.
        assets_folder: Subfolder inside ``docs_folder`` used to store downloaded assets (created if missing).
        file_pattern: Glob pattern of files to scan.
        retrieve_pattern: Regular-expression pattern used to find remote asset URLs.

    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=   r>   r?   r   urlstarget_folderpairs_url_fileir8   r   r9   s               r   fetch_external_assetsrO   K   s"    277<<T<HTXYJ:;-WcVddefg":7GHD9+eL\K]^_GGLLm<MKK-NCI&3(1SYKs3%@AsM2sBGGLL$FGH '
 "5+~F     )zdocs/sourcezfetched-s3-assetsz*.rstz5https?://[-a-zA-Z0-9_]+\.s3\.[-a-zA-Z0-9()_\\+.\\/=]+)rE   r   r
   r#   r   strr   listr)   tupler<   rO    rP   r   <module>rU      s      	 	 S # # &S	 C DI (# C QUV[\_ad\dVeQf ko 2 %,T	!G!G!G !G 	!G
 
!GrP   