3
]ð]   ã               @   s0   d Z ddlmZmZmZmZ G dd„ deƒZdS )z0Tools for representing JavaScript code in BSON.
é    )ÚabcÚstring_typeÚPY3Ú	text_typec               @   sF   e Zd ZdZdZddd„Zedd„ ƒZdd	„ Zd
d„ Z	dZ
dd„ ZdS )ÚCodea�  BSON's JavaScript code type.

    Raises :class:`TypeError` if `code` is not an instance of
    :class:`basestring` (:class:`str` in python 3) or `scope`
    is not ``None`` or an instance of :class:`dict`.

    Scope variables can be set by passing a dictionary as the `scope`
    argument or by using keyword arguments. If a variable is set as a
    keyword argument it will override any setting for that variable in
    the `scope` dictionary.

    :Parameters:
      - `code`: A string containing JavaScript code to be evaluated or another
        instance of Code. In the latter case, the scope of `code` becomes this
        Code's :attr:`scope`.
      - `scope` (optional): dictionary representing the scope in which
        `code` should be evaluated - a mapping from identifiers (as
        strings) to values. Defaults to ``None``. This is applied after any
        scope associated with a given `code` above.
      - `**kwargs` (optional): scope variables can also be passed as
        keyword arguments. These are applied after `scope` and `code`.

    .. versionchanged:: 3.4
      The default value for :attr:`scope` is ``None`` instead of ``{}``.

    é   Nc             K   sÐ   t |tƒstdtj ƒ‚t r<t |tƒr<tj| |jdƒƒ}ntj| |ƒ}y|j	|_
W n tk
rn   d |_
Y nX |d k	rªt |tjƒsŒtdƒ‚|j
d k	r¤|j
j|ƒ n||_
|rÌ|j
d k	rÆ|j
j|ƒ n||_
|S )Nzcode must be an instance of %sÚutf8z!scope must be an instance of dict)Ú
isinstancer   Ú	TypeErrorÚ__name__r   r   ÚstrÚ__new__ÚencodeÚscopeÚ_Code__scopeÚAttributeErrorr   ÚMappingÚupdate)ÚclsÚcoder   ÚkwargsÚself© r   ú,/tmp/pip-build-20mum3z4/pymongo/bson/code.pyr   3   s*    



zCode.__new__c             C   s   | j S )z8Scope dictionary for this instance or ``None``.
        )r   )r   r   r   r   r   R   s    z
Code.scopec             C   s   dt j| ƒ| jf S )NzCode(%s, %r))r   Ú__repr__r   )r   r   r   r   r   X   s    zCode.__repr__c             C   s*   t |tƒr&| jt| ƒf|jt|ƒfkS dS )NF)r	   r   r   r   )r   Úotherr   r   r   Ú__eq__[   s    
zCode.__eq__c             C   s
   | |k S )Nr   )r   r   r   r   r   Ú__ne__b   s    zCode.__ne__)N)r   Ú
__module__Ú__qualname__Ú__doc__Z_type_markerr   Úpropertyr   r   r   Ú__hash__r   r   r   r   r   r      s   
r   N)r    Zbson.py3compatr   r   r   r   r   r   r   r   r   r   Ú<module>   s   