Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Use RichProgressBar class #1694

Merged
merged 6 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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