refactor: Remove AnnotatedDocument class

The AnnotatedDocument class was, essentially, a simple tuple of a document
and a list of annotations. While not bad in a vacuum, it is unwieldy and
passing this around instead of a document, annotations, or both where
necessary is more restrictive and frankly unnecessary.

This commit removes the data class and any instances of its use. Instead,
we now pass the individual components around to anything that needs them.
This also frees us up to pass only annotations around for example.

We also do not iterate through the selected papis documents to work on
in each exporter anymore (since we only pass a single document), but
in the main function itself. This leads to less duplication and makes
the overall run function the overall single source of iteration through
selected documents. Everything else only knows about a single document -
the one it is operating on - which seems much neater.

For now, it does not change much, but should make later work on extra
exporters or extractors easier.
This commit is contained in:
Marty Oehme 2024-01-20 16:34:10 +01:00
parent cd5f787220
commit 765de505bb
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A
6 changed files with 142 additions and 138 deletions

View file

@ -6,27 +6,33 @@ import papis.api
import papis.git
import papis.config
import Levenshtein
from papis_extract.annotation import AnnotatedDocument
from papis_extract.annotation import Annotation
from papis_extract.formatter import Formatter
logger = papis.logging.get_logger(__name__)
def to_stdout(formatter: Formatter, annotated_docs: list[AnnotatedDocument]) -> None:
def to_stdout(
formatter: Formatter,
document: papis.document.Document,
annotations: list[Annotation],
) -> None:
"""Pretty print annotations to stdout.
Gives a nice human-readable representations of
the annotations in somewhat of a list form.
Not intended for machine-readability.
"""
output: str = formatter(annotated_docs)
print(output.rstrip("\n"))
output: str = formatter(document, annotations)
if output:
print(output.rstrip("\n"))
def to_notes(
formatter: Formatter,
annotated_docs: list[AnnotatedDocument],
document: papis.document.Document,
annotations: list[Annotation],
edit: bool,
git: bool,
force: bool,
@ -37,13 +43,12 @@ def to_notes(
belonging to papis documents. Creates new notes for
documents missing a note field or appends to existing.
"""
for entry in annotated_docs:
formatted_annotations = formatter([entry]).split("\n")
if formatted_annotations:
_add_annots_to_note(entry.document, formatted_annotations, force=force)
formatted_annotations = formatter(document, annotations).split("\n")
if formatted_annotations:
_add_annots_to_note(document, formatted_annotations, force=force)
if edit:
papis.commands.edit.edit_notes(entry.document, git=git)
if edit:
papis.commands.edit.edit_notes(document, git=git)
def _add_annots_to_note(