--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, find_spatialite,
sqlite3, sqlite3,
decode_base64_values, decode_base64_values,
progressbar,
rows_from_file, rows_from_file,
Format, Format,
TypeTracker, TypeTracker,
@ -1744,9 +1745,20 @@ def extract(
@click.option("--replace", is_flag=True, help="Replace files with matching primary key") @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("--upsert", is_flag=True, help="Upsert files with matching primary key")
@click.option("--name", type=str, help="File name to use") @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 @load_extension_option
def insert_files( 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 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 # Load all paths so we can show a progress bar
paths_and_relative_paths = list(yield_paths_and_relative_paths()) 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(): def to_insert():
for path, relative_path in bar: for path, relative_path in bar:

View file

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

View file

@ -2,9 +2,11 @@ from sqlite_utils import cli, Database
from click.testing import CliRunner from click.testing import CliRunner
import os import os
import pathlib import pathlib
import pytest
def test_insert_files(): @pytest.mark.parametrize("silent", (False, True))
def test_insert_files(silent):
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(): with runner.isolated_filesystem():
tmpdir = pathlib.Path(".") tmpdir = pathlib.Path(".")
@ -34,7 +36,10 @@ def test_insert_files():
cols += ["-c", "{}:{}".format(coltype, coltype)] cols += ["-c", "{}:{}".format(coltype, coltype)]
result = runner.invoke( result = runner.invoke(
cli.cli, 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, catch_exceptions=False,
) )
assert result.exit_code == 0, result.stdout assert result.exit_code == 0, result.stdout