--silent option for sqlite-utils insert-files, closes #301

This commit is contained in:
Simon Willison 2021-08-02 12:12:16 -07:00
commit 60dea99ef7
3 changed files with 31 additions and 7 deletions

View file

@ -21,6 +21,7 @@ from .utils import (
find_spatialite,
sqlite3,
decode_base64_values,
progressbar,
rows_from_file,
Format,
TypeTracker,
@ -1744,9 +1745,20 @@ def extract(
@click.option("--replace", is_flag=True, help="Replace files with matching primary key")
@click.option("--upsert", is_flag=True, help="Upsert files with matching primary key")
@click.option("--name", type=str, help="File name to use")
@click.option("-s", "--silent", is_flag=True, help="Don't show a progress bar")
@load_extension_option
def insert_files(
path, table, file_or_dir, column, pk, alter, replace, upsert, name, load_extension
path,
table,
file_or_dir,
column,
pk,
alter,
replace,
upsert,
name,
silent,
load_extension,
):
"""
Insert one or more files using BLOB columns in the specified table
@ -1783,7 +1795,7 @@ def insert_files(
# Load all paths so we can show a progress bar
paths_and_relative_paths = list(yield_paths_and_relative_paths())
with click.progressbar(paths_and_relative_paths) as bar:
with progressbar(paths_and_relative_paths, silent=silent) as bar:
def to_insert():
for path, relative_path in bar:

View file

@ -260,14 +260,21 @@ class ValueTracker:
class NullProgressBar:
def __init__(self, *args):
self.args = args
def __iter__(self):
yield from self.args[0]
def update(self, value):
pass
@contextlib.contextmanager
def progressbar(silent=False, **kwargs):
def progressbar(*args, **kwargs):
silent = kwargs.pop("silent")
if silent:
yield NullProgressBar()
yield NullProgressBar(*args)
else:
with click.progressbar(**kwargs) as bar:
with click.progressbar(*args, **kwargs) as bar:
yield bar

View file

@ -2,9 +2,11 @@ from sqlite_utils import cli, Database
from click.testing import CliRunner
import os
import pathlib
import pytest
def test_insert_files():
@pytest.mark.parametrize("silent", (False, True))
def test_insert_files(silent):
runner = CliRunner()
with runner.isolated_filesystem():
tmpdir = pathlib.Path(".")
@ -34,7 +36,10 @@ def test_insert_files():
cols += ["-c", "{}:{}".format(coltype, coltype)]
result = runner.invoke(
cli.cli,
["insert-files", db_path, "files", str(tmpdir)] + cols + ["--pk", "path"],
["insert-files", db_path, "files", str(tmpdir)]
+ cols
+ ["--pk", "path"]
+ (["--silent"] if silent else []),
catch_exceptions=False,
)
assert result.exit_code == 0, result.stdout