def test_instance_container_load():
    instances = get_gedcom_instance_container(
        os.path.join(os.path.dirname(__file__), 'gramps_sample.ged'))

    assert len(instances._data) == 2
    # autocreate
    i = instances[('i', '@I1@')]
    assert len(instances._data) == 3
    assert i.plain_name == 'Keith Lloyd Smith'

    f = instances[('f', '@F1@')]
    assert len(instances._data) == 4
    assert f.wife_individual_id == '@I25@'
    assert f.husband_individual_id == '@I27@'

    # asking does not instantiate
    assert not ('i', '@I2@') in instances

    # load all
    instances.instantiate_all(instances)
    assert len(instances._data) == 59

    assert ('i', '@I2@') in instances

    # get all individuals
    individuals = [
        instance for key, instance in instances.items() if key[0] == 'i'
    ]
    assert len(individuals) == 42
def test_instance_container_labels():
    instances = get_gedcom_instance_container(
        os.path.join(os.path.dirname(__file__), 'gramps_sample.ged'))

    # load all
    instances.instantiate_all(instances)
    assert len(instances._data) == 59

    # get all individuals
    individual_plain_names = [
        instance.plain_name for key, instance in instances.items()
        if key[0] == 'i'
    ]
    individual_birth_labels = [
        instance.birth_label for key, instance in instances.items()
        if key[0] == 'i'
    ]
    individual_death_labels = [
        instance.death_label for key, instance in instances.items()
        if key[0] == 'i'
    ]
    assert str(
        individual_plain_names[1:5]
    ) == "['Keith Lloyd Smith', 'Hans Peter Smith', 'Hanna Smith', 'Herman Julius Nielsen']"
    assert str(
        individual_birth_labels[1:5]
    ) == "['*\\xa011.08.1966', '*\\xa017.04.1904', '*\\xa029.01.1821', '*\\xa031.08.1889']"
    assert str(
        individual_death_labels[1:5]
    ) == "['', '†\\xa029.01.1977', '†\\xa0geschätzt\\xa01896', '†\\xa01945']"
def test_generate_svg_file_ancestor_and_children():
    max_generations = 20
    x_position = 0
    last_individual_pos = None
    chart = AncestorChart(instance_container=get_gedcom_instance_container(
        os.path.join(os.path.dirname(__file__), 'autogenerated.ged')))

    # select individual to show
    gedcom_data = open(
        os.path.join(os.path.dirname(__file__), 'autogenerated.ged'),
        'r').read()
    last_individual_pos = gedcom_data.rfind(' FAMC ', 0, last_individual_pos)
    last_individual_pos = gedcom_data.rfind('\n0 @I', 0, last_individual_pos)
    individual_id = gedcom_data[last_individual_pos + 3:gedcom_data.
                                find(' INDI', last_individual_pos + 1)]

    chart.select_individuals(chart._instances[('i', individual_id)],
                             generations=max_generations)

    individual2_id = individual_id
    i = 0
    while i < 4:
        for cof in chart._instances[('i', individual2_id)].child_of_families:
            if cof.husb.has_graphical_representation():
                if cof.husb.child_of_families:
                    individual2_id = cof.husb.individual_id
                    break
            if cof.wife.has_graphical_representation():
                if cof.wife.child_of_families:
                    individual2_id = cof.wife.individual_id
                    break
        chart.select_family_children(
            chart._instances[('f',
                              cof.family_id)].graphical_representations[0])
        i += 1

    root_individual = chart._instances[('i', individual_id)]
    gr_root_individual = root_individual.graphical_representations[0]
    cof_ids = root_individual.child_of_family_id
    child_of_family = None
    if cof_ids:
        child_of_family = chart._instances[('f', cof_ids[0])]
    chart.place_selected_individuals(
        gr_root_individual, None, child_of_family.graphical_representations[0],
        x_position)
    x_min, x_max = chart._instances[(
        'i',
        individual_id)].graphical_representations[0].get_ancestor_range(None)
    x_position += x_max - x_min + 1

    chart.define_svg_items()
    chart.paint_and_save(
        os.path.join(os.path.dirname(__file__), 'output',
                     'test_svg_ancestor_and_children.svg'))
def test_generate_svg_update_interface_descendant():
    chart = DescendantChart(instance_container=get_gedcom_instance_container(
        os.path.join(os.path.dirname(__file__), 'autogenerated.ged')))

    # select individual to show
    chart.set_chart_configuration({
        'root_individuals': [
            {
                'individual_id': '@I25@',
                'generations': 5
            },
            {
                'individual_id': '@I30@',
                'generations': 2
            },
        ]
    })
    chart.update_chart()

    chart.paint_and_save(
        os.path.join(os.path.dirname(__file__), 'output',
                     'test_svg_descendant_configuration.svg'))
def test_generate_svg_update_interface():
    chart = AncestorChart(instance_container=get_gedcom_instance_container(
        os.path.join(os.path.dirname(__file__), 'autogenerated.ged')))

    # select individual to show
    #chart.set_positioning({'compress':True, 'flip_to_optimize':True})
    chart.set_chart_configuration({
        'root_individuals': [
            {
                'individual_id': '@I25@',
                'generations': 5
            },
            {
                'individual_id': '@I69@',
                'generations': 2
            },
        ]
    })
    chart.update_chart()

    chart.paint_and_save(
        os.path.join(os.path.dirname(__file__), 'output',
                     'test_svg_ancestor_configuration.svg'))
def test_generate_svg_update_interface_parent_placement():
    chart = AncestorChart(instance_container=get_gedcom_instance_container(
        os.path.join(os.path.dirname(__file__), 'autogenerated.ged')))

    # select individual to show
    #chart.set_positioning({'compress':True, 'flip_to_optimize':True})
    chart.set_chart_configuration({
        'root_individuals': [
            {
                'individual_id': '@I450@',
                'generations': 8
            },
            #{'individual_id':'@I69@', 'generations':2},
        ],
        'ancestor_placement': {
            (0, '@F68@'): ((0, '@F137@'), (0, '@I236@')),
            (0, '@F67@'): ((0, '@F136@'), (0, '@I233@'))
        }
    })
    chart.update_chart()

    chart.paint_and_save(
        os.path.join(os.path.dirname(__file__), 'output',
                     'test_svg_ancestor_configuration_parent_placement.svg'))
def test_generate_svg_file():
    max_generations = 20
    x_position = 0

    # select individual to show
    gedcom_data = open(
        os.path.join(os.path.dirname(__file__), 'autogenerated.ged'),
        'r').read()
    last_individual_pos = gedcom_data.rfind('\n0 @I')
    last_individual_pos = gedcom_data.rfind(' FAMC ', 0, last_individual_pos)
    last_individual_pos = gedcom_data.rfind('\n0 @I', 0, last_individual_pos)
    individual_id = gedcom_data[last_individual_pos + 3:gedcom_data.
                                find(' INDI', last_individual_pos + 1)]

    chart = AncestorChart(instance_container=get_gedcom_instance_container(
        os.path.join(os.path.dirname(__file__), 'autogenerated.ged')))
    chart.select_individuals(chart._instances[('i', individual_id)],
                             generations=max_generations)

    root_individual = chart._instances[('i', individual_id)]
    gr_root_individual = root_individual.graphical_representations[0]
    cof_ids = root_individual.child_of_family_id
    child_of_family = None
    if cof_ids:
        child_of_family = chart._instances[('f', cof_ids[0])]
    chart.place_selected_individuals(
        gr_root_individual, None, child_of_family.graphical_representations[0],
        x_position)
    x_min, x_max = gr_root_individual.get_ancestor_range(None)
    x_position += x_max - x_min + 1

    chart.modify_layout(individual_id)

    chart.define_svg_items()
    chart.paint_and_save(
        os.path.join(os.path.dirname(__file__), 'output', 'test_svg.svg'))
def test_photos_in_chart():
    max_generations = 3
    x_position = 0
    chart = AncestorChart(instance_container=get_gedcom_instance_container(
        os.path.join(os.path.dirname(__file__), 'autogenerated.ged')),
                          formatting={
                              'relative_line_thickness': 0.7,
                              'horizontal_step_size': 50,
                              'total_height': 3000,
                              'individual_photo_relative_size': 1.5,
                              'individual_photo_active': True
                          })

    individual_id = '@I106@'

    chart.select_individuals(chart._instances[('i', individual_id)],
                             generations=max_generations)

    images = {}
    import re
    import datetime
    expr = re.compile(r'individual_I6_image_age_(\d+)\.png')
    image_step_size = 365 * 4.0
    birth_ordinal_value = chart._instances[(
        'i', individual_id)].events['birth_or_christening']['ordinal_value']
    sorted_list = []
    for filename in os.listdir(
            os.path.join(os.path.dirname(__file__), 'images')):
        match = expr.match(filename)
        if match:
            year = int(match.group(1)) + 1245
            month = 1
            day = 1
            # elif match.group(3) is None::
            #     month = max(1, int(match.group(3)))
            ordinal_value = datetime.date(year, month, day).toordinal()
            sorted_list.append((ordinal_value, year, filename))
    sorted_list.sort()
    for ordinal_value, year, filename in sorted_list:
        if len(images) > 0:
            max_ordinal = max(list(images.keys()))
        else:
            max_ordinal = -1
        if round(
            (ordinal_value - birth_ordinal_value) / image_step_size) > round(
                (max_ordinal - birth_ordinal_value) / image_step_size):
            images[birth_ordinal_value +
                   round((ordinal_value - birth_ordinal_value) /
                         image_step_size) * image_step_size] = {
                             'filename':
                             os.path.join(os.path.dirname(__file__), 'images',
                                          filename),
                             'size':
                             Image.open(
                                 os.path.join(os.path.dirname(__file__),
                                              'images', filename)).size
                         }

    chart._instances[('i', individual_id)].images.update(images)
    chart._instances[('i', '@I98@')].images.update(images)

    root_individual = chart._instances[('i', individual_id)]
    gr_root_individual = root_individual.graphical_representations[0]
    cof_ids = root_individual.child_of_family_id
    child_of_family = None
    if cof_ids:
        child_of_family = chart._instances[('f', cof_ids[0])]
    chart.place_selected_individuals(
        gr_root_individual, None, child_of_family.graphical_representations[0],
        x_position)
    x_min, x_max = chart._instances[(
        'i',
        individual_id)].graphical_representations[0].get_ancestor_range(None)
    x_position += x_max - x_min + 1
    chart.modify_layout(individual_id)

    chart.define_svg_items()
    chart.paint_and_save(
        os.path.join(os.path.dirname(__file__), 'output',
                     'test_svg_photos.svg'))