Ë
    Ü_Ëià  ã                  ó  — U d Z ddlmZ ddlmZmZmZmZmZ ddl	m
Z
mZ ddlmZmZ ddlmZ 	 d	 	 	 	 	 	 	 dd„Z G d	„ d
eeef   «      Z G d„ de«      Z ej*                  e¬«      Zded<    ej*                  e¬«      Zded<   y)a  Tools for representing raw BSON documents.

Inserting and Retrieving RawBSONDocuments
=========================================

Example: Moving a document between different databases/collections

.. doctest::

  >>> import bson
  >>> from pymongo import MongoClient
  >>> from bson.raw_bson import RawBSONDocument
  >>> client = MongoClient(document_class=RawBSONDocument)
  >>> client.drop_database("db")
  >>> client.drop_database("replica_db")
  >>> db = client.db
  >>> result = db.test.insert_many(
  ...     [{"_id": 1, "a": 1}, {"_id": 2, "b": 1}, {"_id": 3, "c": 1}, {"_id": 4, "d": 1}]
  ... )
  >>> replica_db = client.replica_db
  >>> for doc in db.test.find():
  ...     print(f"raw document: {doc.raw}")
  ...     print(f"decoded document: {bson.decode(doc.raw)}")
  ...     result = replica_db.test.insert_one(doc)
  ...
  raw document: b'...'
  decoded document: {'_id': 1, 'a': 1}
  raw document: b'...'
  decoded document: {'_id': 2, 'b': 1}
  raw document: b'...'
  decoded document: {'_id': 3, 'c': 1}
  raw document: b'...'
  decoded document: {'_id': 4, 'd': 1}

For use cases like moving documents across different databases or writing binary
blobs to disk, using raw BSON documents provides better speed and avoids the
overhead of decoding or encoding BSON.
é    )Úannotations)ÚAnyÚ	ItemsViewÚIteratorÚMappingÚOptional)Ú_get_object_sizeÚ_raw_to_dict)Ú_RAW_BSON_DOCUMENT_MARKERÚCodecOptions)ÚDEFAULT_CODEC_OPTIONSúCodecOptions[RawBSONDocument]c                ó<   — t        | dt        | «      dz
  |i |¬«      S )a  Inflates the top level fields of a BSON document.

    :param bson_bytes: the BSON bytes that compose this document
    :param codec_options: An instance of
        :class:`~bson.codec_options.CodecOptions` whose ``document_class``
        must be :class:`RawBSONDocument`.
    é   é   ©Ú	raw_array)r
   Úlen)Ú
bson_bytesÚcodec_optionsr   s      úP/var/www/pod-logistic/pod-api/venv/lib/python3.12/site-packages/bson/raw_bson.pyÚ_inflate_bsonr   >   s#   € ô ˜
 A¤s¨:£¸Ñ':¸MÈ2ÐYbÔcÐcó    c                  ó¬   — e Zd ZU dZdZeZded<   	 d	 	 	 	 	 dd„Ze	dd„«       Z
dd„Ze	dd	„«       Ze	 	 	 	 	 	 dd
„«       Zdd„Zdd„Zdd„Zdd„Zdd„Zy)ÚRawBSONDocumentzØRepresentation for a MongoDB document that provides access to the raw
    BSON bytes that compose it.

    Only when a field is accessed or modified within the document does
    RawBSONDocument decode its bytes.
    )Ú__rawÚ__inflated_docÚ__codec_optionsr   Ú_RawBSONDocument__codec_optionsNc                óÐ   — || _         d| _        |€t        }n2t        |j                  t
        «      st        d|j                  › �«      ‚|| _        t        |dt        |«      «       y)a  Create a new :class:`RawBSONDocument`

        :class:`RawBSONDocument` is a representation of a BSON document that
        provides access to the underlying raw BSON bytes. Only when a field is
        accessed or modified within the document does RawBSONDocument decode
        its bytes.

        :class:`RawBSONDocument` implements the ``Mapping`` abstract base
        class from the standard library so it can be used like a read-only
        ``dict``::

            >>> from bson import encode
            >>> raw_doc = RawBSONDocument(encode({'_id': 'my_doc'}))
            >>> raw_doc.raw
            b'...'
            >>> raw_doc['_id']
            'my_doc'

        :param bson_bytes: the BSON bytes that compose this document
        :param codec_options: An instance of
            :class:`~bson.codec_options.CodecOptions` whose ``document_class``
            must be :class:`RawBSONDocument`. The default is
            :attr:`DEFAULT_RAW_BSON_OPTIONS`.

        .. versionchanged:: 3.8
          :class:`RawBSONDocument` now validates that the ``bson_bytes``
          passed in represent a single bson document.

        .. versionchanged:: 3.5
          If a :class:`~bson.codec_options.CodecOptions` is passed in, its
          `document_class` must be :class:`RawBSONDocument`.
        Nz<RawBSONDocument cannot use CodecOptions with document class r   )
Ú_RawBSONDocument__rawÚ_RawBSONDocument__inflated_docÚDEFAULT_RAW_BSON_OPTIONSÚ
issubclassÚdocument_classr   Ú	TypeErrorr   r	   r   )Úselfr   r   s      r   Ú__init__zRawBSONDocument.__init__Y   sn   € ðJ  ˆŒ
Ø;?ˆÔð Ð Ü4‰MÜ˜M×8Ñ8¼/ÔJÜðØ&×5Ñ5Ð6ð8óð ð  -ˆÔä˜ Q¬¨J«Õ8r   c                ó   — | j                   S )z+The raw BSON bytes composing this document.)r!   ©r'   s    r   ÚrawzRawBSONDocument.raw�   s   € ð �z‰zÐr   c                ó6   — | j                   j                  «       S )z4Lazily decode and iterate elements in this document.)Ú_RawBSONDocument__inflatedÚitemsr*   s    r   r.   zRawBSONDocument.items’   s   € à�‰×$Ñ$Ó&Ð&r   c                óˆ   — | j                   €+| j                  | j                  | j                  «      | _         | j                   S ©N)r"   r   r!   r   r*   s    r   Ú
__inflatedzRawBSONDocument.__inflated–   s;   € à×ÑÐ&ð #'×"4Ñ"4°T·Z±ZÀ×AUÑAUÓ"VˆDÔØ×"Ñ"Ð"r   c                ó   — t        | |«      S r0   ©r   ©r   r   s     r   r   zRawBSONDocument._inflate_bsonž   s   € ô ˜Z¨Ó7Ð7r   c                ó    — | j                   |   S r0   )r-   )r'   Úitems     r   Ú__getitem__zRawBSONDocument.__getitem__¤   s   € Ø�‰˜tÑ$Ð$r   c                ó,   — t        | j                  «      S r0   )Úiterr-   r*   s    r   Ú__iter__zRawBSONDocument.__iter__§   s   € Ü�D—O‘OÓ$Ð$r   c                ó,   — t        | j                  «      S r0   )r   r-   r*   s    r   Ú__len__zRawBSONDocument.__len__ª   s   € Ü�4—?‘?Ó#Ð#r   c                ó`   — t        |t        «      r| j                  |j                  k(  S t        S r0   )Ú
isinstancer   r!   r+   ÚNotImplemented)r'   Úothers     r   Ú__eq__zRawBSONDocument.__eq__­   s%   € Ü�eœ_Ô-Ø—:‘: §¡Ñ*Ð*ÜÐr   c                óh   — | j                   j                  › d| j                  ›d| j                  ›d�S )Nú(z, codec_options=ú))Ú	__class__Ú__name__r+   r   r*   s    r   Ú__repr__zRawBSONDocument.__repr__²   s3   € Ø—.‘.×)Ñ)Ð*¨!¨D¯H©H¨<Ð7GÈ×H\ÑH\ÐG_Ð_`ÐaÐar   r0   )r   úbytes | memoryviewr   z'Optional[CodecOptions[RawBSONDocument]]ÚreturnÚNone)rI   rH   )rI   zItemsView[str, Any])rI   úMapping[str, Any]©r   rH   r   r   rI   rK   )r6   ÚstrrI   r   )rI   zIterator[str])rI   Úint)r@   r   rI   Úbool)rI   rM   )rF   Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   Ú_type_markerÚ__annotations__r(   Úpropertyr+   r.   r-   Ústaticmethodr   r7   r:   r<   rA   rG   © r   r   r   r   M   sµ   … ñð ?€IØ,€LØ2Ó2ð
 BFð29à&ð29ð ?ð29ð 
ó	29ðh òó ðó'ð ò#ó ð#ð ð8Ø&ð8Ø7Tð8à	ò8ó ð8ó
%ó%ó$óô
br   r   c                  ó.   — e Zd ZdZe	 	 	 	 	 	 dd„«       Zy)Ú_RawArrayBSONDocumentzKA RawBSONDocument that only expands sub-documents and arrays when accessed.c                ó   — t        | |d¬«      S )NTr   r3   r4   s     r   r   z#_RawArrayBSONDocument._inflate_bson¹   s   € ô ˜Z¨À$ÔGÐGr   NrL   )rF   rP   rQ   rR   rW   r   rX   r   r   rZ   rZ   ¶   s4   „ ÙUàðHØ&ðHØ7TðHà	òHó ñHr   rZ   )r%   r#   z#CodecOptions[_RawArrayBSONDocument]Ú_RAW_ARRAY_BSON_OPTIONSN)F)r   rH   r   r   r   rO   rI   zdict[str, Any])rR   Ú
__future__r   Útypingr   r   r   r   r   Úbsonr	   r
   Úbson.codec_optionsr   r   r   ÚDEFAULTr   rM   r   rZ   Úwith_optionsr#   rU   r\   rX   r   r   ú<module>rc      sÃ   ðò%õL #ç >Õ >ç /ß FÝ ?ð ðdØ"ðdà0ðdð ðdð ó	dôfb�g˜c 3˜hÑ'ô fbôRH˜Oô Hð ;O¸'×:NÑ:NØ"ô;Ð Ð7ó ð @T¸w×?SÑ?SØ(ô@Ð Ð<ó ðr   