
    h                         d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZ  G d de	j                        Zd	 Z ej                   e
      Zy)aL  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([{'a': 1},
  ...                               {'b': 1},
  ...                               {'c': 1},
  ...                               {'d': 1}])
  >>> replica_db = client.replica_db
  >>> for doc in db.test.find():
  ...    print("raw document: %r" % (doc.raw,))
  ...    result = replica_db.test.insert_one(doc)
  raw document: '...'
  raw document: '...'
  raw document: '...'
  raw document: '...'
  >>> for doc in replica_db.test.find(projection={'_id': 0}):
  ...     print("decoded document: %r" % (bson.decode(doc.raw),))
  decoded document: {u'a': 1}
  decoded document: {u'b': 1}
  decoded document: {u'c': 1}
  decoded document: {u'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.
    )_get_object_size_raw_to_dict)_RAW_BSON_DOCUMENT_MARKER)DEFAULT_CODEC_OPTIONS)abc	iteritems)SONc                   d    e Zd ZdZdZeZddZed        Z	d Z
ed        Zd Zd	 Zd
 Zd Zd Zy)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_optionsNc                     || _         d| _        |t        }n*|j                  t        urt        d|j                        || _        t        |dt        |             y)a9  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'

        :Parameters:
          - `bson_bytes`: the BSON bytes that compose this document
          - `codec_options` (optional): 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document_classr   	TypeError_RawBSONDocument__codec_optionsr   len)self
bson_bytescodec_optionss      K/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/bson/raw_bson.py__init__zRawBSONDocument.__init__I   sd    D  
"  4M))@+::=   -QJ8    c                     | j                   S )z+The raw BSON bytes composing this document.)r   r   s    r   rawzRawBSONDocument.rawz   s     zzr   c                 ,    t        | j                        S )z4Lazily decode and iterate elements in this document.)r   _RawBSONDocument__inflatedr   s    r   itemszRawBSONDocument.items   s    ))r   c                 |    | j                   %t        | j                  | j                        | _         | j                   S N)r   _inflate_bsonr   r   r   s    r   
__inflatedzRawBSONDocument.__inflated   s5    & #0

D<P<P"QD"""r   c                      | j                   |   S r$   )r!   )r   items     r   __getitem__zRawBSONDocument.__getitem__   s    t$$r   c                 ,    t        | j                        S r$   )iterr!   r   s    r   __iter__zRawBSONDocument.__iter__   s    DOO$$r   c                 ,    t        | j                        S r$   )r   r!   r   s    r   __len__zRawBSONDocument.__len__   s    4??##r   c                 `    t        |t              r| j                  |j                  k(  S t        S r$   )
isinstancer   r   r   NotImplemented)r   others     r   __eq__zRawBSONDocument.__eq__   s%    e_-::**r   c                 <    d| j                   d| j                  dS )NzRawBSONDocument(z, codec_options=))r   r   r   s    r   __repr__zRawBSONDocument.__repr__   s    :>((DDXDXYYr   r$   )__name__
__module____qualname____doc__	__slots__r   _type_markerr   propertyr   r"   r!   r)   r,   r.   r3   r6    r   r   r   r   >   s`     ?I,L/9b  * # #%%$
Zr   r   c                 H    t        | dt        |       dz
  |t                     S )a%  Inflates the top level fields of a BSON document.

    :Parameters:
      - `bson_bytes`: the BSON bytes that compose this document
      - `codec_options`: An instance of
        :class:`~bson.codec_options.CodecOptions` whose ``document_class``
        must be :class:`RawBSONDocument`.
          )r   r   r	   )r   r   s     r   r%   r%      s"     
As:':M35QQr   )r   N)r:   bsonr   r   bson.codec_optionsr   r   DEFAULTbson.py3compatr   r   bson.sonr	   Mappingr   r%   with_optionsr   r>   r   r   <module>rI      sQ   &P 0 8 ? ) ]Zckk ]Z@
R 07//O r   