Add default Ontime export and transient terminal progress

This commit is contained in:
Dandaedre 2026-10-20 10:35:24 +02:00
parent e9c94d94d4
commit aab80df1a8
11 changed files with 387 additions and 55 deletions

View File

@ -8,8 +8,11 @@ with quick-start examples and links to detailed documentation in `docs/`.
- `tools/pptx_videos.py` discovers and extracts embedded PPTX videos, maps them to
slides, probes metadata with FFmpeg's `ffprobe`, and generates timing reports
in Markdown, plain text, JSON, HTML, Typst, and PDF. PDF reports compile through
the Typst CLI.
in Markdown, plain text, JSON, HTML, Typst, PDF, and Ontime 4 JSON. Markdown
and Ontime are the defaults. PDF reports compile through the Typst CLI.
- `tools/ontime.py` maps video references into an importable Ontime rundown.
- `tools/progress.py` provides shared transient terminal progress and plain logs
for redirected output; `tests/test_progress.py` checks display cleanup.
- `tools/exporter.py` uses DeckCraft to export PPTX presentations to PDF or PNG slides,
and PdfCraft to render PDF pages as PNGs. It discovers CLI executables or
locally installed AppImages. PDF-to-PDF is a successful no-op.

View File

@ -4,7 +4,7 @@ Small Python command-line tools for local document and media workflows.
Requires Python 3.9+.
```sh
# Extract embedded videos and report their timings (requires ffprobe).
# Extract videos with Markdown + Ontime reports (requires ffprobe).
python3 tools/pptx_videos.py presentation.pptx
# Export slides to PDF and PNG images (requires DeckCraft).

View File

@ -25,7 +25,8 @@ python3 -m unittest discover -s tests -v
Tests cover export validation, overwrite handling, failed conversions, PPTX
discovery and slide order, temporary video cleanup, report formats, and PDF
compiler invocation. External CLI calls are mocked: CI does not require FFmpeg,
compiler invocation, Ontime event mapping, and terminal progress cleanup.
External CLI calls are mocked: CI does not require FFmpeg,
Typst, DeckCraft, or PdfCraft and does not claim to verify rendering fidelity.
## Jenkins

View File

@ -17,3 +17,9 @@ inside `images/`. Images default to 144 DPI. Existing output requires
`--overwrite`, which replaces generated files and removes stale numbered
slide/page PNGs. PDF-to-PDF is a successful no-op; other input extensions are
errors. Rendering completes in temporary storage before output is copied.
Interactive terminals show an updating progress line, with page counts for PDF
rendering and a spinner during DeckCraft's bulk export. Only output paths, the
final summary, and warnings/errors remain afterward. Redirected output uses
plain line logs. Set `NO_COLOR` to disable colors or `TERM=dumb` to disable
animation as well.

View File

@ -1,6 +1,6 @@
# PPTX video reports
Extract videos and create a Markdown timing report:
Extract videos and create Markdown and Ontime timing reports:
```sh
python3 tools/pptx_videos.py presentation.pptx
@ -10,11 +10,12 @@ Generate one or more report formats without keeping newly extracted videos:
```sh
python3 tools/pptx_videos.py presentation.pptx --timings-only --report html,pdf
python3 tools/pptx_videos.py presentation.pptx --report html,typst,pdf,md,txt,json
python3 tools/pptx_videos.py presentation.pptx --report html,typst,pdf,md,txt,json,ontime
```
`--report` accepts comma-separated formats and can be repeated. Markdown is the
default. Reports are saved as `timings.*` in `<presentation-stem>_videos`, or the
`--report` accepts comma-separated formats and can be repeated. Markdown and
Ontime are included by default; an explicit `--report` replaces this selection.
Reports are saved as `timings.*` in `<presentation-stem>_videos`, or the
directory supplied with `-o`. PDF uses the Typst CLI and retains `timings.typ`
for editing. HTML and Typst/PDF include colored summary cards and pills;
Markdown and TXT use aligned tables. JSON includes numeric durations in seconds,
@ -22,5 +23,41 @@ sizes in bytes, and slide references.
Timings require `ffprobe` (from FFmpeg); PDF additionally requires `typst` on
`PATH`. Use `--extract-only` to extract without a report. Presentations with no
embedded videos produce no output. Terminal progress uses colors automatically;
set `NO_COLOR` to disable them.
embedded videos produce no output.
## Ontime import
```sh
python3 tools/pptx_videos.py presentation.pptx --timings-only --report ontime
```
This writes `timings.ontime.json`, a native **Ontime 4** project. It is separate
from `timings.json`, which remains the general metadata report.
Import it through **Editor → Project → Manage projects**. To bring the rundown
into an existing project, import the file and use **Partial Load**.
See [Ontime's import instructions](https://docs.getontime.no/quick-tips/import-export/).
- Each video/slide reference becomes an event, sorted by slide. A video reused
on several slides gets an event for each slide; other timing reports still
count each media file once.
- Event cues contain the slide number, titles contain filenames, and notes
identify the presentation and archive media path.
- Durations retain milliseconds. Starts are linked, beginning at midnight;
change the first playable event's start to your show time after import.
- Unknown/invalid durations use zero and are marked skipped with a review note.
Unreferenced videos appear last and are also skipped. Durations exceeding
Ontime's 23:59:59 limit require manual review.
- No automatic playback or external-media downloads are configured. Slides
without embedded videos are not included. Add speaking time and apply any
PowerPoint trims or loops in Ontime.
The export was checked against Ontime 4's native project-section parsers;
Ontime 3 is not supported by this export schema.
## Progress output
Interactive terminals show a single animated progress line with completed video
count, elapsed time, and the current extraction/probing/report step. It clears
when finished, leaving report paths, the final summary, and warnings/errors.
Long operations keep animating. Redirecting either output stream uses ordinary
line logs instead. `NO_COLOR` disables colors; `TERM=dumb` also disables animation.

View File

@ -95,6 +95,56 @@ class VideoTests(unittest.TestCase):
self.assertEqual([p.name for p in output.iterdir()], ['clip.mp4'])
self.assertEqual((output / 'clip.mp4').read_bytes(), b'video fixture')
def test_default_reports_include_ontime_and_explicit_choice_overrides(self):
source = self.deck()
with patch.object(videos.shutil, 'which', return_value='/ffprobe'), \
patch.object(videos, 'probe', return_value=(1.25, '32×24', 'mpeg4')):
self.assertEqual(self.invoke(source, '--timings-only'), 0)
output = self.root / 'sample_videos'
self.assertEqual({p.name for p in output.iterdir()}, {'timings.md', 'timings.ontime.json'})
other = self.deck(name='explicit.pptx')
self.assertEqual(self.invoke(other, '--timings-only', '--report', 'json,ontime'), 0)
output = self.root / 'explicit_videos'
self.assertEqual({p.name for p in output.iterdir()}, {'timings.json', 'timings.ontime.json'})
other = self.deck(name='markdown.pptx')
self.assertEqual(self.invoke(other, '--timings-only', '--report', 'md'), 0)
self.assertEqual({p.name for p in (self.root / 'markdown_videos').iterdir()}, {'timings.md'})
def test_ontime_expands_shared_videos_in_slide_order(self):
project = json.loads(videos.render_report(self.report(), 'ontime'))
self.assertEqual(project['settings']['version'], '4.0.0')
rundown = project['rundowns']['default']
events = [rundown['entries'][event_id] for event_id in rundown['order']]
self.assertEqual([event['cue'] for event in events], ['Slide 2', 'Slide 10', 'Slide 12', 'Unreferenced'])
self.assertEqual([event['duration'] for event in events], [1000, 2500, 1000, 0])
self.assertEqual([event['timeStart'] for event in events], [0, 1000, 3500, 4500])
self.assertEqual([event['linkStart'] for event in events], [False, True, True, True])
self.assertEqual([event['skip'] for event in events], [False, False, False, True])
self.assertTrue(all(event['endAction'] == 'none' for event in events))
self.assertEqual(len(set(rundown['order'])), 4)
self.assertEqual(project, json.loads(videos.render_report(self.report(), 'ontime')))
def test_ontime_skips_unknown_duration_and_preserves_milliseconds(self):
data = self.report()
data['videos'][0]['duration_seconds'] = None
data['videos'][1]['duration_seconds'] = 2.345
events = list(json.loads(videos.render_report(data, 'ontime'))['rundowns']['default']['entries'].values())
self.assertTrue(events[0]['skip'])
self.assertIn('Duration unknown', events[0]['note'])
self.assertEqual(events[1]['duration'], 2345)
self.assertEqual(events[1]['timeStart'], 0)
self.assertFalse(events[1]['linkStart'])
def test_ontime_rejects_invalid_durations_without_invalid_json(self):
for seconds in (float('nan'), float('inf'), -1, 86400):
with self.subTest(seconds=seconds):
data = self.report()
data['videos'][0]['duration_seconds'] = seconds
project = json.loads(videos.render_report(data, 'ontime'))
event = next(iter(project['rundowns']['default']['entries'].values()))
self.assertEqual(event['duration'], 0)
self.assertTrue(event['skip'])
def report(self):
rows = [
('a', 'clip.mp4', 1024, 2.5, '1920×1080', 'h264'),

74
tests/test_progress.py Normal file
View File

@ -0,0 +1,74 @@
import contextlib
import io
import os
import unittest
from unittest.mock import patch
from tools import progress
class Terminal(io.StringIO):
def isatty(self):
return True
class ProgressTests(unittest.TestCase):
def test_terminal_steps_are_transient_and_summary_is_retained(self):
output, terminal = Terminal(), Terminal()
with patch.dict(os.environ, {'TERM': 'xterm', 'NO_COLOR': '1'}), \
contextlib.redirect_stdout(output), contextlib.redirect_stderr(terminal):
with progress.Progress() as bar:
bar.set(0, 2)
progress.status('Extracting')
bar.set(1)
progress.status('Probing')
bar.set(2)
progress.status('Done', 'success', persist=True)
self.assertFalse(bar.thread.is_alive())
self.assertIn('Extracting', terminal.getvalue())
self.assertIn('Probing', terminal.getvalue())
self.assertIn('2/2', terminal.getvalue())
self.assertNotIn('Probing\n', terminal.getvalue())
self.assertEqual(output.getvalue(), '[OK] Done\n')
self.assertTrue(terminal.getvalue().endswith('\r\033[2K'))
self.assertNotIn('\033[36m', terminal.getvalue())
self.assertIsNone(progress._active)
def test_redirected_output_logs_each_step_without_escapes(self):
output = io.StringIO()
with contextlib.redirect_stdout(output), contextlib.redirect_stderr(io.StringIO()):
with progress.Progress() as bar:
progress.status('Extracting')
progress.status('Probing')
progress.status('Done', 'success', persist=True)
self.assertIsNone(bar.thread)
self.assertEqual(output.getvalue(), '[INFO] Extracting\n[INFO] Probing\n[OK] Done\n')
self.assertNotIn('\033', output.getvalue())
def test_warnings_remain_and_exception_cleans_up(self):
output, terminal = Terminal(), Terminal()
with patch.dict(os.environ, {'TERM': 'xterm', 'NO_COLOR': '1'}), \
contextlib.redirect_stdout(output), contextlib.redirect_stderr(terminal):
with self.assertRaises(RuntimeError):
with progress.Progress() as bar:
progress.status('Probing')
progress.status('Cannot probe clip', 'warning')
raise RuntimeError('failed')
self.assertIn('[WARN] Cannot probe clip\n', terminal.getvalue())
self.assertTrue(terminal.getvalue().endswith('\r\033[2K'))
self.assertFalse(bar.thread.is_alive())
self.assertIsNone(progress._active)
def test_dumb_terminal_does_not_animate(self):
output, terminal = Terminal(), Terminal()
with patch.dict(os.environ, {'TERM': 'dumb'}), \
contextlib.redirect_stdout(output), contextlib.redirect_stderr(terminal):
with progress.Progress() as bar:
progress.status('Step')
self.assertIsNone(bar.thread)
self.assertEqual(output.getvalue(), '[INFO] Step\n')
self.assertEqual(terminal.getvalue(), '')
if __name__ == '__main__':
unittest.main()

View File

@ -10,17 +10,10 @@ import subprocess
import sys
import tempfile
def status(message, level='info'):
stream = sys.stderr if level in ('warning', 'error') else sys.stdout
label, color = {
'info': ('INFO', '36'), 'success': ('OK', '32'),
'warning': ('WARN', '33'), 'error': ('ERROR', '31'),
}[level]
prefix = f'[{label}]'
if stream.isatty() and 'NO_COLOR' not in os.environ and os.environ.get('TERM') != 'dumb':
prefix = f'\033[{color}m{prefix}\033[0m'
print(f'{prefix} {message}', file=stream, flush=True)
if __package__:
from .progress import Progress, status
else:
from progress import Progress, status
def output_formats(value):
@ -85,10 +78,10 @@ def find_cli(app, override, work):
return path
def export(source, formats, destination, dpi, overwrite, cli_override):
def export(source, formats, destination, dpi, overwrite, cli_override, progress=None):
is_deck = source.suffix.lower() == '.pptx'
if not is_deck and 'pdf' in formats:
status(f'Already PDF; nothing to convert: {source}', 'success')
status(f'Already PDF; nothing to convert: {source}', 'success', persist=True)
formats = [format for format in formats if format != 'pdf']
if not formats:
return
@ -125,10 +118,14 @@ def export(source, formats, destination, dpi, overwrite, cli_override):
pages = info.get('pages')
if type(pages) is not int or pages < 1:
raise RuntimeError('PdfCraft did not report a positive page count.')
if progress:
progress.set(0, pages)
for page in range(1, pages + 1):
status(f'[{page}/{pages}] Rendering page at {dpi} DPI...')
run([cli, 'render', source, '--page', str(page), '--dpi', str(dpi),
'--out', rendered / f'page-{page:04d}.png'])
if progress:
progress.set(page)
images = sorted(rendered.glob('*.png'))
if not images or any(image.stat().st_size == 0 for image in images):
raise RuntimeError(f'{app} did not produce valid image output.')
@ -139,7 +136,7 @@ def export(source, formats, destination, dpi, overwrite, cli_override):
destination.mkdir(parents=True, exist_ok=True)
if 'pdf' in formats:
shutil.copy2(work / 'export.pdf', pdf_target)
status(f'PDF: {pdf_target}', 'success')
status(f'PDF: {pdf_target}', 'success', persist=True)
if 'images' in formats:
images_target.mkdir(exist_ok=True)
if overwrite:
@ -151,7 +148,7 @@ def export(source, formats, destination, dpi, overwrite, cli_override):
if target.is_symlink():
raise RuntimeError(f'Refusing to write through an output symlink: {target}')
shutil.copy2(image, target)
status(f'Images: {images_target}', 'success')
status(f'Images: {images_target}', 'success', persist=True)
def main(argv=None):
@ -173,10 +170,12 @@ def main(argv=None):
parser.error(f'Not a file: {source}')
destination = (args.output_dir.expanduser() if args.output_dir else source.with_name(source.stem + '_export')).resolve()
try:
status(f'Source: {source}')
export(source, args.output, destination, args.dpi, args.overwrite,
args.deckcraft_cli if source.suffix.lower() == '.pptx' else args.pdfcraft_cli)
status('Done.', 'success')
with Progress() as progress:
status(f'Source: {source}')
export(source, args.output, destination, args.dpi, args.overwrite,
args.deckcraft_cli if source.suffix.lower() == '.pptx' else args.pdfcraft_cli,
progress=progress)
status('Done.', 'success', persist=True)
return 0
except (OSError, RuntimeError, ValueError) as error:
status(str(error), 'error')

61
tools/ontime.py Normal file
View File

@ -0,0 +1,61 @@
"""Create an Ontime 4 project from a video timing report."""
import math
from uuid import NAMESPACE_URL, uuid5
def ontime_project(data):
"""Expand shared videos into slide cues; retain uncertain cues as skipped."""
occurrences = [
(slide, video)
for video in data['videos']
for slide in (video['slides'] or [None])
]
occurrences.sort(key=lambda item: (item[0] if item[0] is not None else math.inf,
item[1]['name'], item[1]['archive_path']))
entries = {}
elapsed = 0
has_previous = False
for slide, video in occurrences:
seconds = video['duration_seconds']
valid = seconds is not None and math.isfinite(seconds) and 0 <= seconds <= 86399
duration = round(seconds * 1000) if valid else 0
skipped = not valid or slide is None
cue = f'Slide {slide}' if slide is not None else 'Unreferenced'
note = f'{data["presentation"]} — {video["archive_path"]}. Original media duration; trimming and looping are not applied.'
if not valid:
note += ' Duration unknown or outside Ontime limits; review before enabling.'
if slide is None:
note += ' No slide reference; review before enabling.'
event_id = uuid5(NAMESPACE_URL, f'{data["presentation"]}/{video["archive_path"]}#{slide}').hex
entries[event_id] = {
'id': event_id, 'type': 'event', 'cue': cue, 'title': video['name'],
'note': note, 'duration': duration, 'timeStart': elapsed % 86400000,
'timeEnd': (elapsed + duration) % 86400000,
'timeStrategy': 'lock-duration', 'linkStart': has_previous,
'timerType': 'count-down', 'endAction': 'none',
'skip': skipped, 'colour': '#b7791f' if skipped else '#5744b5',
'timeWarning': min(120000, duration // 5),
'timeDanger': min(60000, duration // 10),
'countToEnd': False, 'flag': False, 'custom': {}, 'triggers': [],
}
if not skipped:
elapsed += duration
has_previous = True
description = (
'Video cues in slide order. Shared videos appear once per referenced slide. '
'Times start at midnight and are linked; set the first cue to your show start. '
'Unknown durations and unreferenced media are skipped pending review.'
)
if data['external_media_slides']:
description += ' External media was not exported (slides: ' + ', '.join(map(str, data['external_media_slides'])) + ').'
return {
'settings': {'version': '4.0.0'},
'project': {'title': data['presentation'], 'description': description},
'rundowns': {'default': {
'id': 'default', 'title': 'Video timings', 'entries': entries,
'order': list(entries), 'flatOrder': list(entries), 'revision': 0,
}},
'customFields': {}, 'urlPresets': [], 'viewSettings': {},
'automation': {'enabledAutomations': False, 'enabledOscIn': False,
'triggers': [], 'automations': {}},
}

View File

@ -3,7 +3,6 @@
import argparse
import html
import json
import os
import posixpath
import re
import shutil
@ -16,11 +15,18 @@ from pathlib import Path
from urllib.parse import quote
from xml.etree import ElementTree as ET
if __package__:
from .ontime import ontime_project
from .progress import Progress, status
else:
from ontime import ontime_project
from progress import Progress, status
VIDEO_EXTS = {'.mp4', '.m4v', '.mov', '.avi', '.wmv', '.webm', '.mpg', '.mpeg', '.mkv', '.asf', '.3gp', '.ts'}
REL_NS = 'http://schemas.openxmlformats.org/package/2006/relationships'
R_NS = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'
P_NS = 'http://schemas.openxmlformats.org/presentationml/2006/main'
REPORT_FORMATS = ('html', 'typst', 'pdf', 'md', 'txt', 'json')
REPORT_FORMATS = ('html', 'typst', 'pdf', 'md', 'txt', 'json', 'ontime')
HEADERS = ('Slide(s)', 'Duration', 'Video', 'Size', 'Resolution', 'Codec')
REPORT_NOTES = (
'Duration is the original media file duration, not slide playback time '
@ -29,21 +35,6 @@ REPORT_NOTES = (
)
def status(message, level='info'):
"""Keep redirected output plain and flush progress before slow operations."""
stream = sys.stderr if level in ('warning', 'error') else sys.stdout
label, color = {
'info': ('INFO', '36'),
'success': ('OK', '32'),
'warning': ('WARN', '33'),
'error': ('ERROR', '31'),
}[level]
prefix = f'[{label}]'
if stream.isatty() and 'NO_COLOR' not in os.environ and os.environ.get('TERM') != 'dumb':
prefix = f'\033[{color}m{prefix}\033[0m'
print(f'{prefix} {message}', file=stream, flush=True)
def xml(z, name):
try:
return ET.fromstring(z.read(name))
@ -209,6 +200,8 @@ def typst_string(value):
def render_report(data, format):
if format == 'ontime':
return json.dumps(ontime_project(data), ensure_ascii=False, indent=2, allow_nan=False) + '\n'
title = f'Video timings — {data["presentation"]}'
metrics = [
('Embedded videos', str(data['embedded_videos'])),
@ -348,16 +341,17 @@ def write_reports(data, formats, out):
for format in formats:
if format == 'pdf':
continue
report = out / ('timings.typ' if format == 'typst' else f'timings.{format}')
extension = {'typst': 'typ', 'ontime': 'ontime.json'}.get(format, format)
report = out / f'timings.{extension}'
status(f'Writing report: {report}')
report.write_text(render_report(data, format), encoding='utf-8')
status(f'Report: {report}', 'success')
status(f'Report: {report}', 'success', persist=True)
if 'pdf' in formats:
report = out / 'timings.pdf'
status(f'Compiling PDF with Typst: {report}')
subprocess.run(['typst', 'compile', str((out / 'timings.typ').resolve()),
str(report.resolve())], check=True, capture_output=True, text=True)
status(f'Report: {report}', 'success')
status(f'Report: {report}', 'success', persist=True)
def main():
@ -369,18 +363,18 @@ def main():
mode.add_argument('--timings-only', action='store_true', help='Report video timings without keeping newly extracted videos')
parser.add_argument('--overwrite', action='store_true', help='Overwrite already extracted files')
parser.add_argument('--report', type=report_formats, action='append', metavar='FORMAT[,FORMAT...]',
help='Report formats: html, typst, pdf, md, txt, json. Repeatable; default: md. PDF requires typst and keeps timings.typ.')
help='Report formats: html, typst, pdf, md, txt, json, ontime. Repeatable; default: md,ontime. PDF requires typst and keeps timings.typ.')
args = parser.parse_args()
if args.extract_only and args.report:
parser.error('--report cannot be combined with --extract-only.')
formats = list(dict.fromkeys(format for group in (args.report or [['md']]) for format in group))
formats = list(dict.fromkeys(format for group in (args.report or [['md', 'ontime']]) for format in group))
source = args.presentation
if not source.is_file():
parser.error(f'Not a file: {source}')
out = args.output or source.with_name(source.stem + '_videos')
try:
status(f'Scanning presentation: {source}')
with zipfile.ZipFile(source) as z:
with Progress() as progress, zipfile.ZipFile(source) as z:
status(f'Scanning presentation: {source}')
videos, uses, external = discover(z)
if external:
status(f'External media on slide(s) {", ".join(map(str, sorted(external)))} will not be downloaded.', 'warning')
@ -399,6 +393,7 @@ def main():
total_bytes = 0
total_seconds = 0.0
known = 0
progress.set(0, len(videos))
for index, part in enumerate(videos, 1):
name = posixpath.basename(part)
dest = out / name
@ -413,6 +408,7 @@ def main():
shutil.copyfileobj(src, dst)
status(' Extraction complete.', 'success')
if args.extract_only:
progress.set(index)
continue
if not dest.is_file():
# A seekable temporary file supports media whose metadata
@ -439,11 +435,12 @@ def main():
if duration is None:
status(' ffprobe could not determine the duration; continuing with available metadata.', 'warning')
rows.append((part, name, size, duration, resolution, codec))
progress.set(index)
if args.extract_only:
status(f'Done: {len(videos)} video(s) processed.', 'success')
status(f'Done: {len(videos)} video(s) processed. Output: {out}', 'success', persist=True)
return 0
write_reports(report_data(source, rows, uses, external, out), formats, out)
status(f'Done: {known}/{len(videos)} durations available, {time_fmt(total_seconds)} total, {size_fmt(total_bytes)}.', 'success')
status(f'Done: {known}/{len(videos)} durations available, {time_fmt(total_seconds)} total, {size_fmt(total_bytes)}.', 'success', persist=True)
return 0
except subprocess.CalledProcessError as e:
status(f'Typst compilation failed: {e.stderr.strip()}', 'error')

104
tools/progress.py Normal file
View File

@ -0,0 +1,104 @@
"""Transient terminal progress with ordinary line logs when redirected."""
import os
import shutil
import sys
import threading
import time
_active = None
def status(message, level='info', *, persist=False):
if _active is not None:
_active.message(message, level, persist)
else:
_print_status(message, level)
def _print_status(message, level):
stream = sys.stderr if level in ('warning', 'error') else sys.stdout
label, color = {
'info': ('INFO', '36'), 'success': ('OK', '32'),
'warning': ('WARN', '33'), 'error': ('ERROR', '31'),
}[level]
prefix = f'[{label}]'
if stream.isatty() and 'NO_COLOR' not in os.environ and os.environ.get('TERM') != 'dumb':
prefix = f'\033[{color}m{prefix}\033[0m'
print(f'{prefix} {message}', file=stream, flush=True)
class Progress:
def __init__(self):
self.stream = sys.stderr
self.interactive = self.stream.isatty() and sys.stdout.isatty() and os.environ.get('TERM') != 'dumb'
self.lock = threading.RLock()
self.stop = threading.Event()
self.thread = None
self.text = ''
self.done = 0
self.total = None
self.started = time.monotonic()
self.previous = None
def __enter__(self):
global _active
self.previous, _active = _active, self
if self.interactive:
self.thread = threading.Thread(target=self._animate, daemon=True)
self.thread.start()
return self
def __exit__(self, *_):
global _active
self.stop.set()
if self.thread:
self.thread.join()
with self.lock:
self._clear()
_active = self.previous
def set(self, done, total=None):
with self.lock:
self.done = done
if total is not None:
self.total = total
self._draw()
def message(self, message, level, persist):
with self.lock:
if not self.interactive:
_print_status(message, level)
elif persist or level in ('warning', 'error'):
self._clear()
_print_status(message, level)
self.text = ''
else:
self.text = ' '.join(str(message).split())
self._draw()
def _clear(self):
if self.interactive:
self.stream.write('\r\033[2K')
self.stream.flush()
def _draw(self):
if not self.interactive or not self.text:
return
elapsed = time.monotonic() - self.started
spinner = '|/-\\'[int(elapsed * 8) % 4]
if self.total:
filled = min(16, max(0, int(16 * self.done / self.total)))
indicator = '[' + '=' * filled + '-' * (16 - filled) + f'] {self.done}/{self.total}'
else:
indicator = spinner
width = max(1, shutil.get_terminal_size((80, 24)).columns - 1)
line = f'{spinner} {indicator} {elapsed:5.1f}s {self.text}'[:width]
if 'NO_COLOR' not in os.environ:
line = f'\033[36m{line}\033[0m'
self.stream.write('\r\033[2K' + line)
self.stream.flush()
def _animate(self):
while not self.stop.wait(0.1):
with self.lock:
self._draw()