U
    ~fhx                     @  s^   d Z ddlm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).    )annotations)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(dddddd	d
ddZ
eddddZeddddZeddddZdddddZddddZddddZdddddZdddd d!Zd"dd#d$Zdd d%d&d'ZdS ))DBRefz,A reference to a document stored in MongoDB.)Z__collectionZ__idZ
__databaseZ__kwargsd   Nstrr   zOptional[str]zOptional[Mapping[str, Any]]None)
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.

        :param collection: name of the collection the document is stored in
        :param id: the value of the document's ``"_id"`` field
        :param database: name of the database to reference
        :param kwargs: 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)
isinstancer   	TypeError_DBRef__collection
_DBRef__id_DBRef__databaseupdate_DBRef__kwargs)selfr   r   r   r   r    r   ./tmp/pip-unpacked-wheel-36gvocj8/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  s0   z| j | W S  tk
r*   t|d Y nX d S N)r   KeyErrorAttributeError)r   r    r   r   r   __getattr__W   s    zDBRef.__getattr__zSON[str, Any]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__bool)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   r4   usZthemr   r   r   __eq__n   s
    
zDBRef.__eq__c                 C  s
   | |k S r!   r   )r   r4   r   r   r   __ne__u   s    zDBRef.__ne__intc                 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   r0   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   propertyr   r   r   r$   r&   r2   r7   r8   r=   r?   r   r   r   r   r
      s*     #r
   N)rC   
__future__r   copyr   typingr   r   r   Zbson._helpersr   r   Zbson.sonr	   r
   r   r   r   r   <module>   s   