Supertool script to separate Residence events

I decided try Supertool to fix some inaccuracy in my DB. I want separate my Residence events which have multiple citations. Each Residence event should have only 1 citation. And Residence date should be the same as citation date. Places for separate Residences should be the same as for common one.
I’ve already found all needed people, events and citations. But I need some help:

  • How create new events and attach citations inside by hande?
  • How detach citation from event by hande?
  • How detach event from person by hande?
[Gramps SuperTool script file]
version=1

[title]
check

[description]

[category]
People

[initial_statements]
count = 0

[statements]
for event in events:
    if event.type != 'Residence':
        continue
    if len(event.citations)<2:
        continue

    for citation in event.citations:
        
        personId = gramps_id
        personHandle = handle
        eventId = event.gramps_id
        eventHandle = event.handle
        citationId = citation.gramps_id
        citationHandle = citation.handle
        citationExactDate = citation.date
        
        count = count + 1
        print(count, ").", "Person:", personId, personHandle, "Event:", eventId, eventHandle,  "Citation:", citationId, citationHandle, citationExactDate)
        
[filter]

[expressions]

[scope]
all

[unwind_lists]
True

[commit_changes]
False

[summary_only]
False

Current log:

2527 ). Person: I9463 f98967fa74e6af07f6257180d6a Event: E23975 f98968615ab118d927c9876a825 Citation: C3494 f9998d07f8493ac3961f20d542 1894-12-16
2528 ). Person: I9466 f9897786bbd1bad4160e9e5513 Event: E23992 f98977f254f5d03947abb754eac Citation: C3461 f9897674b14481ef094e32fa36c 1890-12-28
2529 ). Person: I9466 f9897786bbd1bad4160e9e5513 Event: E23992 f98977f254f5d03947abb754eac Citation: C3508 f99db86de9f6c45fd4a2faa47cc 1886-09-18
2530 ). Person: I9466 f9897786bbd1bad4160e9e5513 Event: E23992 f98977f254f5d03947abb754eac Citation: C3539 f9a66e209c22a80c9e9825ccaf 1888-07-11
2531 ). Person: I9467 f98977ff38f344ab75c7f24e572 Event: E23995 f989786ed726d76090779790a9b Citation: C3461 f9897674b14481ef094e32fa36c 1890-12-28
2532 ). Person: I9467 f98977ff38f344ab75c7f24e572 Event: E23995 f989786ed726d76090779790a9b Citation: C3508 f99db86de9f6c45fd4a2faa47cc 1886-09-18
2533 ). Person: I9467 f98977ff38f344ab75c7f24e572 Event: E23995 f989786ed726d76090779790a9b Citation: C3539 f9a66e209c22a80c9e9825ccaf 1888-07-11
2534 ). Person: I9499 f98ce7502d7446c4e19f458b61 Event: E24100 f98ce890ce41f0cb78096d68d3 Citation: C3473 f98ce58b3a466e83dce18934133 1892-08-17
2535 ). Person: I9499 f98ce7502d7446c4e19f458b61 Event: E24100 f98ce890ce41f0cb78096d68d3 Citation: C3550 f9a98817d627a433779ec2b1891 1889-10-25
2536 ). Person: I9519 f990e777a9e212f44761a07dd08 Event: E24190 f990e803197dcf406148a03750 Citation: C3487 f990e68ca8661d4a50e54d4b60 1894-01-16
2537 ). Person: I9519 f990e777a9e212f44761a07dd08 Event: E24190 f990e803197dcf406148a03750 Citation: C3499 f99c9f9fc7a285959d1b81585da 1884-10-08
2538 ). Person: I9519 f990e777a9e212f44761a07dd08 Event: E24190 f990e803197dcf406148a03750 Citation: C3543 f9a78ec869ca4e912aa2e1e719 1888-10-09
2539 ). Person: I9537 f99adc478142bf010fd92953c64 Event: E24263 f99adcbb93210cd4e80f2bf042d Citation: C3498 f99adb22b265fb641d4a383410f 1884-09-14
2540 ). Person: I9537 f99adc478142bf010fd92953c64 Event: E24263 f99adcbb93210cd4e80f2bf042d Citation: C3537 f9a661944cb40f4d75e08763f36 1888-04-04
2541 ). Person: I9537 f99adc478142bf010fd92953c64 Event: E24263 f99adcbb93210cd4e80f2bf042d Citation: C3556 f9aca75f35a6be0c0f73740210d 1879-01-16
2542 ). Person: I9571 f9a330bdf7d7d30b1efaf7c71f6 Event: E24395 f9a3313823a5a782c29ec369fa Citation: C3516 f9a32ccc07d21b06d018e12ef09 1943-08-08
2543 ). Person: I9571 f9a330bdf7d7d30b1efaf7c71f6 Event: E24395 f9a3313823a5a782c29ec369fa Citation: C3518 f9a33d84c1940d0a95c877ecd33 1943-09-11
2544 ). Person: I9571 f9a330bdf7d7d30b1efaf7c71f6 Event: E24395 f9a3313823a5a782c29ec369fa Citation: C3519 f9a34af740d7dc8d33393a829ed 1943-09-10
2545 ). Person: I9584 f9a3fa0d75a4d0f793fcb20d669 Event: E24444 f9a3fa77c38485f90e90d73f42 Citation: C3526 f9a3f72f4aa68bd9c8f9e14122f 1887-09-20
2546 ). Person: I9584 f9a3fa0d75a4d0f793fcb20d669 Event: E24444 f9a3fa77c38485f90e90d73f42 Citation: C3549 f9a983d60dd49b7c4902d39b67a 1889-10-04
2547 ). Person: I9585 f9a3fabda463303200f020087c6 Event: E24447 f9a3fb5c64d683ccfd8f97ff80a Citation: C3526 f9a3f72f4aa68bd9c8f9e14122f 1887-09-20
2548 ). Person: I9585 f9a3fabda463303200f020087c6 Event: E24447 f9a3fb5c64d683ccfd8f97ff80a Citation: C3549 f9a983d60dd49b7c4902d39b67a 1889-10-04

Could anybody review my first code in Python pls? )))
I did not run it yet, only has written it in text editor according to Gramps 5.1 documentation The gramps.gen.lib Module — Gramps 5.1.0 documentation

[Gramps SuperTool script file]
version=1

[title]
check

[description]

[category]
People

[initial_statements]
count = 0

[statements]

personId = gramps_id
personHandle = handle
personEventRefList = person.get_event_ref_list()

for commonEvent in events:
    if commonEvent.type != 'Residence':
        continue
    if len(commonEvent.citations)<2:
        continue

    commonEventId = commonEvent.gramps_id
    commonEventHandle = commonEvent.handle
    commonEventPlaceHandle = commonEvent.get_place_handle()

    for citation in commonEvent.citations:
        
        citationId = citation.gramps_id
        citationHandle = citation.handle
        citationDateObject = citation.get_date_object()

        newEvent = Event()
        newEvent.set_date_object(citationDateObject)
        newEvent.set_type("Residence")
        newEventCitationList = newEvent.get_citation_list()
        newEventCitationList.append(citation)
        newEvent.set_citation_list(newEventCitationList)
        newEvent.set_place_handle(commonEventPlaceHandle)
        newEventRef = EventRef()
        newEventRef.set_role("Primary")
        commonEvent.remove_citation_references([citationHandle])

        personEventRefList.append(newEventRef)
        person.set_event_ref_list(personEventRefList)

        count = count + 1
        print(count, ").", "Person:", personId, personHandle, "Event:", eventId, eventHandle,  "Citation:", citationId, citationHandle, citationExactDate)

    transaction = db.transaction_begin("Removing event")
    db.remove_event(commonEventHandle, transaction)
    db.transaction_commit(transaction, "Event removed successfully")

[filter]

[expressions]

[scope]
all

[unwind_lists]
True

[commit_changes]
False

[summary_only]
False

and also I dont understand how attach newEvent with newEventRef. I dont see any methods for this.

Updated: also event deleting added.

I hope this is a working version:

[Gramps SuperTool script file]
version=1

[title]
check

[description]

[category]
People

[initial_statements]
count = 0

[statements]

personId = gramps_id
personHandle = handle
personEventRefList = []

for commonEvent in events:
    if commonEvent.type != 'Residence':
        continue
    if len(commonEvent.citations)<2:
        continue

    commonEventId = commonEvent.gramps_id
    commonEventHandle = commonEvent.handle
    commonEventPlaceHandle = commonEvent.obj.get_place_handle()

    for citation in commonEvent.citations:
        
        citationId = citation.gramps_id
        citationHandle = citation.handle
        citationDateObject = citation.obj.get_date_object()

        newEvent = Event()
        newEvent.set_date_object(citationDateObject)
        newEvent.set_type("Residence")
        newEventCitationList = newEvent.get_citation_list()
        newEventCitationList.append(citation.handle)
        newEvent.set_citation_list(newEventCitationList)
        newEvent.set_place_handle(commonEventPlaceHandle)
        db.add_event(newEvent, trans)
        newEventRef = EventRef()
        newEventRef.set_reference_handle(newEvent.handle)
        newEventRef.set_role("Primary")
        commonEvent.obj.remove_citation_references([citationHandle])

        personEventRefList.append(newEventRef)
        person.set_event_ref_list(personEventRefList)

        count = count + 1
        print(count, ").", "Person:", personId, personHandle, "Event:", commonEventId, commonEventHandle,  "Citation:", citationId, citationHandle, citationDateObject)

    db.remove_event(commonEventHandle, trans)
    
[filter]

[expressions]

[scope]
all

[unwind_lists]
False

[commit_changes]
True

[summary_only]
False
2 Likes

Here is the final working script which separates one Residence event to multiple according to citations attached.

[Gramps SuperTool script file]
version=1

[title]
Separate Residence

[description]
# This script processes all individuals in the database and splits Residence events with multiple citations 
# into separate events, each linked to a single citation. This ensures that each Residence event only has 
# one citation attached to it.

[category]
People

[initial_statements]

[statements]
# Loop through all people in the database
personId = gramps_id
personHandle = handle
personEventRefList = []

# Loop through all events associated with the person
for commonEvent in events:
    # Only process Residence events
    if commonEvent.type != 'Residence':
        continue
    # Skip events that do not have multiple citations
    if len(commonEvent.citations) < 2:
        continue

    # Get common event details
    commonEventId = commonEvent.gramps_id
    commonEventHandle = commonEvent.handle
    commonEventObj = commonEvent.obj
    commonEventPlaceHandle = commonEventObj.get_place_handle()

    # Loop through each citation in the common event
    for citation in commonEvent.citations:

        citationId = citation.gramps_id
        citationHandle = citation.handle
        citationObj = citation.obj
        citationDateObject = citationObj.get_date_object()

        # Create a new Residence event with the citation
        newEvent = Event()
        newEvent.set_date_object(citationDateObject)
        newEvent.set_type("Residence")
        newEventCitationList = newEvent.get_citation_list()
        newEventCitationList.append(citationHandle)
        newEvent.set_citation_list(newEventCitationList)
        newEvent.set_place_handle(commonEventPlaceHandle)

        # Add the new event to the database
        db.add_event(newEvent, trans)
        newEventRef = EventRef()
        newEventRef.set_reference_handle(newEvent.handle)
        newEventRef.set_role("Primary")
        # Remove the citation from the common event
        commonEventObj.remove_citation_references([citationHandle])

        # Add the new event reference to the person's event list
        personEventRefList.append(newEventRef)
        person.set_event_ref_list(personEventRefList)

        print(f"Person {personId} events updated successfully")

    # Remove the original common event from the database
    db.remove_event(commonEventHandle, trans)

[filter]

[expressions]

[scope]
all

[unwind_lists]
True

[commit_changes]
True

[summary_only]
False
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.