def test_1_person_env(self): p1 = mock.create_autospec(Person, idn='I1', origin=None, family_connections=None) e = Env({p1.idn: p1}) self.assertEqual(e.find_top(), {p1.idn})
def test_2_unrelated_people_env(self): p1 = mock.create_autospec(Person, idn='I1', origin=None, family_connections=None) p2 = mock.create_autospec(Person, idn='I2', origin=None, family_connections=None) e = Env({p1.idn: p1, p2.idn: p2}) self.assertTrue(e.find_top(), {p1.idn, p2.idn})
def test_2_connected_people_env(self): p1 = mock.create_autospec(Person, idn='I1', origin=None, family_connections=['F1']) p2 = mock.create_autospec(Person, idn='I2', origin=None, family_connections=['F1']) f1 = mock.create_autospec(Family, idn='F1', head='I1', partner='I2') e = Env({p1.idn: p1, p2.idn: p2, f1.idn: f1}) self.assertEqual(e.find_top(), {p1.idn, p2.idn})
def test_selected_has_siblings_and_partner_and_child(self): p1 = mock.create_autospec(Person, idn='I1', origin=None, family_connections=['F1']) p2 = mock.create_autospec(Person, idn='I2', origin=None, family_connections=['F1']) p3 = mock.create_autospec(Person, idn='I3', origin='F1', family_connections=None) p4 = mock.create_autospec(Person, idn='I4', origin='F1', family_connections=['F2']) p5 = mock.create_autospec(Person, idn='I5', origin='F1', family_connections=None) p6 = mock.create_autospec(Person, idn='I6', origin=None, family_connections=['F2']) p7 = mock.create_autospec(Person, idn='I7', origin='F2', family_connections=None) f1 = mock.create_autospec(Family, idn='F1', head='I1', partner='I2', family_connections=['I3']) f2 = mock.create_autospec(Family, idn='F2', head='I4', partner='I6', family_connections=['I7']) e = Env({ p1.idn: p1, p2.idn: p2, p3.idn: p3, p4.idn: p4, p5.idn: p5, p6.idn: p6, p7.idn: p7, f1.idn: f1, f2.idn: f2 }) self.assertEqual(e.find_top(), {p1.idn, p2.idn})
def test_one_child_child(self): p1 = mock.create_autospec(Person, idn='I1', family_connections=['F1']) p2 = mock.create_autospec(Person, idn='I2', family_connections=['F1']) f1 = mock.create_autospec(Family, idn='F1', head='I1', partner='I2', family_connections=['I3']) p3 = mock.create_autospec(Person, idn='I3', origin='F1', family_connections=None) e = Env({p1.idn: p1, p2.idn: p2, p3.idn: p3, f1.idn: f1}) self.assertEqual(e.get_children(p3), set())
def test_selected_parents_order(self): p1 = mock.create_autospec(Person, idn='I1', origin=None, family_connections=['F1']) p2 = mock.create_autospec(Person, idn='I2', origin=None, family_connections=['F1']) f1 = mock.create_autospec(Family, idn='F1', head='I1', partner='I2', family_connections=['I3']) p3 = mock.create_autospec(Person, idn='I3', origin='F1', family_connections=None) e = Env({p1.idn: p1, p2.idn: p2, p3.idn: p3, f1.idn: f1}) self.assertEqual(e.find_top(), {p1.idn, p2.idn})
def test_more_partner_on_left_side(self): p1 = mock.create_autospec(Person, idn='I1', family_connections=['F1']) p2 = mock.create_autospec(Person, idn='I2', family_connections=['F1', 'F2', 'F3']) p3 = mock.create_autospec(Person, idn='I3', family_connections=['F2']) p4 = mock.create_autospec(Person, idn='I4', family_connections=['F3']) f1 = mock.create_autospec(Family, idn='F1', head='I1', partner='I2') f2 = mock.create_autospec(Family, idn='F2', head='I3', partner='I2') f3 = mock.create_autospec(Family, idn='F3', head='I4', partner='I2') e = Env({ p1.idn: p1, p2.idn: p2, p3.idn: p3, p4.idn: p4, f1.idn: f1, f2.idn: f2, f3.idn: f3 }) self.assertEqual(e.get_partners(p2.idn), {p1.idn, p3.idn, p4.idn})
def test_empty_env(self): self.assertEqual(Env().find_top(), set())
def test_no_children(self): p1 = mock.create_autospec(Person, idn='I1', family_connections=None) e = Env({p1.idn: p1}) self.assertEqual(e.get_children(p1), set())
def test_one_partner_on_left_side(self): p1 = mock.create_autospec(Person, idn='I1', family_connections=['F1']) p2 = mock.create_autospec(Person, idn='I2', family_connections=['F1']) f1 = mock.create_autospec(Family, idn='F1', head='I1', partner='I2') e = Env({p1.idn: p1, p2.idn: p2, f1.idn: f1}) self.assertEqual(e.get_partners(p2.idn), {p1.idn})
def test_no_partners(self): p1 = mock.create_autospec(Person, idn='I1', family_connections=None) e = Env({p1.idn: p1}) self.assertEqual(e.get_partners(p1.idn), None)
def __init__(self, filename): self.file = filename self.env = Env() self.data_holder = None self.current_tag = None
class GedcomParser: MAP_TAG_TO_DATA = { "FAMC": "origin", "HUSB": "head", "WIFE": "partner", "BIRT": "birth", "DEAT": "death", "SURN": "surname", "GIVN": "name" } def __init__(self, filename): self.file = filename self.env = Env() self.data_holder = None self.current_tag = None def line_to_tuple(line): ret = line.split() if len(ret) > 0: ret[0] = int(ret[0]) if len(ret) > 2 and ret[0] == 0: temp = ret[1][1:-1] ret[1] = ret[2] ret[2] = temp return ret def create_entry(self): if self.data_holder is not None: idn = str(self.data_holder.get_idn()) self.env.add_entry(idn, self.data_holder) if isinstance(self.data_holder, Person): self.env.next_indi_id += 1 elif isinstance(self.data_holder, Family): self.env.next_fam_id += 1 # def fam_tag() # self.data_holder.add(value[1:-1]) def assign_child_to_family(self): return 0 def assign_person_to_family(self, per): self.data_holder.add(per) def set_birth_tag(self): self.current_tag = "birth" def set_death_tag(self): self.current_tag = "death" '''parse_tag = { # "INDI": lambda x: person(idn=x), "NAME": "GIVN": "SURN": "BIRT": self.set_birth_tag, "DEAT": self.set_death_tag, "FAMC": "FAM": lambda x: family(idn=x), "HUSB": "WIFE": "CHIL": "MARR": "DATE": "PLAC": }''' def parse_tag_after_0(self, tag, value): if tag == "INDI": self.data_holder = Person(idn=value[0]) elif tag == "FAM": self.data_holder = Family(idn=value[0]) # else "not_yet_implemented" # TODO switcher: # https://jaxenter.com/implement-switch-case-statement-python-138315.html def parse_tag_after_1(self, tag, value): # if tag == "NAME": if tag == "FAM" or tag == "CHIL": self.data_holder.add(value[0][1:-1]) elif tag in ["FAMC", "HUSB", "WIFE"]: data_type = GedcomParser.MAP_TAG_TO_DATA[tag] self.data_holder.update_data(data_type, value[0][1:-1]) elif tag == "BIRT" or tag == "DEAT": self.current_tag = GedcomParser.MAP_TAG_TO_DATA[tag] elif tag == "MARR": self.data_holder.update_data("relation_type", "marriage") else: return "not_yet_implemented" # DIV def parse_tag_after_2(self, tag, value): if tag == "DATE": self.data_holder.update_data(self.current_tag, value) elif tag == "GIVN" or tag == "SURN": data_type = GedcomParser.MAP_TAG_TO_DATA[tag] self.data_holder.update_data(data_type, value) # elif tag == "PLACE": # self.data_holder.update_data(self.current_tag, value) # else "not_yet_implemented" def parse_tup(self, tup): if tup is not None: if tup[0] == 0: self.create_entry() self.parse_tag_after_0(tup[1], tup[2:]) elif tup[0] == 1: self.parse_tag_after_1(tup[1], tup[2:]) elif tup[0] == 2: self.parse_tag_after_2(tup[1], tup[2:]) # indidata # sex # famdata # DIV return 0 def parse(self): with open(self.file + '.GED', 'r') as reader: line = reader.readline() while line != '': # print(GedcomParser.parse_line(line), end='') tup = GedcomParser.line_to_tuple(line) self.parse_tup(tup) # create new families and individuals # with correct data line = reader.readline() return self.env