Skip to content

Commit 4eef76b

Browse files
committed
Add more docs to search module
1 parent 0fc95b6 commit 4eef76b

4 files changed

Lines changed: 33 additions & 16 deletions

File tree

recoverpy/lib/search/progress_monitoring.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
def monitor_search_progress(grep_pid: int, progress: SearchProgress) -> None:
1010
while True:
11-
output: str = get_progress_output(grep_pid)
11+
output: str = _get_progress_output(grep_pid)
1212

1313
if not output:
1414
progress.progress_percent = 100.0
@@ -22,7 +22,7 @@ def monitor_search_progress(grep_pid: int, progress: SearchProgress) -> None:
2222
sleep(0.5)
2323

2424

25-
def get_progress_output(grep_pid: int) -> str:
25+
def _get_progress_output(grep_pid: int) -> str:
2626
return check_output(["progress", "-p", str(grep_pid)], stderr=DEVNULL).decode(
2727
"utf8"
2828
)

recoverpy/lib/search/search_engine.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
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+
19
from __future__ import annotations
210

311
from 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) :]

recoverpy/ui/screens/screen_search.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ async def _wait_for_grep_list_focus(self) -> None:
7070
async def _start_search_engine(self) -> None:
7171
await self.search_engine.start_search()
7272
ensure_future(
73-
self._grep_result_list.start_consumer(self.search_engine.list_items_queue)
73+
self._grep_result_list.start_consumer(
74+
self.search_engine.formatted_results_queue
75+
)
7476
)
7577
ensure_future(self._update_progress_labels())
7678

tests/unit/test_search_engine.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_search_progress_after_search(search_engine):
2929

3030

3131
def test_list_items_queue_size(search_engine):
32-
assert search_engine.list_items_queue.qsize() == GREP_RESULT_COUNT
32+
assert search_engine.formatted_results_queue.qsize() == GREP_RESULT_COUNT
3333

3434

3535
def test_list_items_queue_content(search_engine):
@@ -40,8 +40,8 @@ def test_list_items_queue_content(search_engine):
4040
]
4141

4242
results = []
43-
while not search_engine.list_items_queue.empty():
44-
results.append(search_engine.list_items_queue.get_nowait())
43+
while not search_engine.formatted_results_queue.empty():
44+
results.append(search_engine.formatted_results_queue.get_nowait())
4545

4646
for i, result in enumerate(results):
4747
assert expected_list_items[i][0] == result.inode

0 commit comments

Comments
 (0)