o
    €Ï"
  ã                   @   sh   d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ejZ	G dd„ de
ƒZdd	d
„Zdd„ ZdS )zHA utility for setting up lazy compiling of regex to improve performance.é    )Úabsolute_import)Údivision)Úunicode_literalsN)Úlazy_regex_patternsc                   @   s2   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ ZdS )Ú_Lazy_SRE_Patternz A class to lazily compile regex.r   c                 C   s.   t  | d|¡ t  | d|¡ t  | dd ¡ d S )NÚpatternÚflagsÚsre_pattern)ÚobjectÚ__setattr__)Úselfr   r   © r   ú@/tmp/google-cloud-sdk/lib/googlecloudsdk/core/util/lazy_regex.pyÚ__init__#   s   z_Lazy_SRE_Pattern.__init__c                 C   s    t | j| jƒ}t | d|¡ d S )Nr	   )Úreal_compiler   r   r
   r   )r   r	   r   r   r   Ú_compile)   s   z_Lazy_SRE_Pattern._compilec                 C   s   |   ¡  t| j|ƒS ©N)r   Úgetattrr	   )r   Únamer   r   r   Ú__getattr__-   s   z_Lazy_SRE_Pattern.__getattr__c                 C   s   |   ¡  t| j||ƒ d S r   )r   Úsetattrr	   )r   r   Úvaluer   r   r   r   1   s   z_Lazy_SRE_Pattern.__setattr__N©r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r       s    
r   c                 C   s   | t jv r
t| |ƒS t| |ƒS )ai  Return a Lazy or normal SRE_Pattern object depending on the args.

  Patterns in lazy_regex_patterns.PATTERNS are known to be valid, so they will
  be compiled lazily. Other patterns will be compiled immediately, as it is not
  known if they will compile or raise an re.error.

  For more information on the arguments, see:
  https://docs.python.org/3/howto/regex.html#compilation-flags

  Args:
    pattern: The pattern to be compiled.
    flags: Flags to be used during compilation.
  Returns:
    An SRE_Pattern or a _Lazy_SRE_Pattern.
  Raises:
    re.error: If the arguments do not form a valid regex pattern.
  )r   ÚPATTERNSr   r   )r   r   r   r   r   Ú_lazy_compile6   s   


r   c                   C   s
   t t_d S r   )r   ÚreÚcompiler   r   r   r   Úinitialize_lazy_compileM   s   
r!   r   )r   Ú
__future__r   r   r   r   Úgooglecloudsdk.core.utilr   r    r   r
   r   r   r!   r   r   r   r   Ú<module>   s   
