o
    Åîï]Ò  ã                   @   sj   d Z ddlmZmZ ddlmZmZ ddlmZ	m
Z
 ddlmZ G dd„ dejƒZdd	„ Ze	jed
�ZdS )z+Tools for representing raw BSON documents.
é    )Ú_raw_to_dictÚ_get_object_size)ÚabcÚ	iteritems)ÚDEFAULT_CODEC_OPTIONSÚ_RAW_BSON_DOCUMENT_MARKER)ÚSONc                   @   sj   e Zd ZdZdZeZddd„Zedd„ ƒZ	dd	„ Z
ed
d„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )Ú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_optionsNc                 C   sN   || _ d| _|du rt}n|jturtd|jf ƒ‚|| _t|dt|ƒƒ dS )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 %sr   )	Ú_RawBSONDocument__rawÚ_RawBSONDocument__inflated_docÚDEFAULT_RAW_BSON_OPTIONSÚdocument_classr	   Ú	TypeErrorÚ_RawBSONDocument__codec_optionsr   Úlen)ÚselfÚ
bson_bytesÚcodec_options© r   ú?/var/www/html/env/lib/python3.10/site-packages/bson/raw_bson.pyÚ__init__$   s   "
ÿÿzRawBSONDocument.__init__c                 C   s   | j S )z+The raw BSON bytes composing this document.)r   ©r   r   r   r   ÚrawT   s   zRawBSONDocument.rawc                 C   ó
   t | jƒS )z4Lazily decode and iterate elements in this document.)r   Ú_RawBSONDocument__inflatedr   r   r   r   ÚitemsY   s   
zRawBSONDocument.itemsc                 C   s    | j d u rt| j| jƒ| _ | j S ©N)r   Ú_inflate_bsonr   r   r   r   r   r   Ú
__inflated]   s
   
ÿzRawBSONDocument.__inflatedc                 C   s
   | j | S r   )r   )r   Úitemr   r   r   Ú__getitem__g   ó   
zRawBSONDocument.__getitem__c                 C   r   r   )Úiterr   r   r   r   r   Ú__iter__j   r$   zRawBSONDocument.__iter__c                 C   r   r   )r   r   r   r   r   r   Ú__len__m   r$   zRawBSONDocument.__len__c                 C   s   t |tƒr| j|jkS tS r   )Ú
isinstancer	   r   r   ÚNotImplemented)r   Úotherr   r   r   Ú__eq__p   s   
zRawBSONDocument.__eq__c                 C   s   d| j | jf S )Nz%RawBSONDocument(%r, codec_options=%r))r   r   r   r   r   r   Ú__repr__u   s   
ÿzRawBSONDocument.__repr__r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   Ú_type_markerr   Úpropertyr   r   r   r#   r&   r'   r+   r,   r   r   r   r   r	      s    
0

	r	   c                 C   s   t | dt| ƒd |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   r   r   r   r    z   s   
ÿr    )r   N)r0   Úbsonr   r   Úbson.py3compatr   r   Úbson.codec_optionsr   ÚDEFAULTr   Úbson.sonr   ÚMappingr	   r    Úwith_optionsr   r   r   r   r   Ú<module>   s   a