3
]ð]Ò  ã               @   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krt}n|jtk	r4t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   ú0/tmp/pip-build-20mum3z4/pymongo/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   s
   t | jƒS )z4Lazily decode and iterate elements in this document.)r   Ú_RawBSONDocument__inflated)r   r   r   r   ÚitemsY   s    zRawBSONDocument.itemsc             C   s    | j d krt| j| jƒ| _ | j S )N)r   Ú_inflate_bsonr   r   )r   r   r   r   Z
__inflated]   s    
zRawBSONDocument.__inflatedc             C   s
   | j | S )N)r   )r   Úitemr   r   r   Ú__getitem__g   s    zRawBSONDocument.__getitem__c             C   s
   t | jƒS )N)Úiterr   )r   r   r   r   Ú__iter__j   s    zRawBSONDocument.__iter__c             C   s
   t | jƒS )N)r   r   )r   r   r   r   Ú__len__m   s    zRawBSONDocument.__len__c             C   s   t |tƒr| j|jkS tS )N)Ú
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
   r   r   )N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r   Z_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)r+   Zbsonr   r   Zbson.py3compatr   r   Zbson.codec_optionsr   ÚDEFAULTr   Zbson.sonr   ÚMappingr	   r   Zwith_optionsr   r   r   r   r   Ú<module>   s   a