I’ve made a big code updade. Now I can automatically create for a new person:
- attach existing notes
- attach existing citations
- attach existing objects
- attach existing attributes
- attach existing tags
- attach existing events
- create and attach new events with dates, places, citations
It works as expected. It really helps in fast adding Census events for new people.
And also it was my dream:
- auto-create empty Birth and Death events for people who is dead
- auto-create empty Birth event for people who is not dead
from gramps.gen.lib import Person, Event, EventType
from gramps.gen.config import config
from gramps.gen.lib.mediaref import MediaRef
from gramps.gen.lib import Attribute
from gramps.gen.lib.eventref import EventRef
from gramps.gen.db import DbTxn
from gramps.gen.lib.date import Date
import json
CONFIG_PATH = "/home/yurii/.local/share/gramps/gramps52/config.json"
# An existing function which adding a new person
# There are multiple similar functions in Gramps code in different places
def add(self, *obj):
...
person = Person()
...
# add this one row right before EditPerson
self.add_custom_entities(person, CONFIG_PATH)
try:
EditPerson(self.dbstate, self.uistate, [], person)
except WindowActiveError:
pass
def add_custom_entities(self, person, config_path):
"""
Add custom entities to the person based on the configuration in the JSON file.
"""
config = self.load_config(config_path)
if config is None:
print(f"Exiting method: Configuration file at {config_path} is invalid.")
return
# Adding notes to the person
for note_id in config.get("notes", []):
note = self.dbstate.db.get_note_from_gramps_id(note_id)
if note is not None:
note_handle = note.get_handle()
person.add_note(note_handle)
# Adding citations to the person
for citation_id in config.get("citations", []):
citation = self.dbstate.db.get_citation_from_gramps_id(citation_id)
if citation is not None:
citation_handle = citation.get_handle()
person.add_citation(citation_handle)
# Adding media to the person
for media_id in config.get("medias", []):
media = self.dbstate.db.get_media_from_gramps_id(media_id)
if media is not None:
media_handle = media.get_handle()
media_ref = MediaRef()
media_ref.set_reference_handle(media_handle)
person.add_media_reference(media_ref)
# Adding attributes to the person
for attr in config.get("attributes", []):
attribute = Attribute()
attribute.set_type(attr["key"])
attribute.set_value(attr["value"])
person.add_attribute(attribute)
# Adding tags to the person
for tag_name in config.get("tags", []):
tag_handle = None
for handle in self.dbstate.db.get_tag_handles():
tag = self.dbstate.db.get_tag_from_handle(handle)
if tag.get_name() == tag_name:
tag_handle = handle
break
if tag_handle:
person.add_tag(tag_handle)
# Adding events to the person
with DbTxn("Add events", self.dbstate.db) as trans:
for event_config in config.get("events", []):
if "gramps_id" in event_config:
event = self.dbstate.db.get_event_from_gramps_id(event_config["gramps_id"])
if event is None:
print(f"Event with Gramps ID {event_config['gramps_id']} not found.")
continue
else:
event = Event()
# Set event type
event_type_str = event_config["type"].upper()
event_type = getattr(EventType, event_type_str, EventType.CUSTOM)
event.set_type((event_type, event_type_str.capitalize()))
# Set event place (optional)
if "place" in event_config:
place_handle = self.dbstate.db.get_place_from_gramps_id(event_config["place"]).get_handle()
if place_handle:
event.set_place_handle(place_handle)
# Set event date (optional)
if "date" in event_config and event_config["date"]:
event_date = Date()
quality = getattr(Date, event_config["date"]["quality"], Date.QUAL_NONE)
modifier = getattr(Date, event_config["date"]["modifier"], Date.MOD_NONE)
value = tuple(event_config["date"]["value"])
event_date.set(quality=quality, modifier=modifier, calendar=None, value=value, text=None, newyear=0)
event.set_date_object(event_date)
# Add citations to the event (optional)
for citation_id in event_config.get("citations", []):
citation = self.dbstate.db.get_citation_from_gramps_id(citation_id)
if citation is not None:
event.add_citation(citation.get_handle())
# Add the event to the person
event_handle = self.dbstate.db.add_event(event, trans)
event_ref = EventRef()
event_ref.set_reference_handle(event_handle)
person.add_event_ref(event_ref)
@staticmethod
def load_config(config_path=CONFIG_PATH):
"""
Load configuration from a JSON file. If the JSON is malformed or cannot be loaded,
log the error and return None.
"""
try:
with open(config_path, 'r') as file:
return json.load(file)
except (json.JSONDecodeError, FileNotFoundError) as e:
print(f"Error loading config file {config_path}: {e}")
return None
JSON config example:
{
"notes": ["N0001"],
"citations": ["C4574"],
"medias": ["O2698"],
"attributes": [{"key": "Caste", "value": "Imperator"}],
"tags": ["Pr(direction)","Rs(direction)"],
"events": [
{
"type": "Birth",
"date": {
"quality": "QUAL_CALCULATED",
"modifier": "MOD_ABOUT",
"value": [0, 0, 2222, false]
},
"citations": ["C4575"]
},
{"gramps_id": "E0437"},
{
"type": "Death",
"date": {
"quality": "QUAL_ESTIMATED",
"modifier": "MOD_RANGE",
"value": [0, 0, 1851, false, 0, 0, 2222, false]
},
"citations": ["C4575"]
},
{
"type": "Residence",
"place": "P0090",
"date": {
"quality": "QUAL_NONE",
"modifier": "MOD_NONE",
"value": [0, 0, 1851, false]
},
"citations": ["C4575"]
}
]
}