
    0yid                        d Z ddlmZ ddlmZ ddlmZmZmZ 	 ddl	Z	ddlmZ ddlmZ ddlmZ d	d
lmZ ddlmZ ddlmZmZmZ  G d de      Zy# e
$ r dZ	Y =w xY w)z
This module provides the :module:`hyperscan` backend for :class:`~pathspec.pathspec.PathSpec`.

WARNING: The *pathspec._backends.hyperscan* package is not part of the public
API. Its contents and structure are likely to change.
    )annotations)Sequence)AnyCallableOptionalN)_Backend)RegexPattern)override   )enumerate_patterns   )hyperscan_error)HS_FLAGSHyperscanExprDatHyperscanExprDebugc                      e Zd ZdZddd	 	 	 	 	 	 	 d	dZe	 	 	 	 	 	 	 	 	 	 d
d       Zedd       Zedd       Z		 	 	 	 	 	 	 	 	 	 	 	 ddZ
y)HyperscanPsBackendz
	The :class:`HyperscanPsBackend` class is the :module:`hyperscan`
	implementation used by :class:`~pathspec.pathspec.PathSpec` for matching
	files. The Hyperscan database uses block mode for matching files.
	N)_debug_exprs
_test_sortc               T   t         t        |r%t        |d   t              st	        d|d   d      t        |dd      }t        |      }|r&| j                         }| j                  ||||      }nd}g }|| _	        	 || _
        	 || _        	 d	| _        	 t        |      | _        y)
z
		Initialize the :class:`HyperscanPsBackend` instance.

		*patterns* (:class:`Sequence` of :class:`.RegexPattern`) contains the
		compiled patterns.
		Nr   zpatterns[0]=z must be a RegexPattern.TF)filterreverse)dbdebugpatternssort_idsN)	hyperscanr   
isinstancer	   	TypeErrorr   bool_make_db_init_db_dbr   
_expr_data_outdict	_patterns)selfr   r   r   use_patternsdebug_exprsr   	expr_datas           g/var/www/html/content_weaver/venv/lib/python3.12/site-packages/pathspec/_backends/hyperscan/pathspec.py__init__zHyperscanPsBackend.__init__.   s     	j!l;	lhqk%%=>	??#D%, \"+2}}	
	  9 	29+-$( "$
 -6$/
 +5$) -1,>$.    c           	        |sJ |       g }g }|D ]  \  }}|j                   	J ||f       t        |t              sJ |       |j                  j                  }t        |t
              r|}	n(t        |t              sJ |       |j                  d      }	|r)|j                  t        |j                   |d|             n'|j                  t        |j                   |d             |j                  |	        t        t        t        |                  }
| ||
       |
D cg c]  }||   	 }}| j                  ||
t        |      t               |S c c}w )a  
		Initialize the Hyperscan database from the given patterns.

		*db* (:class:`hyperscan.Hyperscan`) is the Hyperscan database.

		*debug* (:class:`bool`) is whether to include additional debugging
		information for the expressions.

		*patterns* (:class:`~collections.abc.Sequence` of :class:`.RegexPattern`)
		contains the patterns.

		*sort_ids* (:class:`callable` or :data:`None`) is a function used to sort
		the compiled expression ids. This is used during testing to ensure the order
		of expressions is not accidentally relied on.

		Returns a :class:`list` indexed by expression id (:class:`int`) to its data
		(:class:`HyperscanExprDat`).
		utf8F)includeindexis_dir_patternregex)r3   r4   r5   )expressionsidselementsflags)r3   r    r	   r6   patternbytesstrencodeappendr   r   listrangelencompiler   )r   r   r   r   r-   exprspattern_indexr;   r6   regex_bytesr8   _HyperscanPsBackend__ids               r.   r$   zHyperscanPsBackend._init_dbt   sh   6 
8 '))% ( mW
//
%?w'??
% Wl
+4W4
+==  5KeS!(5(!,,v&K'__	  %__  	<<7< 	U3u:#C=$'(DE$K(5( **
J		   
 )s   #Ec                    | j                   }| j                   yd| _        |j                  |j                  d      | j                         | j                  \  }}|dk(  rd}||fS )a  
		Check the file against the patterns.

		*file* (:class:`str`) is the normalized file path to check.

		Returns a :class:`tuple` containing whether to include *file* (:class:`bool`
		or :data:`None`), and the index of the last matched pattern (:class:`int` or
		:data:`None`).
		N)NNr   r2   )match_event_handlerr   )r%   r'   scanr>   _HyperscanPsBackend__on_match)r*   filer   out_include	out_indexs        r.   
match_filezHyperscanPsBackend.match_file   se     xx"	XX $)''$++f
4??'C99+y"_9
y	!!r0   c                 J    t        j                  t         j                        S )z[
		Create the Hyperscan database.

		Returns the database (:class:`hyperscan.Database`).
		)mode)r   DatabaseHS_MODE_BLOCK r0   r.   r#   zHyperscanPsBackend._make_db   s     
		!8!8	99r0   c                    | j                   |   }|j                  }| j                  d   }||kD  r|j                  |f| _        yy)zn
		Called on each match.

		*expr_id* (:class:`int`) is the expression id (index) of the matched
		pattern.
		r   N)r&   r4   r'   r3   )	r*   expr_id_from_to_flags_contextexpr_datr4   
prev_indexs	            r.   
__on_matchzHyperscanPsBackend.__on_match   sG    " __W%(
..%yy|*
Z  %(49 r0   )r   zSequence[RegexPattern]r   Optional[bool]r   z Optional[Callable[[list], None]]returnNone)
r   hyperscan.Databaser   r"   r   zlist[tuple[int, RegexPattern]]r   z%Optional[Callable[[list[int]], None]]r_   zlist[HyperscanExprDat])rL   r=   r_   z$tuple[Optional[bool], Optional[int]])r_   ra   )rV   intrW   rb   rX   rb   rY   rb   rZ   r   r_   r^   )__name__
__module____qualname____doc__r/   staticmethodr$   r
   rO   r#   rK   rT   r0   r.   r   r   '   s     "&15D"D 	D
 /D DL JJ	J +J 2	J
 J JX " "6 : :)) 
) 	)
 ) ) )r0   r   )rf   
__future__r   collections.abcr   typingr   r   r   r   ModuleNotFoundErrorpathspec.backendr   pathspec.patternr	   pathspec._typingr
   _utilsr   baser   _baser   r   r   r   rT   r0   r.   <module>rr      sm    # 
 T) T)+  s   A AA