Skip to content

Commit

Permalink
Merge pull request #1694 from glensc/rich-progressbar
Browse files Browse the repository at this point in the history
Refactor: Use RichProgressBar class
  • Loading branch information
glensc committed Jan 7, 2024
2 parents bbbb762 + 12a036d commit 2bf99cd
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 15 deletions.
2 changes: 1 addition & 1 deletion plextraktsync/plan/Walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def find_episodes(self):
def walk_shows(self, shows: set[Media], title="Processing Shows"):
if not shows:
return
yield from self.progressbar(shows, desc=title)
yield from self.progressbar(shows, desc=title, total=len(shows))

def get_plex_episodes(self, episodes: list[Episode]) -> Generator[Media, Any, None]:
it = self.progressbar(episodes, desc="Processing episodes")
Expand Down
64 changes: 64 additions & 0 deletions plextraktsync/rich/RichProgressBar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from functools import cached_property


class RichProgressBar:
def __init__(self, iterable, total, options=None, desc=""):
self.iter = iter(iterable)
self.options = options or {}
self.desc = desc
self.total = total
self.i = 0

def __iter__(self):
return self

def __next__(self):
res = self.iter.__next__()
self.update()
return res

def __enter__(self):
self.progress.__enter__()
return self

def __exit__(self, *exc):
self.progress.__exit__(*exc)

def update(self):
self.i += 1
self.progress.update(self.task_id, completed=self.i)

@cached_property
def task_id(self):
return self.progress.add_task(self.desc, total=self.total)

@cached_property
def progress(self):
from tqdm.rich import FractionColumn, RateColumn

from rich.progress import (BarColumn, Progress, TimeElapsedColumn,
TimeRemainingColumn)

args = (
"[progress.description]{task.description}"
"[progress.percentage]{task.percentage:>4.0f}%",
BarColumn(bar_width=None),
FractionColumn(
unit_scale=False,
unit_divisor=1000,
),
"[",
TimeElapsedColumn(),
"<",
TimeRemainingColumn(),
",",
RateColumn(
unit="it",
unit_scale=False,
unit_divisor=1000,
),
"]"
)
progress = Progress(*args, **self.options)

return progress
16 changes: 2 additions & 14 deletions plextraktsync/util/Factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,11 @@ def progressbar(self):
if not self.run_config.progressbar:
return None

import warnings
from functools import partial

from tqdm import TqdmExperimentalWarning
from tqdm.rich import tqdm
from plextraktsync.rich.RichProgressBar import RichProgressBar

warnings.filterwarnings("ignore", category=TqdmExperimentalWarning)

# Monkeypatch https://github.com/tqdm/tqdm/pull/1395
class Tqdm(tqdm):
def close(self):
if self.disable:
return
self.display()
super().close()

return partial(Tqdm, options={'console': self.console})
return partial(RichProgressBar, options={'console': self.console})

@cached_property
def run_config(self):
Expand Down

0 comments on commit 2bf99cd

Please sign in to comment.