A utility class to handle a context for both a reader and a progress.
| 283 | |
| 284 | |
| 285 | class _ReadContext(ContextManager[_I], Generic[_I]): |
| 286 | """A utility class to handle a context for both a reader and a progress.""" |
| 287 | |
| 288 | def __init__(self, progress: "Progress", reader: _I) -> None: |
| 289 | self.progress = progress |
| 290 | self.reader: _I = reader |
| 291 | |
| 292 | def __enter__(self) -> _I: |
| 293 | self.progress.start() |
| 294 | return self.reader.__enter__() |
| 295 | |
| 296 | def __exit__( |
| 297 | self, |
| 298 | exc_type: Optional[Type[BaseException]], |
| 299 | exc_val: Optional[BaseException], |
| 300 | exc_tb: Optional[TracebackType], |
| 301 | ) -> None: |
| 302 | self.progress.stop() |
| 303 | self.reader.__exit__(exc_type, exc_val, exc_tb) |
| 304 | |
| 305 | |
| 306 | def wrap_file( |