3
OÉ6bÃ  ã               @   sD   d Z ddlZddlmZ ddlmZmZ dd„ ZG dd„ deƒZ	dS )	z4Tools for representing MongoDB regular expressions.
é    N)ÚRE_TYPE)Ústring_typeÚ	text_typec             C   st   d}d| kr|t jO }d| kr(|t jO }d| kr:|t jO }d| krL|t jO }d| kr^|t jO }d| krp|t jO }|S )Nr   ÚiÚlÚmÚsÚuÚx)ÚreÚ
IGNORECASEÚLOCALEÚ	MULTILINEÚDOTALLÚUNICODEÚVERBOSE)Z	str_flagsÚflags© r   úI/var/www/html/sandeepIITI/myenv/lib/python3.6/site-packages/bson/regex.pyÚstr_flags_to_int   s    





r   c               @   sN   e Zd ZdZdZedd„ ƒZddd„Zdd	„ Zd
Z	dd„ Z
dd„ Zdd„ Zd
S )ÚRegexzBSON regular expression data.é   c             C   s(   t |tƒstdt|ƒ ƒ‚t|j|jƒS )a¼  Convert a Python regular expression into a ``Regex`` instance.

        Note that in Python 3, a regular expression compiled from a
        :class:`str` has the ``re.UNICODE`` flag set. If it is undesirable
        to store this flag in a BSON regular expression, unset it first::

          >>> pattern = re.compile('.*')
          >>> regex = Regex.from_native(pattern)
          >>> regex.flags ^= re.UNICODE
          >>> db.collection.insert({'pattern': regex})

        :Parameters:
          - `regex`: A regular expression object from ``re.compile()``.

        .. warning::
           Python regular expressions use a different syntax and different
           set of flags than MongoDB, which uses `PCRE`_. A regular
           expression retrieved from the server may not compile in
           Python, or may match a different set of strings in Python than
           when used in a MongoDB query.

        .. _PCRE: http://www.pcre.org/
        z3regex must be a compiled regular expression, not %s)Ú
isinstancer   Ú	TypeErrorÚtyper   Úpatternr   )ÚclsÚregexr   r   r   Úfrom_native.   s
    
zRegex.from_nativer   c             C   s`   t |ttfƒstdt|ƒ ƒ‚|| _t |tƒr:t|ƒ| _n"t |t	ƒrL|| _ntdt|ƒ ƒ‚dS )ap  BSON regular expression data.

        This class is useful to store and retrieve regular expressions that are
        incompatible with Python's regular expression dialect.

        :Parameters:
          - `pattern`: string
          - `flags`: (optional) an integer bitmask, or a string of flag
            characters like "im" for IGNORECASE and MULTILINE
        z pattern must be a string, not %sz%flags must be a string or int, not %sN)
r   r   Úbytesr   r   r   r   r   r   Úint)Úselfr   r   r   r   r   Ú__init__N   s    

zRegex.__init__c             C   s*   t |tƒr"| j|jko | j|jkS tS d S )N)r   r   r   r   ÚNotImplemented)r!   Úotherr   r   r   Ú__eq__e   s    
zRegex.__eq__Nc             C   s
   | |k S )Nr   )r!   r$   r   r   r   Ú__ne__m   s    zRegex.__ne__c             C   s   d| j | jf S )NzRegex(%r, %r))r   r   )r!   r   r   r   Ú__repr__p   s    zRegex.__repr__c             C   s   t j| j| jƒS )a  Compile this :class:`Regex` as a Python regular expression.

        .. warning::
           Python regular expressions use a different syntax and different
           set of flags than MongoDB, which uses `PCRE`_. A regular
           expression retrieved from the server may not compile in
           Python, or may match a different set of strings in Python than
           when used in a MongoDB query. :meth:`try_compile()` may raise
           :exc:`re.error`.

        .. _PCRE: http://www.pcre.org/
        )r   Úcompiler   r   )r!   r   r   r   Útry_compiles   s    zRegex.try_compile)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Z_type_markerÚclassmethodr   r"   r%   Ú__hash__r&   r'   r)   r   r   r   r   r   *   s    
r   )
r-   r   Zbson.sonr   Zbson.py3compatr   r   r   Úobjectr   r   r   r   r   Ú<module>   s
   