o
    'hnK                  
   @  sP  d dl mZ d dlZd dlmZ d dlmZmZmZm	Z	m
Z
mZmZmZ d dlmZ 	 d Z	 dZ	 dZ	 dZ	 d	Z	 erHd d
lmZ d dlmZ G dd dZejZ	 ejZ	 ejZ	 ejZ	 eefZejejejejejfZ ejdejdejdejdejdiZ!dZ"	 dZ#	 dZ$	 dZ%	 dZ&	 G dd deZ'G dd dZ(G dd de)Z*dS )    )annotationsN)Enum)TYPE_CHECKINGAnyOptionalSequenceTupleTypeUnionoverload)UUID            )array)mmapc                   @  s(   e Zd ZdZ	 eZ	 eZ	 dZ	 dZ	dS )UuidRepresentationr         N)
__name__
__module____qualname__UNSPECIFIEDUUID_SUBTYPESTANDARDOLD_UUID_SUBTYPEPYTHON_LEGACYJAVA_LEGACYCSHARP_LEGACY r    r    G/var/www/html/olx_land/venv/lib/python3.10/site-packages/bson/binary.pyr   D   s    r   zUuidRepresentation.UNSPECIFIEDzUuidRepresentation.STANDARDz UuidRepresentation.PYTHON_LEGACYzUuidRepresentation.JAVA_LEGACYz UuidRepresentation.CSHARP_LEGACYr         	      c                   @  s   e Zd ZdZdZdZdZdS )BinaryVectorDtypeaN  Datatypes of vector subtype.

    :param FLOAT32: (0x27) Pack list of :class:`float` as float32
    :param INT8: (0x03) Pack list of :class:`int` in [-128, 127] as signed int8
    :param PACKED_BIT: (0x10) Pack list of :class:`int` in [0, 255] as unsigned uint8

    The `PACKED_BIT` value represents a special case where vector values themselves
    can only be of two values (0 or 1) but these are packed together into groups of 8,
    a byte. In Python, these are displayed as ints in range [0, 255]

    Each value is of type bytes with a length of one.

    .. versionadded:: 4.10
          '   N)r   r   r   __doc__INT8FLOAT32
PACKED_BITr    r    r    r!   r&      s
    r&   c                   @  s4   e Zd ZdZdZddd
dZdddZdddZdS )BinaryVectorzbVector of numbers along with metadata for binary interoperability.
    .. versionadded:: 4.10
    datadtypepaddingr   r0   Sequence[float | int]r1   r&   r2   intc                 C  s   || _ || _|| _dS )a\  
        :param data: Sequence of numbers representing the mathematical vector.
        :param dtype:  The data type stored in binary
        :param padding: The number of bits in the final byte that are to be ignored
          when a vector element's size is less than a byte
          and the length of the vector is not a multiple of 8.
        Nr/   )selfr0   r1   r2   r    r    r!   __init__   s   
zBinaryVector.__init__returnstrc                 C  s   d| j  d| j d| j dS )NzBinaryVector(dtype=z
, padding=z, data=))r1   r2   r0   r5   r    r    r!   __repr__   s   zBinaryVector.__repr__otherr   boolc                 C  s2   t |tsdS | j|jko| j|jko| j|jkS NF)
isinstancer.   r1   r2   r0   r5   r<   r    r    r!   __eq__   s   
"zBinaryVector.__eq__Nr   )r0   r3   r1   r&   r2   r4   r7   r8   r<   r   r7   r=   )r   r   r   r*   	__slots__r6   r;   rA   r    r    r    r!   r.      s    
r.   c                      s   e Zd ZU dZdZded< efd6ddZee	j
fd7ddZe	j
fd8ddZeed9ddZee	d:d;ddZe		d<d=d"dZd>d#d$Zed?d%d&Zd@ fd(d)ZdAd-d.Zd? fd/d0ZdAd1d2ZdBd4d5Z  ZS )CBinarya  Representation of BSON binary data.

    We want to represent Python strings as the BSON string type.
    We need to wrap binary data so that we can tell
    the difference between what should be considered binary data and
    what should be considered a string when we encode to BSON.

    Subtype 9 provides a space-efficient representation of 1-dimensional vector data.
    Its data is prepended with two bytes of metadata.
    The first (dtype) describes its data type, such as float32 or int8.
    The second (padding) prescribes the number of bits to ignore in the final byte.
    This is relevant when the element size of the dtype is not a multiple of 8.

    Raises TypeError if `subtype` is not an instance of :class:`int`.
    Raises ValueError if `subtype` is not in [0, 256).

    .. note::
      Instances of Binary with subtype 0 will be decoded directly to :class:`bytes`.

    :param data: the binary data to represent. Can be any bytes-like type
        that implements the buffer protocol.
    :param subtype: the `binary subtype
        <https://bsonspec.org/spec.html>`_
        to use

    .. versionchanged:: 3.9
       Support any bytes-like type that implements the buffer protocol.

    .. versionchanged:: 4.10
       Addition of vector subtype.
    r   r4   _Binary__subtypeclsType[Binary]r0   ,Union[memoryview, bytes, _mmap, _array[Any]]subtyper7   c                 C  sR   t |tstdt| |dks|dk rtdt| t| }||_	|S )Nz(subtype must be an instance of int, not    r   z%subtype must be contained in [0, 256))
r?   r4   	TypeErrortype
ValueErrorbytes__new__
memoryviewtobytesrG   )rH   r0   rK   r5   r    r    r!   rQ   '  s   
zBinary.__new__uuidr   uuid_representationc                 C  s   t |tstdt| |tvrtd|tjkrtdt}|tj	kr*|j
}n-|tjkrI|j
}|dd ddd |dd ddd  }n|tjkrR|j}nt}|j
}| ||S )	a  Create a BSON Binary object from a Python UUID.

        Creates a :class:`~bson.binary.Binary` object from a
        :class:`uuid.UUID` instance. Assumes that the native
        :class:`uuid.UUID` instance uses the byte-order implied by the
        provided ``uuid_representation``.

        Raises :exc:`TypeError` if `uuid` is not an instance of
        :class:`~uuid.UUID`.

        :param uuid: A :class:`uuid.UUID` instance.
        :param uuid_representation: A member of
            :class:`~bson.binary.UuidRepresentation`. Default:
            :const:`~bson.binary.UuidRepresentation.STANDARD`.
            See :ref:`handling-uuid-data-example` for details.

        .. versionadded:: 3.11
        z+uuid must be an instance of uuid.UUID, not Guuid_representation must be a value from bson.binary.UuidRepresentationa  cannot encode native uuid.UUID with UuidRepresentation.UNSPECIFIED. UUIDs can be manually converted to bson.Binary instances using bson.Binary.from_uuid() or a different UuidRepresentation can be configured. See the documentation for UuidRepresentation for more information.r   r#   N   )r?   r   rM   rN   ALL_UUID_REPRESENTATIONSrO   r   r   r   r   rP   r   r   bytes_ler   )rH   rT   rU   rK   payload	from_uuidr    r    r!   r\   5  s*   

	

.

zBinary.from_uuidc                 C  s   | j tvrtd| j  d|tvrtd|tjkrtd|tjkr/| j tkr.t| dS n>|tj	krS| j tkrRt| dd ddd	 | dd
 ddd	  dS n|tj
krc| j tkrbt| dS n
| j tkrmt| dS td| j  dt|  )a9  Create a Python UUID from this BSON Binary object.

        Decodes this binary object as a native :class:`uuid.UUID` instance
        with the provided ``uuid_representation``.

        Raises :exc:`ValueError` if this :class:`~bson.binary.Binary` instance
        does not contain a UUID.

        :param uuid_representation: A member of
            :class:`~bson.binary.UuidRepresentation`. Default:
            :const:`~bson.binary.UuidRepresentation.STANDARD`.
            See :ref:`handling-uuid-data-example` for details.

        .. versionadded:: 3.11
        zcannot decode subtype z
 as a uuidrV   z)uuid_representation cannot be UNSPECIFIED)rP   r   r#   NrW   rX   )rZ   z to )rK   ALL_UUID_SUBTYPESrO   rY   r   r   r   r   r   r   r   r   UUID_REPRESENTATION_NAMES)r5   rU   r    r    r!   as_uuidl  s2   






2




zBinary.as_uuidvectorr.   c                 C     d S Nr    )rH   r`   r    r    r!   from_vector  s   zBinary.from_vectorr   Union[list[int], list[float]]r1   r&   r2   c                 C  ra   rb   r    )rH   r`   r1   r2   r    r    r!   rc     s   N+Union[BinaryVector, list[int], list[float]]Optional[BinaryVectorDtype]Optional[int]c                 C  s  t |tr|s	|rtd|j}|j}|j}|du rdn|}|tjkr/d}|r.td|n;|tjkrSd}d|  kr@dkrJn ntd|d	|rR|sRtd
n|tj	krdd}|rctd|nt
d| td|j|}tjdt| | g|R  }| || tdS )a  Create a BSON :class:`~bson.binary.Binary` of Vector subtype.

        To interpret the representation of the numbers, a data type must be included.
        See :class:`~bson.binary.BinaryVectorDtype` for available types and descriptions.

        The dtype and padding are prepended to the binary data's value.

        :param vector: Either a List of values, or a :class:`~bson.binary.BinaryVector` dataclass.
        :param dtype: Data type of the values
        :param padding: For fractional bytes, number of bits to ignore at end of vector.
        :return: Binary packed data identified by dtype and padding.

        .. versionadded:: 4.10
        zkThe first argument, vector, has type BinaryVector. dtype or padding cannot be separately defined, but were.Nr   bz padding does not apply to dtype=Br"   zpadding=z. It must be in [0,1, ..7].z#Empty vector with non-zero padding.fz%s not yet supported<sB<)rK   )r?   r.   rO   r1   r2   r0   r&   r+   r-   r,   NotImplementedErrorstructpackvaluelenVECTOR_SUBTYPE)rH   r`   r1   r2   
format_strmetadatar0   r    r    r!   rc     s<   



 c           
      C  s.  | j tkrtd| j  dd}td| |\}}|d7 }t|}t| | }|tjkrDd}d| | }tt|| |}t	|||S |tj
krst| | }|d }|d r[td	d
}d| | }tt|| |}t	|||S |tjkrd}d| | }tt|| |}	t	|	||S td|j )zFrom the Binary, create a list of numbers, along with dtype and padding.

        :return: BinaryVector

        .. versionadded:: 4.10
        zCannot decode subtype z as a vectorr   rk   r   rh   rl   r   zCCorrupt data. N bytes for a float32 vector must be a multiple of 4.rj   ri   z(Binary Vector dtype %s not yet supported)rK   rr   rO   rn   unpack_fromr&   rq   r+   listr.   r,   r-   rm   name)
r5   positionr1   r2   n_valuesdtype_formatformat_stringr`   n_bytesunpacked_uint8sr    r    r!   	as_vector  s:   



zBinary.as_vectorc                 C  s   | j S )zSubtype of this binary data.)rG   r:   r    r    r!   rK   	  s   zBinary.subtypeTuple[bytes, int]c                   s,   t   d }t|ts|d}|| jfS )Nr   zlatin-1)super__getnewargs__r?   rP   encoderG   )r5   r0   	__class__r    r!   r     s   


zBinary.__getnewargs__r<   r   r=   c                 C  s*   t |tr| jt| f|jt|fkS dS r>   )r?   rF   rG   rP   rK   r@   r    r    r!   rA     s   
zBinary.__eq__c                   s   t   t| jA S rb   )r   __hash__hashrG   r:   r   r    r!   r     s   zBinary.__hash__c                 C  s
   | |k S rb   r    r@   r    r    r!   __ne__   s   
zBinary.__ne__r8   c                 C  s2   | j tkrd| j  dS dt|  d| j  dS )Nz<Binary(REDACTED, z)>zBinary(z, r9   )rG   SENSITIVE_SUBTYPErP   r;   r:   r    r    r!   r;   #  s   
zBinary.__repr__)rH   rI   r0   rJ   rK   r4   r7   rF   )rH   rI   rT   r   rU   r4   r7   rF   )rU   r4   r7   r   )rH   rI   r`   r.   r7   rF   rB   )
rH   rI   r`   rd   r1   r&   r2   r4   r7   rF   )NN)
rH   rI   r`   re   r1   rf   r2   rg   r7   rF   )r7   r.   )r7   r4   )r7   r   rD   rC   )r   r   r   r*   _type_marker__annotations__BINARY_SUBTYPErQ   classmethodr   r   r\   r_   r   rc   r~   propertyrK   r   rA   r   r   r;   __classcell__r    r    r   r!   rF     s:   
  6,
4-

rF   )+
__future__r   rn   enumr   typingr   r   r   r   r   r	   r
   r   rT   r   r   FUNCTION_SUBTYPEOLD_BINARY_SUBTYPEr   r   r   _arrayr   _mmapr   r   r   r   r   r]   r   rY   r^   MD5_SUBTYPECOLUMN_SUBTYPEr   rr   USER_DEFINED_SUBTYPEr&   r.   rP   rF   r    r    r    r!   <module>   sl   (	C