-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogress_bar.py
More file actions
70 lines (58 loc) · 2.39 KB
/
progress_bar.py
File metadata and controls
70 lines (58 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import logging
from typing import Optional
from rich.console import Console
from rich.progress import (
BarColumn,
Progress,
SpinnerColumn,
TaskID,
TextColumn,
TimeElapsedColumn,
TimeRemainingColumn,
)
class ProgressBar:
def __init__(
self, total_files: int, description: str = "Processing files..."
) -> None:
self.total_files = total_files
self.description = description
self.console = Console(stderr=True)
logger = logging.getLogger("codeanalyzer")
current_level = logger.getEffectiveLevel()
# Disable progress if logger level is higher than INFO (e.g., WARNING or ERROR)
self.disabled = current_level >= logging.ERROR
self._progress: Optional[Progress] = None
self._task_id: Optional[TaskID] = None
def __enter__(self):
if not self.disabled:
self._progress = Progress(
SpinnerColumn(spinner_name="dots"),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
TextColumn("[blue]{task.completed}/{task.total} files"),
TimeElapsedColumn(),
TimeRemainingColumn(),
transient=False,
console=self.console, # <-- Use stderr-safe console
)
self._progress.start()
self._task_id = self._progress.add_task(
description=self.description, total=self.total_files
)
return self
def update_description(self, message: str) -> None:
if not self.disabled and self._progress and self._task_id is not None:
self._progress.update(self._task_id, description=message)
def advance(self, n: int = 1) -> None:
if not self.disabled and self._progress and self._task_id is not None:
self._progress.advance(self._task_id, n)
def finish(self, message: Optional[str] = None) -> None:
if not self.disabled and self._progress and self._task_id is not None:
if not self._progress.finished:
self._progress.update(self._task_id, completed=self.total_files)
self._progress.stop()
if message:
logging.getLogger("codeanalyzer").info(message)
def __exit__(self, exc_type, exc_val, exc_tb):
self.finish()