1+ """
2+ SearchEngine initiates the search process upon request from the UI.
3+ A Grep process is started with the requested parameters along with subthreads acting as workers.
4+ The first thread consumes the standard output from Grep and simply enqueues each line into a first queue.
5+ A second thread consumes this queue, filters and formats the raw strings into objects ready to be integrated
6+ into the UI and finally stores these objects in an asynchronous queue to be consumed by the UI.
7+ """
8+
19from __future__ import annotations
210
311from asyncio import AbstractEventLoop
@@ -36,15 +44,15 @@ class SearchEngine:
3644
3745 def __init__ (self , partition : str , searched_string : str ):
3846 self ._initialize_search_components (partition , searched_string )
39- self .results_queue : Queue [bytes ] = Queue ()
40- self .list_items_queue : AsyncQueue [GrepResult ] = AsyncQueue ()
47+ self .raw_grep_results_queue : Queue [bytes ] = Queue ()
48+ self .formatted_results_queue : AsyncQueue [GrepResult ] = AsyncQueue ()
4149
4250 def _initialize_search_components (
4351 self , partition : str , searched_string : str
4452 ) -> None :
4553 self .search_params = SearchParams (partition , searched_string )
4654 self .search_progress = SearchProgress ()
47- self .result_processor = ResultFilter (self .search_params )
55+ self .result_filter = ResultFilter (self .search_params )
4856
4957 async def start_search (self ) -> None :
5058 self ._start_grep_process ()
@@ -61,28 +69,32 @@ def _start_grep_process(self) -> None:
6169
6270 def _start_auxiliary_threads (self ) -> None :
6371 start_grep_stdout_consumer_thread (
64- _consume_grep_stdout , self ._grep_process , self .results_queue
72+ _consume_grep_stdout , self ._grep_process , self .raw_grep_results_queue
6573 )
6674 start_result_formatter_thread (self ._format_results )
6775 start_progress_monitoring_thread (self ._grep_process , self .search_progress )
6876
6977 def _format_results (self ) -> None :
78+ """Initiate formatting and filtering of raw grep results.
79+ A new event loop is created to avoid blocking the main App loop."""
7080 loop = new_event_loop ()
7181 while True :
72- results = self .result_processor .filter_results (self .results_queue )
82+ results = self .result_filter .filter_results (self .raw_grep_results_queue )
7383 self ._process_new_results (results , loop )
7484 log .debug (f"search_engine - Dequeued { len (results )} results" )
7585 sleep (0.1 )
7686
7787 def _process_new_results (self , results : List [str ], loop : AbstractEventLoop ) -> None :
88+ """Consumes filtered grep results, convert them into GrepResult objects
89+ and enqueues them into the formatted results queue for UI."""
7890 for result in results :
7991 grep_result = self ._create_grep_result (
8092 result , self .search_progress .result_count
8193 )
8294
8395 if grep_result .inode not in self ._seen_inodes :
8496 self ._seen_inodes .add (grep_result .inode )
85- loop .run_until_complete (self .list_items_queue .put (grep_result ))
97+ loop .run_until_complete (self .formatted_results_queue .put (grep_result ))
8698 self .search_progress .result_count += 1
8799
88100 def _create_grep_result (self , result : str , result_index : int ) -> GrepResult :
@@ -94,17 +106,15 @@ def _create_grep_result(self, result: str, result_index: int) -> GrepResult:
94106 def _configure_grep_result (
95107 self , grep_result : GrepResult , result_index : int
96108 ) -> None :
109+ """Fix inode number and line start then sets the CSS class for the UI."""
97110 grep_result .inode = self ._fix_inode (grep_result .inode )
98111 grep_result .line = self ._fix_line_start (grep_result .line )
99112 grep_result .css_class = (
100113 "grep-result-odd" if result_index % 2 == 0 else "grep-result-even"
101114 )
102115
103- def _fix_line_start (self , line : str ) -> str :
104- result_index : int = line .find (self .search_params .searched_lines [0 ])
105- return line [min (result_index , 15 ) :]
106-
107116 def _fix_inode (self , inode : int ) -> int :
117+ """Shift inode to the first block containing the searched string."""
108118 inode //= self .search_params .block_size
109119
110120 for _ in range (10 ):
@@ -115,3 +125,8 @@ def _fix_inode(self, inode: int) -> int:
115125 return inode
116126 inode += 1
117127 return inode
128+
129+ def _fix_line_start (self , line : str ) -> str :
130+ """Remove unnecessary characters from the start of the line to improve readability."""
131+ result_index : int = line .find (self .search_params .searched_lines [0 ])
132+ return line [min (result_index , 15 ) :]
0 commit comments