U
    d                     @   s|   d Z ddlZddlmZmZmZmZmZmZ ddl	m
Z
mZ ddlmZ eedddZed	eeZG d
d dee ZdS )z3Tools for representing MongoDB regular expressions.    N)AnyGenericPatternTypeTypeVarUnion)_getstate_slots_setstate_slots)RE_TYPE)	str_flagsreturnc                 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)r   flags r   ./tmp/pip-unpacked-wheel-oblwsawz/bson/regex.pystr_flags_to_int   s    





r   _Tc                   @   s   e Zd ZdZdZeZeZdZ	e
ed  dddddZdeeeef d
dddZeedddZd
ZeedddZedddZddddZd
S )RegexzBSON regular expression data.patternr      zPattern[_T]z	Regex[_T])clsregexr   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_one({'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   r!   r   )r#   r$   r   r   r   from_native7   s    
zRegex.from_nativer   N)r!   r   r   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%   strbytesr&   r'   r!   r   r   int)selfr!   r   r   r   r   __init__U   s    

zRegex.__init__)otherr   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,   r.   r   r   r   __eq__k   s    
zRegex.__eq__c                 C   s
   | |k S r/   r   r1   r   r   r   __ne__s   s    zRegex.__ne__)r   c                 C   s   d| j d| jdS )NzRegex(z, )r    r,   r   r   r   __repr__v   s    zRegex.__repr__c                 C   s   t | 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   r5   r   r   r   try_compiley   s    zRegex.try_compile)r   )__name__
__module____qualname____doc__	__slots__r   __getstate__r	   __setstate__Z_type_markerclassmethodr   r(   r   r   r)   r+   r-   r   boolr2   __hash__r3   r6   r8   r   r   r   r   r   -   s   r   )r<   r   typingr   r   r   r   r   r   Zbson._helpersr   r	   Zbson.sonr
   r)   r+   r   r*   r   r   r   r   r   r   <module>   s    