U
    9%e8                     @   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   s   || _ || _d S N)membersextract_dir)selfr   r	    r   O/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/pooch/processors.py__init__+   s    zExtractorProcessor.__init__c           
   
      s6  j dkrjdkrtdjdkr4|j  _n(|jtjjddd }tj|j_|dkstjjrj	dk	rt
fddj	D stjjd	d
 |j g }tjD ]n\}}}|D ]^}	tjtjtj|j|	 j	dkst fddj	D r|tj||	 qq|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downloadc                 3   s&   | ]}t jt j j|V  qd S r   )ospathexistsjoinr	   .0m)r
   r   r   	<genexpr>X   s   z.ExtractorProcessor.__call__.<locals>.<genexpr>T)exist_okc                 3   s    | ]}  tj|V  qd S r   )
startswithr   r   normpathr   )relpathr   r   r   k   s    )suffixr	   NotImplementedErrorrsplitr   r   sepr   r   r   allmakedirs_extract_filewalkr   r   anyappend)
r
   fnameactionpoocharchive_dirfnamesr   _filesfilenamer   )r   r
   r   __call__/   s>    
zExtractorProcessor.__call__c                 C   s   t dS )z
        This method receives an argument for the archive to extract and the
        destination path. MUST BE IMPLEMENTED BY CHILD CLASSES.
        N)r   )r
   r(   r	   r   r   r   r$   r   s    z ExtractorProcessor._extract_file)NN)__name__
__module____qualname____doc__r   r   r0   r$   r   r   r   r   r      s
   
Cr   c                   @   s   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p}| jdkr4t d|| |j|d nB| jD ]: t d ||  fdd| D }|j||d q:W 5 Q R X 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 | ]"}t j|t j r|qS r   )r   r   r   r   )r   namememberr   r   
<listcomp>   s   z'Unzip._extract_file.<locals>.<listcomp>r   r   )r   r   r   info
extractallnamelist)r
   r(   r	   zip_filesubdir_membersr   r;   r   r$      s&    
  
   	
zUnzip._extract_fileNr1   r2   r3   r4   r   r$   r   r   r   r   r5   z   s   r5   c                   @   s   e Zd ZdZdZd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`).
    z.untarc              	      s   t |dp}| jdkr6t d|| |j|d nB| jD ]: t d ||  fdd| D }|j||d q<W 5 Q R X dS )	r6   r7   Nz"Untarring contents of '%s' to '%s'r8   r9   c                    s,   g | ]$}t j|jt j r|qS r   )r   r   r   r:   r   )r   r?   r;   r   r   r=      s
   
z'Untar._extract_file.<locals>.<listcomp>r>   )r   openr   r   r?   r@   
getmembers)r
   r(   r	   Ztar_filerC   r   r;   r   r$      s&    
  
   
zUntar._extract_fileNrD   r   r   r   r   rE      s   rE   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bzip2rJ   rL   rM   )z.xzz.gzz.bz2rI   c                 C   s   || _ || _d S r   )methodr:   )r
   rN   r:   r   r   r   r   '  s    zDecompress.__init__c              
   C   s   | j dkr|d }ntjtj|| j }|dks@tj|st d||| j | 	|}t
|d(}|
|}t|| W 5 Q R X W 5 Q R X |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)r:   r   r   r   dirnamer   r   r?   rN   _compression_modulerF   shutilcopyfileobj)r
   r(   r)   r*   decompressedmoduleoutput
compressedr   r   r   r0   +  s    


 zDecompress.__call__c                 C   s   d}| j | jkrNd| j  dt| j  d}| j dkrFd||g}t|| j dkrtj|d }|| j	krd	| dt| j	  d}|d
krd||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 rI   zUnrecognized file extension '>   .zip.tar)
rN   moduleslistkeysr   
ValueErrorr   r   splitext
extensions)r
   r(   Zerror_archivesmessageextr   r   r   rP   W  s"    


zDecompress._compression_module)rI   N)r1   r2   r3   r4   rJ   rL   bz2r]   rb   r   r0   rP   r   r   r   r   rH      s   +
,rH   )r4   r   re   rL   rJ   rQ   zipfiler   tarfiler   utilsr   r   r5   rE   rH   r   r   r   r   <module>   s   d=A