Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

Add --no-progress-bar option to commands

Stéphane Brunnerdc26c9b7ccf1
diff --git a/src/documents/management/commands/document_archiver.py b/src/documents/management/commands/document_archiver.py
index 6a6056ed7..96ddebe77 100644
--- a/src/documents/management/commands/document_archiver.py
+++ b/src/documents/management/commands/document_archiver.py
@@ -106,6 +106,12 @@ class Command(BaseCommand):
help="Specify the ID of a document, and this command will only "
"run on this specific document."
)
+ parser.add_argument(
+ "--no-progress-bar",
+ default=False,
+ action="store_true",
+ help="If set, the progress bar will not be shown"
+ )
def handle(self, *args, **options):
@@ -140,7 +146,8 @@ class Command(BaseCommand):
handle_document,
document_ids
),
- total=len(document_ids)
+ total=len(document_ids),
+ disable=options['no_progress_bar']
))
except KeyboardInterrupt:
print("Aborting...")
diff --git a/src/documents/management/commands/document_exporter.py b/src/documents/management/commands/document_exporter.py
index 286d862e3..217877397 100644
--- a/src/documents/management/commands/document_exporter.py
+++ b/src/documents/management/commands/document_exporter.py
@@ -57,6 +57,12 @@ class Command(BaseCommand):
"do not belong to the current export, such as files from "
"deleted documents."
)
+ parser.add_argument(
+ "--no-progress-bar",
+ default=False,
+ action="store_true",
+ help="If set, the progress bar will not be shown"
+ )
def __init__(self, *args, **kwargs):
BaseCommand.__init__(self, *args, **kwargs)
@@ -81,9 +87,9 @@ class Command(BaseCommand):
raise CommandError("That path doesn't appear to be writable")
with FileLock(settings.MEDIA_LOCK):
- self.dump()
+ self.dump(options['no_progress_bar'])
- def dump(self):
+ def dump(self, progress_bar_disable=False):
# 1. Take a snapshot of what files exist in the current export folder
for root, dirs, files in os.walk(self.target):
self.files_in_export_dir.extend(
@@ -124,8 +130,11 @@ class Command(BaseCommand):
"json", User.objects.all()))
# 3. Export files from each document
- for index, document_dict in tqdm.tqdm(enumerate(document_manifest),
- total=len(document_manifest)):
+ for index, document_dict in tqdm.tqdm(
+ enumerate(document_manifest),
+ total=len(document_manifest),
+ disable=progress_bar_disable
+ ):
# 3.1. store files unencrypted
document_dict["fields"]["storage_type"] = Document.STORAGE_TYPE_UNENCRYPTED # NOQA: E501
diff --git a/src/documents/management/commands/document_importer.py b/src/documents/management/commands/document_importer.py
index 9347b0d4c..4606b91fe 100644
--- a/src/documents/management/commands/document_importer.py
+++ b/src/documents/management/commands/document_importer.py
@@ -36,6 +36,12 @@ class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument("source")
+ parser.add_argument(
+ "--no-progress-bar",
+ default=False,
+ action="store_true",
+ help="If set, the progress bar will not be shown"
+ )
def __init__(self, *args, **kwargs):
BaseCommand.__init__(self, *args, **kwargs)
@@ -70,7 +76,7 @@ class Command(BaseCommand):
# Fill up the database with whatever is in the manifest
call_command("loaddata", manifest_path)
- self._import_files_from_manifest()
+ self._import_files_from_manifest(options['no_progress_bar'])
print("Updating search index...")
call_command('document_index', 'reindex')
@@ -111,7 +117,7 @@ class Command(BaseCommand):
f"does not appear to be in the source directory."
)
- def _import_files_from_manifest(self):
+ def _import_files_from_manifest(self, progress_bar_disable):
os.makedirs(settings.ORIGINALS_DIR, exist_ok=True)
os.makedirs(settings.THUMBNAIL_DIR, exist_ok=True)
@@ -123,7 +129,10 @@ class Command(BaseCommand):
lambda r: r["model"] == "documents.document",
self.manifest))
- for record in tqdm.tqdm(manifest_documents):
+ for record in tqdm.tqdm(
+ manifest_documents,
+ disable=progress_bar_disable
+ ):
document = Document.objects.get(pk=record["pk"])
diff --git a/src/documents/management/commands/document_index.py b/src/documents/management/commands/document_index.py
index 5faa70b9f..d76f48293 100644
--- a/src/documents/management/commands/document_index.py
+++ b/src/documents/management/commands/document_index.py
@@ -10,10 +10,16 @@ class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument("command", choices=['reindex', 'optimize'])
+ parser.add_argument(
+ "--no-progress-bar",
+ default=False,
+ action="store_true",
+ help="If set, the progress bar will not be shown"
+ )
def handle(self, *args, **options):
with transaction.atomic():
if options['command'] == 'reindex':
- index_reindex()
+ index_reindex(progress_bar_disable=options['no_progress_bar'])
elif options['command'] == 'optimize':
index_optimize()
diff --git a/src/documents/management/commands/document_renamer.py b/src/documents/management/commands/document_renamer.py
index f311715c6..682705c2d 100644
--- a/src/documents/management/commands/document_renamer.py
+++ b/src/documents/management/commands/document_renamer.py
@@ -13,9 +13,20 @@ class Command(BaseCommand):
This will rename all documents to match the latest filename format.
""".replace(" ", "")
+ def add_arguments(self, parser):
+ parser.add_argument(
+ "--no-progress-bar",
+ default=False,
+ action="store_true",
+ help="If set, the progress bar will not be shown"
+ )
+
def handle(self, *args, **options):
logging.getLogger().handlers[0].level = logging.ERROR
- for document in tqdm.tqdm(Document.objects.all()):
+ for document in tqdm.tqdm(
+ Document.objects.all(),
+ disable=options['no_progress_bar']
+ ):
post_save.send(Document, instance=document)
diff --git a/src/documents/management/commands/document_retagger.py b/src/documents/management/commands/document_retagger.py
index 4a23faa86..ce3f94bf2 100755
--- a/src/documents/management/commands/document_retagger.py
+++ b/src/documents/management/commands/document_retagger.py
@@ -57,6 +57,12 @@ class Command(BaseCommand):
"set correspondent, document and remove correspondents, types"
"and tags that do not match anymore due to changed rules."
)
+ parser.add_argument(
+ "--no-progress-bar",
+ default=False,
+ action="store_true",
+ help="If set, the progress bar will not be shown"
+ )
def handle(self, *args, **options):
@@ -68,7 +74,10 @@ class Command(BaseCommand):
classifier = load_classifier()
- for document in tqdm.tqdm(documents):
+ for document in tqdm.tqdm(
+ documents,
+ disable=options['no_progress_bar']
+ ):
if options['correspondent']:
set_correspondent(
diff --git a/src/documents/management/commands/document_sanity_checker.py b/src/documents/management/commands/document_sanity_checker.py
index c04a5efef..fbbb8d600 100644
--- a/src/documents/management/commands/document_sanity_checker.py
+++ b/src/documents/management/commands/document_sanity_checker.py
@@ -8,8 +8,16 @@ class Command(BaseCommand):
This command checks your document archive for issues.
""".replace(" ", "")
+ def add_arguments(self, parser):
+ parser.add_argument(
+ "--no-progress-bar",
+ default=False,
+ action="store_true",
+ help="If set, the progress bar will not be shown"
+ )
+
def handle(self, *args, **options):
- messages = check_sanity(progress=True)
+ messages = check_sanity(progress=not options['no_progress_bar'])
messages.log_messages()
diff --git a/src/documents/management/commands/document_thumbnails.py b/src/documents/management/commands/document_thumbnails.py
index b7f935e3b..05f059039 100644
--- a/src/documents/management/commands/document_thumbnails.py
+++ b/src/documents/management/commands/document_thumbnails.py
@@ -47,6 +47,12 @@ class Command(BaseCommand):
help="Specify the ID of a document, and this command will only "
"run on this specific document."
)
+ parser.add_argument(
+ "--no-progress-bar",
+ default=False,
+ action="store_true",
+ help="If set, the progress bar will not be shown"
+ )
def handle(self, *args, **options):
logging.getLogger().handlers[0].level = logging.ERROR
@@ -65,5 +71,7 @@ class Command(BaseCommand):
with multiprocessing.Pool() as pool:
list(tqdm.tqdm(
- pool.imap_unordered(_process_document, ids), total=len(ids)
+ pool.imap_unordered(_process_document, ids),
+ total=len(ids),
+ disable=options['no_progress_bar']
))
diff --git a/src/documents/sanity_checker.py b/src/documents/sanity_checker.py
index 2e1777841..26467d3cf 100644
--- a/src/documents/sanity_checker.py
+++ b/src/documents/sanity_checker.py
@@ -60,12 +60,7 @@ def check_sanity(progress=False):
if lockfile in present_files:
present_files.remove(lockfile)
- if progress:
- docs = tqdm(Document.objects.all())
- else:
- docs = Document.objects.all()
-
- for doc in docs:
+ for doc in tqdm(Document.objects.all(), disable=not progress):
# Check sanity of the thumbnail
if not os.path.isfile(doc.thumbnail_path):
messages.error(f"Thumbnail of document {doc.pk} does not exist.")
diff --git a/src/documents/tasks.py b/src/documents/tasks.py
index ddfd2eac3..f24be562f 100644
--- a/src/documents/tasks.py
+++ b/src/documents/tasks.py
@@ -20,13 +20,13 @@ def index_optimize():
writer.commit(optimize=True)
-def index_reindex():
+def index_reindex(progress_bar_disable=False):
documents = Document.objects.all()
ix = index.open_index(recreate=True)
with AsyncWriter(ix) as writer:
- for document in tqdm.tqdm(documents):
+ for document in tqdm.tqdm(documents, disable=progress_bar_disable):
index.update_document(writer, document)