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

@ -7,7 +7,8 @@ import papis.notes
import papis.strings
from papis.document import Document
from papis_extract import extractor, exporter
from papis_extract import exporter, extractor
from papis_extract.annotation import Annotation
from papis_extract.formatter import Formatter, formatters
logger = papis.logging.get_logger(__name__)
@ -44,7 +45,7 @@ papis.config.register_default_settings(DEFAULT_OPTIONS)
"--template",
"-t",
type=click.Choice(
["markdown", "markdown-setext", "markdown-atx", "count", "csv"],
list(formatters.keys()),
case_sensitive=False,
),
help="Choose an output template to format annotations with.",
@ -85,27 +86,34 @@ def main(
logger.warning(papis.strings.no_documents_retrieved_message)
return
formatter = formatters[template]
formatter = formatters.get(template)
run(documents, edit=manual, write=write, git=git, formatter=formatter, force=force)
def run(
documents: list[Document],
formatter: Formatter,
formatter: Formatter | None,
edit: bool = False,
write: bool = False,
git: bool = False,
force: bool = False,
) -> None:
annotated_docs = extractor.start(documents)
if write:
exporter.to_notes(
formatter=formatter,
annotated_docs=annotated_docs,
edit=edit,
git=git,
force=force,
)
else:
exporter.to_stdout(formatter=formatter, annotated_docs=annotated_docs)
for doc in documents:
annotations: list[Annotation] = extractor.start(doc)
if write:
exporter.to_notes(
formatter=formatter or formatters["markdown-atx"],
document=doc,
annotations=annotations,
edit=edit,
git=git,
force=force,
)
else:
exporter.to_stdout(
formatter=formatter or formatters["markdown"],
document=doc,
annotations=annotations,
)