
     
3j>                         S r SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKJ	r	  SSK
Jr  SSKJr   " S S\R                  5      r " S	 S
\5      r " S S\5      r " S S5      rg)z
Post-processing hooks
    N)ZipFile)TarFile   )
get_loggerc                       \ rS rSrSrS
S jr\\R                  S 5       5       r	\R                  S 5       r
\R                  S 5       rS rS	rg)ExtractorProcessor   a  
Abstract base class for extractions from compressed archives.

Subclasses can be used with :meth:`pooch.Pooch.fetch` and
:func:`pooch.retrieve` to unzip a downloaded data file into a folder in the
local data store. :meth:`~pooch.Pooch.fetch` will return a list with the
names of the extracted files instead of the archive.

Parameters
----------
members : list or None
    If None, will unpack all files in the archive. Otherwise, *members*
    must be a list of file names to unpack from the archive. Only these
    files will be unpacked.
extract_dir : str or None
    If None, files will be unpacked to the default location (a folder in
    the same location as the downloaded zip file, with a suffix added).
    Otherwise, files will be unpacked to ``extract_dir``, which is
    interpreted as a *relative path* (relative to the cache location
    provided by :func:`pooch.retrieve` or :meth:`pooch.Pooch.fetch`).

Nc                     Xl         X l        g N)membersextract_dir)selfr   r   s      J/home/wildlama/miniconda3/lib/python3.13/site-packages/pooch/processors.py__init__ExtractorProcessor.__init__1   s    &    c                     g)zz
String appended to unpacked archive folder name.
Only used if extract_dir is None.
MUST BE IMPLEMENTED BY CHILD CLASSES.
N r   s    r   suffixExtractorProcessor.suffix5       r   c                     g)zN
Return all the members in the archive.
MUST BE IMPLEMENTED BY CHILD CLASSES.
Nr   )r   fnames     r   _all_membersExtractorProcessor._all_members>   r   r   c                     g)z}
This method receives an argument for the archive to extract and the
destination path.
MUST BE IMPLEMENTED BY CHILD CLASSES.
Nr   )r   r   r   s      r   _extract_file ExtractorProcessor._extract_fileE   r   r   c           
        ^ ^ T R                   c  UT R                  -   T l         OZUR                  [        R                  R
                  SS9S   n[        R                  R                  UT R                   5      T l         T R                  b  T R                  (       d  T R                  U5      nOT R                  nUS;   dH  [        R                  R                  T R                   5      (       a  [        U 4S jU 5       5      (       d;  [        R                  " T R                   SS9  T R                  UT R                   5        / n[        R                  " T R                   5       H  u  pxn	U	 H  n
[        R                  R                  [        R                  R                  [        R                  R                  UT R                   5      U
5      5      mT R                  b&  [!        U4S jT R                   5       5      (       d  M  UR#                  [        R                  R                  Xz5      5        M     M     U$ )	a  
Extract all files from the given archive.

Parameters
----------
fname : str
    Full path of the zipped file in local storage.
action : str
    Indicates what action was taken by :meth:`pooch.Pooch.fetch` or
    :func:`pooch.retrieve`:

    * ``"download"``: File didn't exist locally and was downloaded
    * ``"update"``: Local file was outdated and was re-download
    * ``"fetch"``: File exists and is updated so it wasn't downloaded

pooch : :class:`pooch.Pooch`
    The instance of :class:`pooch.Pooch` that is calling this.

Returns
-------
fnames : list of str
    A list of the full path to all files in the extracted archive.

r   )maxsplitr   updatedownloadc              3      >#    U  HL  n[         R                  R                  [         R                  R                  TR                  U5      5      v   MN     g 7fr   )ospathexistsjoinr   ).0mr   s     r   	<genexpr>.ExtractorProcessor.__call__.<locals>.<genexpr>u   s9      KRarww||D,<,<a@AA7s   AAT)exist_okc              3      >#    U  H3  nTR                  [        R                  R                  U5      5      v   M5     g 7fr   )
startswithr&   r'   normpath)r*   r+   relpaths     r   r,   r-      s1      /EQG&&rww'7'7':;;\s   ;>)r   r   rsplitr&   r'   sepr)   r   r   r(   allmakedirsr   walkr1   r2   anyappend)r   r   actionpoocharchive_dirr   fnamesr'   _filesfilenamer2   s   `          @r   __call__ExtractorProcessor.__call__M   s   2 #$t{{2D,,rww{{Q,?BK!ww||K9I9IJD <<t||''.GllG--GGNN4#3#344 KR  
 KK((48ud&6&67  ggd&6&67NDU!''**GGLLt7G7G!H(S <<'3 /EI\\/ , , MM"'',,t">? " 8 r   )r   r   )NN)__name__
__module____qualname____firstlineno____doc__r   propertyabcabstractmethodr   r   r   rA   __static_attributes__r   r   r   r   r      sg    .'    	  	 =r   r   c                   4    \ rS rSrSr\S 5       rS rS rSr	g)Unzip   a  
Processor that unpacks a zip archive and returns a list of all files.

Use with :meth:`pooch.Pooch.fetch` or :func:`pooch.retrieve` to unzip a
downloaded data file into a folder in the local data store. The
method/function will return a list with the names of the unzipped files
instead of the zip archive.

The output folder is ``{fname}.unzip``.

Parameters
----------
members : list or None
    If None, will unpack all files in the zip archive. Otherwise, *members*
    must be a list of file names to unpack from the archive. Only these
    files will be unpacked.
extract_dir : str or None
    If None, files will be unpacked to the default location (a folder in
    the same location as the downloaded zip file, with the suffix
    ``.unzip`` added). Otherwise, files will be unpacked to
    ``extract_dir``, which is interpreted as a *relative path* (relative to
    the cache location provided by :func:`pooch.retrieve` or
    :meth:`pooch.Pooch.fetch`).

c                     g)T
String appended to unpacked archive folder name.
Only used if extract_dir is None.
z.unzipr   r   s    r   r   Unzip.suffix        r   c                 p    [        US5       nUR                  5       sSSS5        $ ! , (       d  f       g= fz(Return all members from a given archive.rN)r   namelist)r   r   zip_files      r   r   Unzip._all_members   s&    UC H$$& !  s   '
5c           
      &   [        US5       nU R                  c*  [        5       R                  SX5        UR	                  US9  OU R                   H  n[        5       R                  SXAU5        UR                  5        Vs/ s HW  n[        R                  R                  U5      R                  [        R                  R                  U5      5      (       d  MU  UPMY     nnUR	                  XbS9  M     SSS5        gs  snf ! , (       d  f       g= f)W
This method receives an argument for the archive to extract and the
destination path.
rU   Nz"Unzipping contents of '%s' to '%s')r'   !Extracting '%s' from '%s' to '%s'r   r'   )
r   r   r   info
extractallrV   r&   r'   r1   r0   )r   r   r   rW   membernamesubdir_memberss          r   r   Unzip._extract_file   s    
 UC H||#!!8% ###5"llFL%%;VK %-$5$5$7&$7D77++D1<<RWW=M=Mf=UV $7 # & '''Q! + ! $&% ! s%   A4DAC=C=D=D
Dr   N
rC   rD   rE   rF   rG   rH   r   r   r   rK   r   r   r   rM   rM      s&    4  '
Rr   rM   c                   4    \ rS rSrSr\S 5       rS rS rSr	g)Untar   a  
Processor that unpacks a tar archive and returns a list of all files.

Use with :meth:`pooch.Pooch.fetch` or :func:`pooch.retrieve` to untar a
downloaded data file into a folder in the local data store. The
method/function will return a list with the names of the extracted files
instead of the archive.

The output folder is ``{fname}.untar``.


Parameters
----------
members : list or None
    If None, will unpack all files in the archive. Otherwise, *members*
    must be a list of file names to unpack from the archive. Only these
    files will be unpacked.
extract_dir : str or None
    If None, files will be unpacked to the default location (a folder in
    the same location as the downloaded tar file, with the suffix
    ``.untar`` added). Otherwise, files will be unpacked to
    ``extract_dir``, which is interpreted as a *relative path* (relative to
    the cache location  provided by :func:`pooch.retrieve` or
    :meth:`pooch.Pooch.fetch`).
c                     g)rP   z.untarr   r   s    r   r   Untar.suffix   rR   r   c                     [         R                  " US5       nUR                  5        Vs/ s H  o3R                  PM     snsSSS5        $ s  snf ! , (       d  f       g= frT   )r   open
getmembersr`   )r   r   tar_filer]   s       r   r   Untar._all_members   sE    \\%%*2*=*=*?@*?$II*?@ &%@ &%s   AA AA
Ac           
         [         R                  S:  a  0 OSS0n[        R                  " US5       nU R                  c/  [        5       R                  SX5        UR                  " S
SU0UD6  OU R                   H  n[        5       R                  SXQU5        UR                  5        Vs/ s Ha  n[        R                  R                  UR                  5      R                  [        R                  R                  U5      5      (       d  M_  UPMc     nnUR                  " S
XrS	.UD6  M     SSS5        gs  snf ! , (       d  f       g= f)rZ   )      filterdatarU   Nz"Untarring contents of '%s' to '%s'r'   r[   r\   r   )sysversion_infor   rj   r   r   r]   r^   rk   r&   r'   r1   r`   r0   )r   r   r   filter_kwargrl   r_   r]   ra   s           r   r   Untar._extract_file   s   
 !--7rh=O\\%%||#!!8% ##EEE"llFL%%;VK %-$7$7$9&$9D77++DII6AAGG,,V4 $9 # & ''  .DP) + &%(&) &%s%   A9D;+AD6D6D;6D;;
E	r   Nrc   r   r   r   re   re      s&    4  A
$r   re   c                   J    \ rS rSrSrS\\\\S.rSSSS.r	SS	 jr
S
 rS rSrg)
Decompressi$  a  
Processor that decompress a file and returns the decompressed version.

Use with :meth:`pooch.Pooch.fetch` or :func:`pooch.retrieve` to decompress
a downloaded data file so that it can be easily opened. Useful for data
files that take a long time to decompress (exchanging disk space for
speed).

Supported decompression methods are LZMA (``.xz``), bzip2 (``.bz2``), and
gzip (``.gz``).

File names with the standard extensions (see above) can use
``method="auto"`` to automatically determine the compression method. This
can be overwritten by setting the *method* argument.

.. note::

    To unpack zip and tar archives with one or more files, use
    :class:`pooch.Unzip` and :class:`pooch.Untar` instead.

The output file is ``{fname}.decomp`` by default but it can be changed by
setting the ``name`` parameter.

.. warning::

    Passing in ``name`` can cause existing data to be lost! For example, if
    a file already exists with the specified name it will be overwritten
    with the new decompressed file content. **Use this option with
    caution.**

Parameters
----------
method : str
    Name of the compression method. Can be "auto", "lzma", "xz", "bzip2",
    or "gzip".
name : None or str
    Defines the decompressed file name. The file name will be
    ``{fname}.decomp`` if ``None`` (default) or the given name otherwise.
    Note that the name should **not** include the full (or relative) path,
    it should be just the file name itself.

N)autolzmaxzgzipbzip2rz   r|   r}   )z.xzz.gzz.bz2c                     Xl         X l        g r   methodr`   )r   r   r`   s      r   r   Decompress.__init__S  s    	r   c                 P   U R                   c  US-   nOG[        R                  R                  [        R                  R	                  U5      U R                   5      nUS;   d$  [        R                  R                  U5      (       d~  [        5       R                  SUUU R                  5        U R                  U5      n[        US5       nUR                  U5       n[        R                  " Xv5        SSS5        SSS5        U$ U$ ! , (       d  f       N= f! , (       d  f       U$ = f)a  
Decompress the given file.

The output file will be either ``{fname}.decomp`` or the given *name*
class attribute.

Parameters
----------
fname : str
    Full path of the compressed file in local storage.
action : str
    Indicates what action was taken by :meth:`pooch.Pooch.fetch` or
    :func:`pooch.retrieve`:

    - ``"download"``: File didn't exist locally and was downloaded
    - ``"update"``: Local file was outdated and was re-download
    - ``"fetch"``: File exists and is updated so it wasn't downloaded

pooch : :class:`pooch.Pooch`
    The instance of :class:`pooch.Pooch` that is calling this.

Returns
-------
fname : str
    The full path to the decompressed file.
Nz.decompr"   z-Decompressing '%s' to '%s' using method '%s'.zw+b)r`   r&   r'   r)   dirnamer(   r   r]   r   _compression_modulerj   shutilcopyfileobj)r   r   r:   r;   decompressedmoduleoutput
compresseds           r   rA   Decompress.__call__W  s    6 99 9,L77<<(>		JL++277>>,3O3OL?	 --e4FlE*f[[':&&z: ( + | (' +* s$   DD1D
D	D
D%c                    SnU R                   U R                  ;  aa  SU R                    S[        U R                  R                  5       5       S3nU R                   S;   a  SR	                  X2/5      n[        U5      eU R                   S:X  a  [        R                  R                  U5      S   nX@R                  ;  aM  S	U S[        U R                  R                  5       5       S3nUS
;   a  SR	                  X2/5      n[        U5      eU R                  U R                  U      $ U R                  U R                      $ )z
Get the Python module compatible with fname and the chosen method.

If the *method* attribute is "auto", will select a method based on the
extension. If no recognized extension is in the file name, will raise a
ValueError.
z:To unpack zip/tar archives, use pooch.Unzip/Untar instead.zInvalid compression method 'z'. Must be one of 'z'.>   tarzip ry   zUnrecognized file extension '>   .tar.zip)
r   moduleslistkeysr)   
ValueErrorr&   r'   splitext
extensions)r   r   error_archivesmessageexts        r   r   Decompress._compression_module  s+    V;;dll*.t{{m <##'(9(9(;#<"=RA  {{n,((G#<=W%%;;& ''""5)"-C//)3C5 9''+DOO,@,@,B'C&DBH  **!hh'@AG ))<< 455||DKK((r   r   )ry   N)rC   rD   rE   rF   rG   rz   r|   bz2r   r   r   rA   r   rK   r   r   r   rx   rx   $  s6    )V TtcRG@J*X)r   rx   )rG   rI   r&   r   r|   rz   r   rs   zipfiler   tarfiler   utilsr   ABCr   rM   re   rx   r   r   r   <module>r      sk     	 
    
   q qhER ERPL L^{) {)r   