Refactors Individual class for parser access

Simplifies Individual class initialization by directly passing the parser instance instead of the GedcomParser object.
This change streamlines the code and avoids unnecessary object attribute access.
Also, it ensures name formatting preserves the original capitalization.
This commit is contained in:
AlexKucera committed 2025-11-02 17:25:06 +01:00
1 parent 46502f7d66
commit 759b966598
5 files changed
+62 -73

No files matched your search

+16 -27
View File
@@ -24,18 +24,18 @@ class Individual:
images, and notes.
"""
def __init__(self, element: IndividualElement, gedcom_parser):
def __init__(self, element: IndividualElement, parser):
"""
Create an Individual wrapper around a GEDCOM individual element and its parser.
Create an Individual wrapper around a GEDCOM individual element and parser.
Stores the provided IndividualElement and the parser reference (gedcom_parser.parser) used to resolve cross-references.
Stores the provided IndividualElement and parser used to resolve cross-references.
Parameters:
element (IndividualElement): The GEDCOM individual element to wrap.
gedcom_parser: The GedcomParser instance whose `parser` attribute will be used for resolving references.
parser: The parser instance (e.g., gedcom.parser.Parser) used for resolving references.
"""
self.element = element
self.gedcom = gedcom_parser.parser
self.gedcom = parser
def get_id(self) -> str:
"""
@@ -61,20 +61,20 @@ class Individual:
Return the individual's full name formatted as "First Last".
Returns:
Full name string in title case (e.g., "John Doe"); empty string if no name parts exist.
Full name string preserving original capitalization; empty string if no name parts exist.
"""
first, last = self.get_names()
return f"{first} {last}".strip().title()
return f"{first} {last}".strip()
def get_file_name(self) -> str:
"""
Build a filename-like string for the individual in the form "FamilyName FirstName" optionally followed by the birth year.
Returns:
filename (str): The generated filename string, either "FamilyName FirstName BirthYear" or "FamilyName FirstName" when the birth year is not available; does not include a file extension.
filename (str): The generated filename string, either "FamilyName FirstName BirthYear" or "FamilyName FirstName" when the birth year is not available; does not include a file extension; preserves original name capitalization.
"""
first, last = self.get_names()
name_part = f"{last} {first}".strip().title()
name_part = f"{last} {first}".strip()
birth_year = self.element.get_birth_year()
if birth_year != -1:
@@ -91,7 +91,7 @@ class Individual:
- 'place' (str): Birth place string or '' if unavailable.
- 'year' (str): Birth year as a string or '' if the year is unknown.
"""
date, place, sources = self.element.get_birth_data()
date, place, _sources = self.element.get_birth_data()
year = self.element.get_birth_year()
return {
@@ -109,7 +109,7 @@ class Individual:
- date (str): Death date as a string, or '' if unknown.
- place (str): Death place as a string, or '' if unknown.
"""
date, place, sources = self.element.get_death_data()
date, place, _sources = self.element.get_death_data()
return {"date": date or "", "place": place or ""}
@@ -130,10 +130,7 @@ class Individual:
A list of Individual objects representing the person's parents.
"""
parent_elements = self.gedcom.get_parents(self.element)
return [
Individual(p, type("", (), {"parser": self.gedcom})())
for p in parent_elements
]
return [Individual(p, self.gedcom) for p in parent_elements]
def get_children(self) -> List["Individual"]:
"""
@@ -148,9 +145,7 @@ class Individual:
family, gedcom.tags.GEDCOM_TAG_CHILD
)
for child in child_elements:
children.append(
Individual(child, type("", (), {"parser": self.gedcom})())
)
children.append(Individual(child, self.gedcom))
return children
def get_partners(self) -> List["Individual"]:
@@ -168,9 +163,7 @@ class Individual:
for parent in parent_elements:
# Don't include self
if parent.get_pointer() != self.element.get_pointer():
partners.append(
Individual(parent, type("", (), {"parser": self.gedcom})())
)
partners.append(Individual(parent, self.gedcom))
return partners
def get_families(self) -> List[Dict]:
@@ -190,9 +183,7 @@ class Individual:
partners = []
for parent in self.gedcom.get_family_members(family, "PARENTS"):
if parent.get_pointer() != self.element.get_pointer():
partners.append(
Individual(parent, type("", (), {"parser": self.gedcom})())
)
partners.append(Individual(parent, self.gedcom))
# Get marriage info
marriage_date = ""
@@ -210,9 +201,7 @@ class Individual:
for child in self.gedcom.get_family_members(
family, gedcom.tags.GEDCOM_TAG_CHILD
):
children.append(
Individual(child, type("", (), {"parser": self.gedcom})())
)
children.append(Individual(child, self.gedcom))
families.append(
{
+1 -1
View File
@@ -149,7 +149,7 @@ def convert_gedcom_to_markdown(
return 1
# Wrap individuals in our data model
individuals = [Individual(elem, parser) for elem in individual_elements]
individuals = [Individual(elem, parser.parser) for elem in individual_elements]
# Generate markdown notes
logger.info(f"Generating markdown notes in: {people_dir}")
+10 -10
View File
@@ -40,7 +40,7 @@ class TestIndexGeneration:
"""Get individuals from sample GEDCOM."""
parser = GedcomParser(sample_gedcom_file)
elements = parser.get_individuals()
return [Individual(elem, parser) for elem in elements]
return [Individual(elem, parser.parser) for elem in elements]
def test_generate_index(self, output_dir, individuals):
"""Test basic index generation."""
@@ -104,7 +104,7 @@ class TestAlphabeticalSorting:
parser = GedcomParser(temp_file)
elements = parser.get_individuals()
return [Individual(elem, parser) for elem in elements]
return [Individual(elem, parser.parser) for elem in elements]
def test_sorting_by_last_name(self, output_dir, mixed_individuals):
"""Test that individuals are sorted by last name."""
@@ -163,7 +163,7 @@ class TestLetterGrouping:
parser = GedcomParser(temp_file)
elements = parser.get_individuals()
return [Individual(elem, parser) for elem in elements]
return [Individual(elem, parser.parser) for elem in elements]
def test_letter_headers(self, output_dir, varied_individuals):
"""Test that letter headers are created for each initial."""
@@ -210,7 +210,7 @@ class TestLetterGrouping:
parser = GedcomParser(temp_file)
elements = parser.get_individuals()
individuals = [Individual(elem, parser) for elem in elements]
individuals = [Individual(elem, parser.parser) for elem in elements]
generator = IndexGenerator(output_dir)
index_path = generator.generate_index(individuals)
@@ -228,7 +228,7 @@ class TestWikiLinkFormatting:
"""Get individuals with birth/death dates."""
parser = GedcomParser(sample_gedcom_file)
elements = parser.get_individuals()
return [Individual(elem, parser) for elem in elements]
return [Individual(elem, parser.parser) for elem in elements]
def test_wiki_links_without_subdirs(self, output_dir, individuals_with_dates):
"""Test WikiLink format without subdirectories."""
@@ -257,7 +257,7 @@ class TestLifeSpanFormatting:
"""Get individuals with birth/death dates."""
parser = GedcomParser(sample_gedcom_file)
elements = parser.get_individuals()
return [Individual(elem, parser) for elem in elements]
return [Individual(elem, parser.parser) for elem in elements]
def test_life_span_with_both_dates(self, output_dir, individuals_with_dates):
"""Test life span when both birth and death dates are available."""
@@ -286,7 +286,7 @@ class TestLifeSpanFormatting:
parser = GedcomParser(temp_file)
elements = parser.get_individuals()
individuals = [Individual(elem, parser) for elem in elements]
individuals = [Individual(elem, parser.parser) for elem in elements]
generator = IndexGenerator(output_dir)
index_path = generator.generate_index(individuals)
@@ -311,7 +311,7 @@ class TestLifeSpanFormatting:
parser = GedcomParser(temp_file)
elements = parser.get_individuals()
individuals = [Individual(elem, parser) for elem in elements]
individuals = [Individual(elem, parser.parser) for elem in elements]
generator = IndexGenerator(output_dir)
index_path = generator.generate_index(individuals)
@@ -343,7 +343,7 @@ class TestLifeSpanFormatting:
parser = GedcomParser(temp_file)
elements = parser.get_individuals()
individuals = [Individual(elem, parser) for elem in elements]
individuals = [Individual(elem, parser.parser) for elem in elements]
generator = IndexGenerator(output_dir)
index_path = generator.generate_index(individuals)
@@ -361,7 +361,7 @@ class TestIndexStatistics:
"""Test that total individual count is displayed."""
parser = GedcomParser(sample_gedcom_file)
elements = parser.get_individuals()
individuals = [Individual(elem, parser) for elem in elements]
individuals = [Individual(elem, parser.parser) for elem in elements]
generator = IndexGenerator(output_dir)
index_path = generator.generate_index(individuals)
+21 -21
View File
@@ -26,7 +26,7 @@ class TestIndividualBasicInfo:
parser = GedcomParser(sample_gedcom_file)
individuals = parser.get_individuals()
john = [ind for ind in individuals if 'John' in str(ind.get_name())]
return Individual(john[0], parser)
return Individual(john[0], parser.parser)
def test_get_id(self, john_doe):
"""Test ID extraction without @ symbols."""
@@ -69,10 +69,10 @@ class TestIndividualBasicInfo:
parser = GedcomParser(temp_file)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser)
person = Individual(individuals[0], parser.parser)
filename = person.get_file_name()
assert 'Person Noyear' in filename
assert 'Person NoYear' in filename # Preserves original GEDCOM capitalization
# Should not contain a year
assert not any(char.isdigit() for char in filename)
@@ -96,7 +96,7 @@ class TestIndividualBasicInfo:
parser = GedcomParser(temp_file)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser)
person = Individual(individuals[0], parser.parser)
assert person.get_gender() == 'U'
@@ -110,7 +110,7 @@ class TestBirthAndDeath:
parser = GedcomParser(sample_gedcom_file)
individuals = parser.get_individuals()
john = [ind for ind in individuals if 'John' in str(ind.get_name())]
return Individual(john[0], parser)
return Individual(john[0], parser.parser)
def test_get_birth_info(self, john_doe):
"""Test birth information extraction."""
@@ -141,7 +141,7 @@ class TestBirthAndDeath:
parser = GedcomParser(temp_file)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser)
person = Individual(individuals[0], parser.parser)
birth = person.get_birth_info()
assert birth['date'] == ''
@@ -158,7 +158,7 @@ class TestEvents:
parser = GedcomParser(sample_gedcom_file)
individuals = parser.get_individuals()
john = [ind for ind in individuals if 'John' in str(ind.get_name())]
return Individual(john[0], parser)
return Individual(john[0], parser.parser)
def test_get_events(self, john_doe):
"""Test event extraction."""
@@ -202,7 +202,7 @@ class TestEvents:
parser = GedcomParser(temp_file)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser)
person = Individual(individuals[0], parser.parser)
events = person.get_events()
event_types = [e['type'] for e in events]
@@ -228,7 +228,7 @@ class TestFamilyRelationships:
individuals = parsed_gedcom.get_individuals()
# Alice (I3) should have parents John (I1) and Jane (I2)
alice = [ind for ind in individuals if 'Alice' in str(ind.get_name())]
alice_obj = Individual(alice[0], parsed_gedcom)
alice_obj = Individual(alice[0], parsed_gedcom.parser)
parents = alice_obj.get_parents()
assert len(parents) == 2
@@ -242,7 +242,7 @@ class TestFamilyRelationships:
individuals = parsed_gedcom.get_individuals()
# John (I1) should have child Alice (I3)
john = [ind for ind in individuals if 'John' in str(ind.get_name())]
john_obj = Individual(john[0], parsed_gedcom)
john_obj = Individual(john[0], parsed_gedcom.parser)
children = john_obj.get_children()
assert len(children) >= 1
@@ -255,7 +255,7 @@ class TestFamilyRelationships:
individuals = parsed_gedcom.get_individuals()
# John (I1) should have partner Jane (I2)
john = [ind for ind in individuals if 'John' in str(ind.get_name())]
john_obj = Individual(john[0], parsed_gedcom)
john_obj = Individual(john[0], parsed_gedcom.parser)
partners = john_obj.get_partners()
assert len(partners) >= 1
@@ -267,7 +267,7 @@ class TestFamilyRelationships:
"""Test family data extraction including marriage info."""
individuals = parsed_gedcom.get_individuals()
john = [ind for ind in individuals if 'John' in str(ind.get_name())]
john_obj = Individual(john[0], parsed_gedcom)
john_obj = Individual(john[0], parsed_gedcom.parser)
families = john_obj.get_families()
assert len(families) >= 1
@@ -288,7 +288,7 @@ class TestImagesAndMedia:
parser = GedcomParser(sample_gedcom_file)
individuals = parser.get_individuals()
john = [ind for ind in individuals if 'John' in str(ind.get_name())]
john_obj = Individual(john[0], parser)
john_obj = Individual(john[0], parser.parser)
images = john_obj.get_images()
assert len(images) >= 1
@@ -314,7 +314,7 @@ class TestImagesAndMedia:
parser = GedcomParser(temp_file)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser)
person = Individual(individuals[0], parser.parser)
images = person.get_images()
assert len(images) == 0
@@ -328,7 +328,7 @@ class TestNotes:
parser = GedcomParser(sample_gedcom_file)
individuals = parser.get_individuals()
john = [ind for ind in individuals if 'John' in str(ind.get_name())]
john_obj = Individual(john[0], parser)
john_obj = Individual(john[0], parser.parser)
notes = john_obj.get_notes()
assert len(notes) >= 1
@@ -353,7 +353,7 @@ class TestNotes:
parser = GedcomParser(temp_file)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser)
person = Individual(individuals[0], parser.parser)
notes = person.get_notes()
assert len(notes) >= 1
@@ -376,7 +376,7 @@ class TestNotes:
parser = GedcomParser(temp_file)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser)
person = Individual(individuals[0], parser.parser)
notes = person.get_notes()
assert len(notes) == 0
@@ -392,7 +392,7 @@ class TestStories:
parser = GedcomParser(temp_file)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser)
person = Individual(individuals[0], parser.parser)
stories = person.get_stories()
@@ -417,7 +417,7 @@ class TestStories:
parser = GedcomParser(temp_file)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser)
person = Individual(individuals[0], parser.parser)
stories = person.get_stories()
assert len(stories) == 0
@@ -433,7 +433,7 @@ class TestAttributes:
parser = GedcomParser(temp_file)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser)
person = Individual(individuals[0], parser.parser)
attrs = person.get_attributes()
assert attrs['eyes'] == 'Blue'
@@ -456,7 +456,7 @@ class TestAttributes:
parser = GedcomParser(temp_file)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser)
person = Individual(individuals[0], parser.parser)
attrs = person.get_attributes()
assert len(attrs) == 0
+14 -14
View File
@@ -67,7 +67,7 @@ class TestNoteGeneration:
parser = GedcomParser(sample_gedcom_file)
individuals = parser.get_individuals()
john = [ind for ind in individuals if 'John' in str(ind.get_name())]
return Individual(john[0], parser)
return Individual(john[0], parser.parser)
def test_generate_note(self, generator, john_doe):
"""Test basic note generation."""
@@ -93,7 +93,7 @@ class TestNoteGeneration:
"""Test generating notes for all individuals."""
parser = GedcomParser(sample_gedcom_file)
individual_elements = parser.get_individuals()
individuals = [Individual(elem, parser) for elem in individual_elements]
individuals = [Individual(elem, parser.parser) for elem in individual_elements]
paths = generator.generate_all(individuals)
@@ -116,7 +116,7 @@ class TestMetadataFormatting:
parser = GedcomParser(sample_gedcom_file)
individuals = parser.get_individuals()
john = [ind for ind in individuals if 'John' in str(ind.get_name())]
return Individual(john[0], parser)
return Individual(john[0], parser.parser)
def test_visible_metadata(self, generator, john_doe):
"""Test visible metadata format [key:: value]."""
@@ -162,7 +162,7 @@ class TestWikiLinks:
individual_elements = parser.get_individuals()
# Generate all notes first
individuals = [Individual(elem, parser) for elem in individual_elements]
individuals = [Individual(elem, parser.parser) for elem in individual_elements]
generator.generate_all(individuals)
# Check John's note for WikiLinks to Jane and Alice
@@ -178,7 +178,7 @@ class TestWikiLinks:
"""Test WikiLinks to parents."""
parser = GedcomParser(sample_gedcom_file)
individual_elements = parser.get_individuals()
individuals = [Individual(elem, parser) for elem in individual_elements]
individuals = [Individual(elem, parser.parser) for elem in individual_elements]
generator.generate_all(individuals)
@@ -206,7 +206,7 @@ class TestEventFormatting:
parser = GedcomParser(sample_gedcom_file)
individuals = parser.get_individuals()
john = [ind for ind in individuals if 'John' in str(ind.get_name())]
john_obj = Individual(john[0], parser)
john_obj = Individual(john[0], parser.parser)
file_path = generator.generate_note(john_obj)
content = file_path.read_text()
@@ -221,7 +221,7 @@ class TestEventFormatting:
parser = GedcomParser(sample_gedcom_file)
individuals = parser.get_individuals()
john = [ind for ind in individuals if 'John' in str(ind.get_name())]
john_obj = Individual(john[0], parser)
john_obj = Individual(john[0], parser.parser)
file_path = generator.generate_note(john_obj)
content = file_path.read_text()
@@ -246,7 +246,7 @@ class TestImageHandling:
parser = GedcomParser(sample_gedcom_file)
individuals = parser.get_individuals()
john = [ind for ind in individuals if 'John' in str(ind.get_name())]
john_obj = Individual(john[0], parser)
john_obj = Individual(john[0], parser.parser)
file_path = generator.generate_note(john_obj)
content = file_path.read_text()
@@ -262,7 +262,7 @@ class TestImageHandling:
parser = GedcomParser(sample_gedcom_file)
individuals = parser.get_individuals()
john = [ind for ind in individuals if 'John' in str(ind.get_name())]
john_obj = Individual(john[0], parser)
john_obj = Individual(john[0], parser.parser)
file_path = generator.generate_note(john_obj)
content = file_path.read_text()
@@ -293,7 +293,7 @@ class TestStoryGeneration:
parser = GedcomParser(temp_file)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser)
person = Individual(individuals[0], parser.parser)
# Generate note (should also generate story files if stories are found)
generator.generate_note(person)
@@ -309,7 +309,7 @@ class TestStoryGeneration:
parser = GedcomParser(temp_file)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser)
person = Individual(individuals[0], parser.parser)
file_path = generator.generate_note(person)
content = file_path.read_text()
@@ -326,7 +326,7 @@ class TestStoryGeneration:
parser = GedcomParser(temp_file)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser)
person = Individual(individuals[0], parser.parser)
# Generate twice
generator.generate_note(person)
@@ -358,7 +358,7 @@ class TestFamilyFormatting:
parser = GedcomParser(sample_gedcom_file)
individuals = parser.get_individuals()
john = [ind for ind in individuals if 'John' in str(ind.get_name())]
john_obj = Individual(john[0], parser)
john_obj = Individual(john[0], parser.parser)
file_path = generator.generate_note(john_obj)
content = file_path.read_text()
@@ -404,7 +404,7 @@ class TestFamilyFormatting:
parser = GedcomParser(temp_file)
individuals = parser.get_individuals()
person = Individual(individuals[0], parser)
person = Individual(individuals[0], parser.parser)
file_path = generator.generate_note(person)
content = file_path.read_text()