Detects and converts macOS legacy CR-only line endings to standard Unix LF line endings for GEDCOM file parsing Improves file parsing robustness by automatically handling files with non-standard line endings from older macOS applications
117 lines
3.5 KiB
Python
117 lines
3.5 KiB
Python
"""
|
|
GEDCOM file parser module.
|
|
|
|
This module provides a clean interface to parse GEDCOM files and extract
|
|
individual and family data.
|
|
"""
|
|
|
|
from pathlib import Path
|
|
from typing import List, Optional
|
|
import logging
|
|
|
|
from gedcom.parser import Parser
|
|
from gedcom.element.individual import IndividualElement
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class GedcomParser:
|
|
"""
|
|
Parser for GEDCOM genealogy files.
|
|
|
|
This class wraps the python-gedcom library and provides a clean interface
|
|
for parsing GEDCOM files and extracting individual data.
|
|
"""
|
|
|
|
def __init__(self, file_path: Path):
|
|
"""
|
|
Initialize the parser with a GEDCOM file.
|
|
|
|
Args:
|
|
file_path: Path to the GEDCOM file to parse
|
|
|
|
Raises:
|
|
FileNotFoundError: If the GEDCOM file doesn't exist
|
|
ValueError: If the file cannot be parsed
|
|
"""
|
|
if not file_path.exists():
|
|
raise FileNotFoundError(f"GEDCOM file not found: {file_path}")
|
|
|
|
self.file_path = file_path
|
|
|
|
# Check and fix line endings if needed
|
|
self._fix_line_endings_if_needed()
|
|
|
|
self.parser = Parser()
|
|
|
|
try:
|
|
logger.info(f"Parsing GEDCOM file: {file_path}")
|
|
self.parser.parse_file(str(file_path))
|
|
except Exception as e:
|
|
raise ValueError(f"Failed to parse GEDCOM file: {e}") from e
|
|
|
|
def _fix_line_endings_if_needed(self):
|
|
"""
|
|
Check if GEDCOM file has CR-only line endings and fix them.
|
|
|
|
Some macOS applications export GEDCOM files with old Mac-style
|
|
CR-only line endings, which the parser cannot handle. This method
|
|
detects and fixes such files automatically.
|
|
"""
|
|
# Read first chunk to check line endings
|
|
with open(self.file_path, 'rb') as f:
|
|
sample = f.read(8192) # Read first 8KB
|
|
|
|
# Count different line ending types
|
|
has_crlf = b'\r\n' in sample
|
|
has_lf = b'\n' in sample
|
|
has_cr = b'\r' in sample
|
|
|
|
# If we have CR but no LF, this is a CR-only file
|
|
if has_cr and not has_lf and not has_crlf:
|
|
logger.warning(
|
|
"Detected old Mac-style (CR-only) line endings in GEDCOM file. "
|
|
"Converting to Unix-style (LF) line endings..."
|
|
)
|
|
|
|
# Read entire file and fix line endings
|
|
with open(self.file_path, 'rb') as f:
|
|
content = f.read()
|
|
|
|
# Replace CR with LF
|
|
fixed_content = content.replace(b'\r', b'\n')
|
|
|
|
# Write back to file
|
|
with open(self.file_path, 'wb') as f:
|
|
f.write(fixed_content)
|
|
|
|
logger.info("Line endings fixed successfully")
|
|
|
|
def get_individuals(self) -> List[IndividualElement]:
|
|
"""
|
|
Get all individuals from the GEDCOM file.
|
|
|
|
Returns:
|
|
List of IndividualElement objects representing people in the tree
|
|
"""
|
|
individuals = []
|
|
for element in self.parser.get_root_child_elements():
|
|
if isinstance(element, IndividualElement):
|
|
individuals.append(element)
|
|
|
|
logger.info(f"Found {len(individuals)} individuals")
|
|
return individuals
|
|
|
|
def get_element_by_pointer(self, pointer: str):
|
|
"""
|
|
Get a GEDCOM element by its pointer/ID.
|
|
|
|
Args:
|
|
pointer: The GEDCOM pointer (e.g., '@I123@')
|
|
|
|
Returns:
|
|
The element with the given pointer, or None if not found
|
|
"""
|
|
return self.parser.get_element_dictionary().get(pointer)
|