o
    a&iL5                     @   s   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mZ ddlm	Z	 ddl
mZ G dd dZG d	d
 d
eZG dd deZG dd dZdS )z
Post-processing hooks
    N)ZipFile)TarFile   )
get_loggerc                   @   s.   e Zd ZdZdZd	ddZdd Zdd ZdS )
ExtractorProcessoraF  
    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.

    Nc                 C      || _ || _d S N)membersextract_dir)selfr	   r
    r   ;C:\wamp64\www\opt\env\Lib\site-packages\pooch/processors.py__init__+      
zExtractorProcessor.__init__c                 C   s   | j du r| jdu rtd| jdu r|| j  | _n|jtjjddd }tj|| j| _|dv s9tj| jsHtj	| jdd | 
|| j d	d
 t| jD }|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.

        NzVDerived classes must define either a 'suffix' attribute or an 'extract_dir' attribute.r   )maxsplitr   updatedownloadT)exist_okc                 S   s*   g | ]\}}}|D ]	}t j||q	qS r   )ospathjoin).0r   _filesfnamer   r   r   
<listcomp>X   s    z/ExtractorProcessor.__call__.<locals>.<listcomp>)suffixr
   NotImplementedErrorrsplitr   r   sepr   existsmakedirs_extract_filewalk)r   r   actionpoocharchive_dirfnamesr   r   r   __call__/   s   

zExtractorProcessor.__call__c                 C   s   t )z
        This method receives an argument for the archive to extract and the
        destination path. MUST BE IMPLEMENTED BY CHILD CLASSES.
        )r   )r   r   r
   r   r   r   r#   _   s   z ExtractorProcessor._extract_file)NN)__name__
__module____qualname____doc__r   r   r)   r#   r   r   r   r   r      s    
0r   c                   @      e Zd ZdZdZd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`).

    z.unzipc                    s   t |dF}| jdu rt d|| |j|d n!| jD ]% t d ||  fdd| D }|j||d qW d   dS W d   dS 1 sNw   Y  dS )	o
        This method receives an argument for the archive to extract and the
        destination path.
        rNz"Unzipping contents of '%s' to '%s'r   !Extracting '%s' from '%s' to '%s'c                    s   g | ]	}|  r|qS r   )
startswith)r   namememberr   r   r      s
    
z'Unzip._extract_file.<locals>.<listcomp>r	   r   )r   r	   r   info
extractallnamelist)r   r   r
   zip_filesubdir_membersr   r6   r   r#      s$   


	"zUnzip._extract_fileNr*   r+   r,   r-   r   r#   r   r   r   r   r/   g       r/   c                   @   r.   )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`).
    z.untarc                    s   t |dF}| jdu rt d|| |j|d n!| jD ]% t d ||  fdd| D }|j||d qW d   dS W d   dS 1 sOw   Y  dS )	r0   r1   Nz"Untarring contents of '%s' to '%s'r2   r3   c                    s   g | ]
}|j  r|qS r   )r5   r4   )r   r9   r6   r   r   r      s    
z'Untar._extract_file.<locals>.<listcomp>r8   )r   openr	   r   r9   r:   
getmembers)r   r   r
   Ztar_filer=   r   r6   r   r#      s$   


"zUntar._extract_fileNr>   r   r   r   r   r@      r?   r@   c                   @   sF   e Zd ZdZdeeeedZddddZdd	d
Z	dd Z
d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bzip2rE   rG   rH   )z.xzz.gzz.bz2rD   c                 C   r   r   )methodr5   )r   rI   r5   r   r   r   r     r   zDecompress.__init__c              	   C   s   | j du r
|d }ntjtj|| j }|dv s tj|sht d||| j | 	|}t
|d,}|
|}t|| W d   n1 sKw   Y  W d   |S W d   |S 1 scw   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)r5   r   r   r   dirnamer!   r   r9   rI   _compression_modulerA   shutilcopyfileobj)r   r   r%   r&   decompressedmoduleoutput
compressedr   r   r   r)     s,   




zDecompress.__call__c                 C   s   d}| j | jvr'd| j  dt| j  d}| j dv r#d||g}t|| j dkr^tj|d }|| j	vrVd	| dt| j	  d}|d
v rRd||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 rD   zUnrecognized file extension '>   z.zipz.tar)
rI   moduleslistkeysr   
ValueErrorr   r   splitext
extensions)r   r   Zerror_archivesmessageextr   r   r   rK   @  s*   



zDecompress._compression_module)rD   N)r*   r+   r,   r-   rE   rG   bz2rV   r[   r   r)   rK   r   r   r   r   rC      s    +
,rC   )r-   r   r^   rG   rE   rL   zipfiler   tarfiler   utilsr   r   r/   r@   rC   r   r   r   r   <module>   s   Q;?