o
    
i                     @   s   d Z ddlZddlZddlZddlZddlZeeZ	g dZ
dZdd Zdd Zdd	d
ZdddZdd Zdd ZG dd dejZG dd dejZdS )z(Helper functions for documentation, etc.    N)s3s3ns3us3agsz0///smart_open.utils.QUESTION_MARK_PLACEHOLDER///c                 C   s   zt | }W n8 ty?   zt | \}}}}W n ty&   i  Y  Y S w |s-i  Y S |t| d  }tt|| Y S w dd |j	 D S )Nc                 S   s&   i | ]\}}|j tjjkr||j qS  )defaultinspect	Parameterempty).0nameparamr   r   D/home/ubuntu/.local/lib/python3.10/site-packages/smart_open/utils.py
<dictcomp>/   s
    z"inspect_kwargs.<locals>.<dictcomp>)
r	   	signatureAttributeError
getargspec	TypeErrorlendictzip
parametersitems)kallabler   argsvarargskeywordsdefaultssupported_keywordsr   r   r   inspect_kwargs   s    r    c                    sL   t t|   fddt |D } fdd| D }|r$td| |S )a  Check which keyword arguments the callable supports.

    Parameters
    ----------
    kallable: callable
        A function or method to test
    kwargs: dict
        The keyword arguments to check.  If the callable doesn't support any
        of these, a warning message will get printed.

    Returns
    -------
    dict
        A dictionary of argument names and values supported by the callable.
    c                    s   g | ]}| vr|qS r   r   )r   kr   r   r   
<listcomp>G   s    z check_kwargs.<locals>.<listcomp>c                    s   i | ]\}}| v r||qS r   r   )r   r!   vr"   r   r   r   H   s    z check_kwargs.<locals>.<dictcomp>z*ignoring unsupported keyword arguments: %r)sortedr    r   loggerwarning)r   kwargsunsupported_keywordssupported_kwargsr   r"   r   check_kwargs6   s   r+   c                 C   s    |dur	t | |} t| |} | S )aI  Clamp a numeric value to a specific range.

    Parameters
    ----------
    value: numeric
        The value to clamp.

    minval: numeric
        The lower bound.

    maxval: numeric
        The upper bound.

    Returns
    -------
    numeric
        The clamped value.  It will be in the range ``[minval, maxval]``.

    N)minmax)valueminvalmaxvalr   r   r   clampP   s   

r1   c                 C   sL   | du r|du rt d| du rdnt| }|du rdnt|}d||f S )ak  Create a byte range specifier in accordance with RFC-2616.

    Parameters
    ----------
    start: int, optional
        The start of the byte range.  If unspecified, stop indicated offset from EOF.

    stop: int, optional
        The end of the byte range.  If unspecified, indicates EOF.

    Returns
    -------
    str
        A byte range specifier.

    Nz7make_range_string requires either a stop or start value zbytes=%s-%s)
ValueErrorstr)startstop	start_strstop_strr   r   r   make_range_stringj   s
   r9   c                 C   sH   |  dd\}}| dd\}}| dd\}}|t|t|t|fS )a  Extract units, start, stop, and length from a content range header like "bytes 0-846981/846982".

    Assumes a properly formatted content-range header from S3.
    See werkzeug.http.parse_content_range_header for a more robust version.

    Parameters
    ----------
    content_range: str
        The content-range header to parse.

    Returns
    -------
    tuple (units: str, start: int, stop: int, length: int)
        The units and three integers from the content-range header.

        /-)splitint)content_rangeunitsnumbersrangelengthr5   r6   r   r   r   parse_content_range   s   rE   c                 C   s   t jj| dd}d}|jtv r'd| v r't| vr't}| d|} t jj| dd}|du r-|S |j|d}t j|j|j	|ddS )a  This is a hack to prevent the regular urlsplit from splitting around question marks.

    A question mark (?) in a URL typically indicates the start of a
    querystring, and the standard library's urlparse function handles the
    querystring separately.  Unfortunately, question marks can also appear
    _inside_ the actual URL for some schemas like S3, GS.

    Replaces question marks with a special placeholder substring prior to
    splitting.  This work-around behavior is disabled in the unlikely event the
    placeholder is already part of the URL.  If this affects you, consider
    changing the value of QUESTION_MARK_PLACEHOLDER to something more suitable.

    See Also
    --------
    https://bugs.python.org/issue43882
    https://github.com/python/cpython/blob/3.14/Lib/urllib/parse.py
    https://github.com/piskvorky/smart_open/issues/285
    https://github.com/piskvorky/smart_open/issues/458
    smart_open/utils.py:QUESTION_MARK_PLACEHOLDER
    F)allow_fragmentsN?r2   )
urllibparseurlsplitschemeWORKAROUND_SCHEMESQUESTION_MARK_PLACEHOLDERreplacepathSplitResultnetloc)urlsrplaceholderrO   r   r   r   safe_urlsplit   s   rU   c                   @   s   e Zd Zdd ZdS )TextIOWrapperc                 C   s   |du r
|    dS dS )at  Call close on underlying buffer only when there was no exception.

        Without this patch, TextIOWrapper would call self.buffer.close() during
        exception handling, which is unwanted for e.g. s3 and azure. They only call
        self.close() when there was no exception (self.terminate() otherwise) to avoid
        committing unfinished/failed uploads.
        N)close)selfexc_typeexc_valexc_tbr   r   r   __exit__   s   zTextIOWrapper.__exit__N)__name__
__module____qualname__r\   r   r   r   r   rV      s    rV   c                       sD   e Zd ZdZ fddZdd Z fddZdd	 Zd
d Z  Z	S )FileLikeProxy.c                    s   t  | || _d S N)super__init___FileLikeProxy__inner)rX   outerinner	__class__r   r   rc      s   
zFileLikeProxy.__init__c                 C   
   | j  S )zAThis explicit proxy method is only required for pylance ref #916.)__wrapped__	__enter__rX   r   r   r   rk      s   
zFileLikeProxy.__enter__c              
      s<   zt  j|i |W | jj|i | S | jj|i | w )zExit inner after exiting outer.)rb   r\   rd   )rX   r   r(   rg   r   r   r\      s   (zFileLikeProxy.__exit__c                 C   ri   ra   )rj   __next__rl   r   r   r   rm      s   
zFileLikeProxy.__next__c                 C   s@   z| j  W | j| j kr| j  S S | j| j kr| j  w w ra   )rj   rW   rd   rl   r   r   r   rW      s   
zFileLikeProxy.close)
r]   r^   r_   rd   rc   rk   r\   rm   rW   __classcell__r   r   rg   r   r`      s    r`   )r   N)NN)__doc__r	   iologgingurllib.parserH   wrapt	getLoggerr]   r&   rL   rM   r    r+   r1   r9   rE   rU   rV   ObjectProxyr`   r   r   r   r   <module>   s"   


)