U
    dv                     @   sR   d Z ddlmZ ddlmZmZmZ ddlmZm	Z	 ddl
mZ G dd dZdS )	z@Tools for manipulating DBRefs (references to MongoDB documents).    )deepcopy)AnyMappingOptional)_getstate_slots_setstate_slots)SONc                   @   s   e Zd ZdZdZeZeZdZ	d e
eee
 eee
ef  eddddZee
dd	d
ZeedddZeee
 dddZeedddZee
ef dddZe
dddZeedddZeedddZedddZed dddZdS )!DBRefz,A reference to a document stored in MongoDB.)Z__collectionZ__idZ
__databaseZ__kwargsd   N)
collectioniddatabase_extrakwargsreturnc                 K   sV   t |tstd|dk	r,t |ts,td|| _|| _|| _||pHi  || _dS )a"  Initialize a new :class:`DBRef`.

        Raises :class:`TypeError` if `collection` or `database` is not
        an instance of :class:`str`. `database` is optional and allows
        references to documents to work across databases. Any additional
        keyword arguments will create additional fields in the resultant
        embedded document.

        :Parameters:
          - `collection`: name of the collection the document is stored in
          - `id`: the value of the document's ``"_id"`` field
          - `database` (optional): name of the database to reference
          - `**kwargs` (optional): additional keyword arguments will
            create additional, custom fields

        .. seealso:: The MongoDB documentation on `dbrefs <https://dochub.mongodb.org/core/dbrefs>`_.
        z%collection must be an instance of strNz#database must be an instance of str)
isinstancestr	TypeError_DBRef__collection
_DBRef__id_DBRef__databaseupdate_DBRef__kwargs)selfr   r   r   r   r    r   ./tmp/pip-unpacked-wheel-oblwsawz/bson/dbref.py__init__!   s    
zDBRef.__init__)r   c                 C   s   | j S )z(Get the name of this DBRef's collection.)r   r   r   r   r   r   E   s    zDBRef.collectionc                 C   s   | j S )zGet this DBRef's _id.)r   r   r   r   r   r   J   s    zDBRef.idc                 C   s   | j S )zoGet the name of this DBRef's database.

        Returns None if this DBRef doesn't specify a database.
        )r   r   r   r   r   r   O   s    zDBRef.database)keyr   c                 C   s.   z| j | W S  tk
r(   t|Y nX d S N)r   KeyErrorAttributeError)r   r   r   r   r   __getattr__W   s    zDBRef.__getattr__c                 C   s<   t d| jfd| jfg}| jdk	r,| j|d< || j |S )zsGet the SON document representation of this DBRef.

        Generally not needed by application developers
        z$refz$idNz$db)r   r   r   r   r   r   )r   docr   r   r   as_doc]   s
    

zDBRef.as_docc                 C   s`   d dd | j D }| jd kr>d| jd| j| dS d| jd| jd| j| dS )N c                 S   s    g | ]\}}d | d|qS ), =r   ).0kvr   r   r   
<listcomp>i   s     z"DBRef.__repr__.<locals>.<listcomp>zDBRef(r&   ))joinr   itemsr   r   r   )r   extrar   r   r   __repr__h   s    
zDBRef.__repr__)otherr   c                 C   s>   t |tr:| j| j| j| jf}|j|j|j|jf}||kS tS r   )r   r	   r   r   r   r   NotImplemented)r   r1   usZthemr   r   r   __eq__n   s
    
zDBRef.__eq__c                 C   s
   | |k S r   r   )r   r1   r   r   r   __ne__u   s    zDBRef.__ne__c                 C   s$   t | j| j| jtt| j fS )z)Get a hash value for this :class:`DBRef`.)hashr   r   r   tuplesortedr   r.   r   r   r   r   __hash__x   s    zDBRef.__hash__)memor   c                 C   s.   t t| j|t| j|t| j|t| j|S )z'Support function for `copy.deepcopy()`.)r	   r   r   r   r   r   )r   r:   r   r   r   __deepcopy__~   s    



zDBRef.__deepcopy__)NN)__name__
__module____qualname____doc__	__slots__r   __getstate__r   __setstate__Z_type_markerr   r   r   r   r   propertyr   r   r   r"   r   r$   r0   boolr4   r5   intr9   r;   r   r   r   r   r	      s8     $r	   N)r?   copyr   typingr   r   r   Zbson._helpersr   r   Zbson.sonr   r	   r   r   r   r   <module>   s
   