Fix bidirectional family references in demo family generator

- Add parentin references for fathers and mothers
- Add childof references for children
- Regenerate person XMLs with correct family relationships
- Fixes import errors about missing bidirectional references
This commit is contained in:
Daniel Viegas 2025-11-28 22:14:03 +01:00
parent da620972a6
commit b3488a29e8
2 changed files with 526 additions and 406 deletions

File diff suppressed because it is too large Load Diff

View File

@ -11,7 +11,8 @@ def gen_handle(prefix, num):
return f"_{prefix}{num:08d}" return f"_{prefix}{num:08d}"
# Generate a person # Generate a person
def gen_person(pid, first_name, surname, birth_year, death_year=None, gender="M"): def gen_person(pid, first_name, surname, birth_year, death_year=None, gender="M",
parentin_families=None, childof_families=None):
handle = gen_handle("PERSON", pid) handle = gen_handle("PERSON", pid)
birth_handle = gen_handle("EVENT", pid * 10) birth_handle = gen_handle("EVENT", pid * 10)
death_handle = gen_handle("EVENT", pid * 10 + 1) if death_year else None death_handle = gen_handle("EVENT", pid * 10 + 1) if death_year else None
@ -26,6 +27,16 @@ def gen_person(pid, first_name, surname, birth_year, death_year=None, gender="M"
""" """
if death_handle: if death_handle:
person_xml += f""" <eventref hlink="{death_handle}" role="Primary"/> person_xml += f""" <eventref hlink="{death_handle}" role="Primary"/>
"""
# Add parentin references (for fathers and mothers)
if parentin_families:
for family_handle in parentin_families:
person_xml += f""" <parentin hlink="{family_handle}"/>
"""
# Add childof references (for children)
if childof_families:
for family_handle in childof_families:
person_xml += f""" <childof hlink="{family_handle}"/>
""" """
person_xml += """ </person> person_xml += """ </person>
""" """
@ -110,12 +121,19 @@ def main():
# Father: John Smith, born 1950, died 2010 # Father: John Smith, born 1950, died 2010
father_id = 1 father_id = 1
father_handle = gen_handle("PERSON", father_id) father_handle = gen_handle("PERSON", father_id)
father_person, father_birth, father_death = gen_person(father_id, "John", "Smith", 1950, 2010, "M") main_family_handle = gen_handle("FAMILY", 1)
father_person, father_birth, father_death = gen_person(
father_id, "John", "Smith", 1950, 2010, "M",
parentin_families=[main_family_handle]
)
# Mother: Mary Smith, born 1952, died 2015 # Mother: Mary Smith, born 1952, died 2015
mother_id = 2 mother_id = 2
mother_handle = gen_handle("PERSON", mother_id) mother_handle = gen_handle("PERSON", mother_id)
mother_person, mother_birth, mother_death = gen_person(mother_id, "Mary", "Smith", 1952, 2015, "F") mother_person, mother_birth, mother_death = gen_person(
mother_id, "Mary", "Smith", 1952, 2015, "F",
parentin_families=[main_family_handle]
)
# Generate 15 children # Generate 15 children
children = [] children = []
@ -130,7 +148,10 @@ def main():
death_year = birth_year + random.randint(60, 90) if random.random() < 0.3 else None # 30% chance of death death_year = birth_year + random.randint(60, 90) if random.random() < 0.3 else None # 30% chance of death
child_handle = gen_handle("PERSON", child_id) child_handle = gen_handle("PERSON", child_id)
child_person, child_birth, child_death = gen_person(child_id, first_name, "Smith", birth_year, death_year, gender) child_person, child_birth, child_death = gen_person(
child_id, first_name, "Smith", birth_year, death_year, gender,
childof_families=[main_family_handle]
)
children.append(child_person) children.append(child_person)
child_handles.append(child_handle) child_handles.append(child_handle)
@ -143,6 +164,35 @@ def main():
family_id = 1 family_id = 1
family_xml, marriage_event = gen_family(family_id, father_handle, mother_handle, 1969, child_handles) family_xml, marriage_event = gen_family(family_id, father_handle, mother_handle, 1969, child_handles)
# Track person data for regeneration (needed for children who become parents)
import re
person_data = {}
# Store initial person data
person_data[father_id] = {"handle": father_handle, "name": "John", "surname": "Smith",
"birth": 1950, "death": 2010, "gender": "M",
"parentin": [main_family_handle], "childof": []}
person_data[mother_id] = {"handle": mother_handle, "name": "Mary", "surname": "Smith",
"birth": 1952, "death": 2015, "gender": "F",
"parentin": [main_family_handle], "childof": []}
for i, child_handle in enumerate(child_handles):
child_pid = 3 + i
gender = "M" if i % 2 == 0 else "F"
# Extract name from generated child XML
child_xml = children[i]
name_match = re.search(r'<first>([^<]+)</first>', child_xml)
first_name = name_match.group(1) if name_match else random.choice(male_names if gender == "M" else female_names)
birth_year = 1970 + (i * 2)
# Extract death year from child_events if it exists
death_year = None
for event in child_events:
if f"id=\"E{child_pid*10+1:04d}\"" in event:
match = re.search(r'val="(\d{4})', event)
if match:
death_year = int(match.group(1))
person_data[child_pid] = {"handle": child_handle, "name": first_name, "surname": "Smith",
"birth": birth_year, "death": death_year, "gender": gender,
"parentin": [], "childof": [main_family_handle]}
# Generate grandchildren (children of first 5 children) # Generate grandchildren (children of first 5 children)
grandchildren = [] grandchildren = []
grandchild_events = [] grandchild_events = []
@ -150,6 +200,7 @@ def main():
for i in range(5): # First 5 children have children for i in range(5): # First 5 children have children
parent_handle = child_handles[i] parent_handle = child_handles[i]
parent_pid = 3 + i
parent_gender = "M" if i % 2 == 0 else "F" parent_gender = "M" if i % 2 == 0 else "F"
spouse_gender = "F" if parent_gender == "M" else "M" spouse_gender = "F" if parent_gender == "M" else "M"
@ -157,8 +208,15 @@ def main():
spouse_name = random.choice(female_names if spouse_gender == "F" else male_names) spouse_name = random.choice(female_names if spouse_gender == "F" else male_names)
spouse_birth = 1970 + (i * 2) + random.randint(-2, 2) spouse_birth = 1970 + (i * 2) + random.randint(-2, 2)
spouse_handle = gen_handle("PERSON", grandchild_id) spouse_handle = gen_handle("PERSON", grandchild_id)
child_family_handle = gen_handle("FAMILY", family_id + 1)
person_data[grandchild_id] = {"handle": spouse_handle, "name": spouse_name, "surname": "Smith",
"birth": spouse_birth, "death": None, "gender": spouse_gender,
"parentin": [child_family_handle], "childof": []}
spouse_person, spouse_birth_event, spouse_death_event = gen_person( spouse_person, spouse_birth_event, spouse_death_event = gen_person(
grandchild_id, spouse_name, "Smith", spouse_birth, None, spouse_gender grandchild_id, spouse_name, "Smith", spouse_birth, None, spouse_gender,
parentin_families=[child_family_handle]
) )
grandchildren.append(spouse_person) grandchildren.append(spouse_person)
grandchild_events.append(spouse_birth_event) grandchild_events.append(spouse_birth_event)
@ -166,6 +224,9 @@ def main():
grandchild_events.append(spouse_death_event) grandchild_events.append(spouse_death_event)
grandchild_id += 1 grandchild_id += 1
# Update parent to include parentin reference
person_data[parent_pid]["parentin"].append(child_family_handle)
# Create 3-5 children per couple # Create 3-5 children per couple
num_grandchildren = random.randint(3, 5) num_grandchildren = random.randint(3, 5)
grandchild_handles = [] grandchild_handles = []
@ -174,8 +235,14 @@ def main():
gchild_name = random.choice(male_names if gchild_gender == "M" else female_names) gchild_name = random.choice(male_names if gchild_gender == "M" else female_names)
gchild_birth = 1995 + (i * 3) + j gchild_birth = 1995 + (i * 3) + j
gchild_handle = gen_handle("PERSON", grandchild_id) gchild_handle = gen_handle("PERSON", grandchild_id)
person_data[grandchild_id] = {"handle": gchild_handle, "name": gchild_name, "surname": "Smith",
"birth": gchild_birth, "death": None, "gender": gchild_gender,
"parentin": [], "childof": [child_family_handle]}
gchild_person, gchild_birth_event, gchild_death_event = gen_person( gchild_person, gchild_birth_event, gchild_death_event = gen_person(
grandchild_id, gchild_name, "Smith", gchild_birth, None, gchild_gender grandchild_id, gchild_name, "Smith", gchild_birth, None, gchild_gender,
childof_families=[child_family_handle]
) )
grandchildren.append(gchild_person) grandchildren.append(gchild_person)
grandchild_handles.append(gchild_handle) grandchild_handles.append(gchild_handle)
@ -190,6 +257,17 @@ def main():
family_xml += fam_xml family_xml += fam_xml
child_events.append(fam_marriage) child_events.append(fam_marriage)
# Regenerate children XMLs with updated family references
children = []
for i, child_handle in enumerate(child_handles):
child_pid = 3 + i
data = person_data[child_pid]
child_person, _, _ = gen_person(
child_pid, data["name"], data["surname"], data["birth"], data["death"], data["gender"],
parentin_families=data["parentin"], childof_families=data["childof"]
)
children.append(child_person)
# Write XML file # Write XML file
xml_content = f"""<?xml version="1.0" encoding="UTF-8"?> xml_content = f"""<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE database PUBLIC "-//Gramps//DTD Gramps XML 1.7.1//EN" <!DOCTYPE database PUBLIC "-//Gramps//DTD Gramps XML 1.7.1//EN"