Skip to content

Commit

Permalink
Refactor presence roles into plugins (#1698)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartfeenstra authored Jul 11, 2024
1 parent d806f24 commit 470b933
Show file tree
Hide file tree
Showing 23 changed files with 235 additions and 213 deletions.
3 changes: 2 additions & 1 deletion betty/deriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from typing_extensions import override

from betty.locale import DateRange, Date, Localizer
from betty.model.ancestry import Person, Presence, Event, Subject, Ancestry
from betty.model.ancestry import Person, Presence, Event, Ancestry
from betty.model.presence_role import Subject
from betty.model.event_type import (
DerivableEventType,
CreatableDerivableEventType,
Expand Down
2 changes: 1 addition & 1 deletion betty/extension/cotton_candy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@
Person,
Presence,
is_public,
Subject,
HasFiles,
File,
Place,
)
from betty.model.presence_role import Subject
from betty.model.event_type import StartOfLifeEventType, EndOfLifeEventType
from betty.os import link_or_copy
from betty.project import EntityReferenceSequence, EntityReference
Expand Down
2 changes: 1 addition & 1 deletion betty/extension/demo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
PlaceName,
Person,
Presence,
Subject,
PersonName,
Link,
Source,
Expand All @@ -32,6 +31,7 @@
Enclosure,
Note,
)
from betty.model.presence_role import Subject
from betty.model.event_type import Marriage, Birth, Death
from betty.project import (
LocaleConfiguration,
Expand Down
14 changes: 8 additions & 6 deletions betty/gramps/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
Event,
Person,
PersonName,
Subject,
Witness,
Beneficiary,
Attendee,
Presence,
PlaceName,
Enclosure,
Expand All @@ -47,10 +43,16 @@
HasFiles,
HasCitations,
HasPrivacy,
HasNotes,
)
from betty.model.presence_role import (
Subject,
Witness,
Beneficiary,
Attendee,
Speaker,
Celebrant,
Organizer,
HasNotes,
)
from betty.model.event_type import (
Birth,
Expand Down Expand Up @@ -642,7 +644,7 @@ def _load_eventref(self, person_id: str, eventref: ElementTree.Element) -> None:
person_id=person_id,
event_handle=event_handle,
gramps_presence_role=gramps_presence_role,
betty_presence_role=role.label,
betty_presence_role=role.plugin_label(),
)
)

Expand Down
3 changes: 1 addition & 2 deletions betty/jinja2/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
from betty.model.ancestry import (
HasLinks,
HasFiles,
Subject,
Witness,
is_private,
is_public,
Event,
)
from betty.model.presence_role import Subject, Witness
from betty.model.event_type import StartOfLifeEventType, EndOfLifeEventType

if TYPE_CHECKING:
Expand Down
172 changes: 3 additions & 169 deletions betty/model/ancestry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from __future__ import annotations

from abc import ABC, abstractmethod
from contextlib import suppress
from enum import Enum
from reprlib import recursive_repr
Expand Down Expand Up @@ -37,6 +36,7 @@
GeneratedEntityId,
)
from betty.model.event_type import EventType, UnknownEventType
from betty.model.presence_role import PresenceRole, ref_role, Subject
from betty.serde.dump import DictDump, Dump, dump_default
from betty.string import camel_case_to_kebab_case

Expand Down Expand Up @@ -1524,172 +1524,6 @@ async def linked_data_schema(cls, project: Project) -> DictDump[Dump]:
return schema


class PresenceRole(ABC):
"""
A person's role at an event.
"""

@classmethod
@abstractmethod
def name(cls) -> str:
"""
The machine name.
"""
pass

@property
@abstractmethod
def label(self) -> Localizable:
"""
The human-readable label.
"""
pass


def ref_role(root_schema: DictDump[Dump]) -> DictDump[Dump]:
"""
Reference the PresenceRole schema.
"""
definitions = dump_default(root_schema, "definitions", dict)
if "role" not in definitions:
definitions["role"] = {
"type": "string",
"description": "A person's role in an event.",
}
return {
"$ref": "#/definitions/role",
}


@final
class Subject(PresenceRole):
"""
Someone was the subject of the event.
The meaning of this role depends on the event type. For example, for :py:class:`betty.model.event_type.Marriage`,
the subjects are the people who got married. For :py:class:`betty.model.event_type.Death` it is the person who
died.
"""

@override
@classmethod
def name(cls) -> str:
return "subject"

@override
@property
def label(self) -> Localizable:
return _("Subject") # pragma: no cover


@final
class Witness(PresenceRole):
"""
Someone `witnessed <https://en.wikipedia.org/wiki/Witness>`_ the event.
"""

@override
@classmethod
def name(cls) -> str:
return "witness" # pragma: no cover

@override
@property
def label(self) -> Localizable:
return _("Witness") # pragma: no cover


@final
class Beneficiary(PresenceRole):
"""
Someone was a `benificiary <https://en.wikipedia.org/wiki/Beneficiary>`_ in the event, such as a :py:class:`betty.model.event_type.Will`.
"""

@override
@classmethod
def name(cls) -> str:
return "beneficiary" # pragma: no cover

@override
@property
def label(self) -> Localizable:
return _("Beneficiary") # pragma: no cover


@final
class Attendee(PresenceRole):
"""
Someone attended the event (further details unknown).
"""

@override
@classmethod
def name(cls) -> str:
return "attendee" # pragma: no cover

@override
@property
def label(self) -> Localizable:
return _("Attendee") # pragma: no cover


@final
class Speaker(PresenceRole):
"""
Someone performed public speaking at the event.
"""

@override
@classmethod
def name(cls) -> str:
return "speaker" # pragma: no cover

@override
@property
def label(self) -> Localizable:
return _("Speaker") # pragma: no cover


@final
class Celebrant(PresenceRole):
"""
Someone was the `celebrant <https://en.wikipedia.org/wiki/Officiant>`_ at the event.
This includes but is not limited to:
- civil servant
- religious leader
- civilian
"""

@override
@classmethod
def name(cls) -> str:
return "celebrant" # pragma: no cover

@override
@property
def label(self) -> Localizable:
return _("Celebrant") # pragma: no cover


@final
class Organizer(PresenceRole):
"""
Someone organized the event.
"""

@override
@classmethod
def name(cls) -> str:
return "organizer" # pragma: no cover

@override
@property
def label(self) -> Localizable:
return _("Organizer") # pragma: no cover


@final
@many_to_one_to_many(
"betty.model.ancestry:Person",
Expand Down Expand Up @@ -1920,7 +1754,7 @@ def _dump_event_presence(
),
}
if presence.public:
dump["role"] = presence.role.name()
dump["role"] = presence.role.plugin_id()
return dump

@override
Expand Down Expand Up @@ -2341,7 +2175,7 @@ def _dump_person_presence(
}
dump_context(dump, event="performerIn")
if presence.public:
dump["role"] = presence.role.name()
dump["role"] = presence.role.plugin_id()
return dump

@override
Expand Down
Loading

0 comments on commit 470b933

Please sign in to comment.