o
    'h                     @  s   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dZG dd deeef ZG dd deZejedZded< ejedZded< dS )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_OPTIONSF
bson_bytesbytescodec_optionsCodecOptions[RawBSONDocument]	raw_arrayboolreturndict[str, Any]c                 C  s   t | dt| d |i |d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`.
          r   )r	   len)r   r   r    r   I/var/www/html/olx_land/venv/lib/python3.10/site-packages/bson/raw_bson.py_inflate_bson>   s   
r   c                   @  s   e Zd ZU dZdZeZded< 	d)d*ddZe	d+ddZ
d,ddZe	d-ddZed.ddZd/ddZd0ddZd1d!d"Zd2d%d&Zd3d'd(ZdS )4RawBSONDocumentzRepresentation 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_optionsNr   r   r   'Optional[CodecOptions[RawBSONDocument]]r   Nonec                 C  sP   || _ d| _|du rt}nt|jtstd|j || _t|dt	| dS )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   r   r   r   __init__W   s   #zRawBSONDocument.__init__c                 C  s   | j S )z+The raw BSON bytes composing this document.)r#   r)   r   r   r   raw   s   zRawBSONDocument.rawItemsView[str, Any]c                 C  s
   | j  S )z4Lazily decode and iterate elements in this document.)_RawBSONDocument__inflateditemsr+   r   r   r   r/      s   
zRawBSONDocument.itemsMapping[str, Any]c                 C  s"   | j d u r| | j| j| _ | j S N)r$   r   r#   r    r+   r   r   r   
__inflated   s   
zRawBSONDocument.__inflatedc                 C  s
   t | |S r1   r   r   r   r   r   r   r      s   
zRawBSONDocument._inflate_bsonitemstrr   c                 C  s
   | j | S r1   )r.   )r)   r5   r   r   r   __getitem__      
zRawBSONDocument.__getitem__Iterator[str]c                 C  
   t | jS r1   )iterr.   r+   r   r   r   __iter__   r8   zRawBSONDocument.__iter__intc                 C  r:   r1   )r   r.   r+   r   r   r   __len__   r8   zRawBSONDocument.__len__otherr   c                 C  s   t |tr| j|jkS tS r1   )
isinstancer   r#   r,   NotImplemented)r)   r?   r   r   r   __eq__   s   
zRawBSONDocument.__eq__c                 C  s   | j j d| jd| jdS )N(z, codec_options=))	__class____name__r,   r    r+   r   r   r   __repr__   s   zRawBSONDocument.__repr__r1   )r   r   r   r!   r   r"   )r   r   )r   r-   )r   r0   r   r   r   r   r   r0   )r5   r6   r   r   )r   r9   )r   r=   )r?   r   r   r   )r   r6   )rF   
__module____qualname____doc__	__slots__r
   _type_marker__annotations__r*   propertyr,   r/   r.   staticmethodr   r7   r<   r>   rB   rG   r   r   r   r   r   K   s&   
 2




r   c                   @  s   e Zd ZdZeddd	Zd
S )_RawArrayBSONDocumentzKA RawBSONDocument that only expands sub-documents and arrays when accessed.r   r   r   r   r   r0   c                 C  s   t | |ddS )NTr   r3   r4   r   r   r   r      s   z#_RawArrayBSONDocument._inflate_bsonNrH   )rF   rI   rJ   rK   rP   r   r   r   r   r   rQ      s    rQ   )r'   r%   z#CodecOptions[_RawArrayBSONDocument]_RAW_ARRAY_BSON_OPTIONSN)F)r   r   r   r   r   r   r   r   )rK   
__future__r   typingr   r   r   r   r   bsonr   r	   bson.codec_optionsr
   r   r   DEFAULTr   r6   r   rQ   with_optionsr%   rN   rR   r   r   r   r   <module>   s"   &g
