o
    
i                     @   s   d Z ddlZddlZddlZeeZi ZdZ		 dZ
	 dd Zdd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd Ze
dfddZede ede ede ede dS )z=Implements the compression layer of the `smart_open` library.    Ndisableinfer_from_extensionc                   C   s   t tgt  S )z}Return the list of supported compression types available to open.

    See compression paratemeter to smart_open.open().
    )NO_COMPRESSIONINFER_FROM_EXTENSIONget_supported_extensions r   r   J/home/ubuntu/.local/lib/python3.10/site-packages/smart_open/compression.pyget_supported_compression_types   s   r	   c                   C   s   t t S )zLReturn the list of file extensions for which we have registered compressors.)sorted_COMPRESSOR_REGISTRYkeysr   r   r   r   r   "   s   r   c                 C   sD   | r| d dkst d|  |  } | tv rtd|  |t| < dS )al  Register a callback for transparently decompressing files with a specific extension.

    Parameters
    ----------
    ext: str
        The extension.  Must include the leading period, e.g. `.gz`.
    callback: callable
        The callback.  It must accept two position arguments, file_obj and mode.
        This function will be called when `smart_open` is opening a file with
        the specified extension.

    Examples
    --------

    Instruct smart_open to use the `lzma` module whenever opening a file
    with a .xz extension (see README.rst for the complete example showing I/O):

    >>> def _handle_xz(file_obj, mode):
    ...     import lzma
    ...     return lzma.LZMAFile(filename=file_obj, mode=mode)
    >>>
    >>> register_compressor('.xz', _handle_xz)

    This is just an example: `lzma` is in the standard library and is registered by default.

    r   .z,ext must be a string starting with ., not %rz.overriding existing compression handler for %rN)
ValueErrorlowerr   loggerwarning)extcallbackr   r   r   register_compressor'   s   r   c                    s   | j  fdd}|| _ dS )a  Ensure that closing the `outer` stream closes the `inner` stream as well.

    Deprecated: `smart_open.open().__exit__` now always calls `__exit__` on the
    underlying filestream.

    Use this when your compression library's `close` method does not
    automatically close the underlying filestream.  See
    https://github.com/piskvorky/smart_open/issues/630 for an
    explanation why that is a problem for smart_open.
    c                     sB   z  W  rd   }|   d S d S  r d   }|   w w )Nclose)argsfpinnerouter_closer   r   
close_bothW   s   


ztweak_close.<locals>.close_bothNr   )outerr   r   r   r   r   tweak_closeJ   s   
	r   c                 C   s@   | }d|v rd|v rt |}|S d|v rd|v rt |}|S )Nbwr)ioBufferedWriterBufferedReader)file_objmoderesultr   r   r   _maybe_wrap_bufferedc   s   

r(   c                 C       dd l }|j| |d}t||S Nr   )filenamer&   )bz2openr(   )r%   r&   r,   r'   r   r   r   _handle_bz2m      
r.   c                 C   r)   r*   )gzipr-   r(   )r%   r&   r0   r'   r   r   r   _handle_gzips   r/   r1   c                 C   sD   dd l }|jdkrddlm} nddlm} |j| |d}t||S )Nr   )      )zstd)r&   )sysversion_infocompressionr4   	backportsr-   r(   )r%   r&   r5   r4   r'   r   r   r   _handle_zstdy   s   

r9   c                 C   r)   r*   )lzmar-   r(   )r%   r&   r:   r'   r   r   r   
_handle_xz   r/   r;   c              	   C   s   |t kr| S |tkr/z	|p| j }W n ttfy&   td|  |  Y S w tj	
|\}}|tv r>|dr>td| zt| }W n tyO   |  Y S w || |S )a  
    Wrap `file_obj` with an appropriate [de]compression mechanism based on its file extension.

    If the filename extension isn't recognized, simply return the original `file_obj` unchanged.

    `file_obj` must either be a filehandle object, or a class which behaves like one.

    If `filename` is specified, it will be used to extract the extension.
    If not, the `file_obj.name` attribute is used as the filename.

    zRunable to transparently decompress %r because it seems to lack a string-like .name+z3transparent (de)compression unsupported for mode %r)r   r   namer   AttributeError	TypeErrorr   r   ospathsplitextr   endswithr   KeyError)r%   r&   r7   r+   _r   r   r   r   compression_wrapper   s*   
rF   z.bz2z.gzz.zstz.xz)__doc__r"   loggingos.pathr@   	getLogger__name__r   r   r   r   r	   r   r   r   r(   r.   r1   r9   r;   rF   r   r   r   r   <module>   s0   
#


'

