o
    Bj0                     @  sz   U d dl mZ d dlmZ d dlmZ d dlZd dlZe Z	da
ded< dd	d
ZdddZedddZdddZdS )    )annotations)Iterator)contextmanagerN	base58btcstr_default_encodingreturnc                   C  s   t S )zEReturn the current default multibase encoding (e.g. ``'base58btc'``).)r    r	   r	   9/Users/sumanjeet/code/py-libp2p/libp2p/encoding_config.pyget_default_encoding   s   r   encodingNonec                 C  sZ   t | sdt  }td| d| t | aW d   dS 1 s&w   Y  dS )u  
    Set the process-wide default multibase encoding.

    Writes are protected by an internal lock.  Reads via
    :func:`get_default_encoding` are **not** locked, so a concurrent
    reader may briefly see a stale value.

    Parameters
    ----------
    encoding : str
        A multibase encoding name recognised by *py-multibase*
        (e.g. ``'base58btc'``, ``'base32'``, ``'base64'``,
        ``'base64url'``, …).

    Raises
    ------
    ValueError
        If *encoding* is not supported by *py-multibase*.

    z, zUnsupported encoding z. Supported encodings: N)	multibaseis_encoding_supportedjoinlist_encodings
ValueError_lockr   )r   	supportedr	   r	   r
   set_default_encoding   s   
"r   Iterator[None]c                 c  sr    t  }t|  zdV  W t |aW d   dS 1 sw   Y  dS t
 |aW d   w 1 s3w   Y  w )ur  
    Temporarily override the default encoding within a ``with`` block.

    The previous encoding is restored when the block exits—even on exceptions.

    .. warning:: **Thread-safety caveat**

       The lock is only held for the individual *write* operations (setting
       and restoring the default), **not** for the entire duration of the
       ``with`` block.  This means that if two threads use
       ``encoding_override`` concurrently, they can interleave and one
       thread's override may be visible to the other.  For multi-threaded
       applications that require isolation, prefer passing an explicit
       *encoding* parameter to ``to_multibase()`` / ``bytes_to_multibase()``
       instead of relying on the global default.

    Parameters
    ----------
    encoding : str
        A multibase encoding name recognised by *py-multibase*.

    Raises
    ------
    ValueError
        If *encoding* is not supported.

    Example
    -------
    >>> with encoding_override("base32"):
    ...     peer_id.to_multibase()  # uses base32
    ...
    >>> peer_id.to_multibase()      # back to the previous default

    N)r   r   r   r   )r   previousr	   r	   r
   encoding_override3   s   $(r   	list[str]c                   C  s   t t S )zGReturn a sorted list of all encoding names supported by *py-multibase*.)sortedr   r   r	   r	   r	   r
   list_supported_encodingsa   s   r   )r   r   )r   r   r   r   )r   r   r   r   )r   r   )
__future__r   collections.abcr   
contextlibr   	threadingr   Lockr   r   __annotations__r   r   r   r   r	   r	   r	   r
   <module>   s    

!-