22
33from rich .progress import Progress
44from rich .progress import SpinnerColumn
5+ from rich .progress import TaskID
56from rich .progress import TextColumn
67from rich .progress import TimeElapsedColumn
78
89import 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
2141def 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