3
OÉ6bÆ  ã               @   s@   d Z ddlmZ ddlmZmZ ddlmZ G dd„ deƒZ	dS )z@Tools for manipulating DBRefs (references to MongoDB documents).é    )Údeepcopy)Ú	iteritemsÚstring_type)ÚSONc               @   s†   e Zd ZdZdZdi fdd„Zedd„ ƒZedd	„ ƒZed
d„ ƒZ	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚDBRefz1A reference to a document stored in MongoDB.
    éd   Nc             K   s`   t |tƒstdtj ƒ‚|dk	r:t |tƒ r:tdtj ƒ‚|| _|| _|| _|j|ƒ || _dS )aD  Initialize a new :class:`DBRef`.

        Raises :class:`TypeError` if `collection` or `database` is not
        an instance of :class:`basestring` (:class:`str` in python 3).
        `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 %sNz"database must be an instance of %s)	Ú
isinstancer   Ú	TypeErrorÚ__name__Ú_DBRef__collectionÚ
_DBRef__idÚ_DBRef__databaseÚupdateÚ_DBRef__kwargs)ÚselfÚ
collectionÚidÚdatabaseÚ_extraÚkwargs© r   úI/var/www/html/sandeepIITI/myenv/lib/python3.6/site-packages/bson/dbref.pyÚ__init__   s    



zDBRef.__init__c             C   s   | j S )z<Get the name of this DBRef's collection as unicode.
        )r   )r   r   r   r   r   =   s    zDBRef.collectionc             C   s   | j S )zGet this DBRef's _id.
        )r   )r   r   r   r   r   C   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   I   s    zDBRef.databasec             C   s,   y
| j | S  tk
r&   t|ƒ‚Y nX d S )N)r   ÚKeyErrorÚAttributeError)r   Úkeyr   r   r   Ú__getattr__Q   s    
zDBRef.__getattr__c             C   s   | j j|ƒ d S )N)Ú__dict__r   )r   Ústater   r   r   Ú__setstate__Z   s    zDBRef.__setstate__c             C   s<   t d| jfd| jfgƒ}| jdk	r,| j|d< |j| 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   sL   dj dd„ t| jƒD ƒƒ}| jd kr6d| j| j|f S d| j| j| j|f S )NÚ c             S   s   g | ]\}}d ||f ‘qS )z, %s=%rr   )Ú.0ÚkÚvr   r   r   ú
<listcomp>j   s   z"DBRef.__repr__.<locals>.<listcomp>zDBRef(%r, %r%s)zDBRef(%r, %r, %r%s))Újoinr   r   r   r   r   )r   Úextrar   r   r   Ú__repr__i   s    


zDBRef.__repr__c             C   s>   t |tƒr:| j| j| j| jf}|j|j|j|jf}||kS tS )N)r   r   r   r   r   r   ÚNotImplemented)r   ÚotherÚusZthemr   r   r   Ú__eq__q   s    
zDBRef.__eq__c             C   s
   | |k S )Nr   )r   r+   r   r   r   Ú__ne__z   s    zDBRef.__ne__c             C   s$   t | j| j| jtt| jjƒ ƒƒfƒS )z)Get a hash value for this :class:`DBRef`.)Úhashr   r   r   ÚtupleÚsortedr   Úitems)r   r   r   r   Ú__hash__}   s    zDBRef.__hash__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   Úmemor   r   r   Ú__deepcopy__‚   s    

zDBRef.__deepcopy__)r
   Ú
__module__Ú__qualname__Ú__doc__Z_type_markerr   Úpropertyr   r   r   r   r   r!   r)   r-   r.   r3   r5   r   r   r   r   r      s   		r   N)
r8   Úcopyr   Zbson.py3compatr   r   Zbson.sonr   Úobjectr   r   r   r   r   Ú<module>   s   