
    [gK                         S SK Jr  S SKrS SKJrJrJrJr  S SKJ	r	  S SK
Jr  \R                  " \5      r " S S5      rg)    )defaultdictN)CallableDictListSet)BaseLunrException)Tokenc                       \ rS rSr% Sr0 r\\\4   \	S'   S r
S rS r\SS j5       r\S	 5       rS
 rS rS rS rS rS\S\\   4S jrSS jrSS jrS rS rSrg)Pipeline   zlunr.Pipelines maintain a list of functions to be applied to all tokens
in documents entering the search index and queries ran agains the index.

registered_functionsc                 :    / U l         [        [        5      U l        g N)_stackr   set_skipselfs    //usr/lib/python3/dist-packages/lunr/pipeline.py__init__Pipeline.__init__   s    &(/:3/?
    c                 ,    [        U R                  5      $ r   )lenr   r   s    r   __len__Pipeline.__len__   s    4;;r   c                 d    SR                  SR                  S U R                   5       5      5      $ )Nz<Pipeline stack="{}">,c              3   8   #    U  H  oR                   v   M     g 7fr   )label).0fns     r   	<genexpr>$Pipeline.__repr__.<locals>.<genexpr>   s     6V+Bxx+s   )formatjoinr   r   s    r   __repr__Pipeline.__repr__   s&    &--chh6V$++6V.VWWr   Nc                     U=(       d    UR                   nX R                  ;   a  [        R                  SU5        X!l        XR                  UR                  '   g)z&Register a function with the pipeline.z+Overwriting existing registered function %sN)__name__r   logwarningr    )clsr"   r    s      r   register_functionPipeline.register_function   sD     $,,,KKEuM-/  *r   c                     U " 5       nU H$  n U R                   U   nUR                  U5        M&     U$ ! [         a    [        SR	                  U5      5      ef = f)z'Loads a previously serialised pipeline.z$Cannot load unregistered function {})r   addKeyErrorr   r%   )r-   
serialisedpipelinefn_namer"   s        r   loadPipeline.load)   sk     5!G!--g6 R  "   ':AA'J s	   4%Ac                 n    U H/  nU R                  U5        U R                  R                  U5        M1     g)zAdds new functions to the end of the pipeline.

Functions must accept three arguments:
- Token: A lunr.Token object which will be updated
- i: The index of the token in the set
- tokens: A list of tokens representing the set
N)warn_if_function_not_registeredr   append)r   argsr"   s      r   r1   Pipeline.add9   s.     B004KKr" r   c           
           UR                   U R                  ;   $ ! [         a2    [        R	                  SR                  [        USU5      5      5         g f = f)NzbFunction "{}" is not registered with pipeline. This may cause problems when serialising the index.r    )r    r   AttributeErrorr+   r,   r%   getattrr   r"   s     r   r9   (Pipeline.warn_if_function_not_registeredE   sT    	88t8888 	KKFFLfB,G	s    9AAc                     U R                  U5         U R                  R                  U5      nU R                  R                  US-   U5        g! [         a  n[        S5      UeSnAff = f)zLAdds a single function after a function that already exists in the
pipeline.   Cannot find existing_fnNr9   r   indexinsert
ValueErrorr   r   existing_fnnew_fnrF   es        r   afterPipeline.afterP   sd     	,,V4	FKK%%k2EKKuqy&1 	F#$=>AE	Fs   :A 
A)A$$A)c                     U R                  U5         U R                  R                  U5      nU R                  R                  X25        g! [         a  n[        S5      UeSnAff = f)zOAdds a single function before a function that already exists in the
pipeline.

rD   NrE   rI   s        r   beforePipeline.beforeZ   s^    
 	,,V4	FKK%%k2EKKu- 	F#$=>AE	Fs   6A
 

A%A  A%c                 \     U R                   R                  U5        g! [         a     gf = f)z%Removes a function from the pipeline.N)r   removerH   r@   s     r   rS   Pipeline.removef   s+    	KKr" 		s    
++r"   field_namesc                 @    U R                   U   R                  U5        g)z
Make the pipeline skip the function based on field name we're processing.

This relies on passing the field name to Pipeline.run().
N)r   update)r   r"   rU   s      r   skipPipeline.skipm   s     	

2k*r   c                 4   U R                    H  nU(       a  X R                  U   ;   a  M  / n[        U5       HV  u  pVU" XeU5      nU(       d  M  [        U[        [
        45      (       a  UR                  U5        ME  UR                  U5        MX     UnM     U$ )a  
Runs the current list of functions that make up the pipeline against
the passed tokens.

:param tokens: The tokens to process.
:param field_name: The name of the field these tokens belongs to, can be ommited.
    Used to skip some functions based on field names.
)r   r   	enumerate
isinstancelisttupleextendr:   )r   tokens
field_namer"   resultsitokenresults           r   runPipeline.runu   s     ++BjJJrN:G%f- Ef-ftUm44NN6*NN6* . F! $ r   c                 z    [        X5      nU R                  U/5       Vs/ s H  n[        U5      PM     sn$ s  snf )a-  Convenience method for passing a string through a pipeline and
getting strings out. This method takes care of wrapping the passed
string in a token and mapping the resulting tokens back to strings.

.. note:: This ignores the skipped functions since we can't
    access field names from this context.
)r	   rf   str)r   stringmetadatard   tkns        r   
run_stringPipeline.run_string   s7     f'$(HHeW$56$5SC$5666s   8c                     / U l         g r   )r   r   s    r   resetPipeline.reset   s	    r   c                 X    U R                    Vs/ s H  oR                  PM     sn$ s  snf r   )r   r    r@   s     r   	serializePipeline.serialize   s     #';;/;R;///s   ')r   r   r   )r*   
__module____qualname____firstlineno____doc__r   r   ri   r   __annotations__r   r   r'   classmethodr.   r6   r1   r9   rM   rP   rS   r   rX   rf   rm   rp   rs   __static_attributes__ r   r   r   r      s    
 13$sH}-2@ X
 0 0  
#	F
F+x +d3i +:	70r   r   )collectionsr   loggingtypingr   r   r   r   lunr.exceptionsr   
lunr.tokenr	   	getLoggerr*   r+   r   r|   r   r   <module>r      s3    #  , , - !V0 V0r   