Skip to content

Python API: Conversion: format handlers

tina_mgr.convert.formats.f_base.FormatHandler

A handler for converting the Tina database between different formats.

Source code in python/src/tina_mgr/convert/formats/f_base.py
class FormatHandler:
    """A handler for converting the Tina database between different formats."""

    @classmethod
    def _do_dumps(cls, entries: list[db.TinaEntry]) -> str:
        """Store entries encoded in that format."""
        raise NotImplementedError(repr((cls, "dumps")))

    @classmethod
    def _do_loads(cls, contents: str) -> list[db.TinaEntry]:
        """Load entries encoded in that format."""
        raise NotImplementedError(repr((cls, "loads")))

    @classmethod
    def dumps(cls, entries: list[db.TinaEntry]) -> str:
        """Store entries encoded in that format."""
        parse.check_for_duplicates(entries)
        return cls._do_dumps(entries)

    @classmethod
    def loads(cls, contents: str) -> list[db.TinaEntry]:
        """Load entries encoded in that format."""
        res: Final = cls._do_loads(contents)
        parse.check_for_duplicates(res)
        return res

dumps(entries) classmethod

Store entries encoded in that format.

Source code in python/src/tina_mgr/convert/formats/f_base.py
@classmethod
def dumps(cls, entries: list[db.TinaEntry]) -> str:
    """Store entries encoded in that format."""
    parse.check_for_duplicates(entries)
    return cls._do_dumps(entries)

loads(contents) classmethod

Load entries encoded in that format.

Source code in python/src/tina_mgr/convert/formats/f_base.py
@classmethod
def loads(cls, contents: str) -> list[db.TinaEntry]:
    """Load entries encoded in that format."""
    res: Final = cls._do_loads(contents)
    parse.check_for_duplicates(res)
    return res

tina_mgr.convert.formats.f_json.JSONFormatHandler

Bases: FormatHandler

Encode and decode Tina database entries in a hierarchical structure.

Source code in python/src/tina_mgr/convert/formats/f_json.py
class JSONFormatHandler(f_base.FormatHandler):
    """Encode and decode Tina database entries in a hierarchical structure."""

    @classmethod
    def _do_dumps(cls, entries: list[db.TinaEntry]) -> str:
        """Format Tina database entries into the JSON structure."""
        return json.dumps(f_base.serialize(entries))

    @classmethod
    def _do_loads(cls, contents: str) -> list[db.TinaEntry]:
        """Parse the JSON structure into database entries."""
        try:
            top: Final = json.loads(contents)
        except ValueError as err:
            raise JSONDeserializationError(err) from err

        return f_base.deserialize(top)

tina_mgr.convert.formats.f_tina.TinaFormatHandler

Bases: FormatHandler

Encode and decode Tina database entries.

Source code in python/src/tina_mgr/convert/formats/f_tina.py
class TinaFormatHandler(f_base.FormatHandler):
    """Encode and decode Tina database entries."""

    @classmethod
    def _do_dumps(cls, entries: list[db.TinaEntry]) -> str:
        """Format Tina database entries."""

        def dump_single(entry: db.TinaEntry, category: str | None) -> str:
            """Dump a single entry into a multiline string."""
            cat: Final = f"Category: {category}\n" if category is not None else ""
            return f"Item-ID: {entry.id}\nDescription: {entry.desc}\n{cat}"

        def dump_single_rec(entry: db.TinaEntry, category: str | None) -> list[str]:
            """Dump a single entry and all its descendants."""
            return [
                dump_single(entry, category),
                *itertools.chain(*(dump_single_rec(child, entry.id) for child in entry.children)),
            ]

        return "\n".join(itertools.chain(*(dump_single_rec(entry, None) for entry in entries)))

    @classmethod
    def _do_loads(cls, contents: str) -> list[db.TinaEntry]:
        """Parse Tina database entries."""
        return parse.loads(contents)

tina_mgr.convert.formats.f_yaml.YAMLFormatHandler

Bases: FormatHandler

Encode and decode Tina database entries in a hierarchical structure.

Source code in python/src/tina_mgr/convert/formats/f_yaml.py
class YAMLFormatHandler(f_base.FormatHandler):
    """Encode and decode Tina database entries in a hierarchical structure."""

    @classmethod
    def _do_dumps(cls, entries: list[db.TinaEntry]) -> str:
        """Format Tina database entries into the YAML structure."""
        return yaml.safe_dump(f_base.serialize(entries))

    @classmethod
    def _do_loads(cls, contents: str) -> list[db.TinaEntry]:
        """Parse the YAML structure into database entries."""
        try:
            top: Final = yaml.safe_load(contents)
        except yaml.YAMLError as err:
            raise YAMLDeserializationError(err) from err

        return f_base.deserialize(top)