Skip to content

Python API: Parsing the native Tina database

tina_mgr.parse.ParseError dataclass

Bases: Error

An error that occurred during the parsing of the Tina database.

Source code in python/src/tina_mgr/parse.py
@dataclasses.dataclass
class ParseError(defs.Error):
    """An error that occurred during the parsing of the Tina database."""

    element: str
    """The element that we tried to parse."""

    obj: Any
    """The object that `pyparsing` returned instead."""

    def __str__(self) -> str:
        """Provide a human-readable description of the error."""
        return f"Could not parse a {self.element} Tina element, got unexpected {self.obj!r}"

element: str instance-attribute

The element that we tried to parse.

obj: Any instance-attribute

The object that pyparsing returned instead.

__str__()

Provide a human-readable description of the error.

Source code in python/src/tina_mgr/parse.py
def __str__(self) -> str:
    """Provide a human-readable description of the error."""
    return f"Could not parse a {self.element} Tina element, got unexpected {self.obj!r}"

tina_mgr.parse.loads(contents)

Parse the contents of a Tina database file.

Source code in python/src/tina_mgr/parse.py
def loads(contents: str) -> list[db.TinaEntry]:
    """Parse the contents of a Tina database file."""
    raw_entries: Final = _p_entries_complete.parse_string(contents, parse_all=True).as_list()
    entries: Final = {
        entry.id: db.TinaEntry(id=entry.id, desc=entry.desc, children=[]) for entry in raw_entries
    }

    for entry in raw_entries:
        if entry.category is not None:
            entries[entry.category].children.append(entries[entry.id])

    res: Final = [entries[entry.id] for entry in raw_entries if entry.category is None]
    unreachable: Final = sorted(set(entries) - _get_reachable_entries(res))
    if unreachable:
        raise RuntimeError(repr(unreachable))

    check_for_duplicates(res)
    return res