Compare commits
2 commits
984025b472
...
5a99665d7e
Author | SHA1 | Date | |
---|---|---|---|
5a99665d7e | |||
aeb18ae358 |
3 changed files with 41 additions and 16 deletions
|
@ -8,10 +8,7 @@ import papis.strings
|
||||||
from papis.document import Document
|
from papis.document import Document
|
||||||
|
|
||||||
from papis_extract import extractor, exporter
|
from papis_extract import extractor, exporter
|
||||||
from papis_extract.formatter import (
|
from papis_extract.formatter import Formatter, formatters
|
||||||
Formatter,
|
|
||||||
formatters
|
|
||||||
)
|
|
||||||
|
|
||||||
logger = papis.logging.get_logger(__name__)
|
logger = papis.logging.get_logger(__name__)
|
||||||
|
|
||||||
|
@ -52,6 +49,11 @@ papis.config.register_default_settings(DEFAULT_OPTIONS)
|
||||||
),
|
),
|
||||||
help="Choose an output template to format annotations with.",
|
help="Choose an output template to format annotations with.",
|
||||||
)
|
)
|
||||||
|
@click.option(
|
||||||
|
"--force/--no-force",
|
||||||
|
"-f",
|
||||||
|
help="Do not drop any annotations because they already exist.",
|
||||||
|
)
|
||||||
def main(
|
def main(
|
||||||
query: str,
|
query: str,
|
||||||
# _papis_id: bool,
|
# _papis_id: bool,
|
||||||
|
@ -63,6 +65,7 @@ def main(
|
||||||
write: bool,
|
write: bool,
|
||||||
template: str,
|
template: str,
|
||||||
git: bool,
|
git: bool,
|
||||||
|
force: bool,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Extract annotations from any pdf document.
|
"""Extract annotations from any pdf document.
|
||||||
|
|
||||||
|
@ -84,7 +87,7 @@ def main(
|
||||||
|
|
||||||
formatter = formatters[template]
|
formatter = formatters[template]
|
||||||
|
|
||||||
run(documents, edit=manual, write=write, git=git, formatter=formatter)
|
run(documents, edit=manual, write=write, git=git, formatter=formatter, force=force)
|
||||||
|
|
||||||
|
|
||||||
def run(
|
def run(
|
||||||
|
@ -93,11 +96,12 @@ def run(
|
||||||
edit: bool = False,
|
edit: bool = False,
|
||||||
write: bool = False,
|
write: bool = False,
|
||||||
git: bool = False,
|
git: bool = False,
|
||||||
|
force: bool = False,
|
||||||
) -> None:
|
) -> None:
|
||||||
annotated_docs = extractor.start(documents)
|
annotated_docs = extractor.start(documents)
|
||||||
if write:
|
if write:
|
||||||
exporter.to_notes(
|
exporter.to_notes(
|
||||||
formatter=formatter, annotated_docs=annotated_docs, edit=edit, git=git
|
formatter=formatter, annotated_docs=annotated_docs, edit=edit, git=git, force=force
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
exporter.to_stdout(formatter=formatter, annotated_docs=annotated_docs)
|
exporter.to_stdout(formatter=formatter, annotated_docs=annotated_docs)
|
||||||
|
|
|
@ -25,7 +25,11 @@ def to_stdout(formatter: Formatter, annotated_docs: list[AnnotatedDocument]) ->
|
||||||
|
|
||||||
|
|
||||||
def to_notes(
|
def to_notes(
|
||||||
formatter: Formatter, annotated_docs: list[AnnotatedDocument], edit: bool, git: bool
|
formatter: Formatter,
|
||||||
|
annotated_docs: list[AnnotatedDocument],
|
||||||
|
edit: bool,
|
||||||
|
git: bool,
|
||||||
|
force: bool,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Write annotations into document notes.
|
"""Write annotations into document notes.
|
||||||
|
|
||||||
|
@ -36,7 +40,7 @@ def to_notes(
|
||||||
for entry in annotated_docs:
|
for entry in annotated_docs:
|
||||||
formatted_annotations = formatter([entry]).split("\n")
|
formatted_annotations = formatter([entry]).split("\n")
|
||||||
if formatted_annotations:
|
if formatted_annotations:
|
||||||
_add_annots_to_note(entry.document, formatted_annotations)
|
_add_annots_to_note(entry.document, formatted_annotations, force=force)
|
||||||
|
|
||||||
if edit:
|
if edit:
|
||||||
papis.commands.edit.edit_notes(entry.document, git=git)
|
papis.commands.edit.edit_notes(entry.document, git=git)
|
||||||
|
@ -46,11 +50,26 @@ def _add_annots_to_note(
|
||||||
document: papis.document.Document,
|
document: papis.document.Document,
|
||||||
formatted_annotations: list[str],
|
formatted_annotations: list[str],
|
||||||
git: bool = False,
|
git: bool = False,
|
||||||
|
force: bool = False,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Append new annotations to the end of a note.
|
"""
|
||||||
|
Append new annotations to the end of a note.
|
||||||
|
|
||||||
Looks through note to determine any new annotations which should be
|
This function appends new annotations to the end of a note file. It takes in a
|
||||||
added and adds them to the end of the note file.
|
document object containing the note, a list of formatted annotations to be
|
||||||
|
added, and optional flags git and force. If git is True, the changes will be
|
||||||
|
committed to git. If force is True, the annotations will be added even if they
|
||||||
|
already exist in the note.
|
||||||
|
|
||||||
|
:param document: The document object representing the note
|
||||||
|
:type document: class:`papis.document.Document`
|
||||||
|
:param formatted_annotations: A list of already formatted annotations to be added
|
||||||
|
:type formatted_annotations: list[str]
|
||||||
|
:param git: Flag indicating whether to commit changes to git, defaults to False.
|
||||||
|
:type git: bool, optional
|
||||||
|
:param force: Flag indicating whether to force adding annotations even if they
|
||||||
|
already exist, defaults to False.
|
||||||
|
:type force: bool, optional
|
||||||
"""
|
"""
|
||||||
logger.debug("Adding annotations to note.")
|
logger.debug("Adding annotations to note.")
|
||||||
notes_path = papis.notes.notes_path_ensured(document)
|
notes_path = papis.notes.notes_path_ensured(document)
|
||||||
|
@ -59,9 +78,9 @@ def _add_annots_to_note(
|
||||||
with open(notes_path, "r") as file_read:
|
with open(notes_path, "r") as file_read:
|
||||||
existing = file_read.readlines()
|
existing = file_read.readlines()
|
||||||
|
|
||||||
new_annotations: list[str] = _drop_existing_annotations(
|
new_annotations: list[str] = []
|
||||||
formatted_annotations, existing
|
if not force:
|
||||||
)
|
new_annotations = _drop_existing_annotations(formatted_annotations, existing)
|
||||||
if not new_annotations:
|
if not new_annotations:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
|
@ -26,13 +26,14 @@ def test_formatting_replacements(fmt_string, expected):
|
||||||
|
|
||||||
assert sut.format(fmt_string) == expected
|
assert sut.format(fmt_string) == expected
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"fmt_string,expected",
|
"fmt_string,expected",
|
||||||
[
|
[
|
||||||
("{{doc.title}}", "document-title"),
|
("{{doc.title}}", "document-title"),
|
||||||
("{{doc.title}}-{{doc.author}}", "document-title-document-author"),
|
("{{doc.title}}-{{doc.author}}", "document-title-document-author"),
|
||||||
("{{quote}} ({{doc.author}})", "I am the text value (document-author)"),
|
("{{quote}} ({{doc.author}})", "I am the text value (document-author)"),
|
||||||
]
|
],
|
||||||
)
|
)
|
||||||
def test_formatting_document_access(fmt_string, expected):
|
def test_formatting_document_access(fmt_string, expected):
|
||||||
sut = Annotation(
|
sut = Annotation(
|
||||||
|
@ -44,6 +45,7 @@ def test_formatting_document_access(fmt_string, expected):
|
||||||
|
|
||||||
assert sut.format(fmt_string, doc=doc) == expected
|
assert sut.format(fmt_string, doc=doc) == expected
|
||||||
|
|
||||||
|
|
||||||
def test_colorname_matches_exact():
|
def test_colorname_matches_exact():
|
||||||
sut = Annotation("testfile", colors=(1.0, 0.0, 0.0), minimum_similarity_color=1.0)
|
sut = Annotation("testfile", colors=(1.0, 0.0, 0.0), minimum_similarity_color=1.0)
|
||||||
c_name = sut.colorname
|
c_name = sut.colorname
|
||||||
|
|
Loading…
Reference in a new issue