Skip to content

Commit 5988086

Browse files
committed
exporter: manage progress in ExportProgressManager
1 parent fb57474 commit 5988086

2 files changed

Lines changed: 34 additions & 32 deletions

File tree

src/flareio_cli/exporters/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def _export_to_csv(
5555

5656
with (
5757
open(output_file, "a+", encoding="utf-8") as f_output,
58-
export_progress(object_name=object_name) as update_progress,
58+
export_progress(object_name=object_name) as progress_manager,
5959
):
6060
writer = PydanticCsvWriter(
6161
file=f_output,
@@ -71,7 +71,7 @@ def _export_to_csv(
7171

7272
cursor.save(page.next)
7373

74-
update_progress(
74+
progress_manager.update_progress(
7575
incr_completed=len(page.items),
7676
new_cursor=cursor.value(),
7777
)

src/flareio_cli/progress.py

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,46 @@
22

33
from rich.progress import Progress
44
from rich.progress import SpinnerColumn
5+
from rich.progress import TaskID
56
from rich.progress import TextColumn
67
from rich.progress import TimeElapsedColumn
78

89
import typing as t
910

1011

11-
class ExportProgressUpdate(t.Protocol):
12-
def __call__(
12+
class ExportProgressManager:
13+
def __init__(
14+
self,
15+
*,
16+
progress: Progress,
17+
task_id: TaskID,
18+
) -> None:
19+
self.progress: Progress = progress
20+
self.task_id: TaskID = task_id
21+
22+
self.current_completed: int = 0
23+
self.current_cursor: str | None = None
24+
25+
def update_progress(
1326
self,
1427
*,
1528
incr_completed: int,
1629
new_cursor: str | None = None,
17-
) -> None: ...
30+
) -> None:
31+
self.current_completed = self.current_completed + incr_completed
32+
33+
self.progress.update(
34+
self.task_id,
35+
completed=self.current_completed,
36+
cursor=new_cursor or self.current_cursor,
37+
)
1838

1939

2040
@contextlib.contextmanager
2141
def export_progress(
2242
*,
2343
object_name: str,
24-
) -> t.Iterator[ExportProgressUpdate]:
44+
) -> t.Iterator[ExportProgressManager]:
2545
"""
2646
Standard rich progress indicators so that all exports look the same.
2747
"""
@@ -32,30 +52,12 @@ def export_progress(
3252
TimeElapsedColumn(),
3353
TextColumn("[grey70]cursor={task.fields[cursor]}"),
3454
) as progress:
35-
current_completed: int = 0
36-
current_cursor: str | None = None
37-
38-
def _incr_progress(
39-
*,
40-
incr_completed: int,
41-
new_cursor: str | None = None,
42-
) -> None:
43-
nonlocal current_completed
44-
current_completed = current_completed + incr_completed
45-
46-
nonlocal current_cursor
47-
current_cursor = new_cursor or current_cursor
48-
49-
progress.update(
50-
progress_task,
51-
completed=current_completed,
52-
cursor=current_cursor,
53-
)
54-
55-
progress_task = progress.add_task(
56-
description=f"Exporting {object_name}...",
57-
total=None,
58-
cursor=None,
55+
progress_manager = ExportProgressManager(
56+
progress=progress,
57+
task_id=progress.add_task(
58+
description=f"Exporting {object_name}...",
59+
total=None,
60+
cursor=None,
61+
),
5962
)
60-
61-
yield _incr_progress
63+
yield progress_manager

0 commit comments

Comments
 (0)