fix: Only inform if no extractor finds valid files
Until now whenever an extractor could not find any valid files for a document it would inform the user of this case. However, this is not very useful: if you have a pdf and an epub extractor running, it would inform you for each document which only had one of the two formats as well as those which actually did not have any valid files for *any* of the extractors running. This commit changes the behavior to only inform the user when none of the running extractors find a valid file, since that is the actual case a user might want to be informed about.
This commit is contained in:
parent
97b7ec0dc9
commit
779519f580
2 changed files with 16 additions and 8 deletions
|
@ -1,3 +1,4 @@
|
|||
import re
|
||||
import click
|
||||
import papis.cli
|
||||
import papis.config
|
||||
|
@ -145,10 +146,18 @@ def run(
|
|||
doc_annots: list[tuple[Document, list[Annotation]]] = []
|
||||
for doc in documents:
|
||||
annotations: list[Annotation] = []
|
||||
valid_files: int = 0
|
||||
for ext in extractors:
|
||||
if not ext:
|
||||
continue
|
||||
annotations.extend(extraction.start(ext, doc))
|
||||
added = extraction.start(ext, doc)
|
||||
if added is not None:
|
||||
valid_files += 1
|
||||
annotations.extend(added)
|
||||
doc_annots.append((doc, annotations))
|
||||
|
||||
if valid_files == 0:
|
||||
# have to remove curlys or papis logger gets upset
|
||||
desc = re.sub("[{}]", "", papis.document.describe(doc))
|
||||
logger.info(f"Document {desc} has no valid extractors for any of its files.")
|
||||
exporter.run(doc_annots)
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import re
|
||||
from pathlib import Path
|
||||
from typing import Protocol
|
||||
|
||||
|
@ -22,11 +21,13 @@ class Extractor(Protocol):
|
|||
def start(
|
||||
extractor: Extractor,
|
||||
document: Document,
|
||||
) -> list[Annotation]:
|
||||
) -> list[Annotation] | None:
|
||||
"""Extract all annotations from passed documents.
|
||||
|
||||
Returns all annotations contained in the papis
|
||||
documents passed in.
|
||||
documents passed in (empty list if no annotations).
|
||||
If there are no files that the extractor can process,
|
||||
returns None instead.
|
||||
"""
|
||||
annotations: list[Annotation] = []
|
||||
file_available: bool = False
|
||||
|
@ -40,11 +41,9 @@ def start(
|
|||
try:
|
||||
annotations.extend(extractor.run(fname))
|
||||
except ExtractionError as e:
|
||||
print(f"File extraction errors for {file}.\n{e}")
|
||||
logger.error(f"File extraction errors for {file}. File may be damaged.\n{e}")
|
||||
|
||||
if not file_available:
|
||||
# have to remove curlys or papis logger gets upset
|
||||
desc = re.sub("[{}]", "", papis.document.describe(document))
|
||||
logger.info(f"No {type(extractor)} file for document: {desc}")
|
||||
return None
|
||||
|
||||
return annotations
|
||||
|
|
Loading…
Reference in a new issue