Bidirectional Middlewares

Bidirectional Middlewares are based on IdenBidiMiddleware, to provide PairStack’s with a tranform when reading a bibtex file, and an equivalent reverse transform to undo it when writing.

For Example, BraceWrapper provides unwrapping fields on read (so entry.Field("title", "{This is an Example}") is transformed to entry.Field("title", "This is an Example")), and then when writing the reverse occurs.

Where normal middlewares implement transform and transform_{type} methods, bidirectional middlewares implement read_transform_{type} and write_transform_{type} methods. So BraceWrapper implements read_transform_Entry and write_transform_Entry methods.

Meanwhile, when read and write middlewares are already implemented (eg: IsbnWriter and IsbnValidator), then the bidirectional version can be as simple as:

from bibble.util.middlecore import IdenBidiMiddleware
from bibble.metadata import IsbnValidator, IsbnWriter

class BidiIsbn(IdenBidiMiddleware):

    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self._reader = IsbnValidator()
        self._writer = IsbnWriter()

    def read_transform_Entry(self, entry:Entry, library:Library) -> list[Entry]:
        return self._reader.transform_Entry(entry, library)

    def write_transform_Entry(self, entry:Entry, library:Library) -> list[Entry]:
        return self._writer.transform_Entry(entry, library)