Progress bar for sqlite-utils insert command, closes #173

This commit is contained in:
Simon Willison 2020-10-27 11:16:02 -07:00 committed by GitHub
commit f99a236529
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 5 deletions

View file

@ -1,4 +1,7 @@
import base64
import click
import contextlib
import io
import os
try:
@ -87,3 +90,24 @@ def find_spatialite():
if os.path.exists(path):
return path
return None
class UpdateWrapper:
def __init__(self, wrapped, update):
self._wrapped = wrapped
self._update = update
def __iter__(self):
for line in self._wrapped:
self._update(len(line))
yield line
@contextlib.contextmanager
def file_progress(file, silent=False, **kwargs):
if silent or file.raw.fileno() == 0: # 0 = stdin
yield file
else:
file_length = os.path.getsize(file.raw.name)
with click.progressbar(length=file_length, **kwargs) as bar:
yield UpdateWrapper(file, bar.update)