Skip to content

Commit fb8e8e9

Browse files
committed
GH-49: Fix the progress logging
1 parent de94b84 commit fb8e8e9

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

src/thumbnails/generator.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from .constants import DEFAULT_SKIP
1313
from .pathtools import listdir
1414
from .pathtools import metadata_path
15+
from .progress import Progress
1516
from .thumbnail import ThumbnailExistsError
1617
from .thumbnail import ThumbnailFactory
1718
from .video import Video
@@ -46,6 +47,8 @@ def worker(video, fmt, base, skip, output):
4647
thumbnail.generate()
4748

4849
def generate(self):
50+
Progress.stop()
51+
4952
self.inputs = [file for file in self.inputs if re.match(r"^.*\.(?:(?!png|vtt|json).)+$", file)]
5053
self.inputs = dict(zip(map(lambda i: metadata_path(i, self.output, self.format), self.inputs), self.inputs))
5154

@@ -70,3 +73,5 @@ def generate(self):
7073
),
7174
videos,
7275
)
76+
77+
Progress.stop()

src/thumbnails/progress.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
1-
from rich.live import Live
2-
from rich.status import Status
1+
from rich.progress import Progress as RichProgress
2+
from rich.progress import SpinnerColumn
3+
from rich.progress import TextColumn
34

45

56
class Progress:
6-
def __init__(self, status, done="Process completed"):
7-
self.status = Status(status)
8-
self.live = Live(self.status)
7+
_instance = RichProgress(SpinnerColumn(), TextColumn("[white]{task.description}{task.fields[status]}"))
8+
9+
def __init__(self, description, done="Process completed"):
10+
self.task = self._instance.add_task(description, status=" ... [yellow]processing")
911
self.done = done
1012

11-
def update(self, status):
12-
self.status.update(status)
13-
self.live.update(self.status)
13+
def update(self, description, status=" ... [yellow]processing"):
14+
self._instance.update(self.task, description=description, status=status, refresh=True)
15+
16+
@classmethod
17+
def start(cls):
18+
cls._instance.start()
19+
20+
@classmethod
21+
def stop(cls):
22+
cls._instance.stop()
1423

1524
def __enter__(self):
16-
self.live.start()
1725
return self
1826

1927
def __exit__(self, *_):
20-
self.live.update("[white]%s ... [/white][green]done[/green]" % self.done)
21-
self.live.stop()
28+
self.update(self.done, status=" ... [green]done")

src/thumbnails/thumbnail.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def __init__(self, video, base, skip, output):
4747
self.metadata_path = self._get_metadata_path()
4848
self._perform_skip()
4949

50-
with Progress("Extracting frames ...", "Frames have been extracted"):
50+
with Progress("Extracting frames", "Frames have been extracted"):
5151
self.extract_frames()
5252

5353
def _get_metadata_path(self):
@@ -103,7 +103,7 @@ def prepare_frames(self):
103103
master = Image.new(mode="RGBA", size=next(thumbnails))
104104
master_path = os.path.join(self.thumbnail_dir, extract_name(self.filepath) + ".png")
105105

106-
with Progress("Processing frames ...", "Frames have been merged into one") as progress:
106+
with Progress("Processing frames", "Frames have been merged into one") as progress:
107107
for frame, *_, x, y in thumbnails:
108108
progress.update("Processing '%s'" % frame)
109109
with Image.open(frame) as image:
@@ -123,7 +123,7 @@ def format_time(secs):
123123
route = os.path.join(prefix, extract_name(self.filepath) + ".png")
124124
route = pathlib.Path(route).as_posix()
125125

126-
with Progress("Generating thumbnail metadata ...", "Thumbnail metadata has been generated") as progress:
126+
with Progress("Generating thumbnail metadata", "Thumbnail metadata has been generated") as progress:
127127
for _, start, end, x, y in self.thumbnails():
128128
progress.update("Generating metadata for '%s'" % route)
129129
thumbnail_data = "%s --> %s\n%s#xywh=%d,%d,%d,%d\n\n" % (

0 commit comments

Comments
 (0)