
    xKg=                         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y)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y)	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                      || _         || _        y N)membersextract_dir)selfr   r   s      T/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/pooch/processors.py__init__zExtractorProcessor.__init__/   s    &    c                      y)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                      y)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                      y)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           
      N     j                   | j                  z    _         n]|j                  t        j                  j
                  d      d   }t        j                  j                  | j                          _          j                   j                  s j                  |      }n j                  }|dv s=t        j                  j                   j                         rt         fd|D              s=t        j                   j                   d        j                  | j                          g }t        j                   j                         D ]  \  }}}	|	D ]  }
t        j                  j                  t        j                  j                  t        j                  j                  | j                         |
             j                  t!        fd j                  D              s|j#                  t        j                  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.

        r   )maxsplitr   updatedownloadc              3      K   | ]K  }t         j                  j                  t         j                  j                  j                  |             M y wr
   )ospathexistsjoinr   ).0mr   s     r   	<genexpr>z.ExtractorProcessor.__call__.<locals>.<genexpr>s   s7      KRarww||D,<,<a@A7s   AAT)exist_okc              3   z   K   | ]2  }j                  t        j                  j                  |             4 y wr
   )
startswithr!   r"   normpath)r%   r&   relpaths     r   r'   z.ExtractorProcessor.__call__.<locals>.<genexpr>   s/      /EQG&&rww'7'7':;\s   8;)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,,rww{{Q,?BK!ww||K9I9IJD <<t||''.GllG--GGNN4#3#34 KR 
 KK((48ud&6&67  ggd&6&67ND!U!''**GGLLt7G7G!H(S <<'3 /EI\\/ , MM"'',,tX">? " 8 r   )NN)__name__
__module____qualname____doc__r   propertyabcabstractmethodr   r   r   r;   r   r   r   r   r      sg    .'    	  	 =r   r   c                   ,    e Zd ZdZed        Zd Zd Zy)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                      y)l
        String appended to unpacked archive folder name.
        Only used if extract_dir is None.
        z.unzipr   r   s    r   r   zUnzip.suffix        r   c                 f    t        |d      5 }|j                         cddd       S # 1 sw Y   yxY wz(Return all members from a given archive.rN)r   namelist)r   r   zip_files      r   r   zUnzip._all_members   s&    UC H$$& !  s   '0c           
         t        |d      5 }| j                  .t               j                  d||       |j	                  |       n| j                  D ]  }t               j                  d|||       |j                         D cg c]O  }t        j                  j                  |      j                  t        j                  j                  |            r|Q }}|j	                  ||        ddd       yc c}w # 1 sw Y   yxY w)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'r   r"   )
r   r   r   info
extractallrK   r!   r"   r+   r*   )r   r   r   rL   membernamesubdir_memberss          r   r   zUnzip._extract_file   s    
 UC H||#!!8% ###5"llFL%%;VUK %-$5$5$7&$7D77++D1<<RWW=M=Mf=UV $7 # & ''['Q! + ! $&% ! s   A8C>AC9C>9C>>DNr<   r=   r>   r?   r@   r   r   r   r   r   r   rD   rD      s&    4  '
Rr   rD   c                   ,    e Zd ZdZed        Zd Zd Zy)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                      y)rF   z.untarr   r   s    r   r   zUntar.suffix   rG   r   c                     t        j                  |d      5 }|j                         D cg c]  }|j                   c}cddd       S c c}w # 1 sw Y   yxY wrI   )r   open
getmembersrU   )r   r   tar_filerR   s       r   r   zUntar._all_members   sG    \\%%*2*=*=*?@*?$DII*?@ &%@ &%s   AA	A	AAc           
      <   t        j                  |d      5 }| j                  .t               j	                  d||       |j                  |       n| j                  D ]  }t               j	                  d|||       |j                         D cg c]Y  }t        j                  j                  |j                        j                  t        j                  j                  |            r|[ }}|j                  ||        ddd       yc c}w # 1 sw Y   yxY w)rN   rJ   Nz"Untarring contents of '%s' to '%s'rO   rP   rQ   )r   r\   r   r   rR   rS   r]   r!   r"   r+   rU   r*   )r   r   r   r^   rT   rR   rV   s          r   r   zUntar._extract_file   s    
 \\%%||#!!8% ###5"llFL%%;VUK %-$7$7$9&$9D77++DII6AAGG,,V4 $9 # & ''['Q) + &%(&) &%s   A8DAD-DDDNrW   r   r   r   rY   rY      s'    4  A
!Rr   rY   c                   @    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y)
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bzip2rc   re   rf   )z.xzz.gzz.bz2c                      || _         || _        y r
   )methodrU   )r   rh   rU   s      r   r   zDecompress.__init__N  s    	r   c                 ,   | j                   |dz   }nGt        j                  j                  t        j                  j	                  |      | j                         }|dv st        j                  j                  |      s~t               j                  d||| j                         | j                  |      }t        |d      5 }|j                  |      5 }t        j                  ||       ddd       ddd       |S |S # 1 sw Y   xY w# 1 sw Y   |S xY w)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)rU   r!   r"   r$   dirnamer#   r   rR   rh   _compression_moduler\   shutilcopyfileobj)r   r   r4   r5   decompressedmoduleoutput
compresseds           r   r;   zDecompress.__call__R  s    6 99 9,L77<<(>		JL++277>>,3OL?	 --e4FlE*f[[':&&z6: ( + | (' + s$    D	C=)D	=D	D		Dc                 v   d}| j                   | j                  vr`d| j                    dt        | j                  j                                d}| j                   dv rdj	                  ||g      }t        |      | j                   dk(  rt        j                  j                  |      d   }|| j                  vrLd	| dt        | j                  j                                d}|d
v rdj	                  ||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 rb   zUnrecognized file extension '>   .tar.zip)
rh   moduleslistkeysr$   
ValueErrorr!   r"   splitext
extensions)r   r   error_archivesmessageexts        r   rk   zDecompress._compression_module~  s1    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   )rb   N)r<   r=   r>   r?   rc   re   bz2ry   r~   r   r;   rk   r   r   r   ra   ra     s6    )V TtcRG@J*X)r   ra   )r?   rA   r!   r   re   rc   rl   zipfiler   tarfiler   utilsr   ABCr   rD   rY   ra   r   r   r   <module>r      sj     	 
      q qhER ERPIR IRX{) {)r   