chore: Update for papis 0.14

This commit is contained in:
Marty Oehme 2025-06-09 11:09:47 +02:00
parent 57cf39bcd5
commit a076880913
Signed by: Marty
GPG key ID: 4E535BC19C61886E

View file

@ -1,9 +1,9 @@
import re import re
from typing import Any from typing import Any, override
import papis.format
import papis.config import papis.config
import papis.document import papis.document
import papis.format
import papis.logging import papis.logging
logger = papis.logging.get_logger(__name__) logger = papis.logging.get_logger(__name__)
@ -18,17 +18,38 @@ DEFAULT_OPTIONS = {
papis.config.register_default_settings(DEFAULT_OPTIONS) papis.config.register_default_settings(DEFAULT_OPTIONS)
class BBTFormatter(papis.format.Formater): class BBTFormatter(papis.format.Formatter):
"""Provides zotero better-bibtex-like keys.""" """Provides zotero better-bibtex-like keys."""
@override
def format( def format(
self, self,
fmt: str, fmt: str,
doc: papis.format.FormatDocType, doc: papis.document.DocumentLike,
doc_key: str = "", doc_key: str = "",
additional: dict[str, Any] = {}, additional: dict[str, Any] | None = None,
default: str | None = None,
) -> str: ) -> str:
if fmt.startswith("bbt"): if fmt.startswith("bbt"):
formatted = self.use_bbt(doc)
return formatted
else:
fallback_formatter = papis.config.getstring(
"fallback", "plugins.bbt-formatter"
)
# NOTE sure would be nice to have a less hacky way of calling another formatter
_saved = papis.format.FORMATTER
papis.format.FORMATTER = None
fallback_formatted: str = papis.format.get_formatter(
fallback_formatter
).format(fmt, doc, doc_key, additional, default)
papis.format.FORMATTER = _saved
return fallback_formatted
def use_bbt(self, doc: papis.document.DocumentLike) -> str:
author_unfmt = ( author_unfmt = (
doc["author_list"][0]["family"] doc["author_list"][0]["family"]
if "author_list" in doc if "author_list" in doc
@ -40,17 +61,6 @@ class BBTFormatter(papis.format.Formater):
year = self.get_year(int(doc["year"]) if "year" in doc else 0000) year = self.get_year(int(doc["year"]) if "year" in doc else 0000)
title = self.get_title(doc["title"] if "title" in doc else "NO TITLE") title = self.get_title(doc["title"] if "title" in doc else "NO TITLE")
return f"{author}{year}{title}" return f"{author}{year}{title}"
else:
# TODO find less hacky way of calling another formatter?
papis.format._FORMATER = None
fallback_formatter = papis.config.getstring(
"fallback", "plugins.bbt-formatter"
)
formatter = papis.format.get_formater(fallback_formatter).format(
fmt, doc, doc_key=doc_key, additional=additional
)
papis.format._FORMATER = None
return formatter
def get_year(self, year: int) -> str: def get_year(self, year: int) -> str:
"""Returns year string according to set year display options. """Returns year string according to set year display options.