refactor: Make formatters functions

Formatters have been classes so far which contained some data (the
tamplate to use for formatting and the annotations and documents to
format) and the actual formatting logic (an execute function).

However, we can inject the annotations to be formatted and the templates
so far are static only, so they can be simple variables (we can think
about how to inject them at another point should it come up, no
bikeshedding now).

This way, we can simply pass around one function per formatter, which
should make the code much lighter, easier to add to and especially less
stateful which means less areas of broken interactions to worry about.
This commit is contained in:
Marty Oehme 2023-09-21 21:54:24 +02:00
parent 929e70d7ac
commit 7ee8d4911e
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A
3 changed files with 70 additions and 110 deletions

View file

@ -8,12 +8,7 @@ import papis.strings
from papis.document import Document
from papis_extract import extractor, exporter
from papis_extract.formatter import (
CountFormatter,
CsvFormatter,
MarkdownFormatter,
Formatter,
)
from papis_extract.formatter import Formatter, format_count, format_csv, format_markdown
logger = papis.logging.get_logger(__name__)
@ -82,11 +77,11 @@ def main(
return
if template == "csv":
formatter = CsvFormatter()
formatter = format_csv
elif template == "count":
formatter = CountFormatter()
formatter = format_count
else:
formatter = MarkdownFormatter()
formatter = format_markdown
run(documents, edit=manual, write=write, git=git, formatter=formatter)
@ -98,8 +93,10 @@ def run(
write: bool = False,
git: bool = False,
) -> None:
formatter.annotated_docs = extractor.start(documents)
annotated_docs = extractor.start(documents)
if write:
exporter.to_notes(formatter, edit=edit, git=git)
exporter.to_notes(
formatter=formatter, annotated_docs=annotated_docs, edit=edit, git=git
)
else:
exporter.to_stdout(formatter)
exporter.to_stdout(formatter=formatter, annotated_docs=annotated_docs)