diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index 3f32413..bd063f6 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -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: diff --git a/sqlite_utils/utils.py b/sqlite_utils/utils.py index a781469..00a3c02 100644 --- a/sqlite_utils/utils.py +++ b/sqlite_utils/utils.py @@ -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 diff --git a/tests/test_insert_files.py b/tests/test_insert_files.py index 1a11d2c..1e30a8d 100644 --- a/tests/test_insert_files.py +++ b/tests/test_insert_files.py @@ -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