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

@ -1,5 +1,5 @@
from papis.document import Document
from papis_extract.annotation import AnnotatedDocument, Annotation
from papis_extract.annotation import Annotation
from papis_extract.formatter import (
format_count,
@ -9,13 +9,11 @@ from papis_extract.formatter import (
format_markdown_setext,
)
an_doc: AnnotatedDocument = AnnotatedDocument(
Document(data={"author": "document-author", "title": "document-title"}),
[
Annotation("myfile.pdf", text="my lovely text"),
Annotation("myfile.pdf", text="my second text", content="with note"),
],
)
document = Document(data={"author": "document-author", "title": "document-title"})
annotations = [
Annotation("myfile.pdf", text="my lovely text"),
Annotation("myfile.pdf", text="my second text", content="with note"),
]
md_default_output = """============== ---------------
document-title - document-author
============== ---------------
@ -28,12 +26,12 @@ document-title - document-author
def test_markdown_default():
fmt = format_markdown
assert fmt([an_doc]) == md_default_output
assert fmt(document, annotations) == md_default_output
def test_markdown_atx():
fmt = format_markdown_atx
assert fmt([an_doc]) == (
assert fmt(document, annotations) == (
"""# document-title - document-author
> my lovely text
@ -45,17 +43,17 @@ def test_markdown_atx():
def test_markdown_setext():
fmt = format_markdown_setext
assert fmt([an_doc]) == md_default_output
assert fmt(document, annotations) == md_default_output
def test_count_default():
fmt = format_count
assert fmt([an_doc]) == ("""document-author - document-title: 2""")
assert fmt(document, annotations) == ("""document-author - document-title: 2""")
def test_csv_default():
fmt = format_csv
assert fmt([an_doc]) == (
assert fmt(document, annotations) == (
"type,tag,page,quote,note,author,title,ref,file\n"
'Highlight,,0,"my lovely text","","document-author",'
'"document-title","","myfile.pdf"\n'
@ -66,15 +64,12 @@ def test_csv_default():
# sadpath - no annotations contained for each format
def test_markdown_no_annotations():
d: AnnotatedDocument = AnnotatedDocument(Document(data={}), [])
assert format_markdown([d]) == ""
assert format_markdown(document, []) == ""
def test_count_no_annotations():
d: AnnotatedDocument = AnnotatedDocument(Document(data={}), [])
assert format_count([d]) == ""
assert format_count(document, []) == ""
def test_csv_no_annotations():
d: AnnotatedDocument = AnnotatedDocument(Document(data={}), [])
assert format_csv([d]) == "type,tag,page,quote,note,author,title,ref,file"
assert format_csv(document, []) == ""