
    -Ph=                         d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl	m
Z
 ddlmZ  G d dej                  Z G d	 d
e          Z G d de          Z G d d          ZdS )z
Post-processing hooks
    N)ZipFile)TarFile   )
get_loggerc                       e Zd ZdZddZeej        d                         Zej        d             Z	ej        d             Z
d ZdS )	ExtractorProcessora  
    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                 "    || _         || _        d S N)membersextract_dir)selfr   r   s      P/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/pooch/processors.py__init__zExtractorProcessor.__init__/   s    &    c                     dS )z
        String appended to unpacked archive folder name.
        Only used if extract_dir is None.
        MUST BE IMPLEMENTED BY CHILD CLASSES.
        N r   s    r   suffixzExtractorProcessor.suffix3         r   c                     dS )zf
        Return all the members in the archive.
        MUST BE IMPLEMENTED BY CHILD CLASSES.
        Nr   )r   fnames     r   _all_memberszExtractorProcessor._all_members<   r   r   c                     dS )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_filez ExtractorProcessor._extract_fileC   r   r   c           
           j         | j        z    _         nV|                    t          j        j        d          d         }t          j                            | j                    _          j         j        s                     |          }n j        }|dv s?t          j        	                     j                   rt           fd|D                       s6t          j         j         d                                | j                    g }t          j         j                   D ]\  }}}	|	D ]}
t          j                            t          j                            t          j                            | j                   |
                     j         t!          fd	 j        D                       r3|                    t          j                            ||
                     |S )
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.

        Nr   )maxsplitr   updatedownloadc              3      K   | ]F}t           j                            t           j                            j        |                    V  Gd S r
   )ospathexistsjoinr   ).0mr   s     r   	<genexpr>z.ExtractorProcessor.__call__.<locals>.<genexpr>s   sU        FGrw||D,<a@@AA     r   T)exist_okc              3   |   K   | ]6}                     t          j                            |                    V  7d S r
   )
startswithr!   r"   normpath)r%   r&   relpaths     r   r'   z.ExtractorProcessor.__call__.<locals>.<genexpr>   sR       / /@AG&&rw'7'7':':;;/ / / / / /r   )r   r   rsplitr!   r"   sepr$   r   r   r#   allmakedirsr   walkr+   r,   anyappend)r   r   actionpoocharchive_dirr   fnamesr"   _filesfilenamer,   s   `          @r   __call__zExtractorProcessor.__call__K   s   2 #$t{2D,,rw{Q,??BK!w||K9IJJD <t|''..GGlG---GNN4#344 .    KR     . K(48888ud&6777  gd&677 	@ 	@ND!U! @ @'**GLLt7G!H!H(SS  <'3 / / / /EI\/ / / , ,' MM"',,tX">">???@ r   )NN)__name__
__module____qualname____doc__r   propertyabcabstractmethodr   r   r   r;   r   r   r   r   r      s         .' ' ' '    X 	   	  = = = = =r   r   c                   4    e Zd ZdZed             Zd Zd ZdS )Unzipa  
    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                     dS )l
        String appended to unpacked archive folder name.
        Only used if extract_dir is None.
        z.unzipr   r   s    r   r   zUnzip.suffix   	     xr   c                 ~    t          |d          5 }|                                cddd           S # 1 swxY w Y   dS )(Return all members from a given archive.rN)r   namelist)r   r   zip_files      r   r   zUnzip._all_members   s    UC   	'H$$&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	's   266c                    t          |d          5 }| j        :t                                          d||           |                    |           ne| j        D ]]t                                          d||           fd|                                D             }|                    ||           ^ddd           dS # 1 swxY w Y   dS )o
        This method receives an argument for the archive to extract and the
        destination path.
        rJ   Nz"Unzipping contents of '%s' to '%s'r"   !Extracting '%s' from '%s' to '%s'c                     g | ]S}t           j                            |                              t           j                                                Q|TS r   )r!   r"   r+   r*   )r%   namemembers     r   
<listcomp>z'Unzip._extract_file.<locals>.<listcomp>   s]     & & & 7++D11<<RW=M=Mf=U=UVV&& & &r   r   r"   )r   r   r   info
extractallrK   )r   r   r   rL   subdir_membersrS   s        @r   r   zUnzip._extract_file   sZ   
 UC   	RH|#!!8%   ###5555"l R RFLL%%;VUK  & & & &$,$5$5$7$7& & &N ''['QQQQ1	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	Rs   B'CC
C
Nr<   r=   r>   r?   r@   r   r   r   r   r   r   rD   rD      s\         4   X' ' '
R R R R Rr   rD   c                   4    e Zd ZdZed             Zd Zd ZdS )Untara  
    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                     dS )rF   z.untarr   r   s    r   r   zUntar.suffix   rG   r   c                     t          j        |d          5 }d |                                D             cddd           S # 1 swxY w Y   dS )rI   rJ   c                     g | ]	}|j         
S r   )rR   )r%   rV   s     r   rT   z&Untar._all_members.<locals>.<listcomp>   s    @@@$DI@@@r   N)r   open
getmembers)r   r   tar_files      r   r   zUntar._all_members   s    \%%% 	A@@(*=*=*?*?@@@	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	A 	As   AAAc                    t          j        |d          5 }| j        :t                                          d||           |                    |           ne| j        D ]]t                                          d||           fd|                                D             }|                    ||           ^ddd           dS # 1 swxY w Y   dS )rN   rJ   Nz"Untarring contents of '%s' to '%s'rO   rP   c                     g | ]X}t           j                            |j                                      t           j                                                V|YS r   )r!   r"   r+   rR   r*   )r%   rV   rS   s     r   rT   z'Untar._extract_file.<locals>.<listcomp>  sd     & & & 7++DI66AAG,,V44 && & &r   rU   )r   r_   r   r   rV   rW   r`   )r   r   r   ra   rX   rS   s        @r   r   zUntar._extract_file   s\   
 \%%% 	R|#!!8%   ###5555"l R RFLL%%;VUK  & & & &$,$7$7$9$9& & &N ''['QQQQ9	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	Rs   B'CCCNrY   r   r   r   r[   r[      s_         4   XA A A
!R !R !R !R !Rr   r[   c                   B    e Zd ZdZdeeeedZddddZdd	Z	d
 Z
d ZdS )
Decompressa3  
    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bzip2rg   ri   rj   )z.xzz.gzz.bz2rf   c                 "    || _         || _        d S r
   )methodrR   )r   rl   rR   s      r   r   zDecompress.__init__N  s    			r   c                 :   | j         |dz   }nBt          j                            t          j                            |          | j                   }|dv st          j                            |          st                                          d||| j                   | 	                    |          }t          |d          5 }|
                    |          5 }t          j        ||           ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   |S )aK  
        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)rR   r!   r"   r$   dirnamer#   r   rV   rl   _compression_moduler_   shutilcopyfileobj)r   r   r4   r5   decompressedmoduleoutput
compresseds           r   r;   zDecompress.__call__R  s|   6 9 9,LL7<<(>(>	JJL+++27>>,3O3O+LL?	   --e44FlE** ;f[['' ;:&z6:::; ; ; ; ; ; ; ; ; ; ; ; ; ; ;; ; ; ; ; ; ; ; ; ; ; ; ; ; ; s6   DC9-D9C=	=D C=	DDDc                 X   d}| j         | j        vrad| j          dt          | j                                                   d}| j         dv rd                    ||g          }t          |          | j         dk    rt          j                            |          d         }|| j	        vrWd	| dt          | j	                                                   d}|d
v rd                    ||g          }t          |          | j        | j	        |                  S | j        | j                  S )a  
        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 rf   zUnrecognized file extension '>   .tar.zip)
rl   moduleslistkeysr$   
ValueErrorr!   r"   splitext
extensions)r   r   error_archivesmessageexts        r   ro   zDecompress._compression_module~  s_    V;dl**At{ A A#'(9(9(;(;#<#<A A A  {n,,((G^#<==W%%%;&  '""5))"-C$/))HC H H'+DO,@,@,B,B'C'CH H H  ***!hh'@AAG )))< 455|DK((r   )rf   N)r<   r=   r>   r?   rg   ri   bz2r}   r   r   r;   ro   r   r   r   re   re     sv        ) )V TtcRRG@@J   * * *X) ) ) ) )r   re   )r?   rA   r!   r   ri   rg   rp   zipfiler   tarfiler   utilsr   ABCr   rD   r[   re   r   r   r   <module>r      sf    


 				 



                     q q q q q q q qhER ER ER ER ER ER ER ERPIR IR IR IR IR IR IR IRX{) {) {) {) {) {) {) {) {) {)r   