U
    be>                  
   @   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	Z	ddl
ZddlZddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ eeZejdd	Z d
d Z!dd Z"e"Z#e$Z%dddddddej&df	ddZ$dd Z'd#ddZ(dd Z)d$ddZ*G dd de+Z,dd Z-d%d d!Zze.e$ e/e" W n8 e0k
r Z1 ze2d" e3e1 W 5 dZ1[1X Y nX dS )&zuImplements the majority of smart_open's top-level API.

The main functions are:

  * ``parse_uri()``
  * ``open()``

    N)doctools)	transport)register_compressor)check_kwargs)inspect_kwargsF)do_setlocalec                 C   s(   t jdkrd| krd|  } tj| jS )z0Returns the scheme of the URL only, as a string.ntz://zfile://)osnameurllibparseurlsplitscheme)uri_as_string r   [/var/www/html/services/stratfitenv/lib/python3.8/site-packages/smart_open/smart_open_lib.py_sniff_scheme2   s    r   c                 C   s:   t | }t|}|| }tdt| }|f |S )a  
    Parse the given URI from a string.

    Parameters
    ----------
    uri_as_string: str
        The URI to parse.

    Returns
    -------
    collections.namedtuple
        The parsed URI.

    Notes
    -----
    smart_open/doctools.py magic goes here
    Uri)r   r   get_transport	parse_uricollections
namedtuplesortedkeys)r   r   	submoduleas_dictr   r   r   r   r   >   s
    

r   rTc
              
   C   sz  t dt  t|ts td|t kr:td| |	dkrFi }	t	| ||||||d}
|
dk	rh|
S |dk	rd|kr|
dd}t| tjrt| } |}|r|nt}zt|}W n0 tk
r } zt|jd W 5 d}~X Y nX t| ||	}t|||}d|ks|dk	r(t|||||d	}n|}||krvd
g}|D ]4}zt||t|| W n tk
rp   Y nX q@|S )a  Open the URI object, returning a file-like object.

    The URI is usually a string in a variety of formats.
    For a full list of examples, see the :func:`parse_uri` function.

    The URI may also be one of:

    - an instance of the pathlib.Path class
    - a stream (anything that implements io.IOBase-like functionality)

    Parameters
    ----------
    uri: str or object
        The object to open.
    mode: str, optional
        Mimicks built-in open parameter of the same name.
    buffering: int, optional
        Mimicks built-in open parameter of the same name.
    encoding: str, optional
        Mimicks built-in open parameter of the same name.
    errors: str, optional
        Mimicks built-in open parameter of the same name.
    newline: str, optional
        Mimicks built-in open parameter of the same name.
    closefd: boolean, optional
        Mimicks built-in open parameter of the same name.  Ignored.
    opener: object, optional
        Mimicks built-in open parameter of the same name.  Ignored.
    compression: str, optional (see smart_open.compression.get_supported_compression_types)
        Explicitly specify the compression/decompression behavior.
    transport_params: dict, optional
        Additional parameters for the transport layer (see notes below).

    Returns
    -------
    A file-like object.

    Notes
    -----
    smart_open has several implementations for its transport layer (e.g. S3, HTTP).
    Each transport layer has a different set of keyword arguments for overriding
    default behavior.  If you specify a keyword argument that is *not* supported
    by the transport layer being used, smart_open will ignore that argument and
    log a warning message.

    smart_open/doctools.py magic goes here

    See Also
    --------
    - `Standard library reference <https://docs.python.org/3.7/library/functions.html#open>`__
    - `smart_open README.rst
      <https://github.com/RaRe-Technologies/smart_open/blob/master/README.rst>`__

    z%rzmode should be a stringzinvalid compression type: N)compression	bufferingencodingerrorsnewlineb r   )r    r!   r"   Zto_boto3)loggerdebuglocals
isinstancestr	TypeErrorso_compressionZget_supported_compression_types
ValueError_shortcut_openreplacepathlibPathDEFAULT_ENCODING_get_binary_modeNotImplementedErrorargs_open_binary_streamZcompression_wrapper_encoding_wrappersetattrgetattrAttributeError)urimoder   r    r!   r"   closefdopenerr   transport_paramsfobjZexplicit_encodingZbinary_modevebinarydecompresseddecodedZpromoted_attrsattrr   r   r   opend   s^    B
	
 

rE   c                    s   t | g  dkr$dkr$tdfdddD }t|dkrJtd fd	d
}dkrj|d n,dkr||d ndkr|d ntddkr|d n.dkr̈d  d n
 d dkr|d rtd|  d S )Ntr#   z'can't have text and binary mode at oncec                    s   g | ]}  |qS r   )count).0x)r;   r   r   
<listcomp>  s     z$_get_binary_mode.<locals>.<listcomp>Zrwa   z6must have exactly one of create/read/write/append modec                    s     |  d S N)appendpopindex)charZbinmoder;   r   r   transfer  s    z"_get_binary_mode.<locals>.transferawr   zKMust have exactly one of create/read/write/append mode and at most one plus+zinvalid mode: %rr$   )listr,   sumrN   rO   rM   join)Zmode_strcountsrR   r   rQ   r   r2      s8    




r2   c                 C   s   t | tsdS t| }|tjtjfkr*dS t| }|tj	kr^t
|\}	}
|
t krldS n|tjkrldS i }|dk	r||d< |dd}|dk	r||d< |rd|kr||d< t||fd|i|S )ab  Try to open the URI using the standard library io.open function.

    This can be much faster than the alternative of opening in binary mode and
    then decoding.

    This is only possible under the following conditions:

        1. Opening a local file; and
        2. Compression is disabled

    If it is not possible to use the built-in open for the specified URI, returns None.

    :param str uri: A string indicating what to open.
    :param str mode: The mode to pass to the open function.
    :param str compression: The compression type selected.
    :returns: The opened file
    :rtype: file
    Nr    r#   r$   r"   r!   r   )r(   r)   r   r   Z	NO_SCHEMEso_fileZSCHEMEZextract_local_pathr+   INFER_FROM_EXTENSIONPsplitextZget_supported_extensionsNO_COMPRESSIONr.   _builtin_open)r:   r;   r   r   r    r!   r"   r   Z
local_path_	extensionZopen_kwargsr   r   r   r-   4  s*    



r-   c                 C   s~   |dkrt d| t| tr0t| |dd}|S t| tsJtdt|  t| }t	|}|
| ||}t|dsz| |_|S )a  Open an arbitrary URI in the specified binary mode.

    Not all modes are supported for all protocols.

    :arg uri: The URI to open.  May be a string, or something else.
    :arg str mode: The mode to open with.  Must be rb, wb or ab.
    :arg transport_params: Keyword argumens for the transport layer.
    :returns: A named file object
    :rtype: file-like object with a .name attribute
    )rbzrb+wbzwb+abzab+zunsupported mode: %rF)r<   zdon't know how to handle uri %sr
   )r3   r(   intr_   r)   r*   reprr   r   r   Zopen_urihasattrr
   )r:   r;   r>   r?   r   r   r   r   r   r5   n  s    



r5   c                 C   sF   t dt  d|kr"|dkr"| S |dkr.t}tj| |||dd} | S )a  Decode bytes into text, if necessary.

    If mode specifies binary access, does nothing, unless the encoding is
    specified.  A non-null encoding implies text mode.

    :arg fileobj: must quack like a filehandle object.
    :arg str mode: is the mode which was originally requested by the user.
    :arg str encoding: The text encoding to use.  If mode is binary, overrides mode.
    :arg str errors: The method to use when handling encoding/decoding errors.
    :returns: a file object
    zencoding_wrapper: %rr#   NT)r    r!   r"   write_through)r%   r&   r'   r1   ioTextIOWrapper)fileobjr;   r    r!   r"   r   r   r   r6     s    r6   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	patch_pathlibz*Replace `Path.open` with `smart_open.open`c                 C   s   t t| _d S rL   )_patch_pathlibrE   old_implselfr   r   r   __init__  s    zpatch_pathlib.__init__c                 C   s   | S rL   r   ro   r   r   r   	__enter__  s    zpatch_pathlib.__enter__c                 C   s   t | j d S rL   )rm   rn   )rp   exc_typeexc_valexc_tbr   r   r   __exit__  s    zpatch_pathlib.__exit__N)__name__
__module____qualname____doc__rq   rr   rv   r   r   r   r   rl     s   rl   c                 C   s   t jj}| t j_|S )zReplace `Path.open` with `func`)r/   r0   rE   )funcrn   r   r   r   rm     s    rm   rb   c	                 K   sX   d}
|	rt dt|	|
f d|
 }tj|t d |r>tj}ntj}~	~
~~tf t S )Nz^https://github.com/RaRe-Technologies/smart_open/blob/develop/MIGRATING_FROM_OLDER_VERSIONS.rstzUThe following keyword parameters are not supported: %r. See  %s for more information.z9This function is deprecated.  See %s for more information)category)	DeprecationWarningr   warningswarnr+   r^   r[   rE   r'   )r:   r;   r   r    r!   r"   r<   r=   Zignore_extensionkwargsurlmessager   r   r   r   
smart_open  s    
r   zEncountered a non-fatal error while building docstrings (see below). help(smart_open) will provide incomplete information as a result. For full help text, see <https://github.com/RaRe-Technologies/smart_open/blob/master/help.txt>.)r   NNN)NNN)rb   r   NNNTNF)4rz   r   ri   localeloggingr	   os.pathpathr\   r/   urllib.parser   r~   Zsmart_open.local_file
local_filerZ   Zsmart_open.compressionr   r+   r   r   r   r   Zsmart_open.utilsr   Z_check_kwargsr   Z_inspect_kwargs	getLoggerrw   r%   getpreferredencodingr1   r   r   Z
_parse_urirE   r_   r[   r2   r-   r5   r6   objectrl   rm   Ztweak_open_docstringZtweak_parse_uri_docstring	Exceptionexerror	exceptionr   r   r   r   <module>	   sx   	
!
 :    
:)
'	        
-
