o
    Åîï]Ñ  ã                   @   s¨   d dl mZ d dlmZ 	 d Z	 dZ	 dZ	 dZ	 dZ	 eZ		 eZ
	 dZ	 dZ	 eefZe	e
eefZe
d	e	d
edediZdZ	 dZ	 G dd„ deƒZG dd„ deƒZdS )é    )ÚUUID)ÚPY3é   é   é   é   é   é   ÚPYTHON_LEGACYÚSTANDARDÚJAVA_LEGACYÚCSHARP_LEGACYé€   c                       s`   e Zd ZdZdZefdd„Zedd„ ƒZ‡ fdd„Z	d	d
„ Z
‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚBinaryaÖ  Representation of BSON binary data.

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

    Raises TypeError if `data` is not an instance of :class:`bytes`
    (:class:`str` in python 2) or `subtype` is not an instance of
    :class:`int`. Raises ValueError if `subtype` is not in [0, 256).

    .. note::
      In python 3 instances of Binary with subtype 0 will be decoded
      directly to :class:`bytes`.

    :Parameters:
      - `data`: the binary data to represent. Can be any bytes-like type
        that implements the buffer protocol.
      - `subtype` (optional): the `binary subtype
        <http://bsonspec.org/#/specification>`_
        to use

    .. versionchanged:: 3.9
      Support any bytes-like type that implements the buffer protocol.
    r   c                 C   sH   t |tƒs	tdƒ‚|dks|dk rtdƒ‚t | t|ƒ ¡ ¡}||_|S )Nz"subtype must be an instance of inté   r   z%subtype must be contained in [0, 256))	Ú
isinstanceÚintÚ	TypeErrorÚ
ValueErrorÚbytesÚ__new__Ú
memoryviewÚtobytesÚ_Binary__subtype)ÚclsÚdataÚsubtypeÚself© r   ú=/var/www/html/env/lib/python3.10/site-packages/bson/binary.pyr   ”   s   
zBinary.__new__c                 C   ó   | j S )z%Subtype of this binary data.
        )r   ©r   r   r   r   r   ž   ó   zBinary.subtypec                    s4   t t| ƒ ¡ d }trt|tƒs| d¡}|| jfS )Nr   zlatin-1)Úsuperr   Ú__getnewargs__r   r   r   Úencoder   )r   r   ©Ú	__class__r   r   r$   ¤   s   

zBinary.__getnewargs__c                 C   s*   t |tƒr| jt| ƒf|jt|ƒfkS dS )NF)r   r   r   r   r   ©r   Úotherr   r   r   Ú__eq__«   s
   
ÿzBinary.__eq__c                    s   t t| ƒ ¡ t| jƒA S ©N)r#   r   Ú__hash__Úhashr   r!   r&   r   r   r,   ´   s   zBinary.__hash__c                 C   s
   | |k S r+   r   r(   r   r   r   Ú__ne__·   ó   
zBinary.__ne__c                 C   s   dt  | ¡| jf S )NzBinary(%s, %s))r   Ú__repr__r   r!   r   r   r   r0   º   s   zBinary.__repr__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú_type_markerÚBINARY_SUBTYPEr   Úpropertyr   r$   r*   r,   r.   r0   Ú__classcell__r   r   r&   r   r   w   s    

	r   c                   @   s4   e Zd ZdZdd„ Zdd„ Zedd„ ƒZdd	„ Zd
S )Ú
UUIDLegacyaç  UUID wrapper to support working with UUIDs stored as PYTHON_LEGACY.

    .. doctest::

      >>> import uuid
      >>> from bson.binary import Binary, UUIDLegacy, STANDARD
      >>> from bson.codec_options import CodecOptions
      >>> my_uuid = uuid.uuid4()
      >>> coll = db.get_collection('test',
      ...                          CodecOptions(uuid_representation=STANDARD))
      >>> coll.insert_one({'uuid': Binary(my_uuid.bytes, 3)}).inserted_id
      ObjectId('...')
      >>> coll.count_documents({'uuid': my_uuid})
      0
      >>> coll.count_documents({'uuid': UUIDLegacy(my_uuid)})
      1
      >>> coll.find({'uuid': UUIDLegacy(my_uuid)})[0]['uuid']
      UUID('...')
      >>>
      >>> # Convert from subtype 3 to subtype 4
      >>> doc = coll.find_one({'uuid': UUIDLegacy(my_uuid)})
      >>> coll.replace_one({"_id": doc["_id"]}, doc).matched_count
      1
      >>> coll.count_documents({'uuid': UUIDLegacy(my_uuid)})
      0
      >>> coll.count_documents({'uuid': {'$in': [UUIDLegacy(my_uuid), my_uuid]}})
      1
      >>> coll.find_one({'uuid': my_uuid})['uuid']
      UUID('...')

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

    :Parameters:
      - `obj`: An instance of :class:`~uuid.UUID`.
    c                 C   s,   t |tƒs	tdƒ‚t | |jt¡}||_|S )Nz$obj must be an instance of uuid.UUID)r   r   r   r   r   r   ÚOLD_UUID_SUBTYPEÚ_UUIDLegacy__uuid)r   Úobjr   r   r   r   r   ã   s
   
zUUIDLegacy.__new__c                 C   s   | j fS r+   ©r;   r!   r   r   r   r$   ê   s   zUUIDLegacy.__getnewargs__c                 C   r    )z;UUID instance wrapped by this UUIDLegacy instance.
        r=   r!   r   r   r   Úuuidî   r"   zUUIDLegacy.uuidc                 C   s
   d| j  S )NzUUIDLegacy('%s')r=   r!   r   r   r   r0   ô   r/   zUUIDLegacy.__repr__N)	r1   r2   r3   r4   r   r$   r7   r>   r0   r   r   r   r   r9   ¾   s    $
r9   N)r>   r   Úbson.py3compatr   r6   ÚFUNCTION_SUBTYPEÚOLD_BINARY_SUBTYPEr:   ÚUUID_SUBTYPEr   r
   r   r   ÚALL_UUID_SUBTYPESÚALL_UUID_REPRESENTATIONSÚUUID_REPRESENTATION_NAMESÚMD5_SUBTYPEÚUSER_DEFINED_SUBTYPEr   r   r9   r   r   r   r   Ú<module>   sD   			üG