-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #680 from onekey-sec/refactor-progress-reporting
processing: extract progress reporting from business logic
- Loading branch information
Showing
4 changed files
with
115 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from typing import Protocol | ||
|
||
from rich import progress | ||
from rich.style import Style | ||
|
||
from .models import TaskResult | ||
|
||
|
||
class ProgressReporter(Protocol): | ||
def __enter__(self): | ||
... | ||
|
||
def __exit__(self, _exc_type, _exc_value, _tb): | ||
... | ||
|
||
def update(self, result: TaskResult): | ||
... | ||
|
||
|
||
class NullProgressReporter: | ||
def __enter__(self): | ||
pass | ||
|
||
def __exit__(self, _exc_type, _exc_value, _tb): | ||
pass | ||
|
||
def update(self, result: TaskResult): | ||
pass | ||
|
||
|
||
class RichConsoleProgressReporter: | ||
def __init__(self): | ||
self._progress = progress.Progress( | ||
progress.TextColumn( | ||
"Extraction progress: {task.percentage:>3.0f}%", | ||
style=Style(color="#00FFC8"), | ||
), | ||
progress.BarColumn( | ||
complete_style=Style(color="#00FFC8"), style=Style(color="#002060") | ||
), | ||
) | ||
self._overall_progress_task = self._progress.add_task("Extraction progress:") | ||
|
||
def __enter__(self): | ||
self._progress.start() | ||
|
||
def __exit__(self, _exc_type, _exc_value, _tb): | ||
self._progress.remove_task(self._overall_progress_task) | ||
self._progress.stop() | ||
|
||
def update(self, result: TaskResult): | ||
if (total := self._progress.tasks[0].total) is not None: | ||
self._progress.update( | ||
self._overall_progress_task, | ||
advance=1, | ||
total=total + len(result.subtasks), | ||
) |