Change annotation color to simple rgb tuple
Some checks failed
ci/woodpecker/push/test unknown status
ci/woodpecker/push/lint Pipeline failed
ci/woodpecker/push/static_analysis Pipeline was successful

This commit is contained in:
Marty Oehme 2023-08-29 22:23:52 +02:00
parent 256117d451
commit 20873e6ef8
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A
3 changed files with 14 additions and 8 deletions

View file

@ -23,7 +23,7 @@ class Annotation:
"""A PDF annotation object""" """A PDF annotation object"""
file: str file: str
colors: dict = field(default_factory=lambda: {"stroke": (0.0, 0.0, 0.0)}) colors: tuple[float, float, float] = field(default_factory=lambda: (0.0, 0.0, 0.0))
content: str = "" content: str = ""
page: int = 0 page: int = 0
tag: str = "" tag: str = ""
@ -56,7 +56,7 @@ class Annotation:
using euclidian distance between the two color vectors. using euclidian distance between the two color vectors.
""" """
annot_colors = ( annot_colors = (
self.colors.get("stroke") or self.colors.get("fill") or (0.0, 0.0, 0.0) self.colors or (0.0, 0.0, 0.0)
) )
nearest = None nearest = None
minimum_similarity = ( minimum_similarity = (

View file

@ -14,6 +14,7 @@ from papis_extract.annotation_data import Annotation, AnnotatedDocument
logger = papis.logging.get_logger(__name__) logger = papis.logging.get_logger(__name__)
def start( def start(
documents: list[Document], documents: list[Document],
) -> list[AnnotatedDocument]: ) -> list[AnnotatedDocument]:
@ -45,6 +46,7 @@ def start(
output.append(AnnotatedDocument(doc, annotations)) output.append(AnnotatedDocument(doc, annotations))
return output return output
def extract(filename: Path) -> list[Annotation]: def extract(filename: Path) -> list[Annotation]:
"""Extract annotations from a file. """Extract annotations from a file.
@ -58,11 +60,16 @@ def extract(filename: Path) -> list[Annotation]:
quote, note = _retrieve_annotation_content(page, annot) quote, note = _retrieve_annotation_content(page, annot)
if not quote and not note: if not quote and not note:
continue continue
col = (
annot.colors.get("fill")
or annot.colors.get("stroke")
or (0.0, 0.0, 0.0)
)
a = Annotation( a = Annotation(
file=str(filename), file=str(filename),
text=quote or "", text=quote or "",
content=note or "", content=note or "",
colors=annot.colors, colors=col,
type=annot.type[1], type=annot.type[1],
page=(page.number or 0) + 1, page=(page.number or 0) + 1,
) )
@ -79,8 +86,6 @@ def is_pdf(fname: Path) -> bool:
return magic.from_file(fname, mime=True) == "application/pdf" return magic.from_file(fname, mime=True) == "application/pdf"
def _is_file_processable(fname: Path) -> bool: def _is_file_processable(fname: Path) -> bool:
if not fname.is_file(): if not fname.is_file():
logger.error(f"File {str(fname)} not readable.") logger.error(f"File {str(fname)} not readable.")
@ -89,6 +94,7 @@ def _is_file_processable(fname: Path) -> bool:
return False return False
return True return True
def _tag_from_colorname(colorname: str) -> str: def _tag_from_colorname(colorname: str) -> str:
color_mapping: dict[str, str] = getdict("tags", "plugins.extract") color_mapping: dict[str, str] = getdict("tags", "plugins.extract")
if not color_mapping: if not color_mapping:

View file

@ -27,7 +27,7 @@ def test_formatting(fmt_string, expected):
def test_colorname_matches_exact(): def test_colorname_matches_exact():
sut = Annotation( sut = Annotation(
"testfile", colors={"stroke": (1.0,0.0,0.0)}, minimum_similarity_color=1.0 "testfile", colors=(1.0,0.0,0.0), minimum_similarity_color=1.0
) )
c_name = sut.colorname c_name = sut.colorname
assert c_name == "red" assert c_name == "red"
@ -45,7 +45,7 @@ def test_colorname_matches_exact():
) )
def test_matches_inexact_colorname(color_value): def test_matches_inexact_colorname(color_value):
sut = Annotation( sut = Annotation(
"testfile", colors={"stroke": color_value}, minimum_similarity_color=0.833 "testfile", colors=color_value, minimum_similarity_color=0.833
) )
c_name = sut.colorname c_name = sut.colorname
assert c_name == "red" assert c_name == "red"