U
    ~fh                     @  s   d Z ddlm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 ddd	d
dZe	deeZG dd dee ZdS )z3Tools for representing MongoDB regular expressions.    )annotationsN)AnyGenericPatternTypeTypeVarUnion)_getstate_slots_setstate_slots)RE_TYPEstrint)	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-36gvocj8/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
dddddd	ZdddddddZdddddZdZdddddZddddZddddZdS ) RegexzBSON regular expression data.patternr      zType[Regex[Any]]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})

        :param 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_native8   s    
zRegex.from_nativer   r!   zUnion[str, int]None)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 )aR  BSON regular expression data.

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

        :param pattern: string
        :param flags: 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   )selfr$   r   r   r   r   __init__U   s    


zRegex.__init__r   bool)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.   r1   r   r   r   __eq__j   s    
zRegex.__eq__Nc                 C  s
   | |k S r2   r   r4   r   r   r   __ne__r   s    zRegex.__ne__r   )r   c                 C  s   d| j d| jdS )NzRegex(z, )r#   r.   r   r   r   __repr__u   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   r8   r   r   r   try_compilex   s    zRegex.try_compile)r   )__name__
__module____qualname____doc__	__slots__r	   __getstate__r
   __setstate__Z_type_markerclassmethodr+   r/   r5   __hash__r6   r9   r;   r   r   r   r   r"   .   s   r"   )r?   
__future__r   r   typingr   r   r   r   r   r   Zbson._helpersr	   r
   Zbson.sonr   r    r   r-   r!   r"   r   r   r   r   <module>   s    