
    G@dz6                         d Z ddlZddlmZ ddlZddlZddlZddlm	Z	m
Z
mZmZ ddlmZ ddlmZ ddlmZ dZdZd	Z G d
 de          ZdS )zSearch thread.    N)QMutexQMutexLockerQThreadSignal)_)is_text_file)SpyderPalettez...P   (   c                        e Zd ZdZg dZg dZdgZ ee          Z	 ee
          Z ee
          Z ee          Z eee          Z ee          ZdZdZd fd	Zd	 Zd
 Zd Zd Zd Zd Zd Zd Z xZS )SearchThreadzFind in files search thread.)z.pyz.pywz.pyxz.ipyz.pyiz.pyt)z.ipynb.mdz.cz.cppz.hz.cxxz.fz.f03z.f90z.jsonz.datz.csvz.tsvz.txtr   z.rstz.ymlz.yamlz.iniz.batz.shz.uiz.svgr   	     c                    t                                          |           || _        || _        || _        t                      | _        d | _        d | _        d | _	        d | _
        d | _        d | _        d | _        d | _        d | _        d| _        d| _	        d| _        i | _        d| _        g | _        g | _        d| _        d S )NTr   F)super__init__search_text
text_colormax_resultsr   mutexstoppedpathlisttotal_matches
error_flagrootpathexcludetextstext_re	completedcase_sensitiveis_fileresults	num_filesfilespartial_resultstotal_items)selfparentr   r   r   	__class__s        Plib/python3.11/site-packages/spyder/plugins/findinfiles/widgets/search_thread.pyr   zSearchThread.__init__9   s       &$&XX
!
"
!    c                     || _         |rt          j        |          | _        || _        || _        || _        d| _        d| _        || _	        d S )NF)
r   recompiler   r   r   r"   r   r    r!   )r(   pathr"   r   r   r   r!   s          r+   
initializezSearchThread.initializeS   sS     	/:g..DL
,r,   c                 d   	 g | _         | j        r|                     | j                   n|                     | j                   n7# t
          $ r* t          j                     t          d          | _	        Y nw xY w| 
                                 | j                            | j                   d S )Nz&Unexpected error: see internal console)	filenamesr"   find_string_in_filer   find_files_in_path	Exception	traceback	print_excr   r   stopsig_finishedemitr    r(   s    r+   runzSearchThread.run_   s    	JDN| 7((7777''666 	J 	J 	J !!! HIIDOOO	J 			t~.....s   AA 1A:9A:c                 n    t          | j                  5  d| _        d d d            d S # 1 swxY w Y   d S )NT)r   r   r   r<   s    r+   r9   zSearchThread.stopo   s}    $*%% 	  	 DL	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 s   *..c                 .   | j         g | _         | j                             |           t          j        |          D ]\  }}}t	          | j                  5  | j        r	 d d d             dS 	 d d d            n# 1 swxY w Y   	 |d d          D ]}t	          | j                  5  | j        r	 d d d              dS 	 d d d            n# 1 swxY w Y   t          j                            ||          }t          j	        |          j
        }t          j        |          s|                    |           | j        r=t          j        | j        |t          j        z             r|                    |           |                    d          r|                    |           |D ]}t	          | j                  5  | j        r	 d d d              dS 	 d d d            n# 1 swxY w Y   t          j                            ||          }t%          j        |          d         }	t          j	        |          j
        }
t          j        |
          s| j        rt          j        | j        |          r|	| j        v r|	| j        v s|	| j        v st1          |          r|                     |           # t          j        $ r t7          d          | _        Y  dS t:          $ r Y  dS w xY w| j        r|                                  dS )NF.   zinvalid regular expressionT) r   appendoswalkr   r   r   r0   joinstatst_modeS_ISDIRremover   r.   searchsep
startswithospsplitextS_ISREGSKIPPED_EXTENSIONSPYTHON_EXTENSIONSUSEFUL_EXTENSIONSr   r4   errorr   r   FileNotFoundErrorr&   process_results)r(   r0   dirsr%   ddirnamest_dir_modeffilenameextst_file_modes              r+   r5   zSearchThread.find_files_in_paths   s\   = DMT"""!# ;	 ;	D$dj)) ! !< ! ! ! ! ! ! ! ! ! !!! ! ! ! ! ! ! ! ! ! ! ! ! ! !7aaa ' 'A%dj11 ) )< )#() ) ) ) ) ) ) ) ) ))) ) ) ) ) ) ) ) ) ) ) ) ) ) ) !gll433G #%''"2"2":K<44 'A 'IdlGbf4DEE' Ac** 'A  ; ;A%dj11 ) )< )#() ) ) ) ) ) ) ) ) ))) ) ) ) ) ) ) ) ) ) ) ) ) ) )  "w||D!44H,x003C $&78#4#4#<L<55 !  | !	$,(I(I !  d555  t5554#999(22 :00:::7;8 8   "#$@"A"Auuu$   uuu  	#  """ts   	A<<B 	B 	!K)	C2KKCKCC.K	G)KK)G--K0G-1CK#K7)	K76K7c           	      Z	   d| _         | j                            |           	 t          t	          |d                    D ],\  }}| j        D ]\  }}t          | j                  5  | j        r	 d d d              dS 	 d d d            n# 1 swxY w Y   |}| j	        s|
                                }| j        rt          j        ||          }| n|                    |          }|dk    r n	 |                    |          }n# t           $ r |}Y nw xY w| j	        s|
                                }| j        rt          j        ||          D ]t}	t          | j                  5  | j        r	 d d d              dS 	 d d d            n# 1 swxY w Y   | xj        dz  c_        |	                                |	                                }}
	 t+          |d |
                             |                    }|t+          ||
|                             |                    z   }n# t           $ r |
}|}Y nw xY w| j                            t1          j        |          |dz   |||f           t+          | j                  d| j        z  k    r4|                                  | j        | j        k     r| xj        dz  c_        v|                    |          }|dk    rt          | j                  5  | j        r	 d d d             dS 	 d d d            n# 1 swxY w Y   | xj        dz  c_        	 t+          |d |                             |                    }|t+          |                    |                    z   }n$# t           $ r |}|t+          |          z   }Y nw xY w| j                            t1          j        |          |dz   |||f           t+          | j                  d| j        z  k    r4|                                  | j        | j        k     r| xj        dz  c_        | j        D ]&\  }}|                    ||dz             }|dk    r n'|dk    .n5# t:          $ r(}|j        \  }}t?          d          | _         Y d }~nd }~ww xY wd| _         d S )NFrbrA      z)permission denied errors were encounteredT)!r   sig_current_filer;   	enumerateopenr   r   r   r   r!   lowerr   r.   rJ   finddecodeUnicodeDecodeErrorfinditerr   startendlenr&   rB   rM   abspathpowerrU   	max_powerIOErrorargsr   r    )r(   fnamelinenolinetextencline_searchfoundline_decmatchbstartbendrj   rk   xxx_todo_changeme_errno	_strerrors                    r+   r4   z SearchThread.find_string_in_file   sq   ""5)))P	M )$ud*;*; < < K& K&!% " "ID#%dj11 ) )< )#() ) ) ) ) ) ) ) ) ))) ) ) ) ) ) ) ) ) ) ) ) ) ) ) #'K. :&1&7&7&9&9| " "	$ < < ,!E - !, 0 0 6 6 2::!E &$#{{3//HH) $ $ $#HHH$ * (::<<D< 3&!#T4!8!8 0 0)$*55 - -#| -',- - - - - - - - - --- - - - - - - - - - - - - - - **a/**',{{}}eiikk'$'WfW(<(<S(A(A$B$BE"'#d6$;.?.F.Fs.K.K*L*L"LCC1 ' ' '$*E"&CCC' ,33S[5G5G5;aZ5:585=	5? @ @ @
 t3444:FF 00222#zDN:: $

a

+0. !IIdOOE"**)$*55 - -#| -',- - - - - - - - --- - - - - - - - - - - - - - - **a/**4$'VeV(;(;C(@(@$A$AE"'#dkk#.>.>*?*?"?CC1 4 4 4$)E"'#d))"3CCC4 ,33S[5G5G5;aZ5:585=	5? @ @ @
 t3444:FF 00222#zDN:: $

a

)- & &ID#$(IIdEAI$>$>E$rzz %  *3  "**cK&Z  	M 	M 	M"3"8VY KLLDOOOOOO	M s  AQ/ &	B/Q/ ?Q/ BQ/ BAQ/ 3D	Q/ 	DQ/ DAQ/ *	F3Q/ Q/ FQ/ F;Q/ AH+*Q/ +H<9Q/ ;H<<B?Q/ ;	LQ/ Q/ L#	#Q/ &L#	'Q/ ;AN
Q/ N,)Q/ +N,,CQ/ /
R!9RR!c                 |   g }| j         }| j        D ]}| j        | j        k     r|\  }}}}}|| j        vrD| j                            |           | j                            |           | xj        dz  c_        | 	                    |||          }|||||f}	|                    |	           | xj        dz  c_        d| j
        z  }
| j        }|dk    rt          d          }n2t          d          }t          d          }|dk    r|dz  }d||||fz  }|
|z   }
g | _        | j                            ||
           d	S )
aT  
        Process all matches found inside a file.

        Creates the necessary files and emits signal for the creation of file
        item.

        Creates the necessary data for lines found and emits signal for the
        creation of line items in batch.

        Creates the title based on the last entry of the lines batch.
        rA   z'%s' - r   zString not foundz
matches infilesz%d %s %d %sN)r   r&   r'   r   r%   rB   sig_file_matchr;   r$   truncate_resultr   r   sig_line_match)r(   itemsnum_matchesresultr[   rs   colno	match_endrt   itemtitlenb_filesru   text_matches
text_filess                  r+   rU   zSearchThread.process_results  s|    (* 	& 	&F$"222;A8&%D4:--J%%h///',,X666NNa'NN++D%CC &%yAT"""  A%   D,,>q=='((DD\??L6J!||c!
 K$,j$: :D!  .....r,   c                    ddddddfd}t          |          }|d|         |||         ||d         }}}t          |          t          k    rt          |          t          |          z
  d	z  }|                    d
          }t          |          }	|	dk    r6|d         }t          |          t          k    rt
          || d         z   }|g}|                    d
          }t          |          }
|
dk    r5|d         }t          |          t          k    r|d|         t
          z   }|g}|dd         }|dd         }t          |          |	k     rt
          g|z   }t          |          |
k     r|t
          gz   }d
                    |          }d
                    |          }t          |          t          k    rt
          |dd         z   }t          |          t          k    r|dd         t
          z   }t          j        }t          d                    |||g          d| j
         d ||           d| d ||           d ||           d          }|S )zU
        Shorten text on line to display the match within `max_line_length`.
        z&amp;z&quot;z&apos;z&gt;z&lt;)&"'><c                 F    d                     fd| D                       S )zProduce entities within text. c              3   D   K   | ]}                     ||          V  d S N)get).0chtml_escape_tables     r+   	<genexpr>zDSearchThread.truncate_result.<locals>.html_escape.<locals>.<genexpr>L  s4      EE1,00A66EEEEEEr,   )rE   )ru   r   s    r+   html_escapez1SearchThread.truncate_result.<locals>.html_escapeJ  s*    77EEEEEEEEEEr,   Nra    rA   r      i   r   z<span style="color:z">z<span style="background-color:z</span>)ru   formatted_text)strrl   MAX_RESULT_LENGTHsplitMAX_NUM_CHAR_FRAGMENTELLIPSISrE   r	   COLOR_OCCURRENCE_4dictr   )r(   rt   rj   rk   r   leftrz   rightoffsetnum_left_wordsnum_right_wordsmatch_color
trunc_liner   s                @r+   r   zSearchThread.truncate_result>  s   
 
 
	F 	F 	F 	F 	F 4yy!&5&\4c	?DJUet99((($ii#e**,2F::c??D YYN""Awt99444#dF788n4DvKK$$E!%jjO!##au:: 555!'6'NX5E9D"1"IE4yy>)) zD(5zzO++
*88D>>DHHUOOE4yy000$stt*,5zz111crc
X-#6$u-..do  ;t$$ 1<  ;u%% 
 ;u%%  
 
 

 r,   c                 6    | j         | j        | j        | j        fS r   )r#   r   r   r   r<   s    r+   get_resultszSearchThread.get_results  s    |T]D,>OOr,   )r   )__name__
__module____qualname____doc__rQ   rR   rP   r   boolr:   r   rb   sig_current_folderobjectr   r   sig_out_printrn   ro   r   r1   r=   r9   r5   r4   rU   r   r   __classcell__)r*   s   @r+   r   r   "   sW       &&GGG   !6$<<Lvc{{VF^^NVFF++NF6NNM EI     4
- 
- 
-/ / /      E E ENU U Un+/ +/ +/ZH H HTP P P P P P Pr,   r   )r   rC   os.pathr0   rM   r.   rF   r7   qtpy.QtCorer   r   r   r   spyder.api.translationsr   spyder.utils.encodingr   spyder.utils.paletter	   r   r   r   r    r,   r+   <module>r      s     
			       				      > = = = = = = = = = = = & % % % % % . . . . . . . . . . . .
   
gP gP gP gP gP7 gP gP gP gP gPr,   