Source code for betty.extension.gramps

"""
Integrate Betty with `Gramps <https://gramps-project.org>`_.
"""

from __future__ import annotations

from typing import TYPE_CHECKING
from typing import final

from typing_extensions import override

from betty.extension.gramps.config import GrampsConfiguration
from betty.gramps.loader import GrampsLoader
from betty.load import LoadAncestryEvent
from betty.locale.localizable import static, _, Localizable
from betty.project.extension import ConfigurableExtension

if TYPE_CHECKING:
    from betty.event_dispatcher import EventHandlerRegistry
    from betty.machine_name import MachineName


async def _load_ancestry(event: LoadAncestryEvent) -> None:
    project = event.project
    gramps_configuration = project.configuration.extensions[
        Gramps
    ].extension_configuration
    assert isinstance(gramps_configuration, GrampsConfiguration)
    for family_tree_configuration in gramps_configuration.family_trees:
        file_path = family_tree_configuration.file_path
        if file_path:
            await GrampsLoader(
                project.ancestry,
                attribute_prefix_key=project.configuration.name,
                factory=project.new,
                localizer=project.app.localizer,
                event_type_map={
                    event_type_configuration.gramps_event_type: await project.event_types.get(
                        event_type_configuration.event_type_id
                    )
                    for event_type_configuration in family_tree_configuration.event_types.values()
                },
            ).load_file(file_path)


[docs] @final class Gramps(ConfigurableExtension[GrampsConfiguration]): """ Integrate Betty with `Gramps <https://gramps-project.org>`_. """
[docs] @override @classmethod def plugin_id(cls) -> MachineName: return "gramps"
[docs] @override @classmethod def default_configuration(cls) -> GrampsConfiguration: return GrampsConfiguration()
[docs] @override def register_event_handlers(self, registry: EventHandlerRegistry) -> None: registry.add_handler(LoadAncestryEvent, _load_ancestry)
[docs] @override @classmethod def plugin_label(cls) -> Localizable: return static("Gramps")
[docs] @override @classmethod def plugin_description(cls) -> Localizable: return _('Load <a href="https://gramps-project.org/">Gramps</a> family trees.')