예제 #1
0
def build_lists_from_file(filename):
    """Parses the Ged file and builds the Family and Individual objects.
    Returns a touple of dictionaries, in the for ({},{}), where the first is
    the list of families (indexed by family ID) and the second is the list of
    individuals (indexed by the individual ID).
    """
    load_valid_tags()
    lines = open(filename, "r").readlines()
    individuals = {}
    families = {}

    def generateUntilLevelZero(seq):
        """Creates a list of GedLine objects until the given sequence has a 
        GedLine with level zero. This is useful for retrieving a sequence of 
        lines of a single object (family of individual)
        """
        for e in seq:
            line = GedLine(e)
            if line.level() != 0:
                yield line
            else:
                break

    index = 0  # line index
    while index < len(lines)-1:
        line = GedLine(lines[index])

        if line.level() == 0 and line.content() == "INDI":
            # Create Individual
            ind = Individual(line.tag())  # Tag is the ID
            individual_lines = generateUntilLevelZero(lines[index+1:])
            for l in individual_lines:
                if l.tag() == "NAME":
                    ind.name = l.content()
                elif l.tag() == "GIVN":
                    ind.givenname = l.content()
                elif l.tag() == "SURN":
                    ind.surname = l.content()
                elif l.tag() == "SEX":
                    ind.sex = l.content()
                elif l.tag() == "NOTE":
                    ind.note = l.content()
                elif l.tag() == "BIRT":
                    ind.setbirthday(individual_lines.next().content())
                elif l.tag() == "DEAT":
                    ind.setdeathday(individual_lines.next().content())
            individuals[ind.id] = ind

        elif line.level() == 0 and line.content() == "FAM":
            # Create Family
            fam = Family(line.tag())  # Tag is the ID
            family_lines = generateUntilLevelZero(lines[index+1:])

            for l in family_lines:
                if l.tag() == "HUSB":
                    fam.husband = l.content()
                elif l.tag() == "WIFE":
                    fam.wife = l.content()
                elif l.tag() == "CHIL":
                    fam.children.append(l.content())
                elif l.tag() == "MARR":
                    fam.setmarrydate(family_lines.next().content())
            families[fam.id] = fam

        index += 1

    return families, individuals