2020-06-11 20:10:49 +02:00
|
|
|
from datetime import date
|
2020-08-30 14:31:44 +02:00
|
|
|
from person import Person
|
|
|
|
|
from mariage import Mariage
|
2020-06-11 20:10:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
event_types = ['N', 'M', 'D']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Event:
|
|
|
|
|
def __init__(self, event_type: str, event_date: date, current_date: date,
|
|
|
|
|
person_a: Person, person_b: Person = None):
|
|
|
|
|
if event_type not in event_types:
|
|
|
|
|
raise ValueError('{} is not in {}'.format(event_type, event_types))
|
|
|
|
|
self._type = event_type
|
|
|
|
|
self._date = event_date
|
|
|
|
|
self._current_date = current_date
|
|
|
|
|
self._p_a = person_a
|
|
|
|
|
self._p_b = person_b
|
|
|
|
|
|
|
|
|
|
def get_year_span(self) -> int:
|
|
|
|
|
return self._current_date.year - self._date.year
|
|
|
|
|
|
|
|
|
|
def event_description(self) -> str:
|
|
|
|
|
y = self.get_year_span()
|
|
|
|
|
if self._type == 'N':
|
|
|
|
|
return '{}{} né{} il y a {} an{}'.format(
|
|
|
|
|
self._p_a.formatted_name(),
|
|
|
|
|
' (a épousé {})'.format(self._p_b.formatted_name()) if self._p_b else '',
|
|
|
|
|
'' if self._p_a.is_male() else 'e',
|
|
|
|
|
y,
|
|
|
|
|
's' if y > 1 else ''
|
|
|
|
|
)
|
|
|
|
|
if self._type == 'M':
|
|
|
|
|
return '{} a épousé {} il y a {} an{}'.format(
|
|
|
|
|
self._p_a.formatted_name(),
|
|
|
|
|
self._p_b.formatted_name(),
|
|
|
|
|
y,
|
|
|
|
|
's' if y > 1 else ''
|
|
|
|
|
)
|
|
|
|
|
return '{}{} décédé{} il y a {} ans{}'.format(
|
|
|
|
|
self._p_a.formatted_name(),
|
|
|
|
|
' (a épousé {})'.format(self._p_b.formatted_name()) if self._p_b else '',
|
|
|
|
|
'' if self._p_a.is_male() else 'e',
|
|
|
|
|
y,
|
|
|
|
|
's' if y > 1 else ''
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def event_date_as_str(self) -> str:
|
|
|
|
|
day = str(self._date.day) if self._date.day > 9 else '0' + str(self._date.day)
|
|
|
|
|
month = str(self._date.month) if self._date.month > 9 else '0' + str(self._date.month)
|
|
|
|
|
return '{}/{}/{}'.format(day, month, self._date.year)
|
|
|
|
|
|
|
|
|
|
def get_actual_weekday(self) -> int:
|
|
|
|
|
return self._current_date.weekday()
|
|
|
|
|
|
|
|
|
|
def type_str(self):
|
|
|
|
|
return self._type
|
|
|
|
|
|
|
|
|
|
|
2020-06-11 21:27:15 +02:00
|
|
|
def load_persons_to_dict(members_file: str) -> dict:
|
2020-06-11 20:10:49 +02:00
|
|
|
id_to_person = {}
|
|
|
|
|
first_line = True
|
|
|
|
|
with open(members_file, 'r', encoding='utf-8') as members:
|
|
|
|
|
for line in members:
|
|
|
|
|
if first_line:
|
|
|
|
|
first_line = False
|
|
|
|
|
continue
|
|
|
|
|
line.strip()
|
|
|
|
|
infos = line.split(';')
|
|
|
|
|
birth = infos[5].split('-')
|
|
|
|
|
death = None if infos[6] == 'NULL' else infos[6].split('-')
|
|
|
|
|
p = Person(int(infos[0]),
|
|
|
|
|
'M' if infos[3] == '1' else 'F',
|
|
|
|
|
infos[2],
|
|
|
|
|
infos[1],
|
|
|
|
|
date(int(birth[0]), int(birth[1]), int(birth[2])),
|
|
|
|
|
None if not death else date(int(death[0]), int(death[1]), int(death[2])),
|
|
|
|
|
infos[4] == '1',
|
|
|
|
|
infos[7] == '1')
|
|
|
|
|
id_to_person[int(infos[0])] = p
|
|
|
|
|
return id_to_person
|
|
|
|
|
|
|
|
|
|
|
2020-06-11 21:27:15 +02:00
|
|
|
def load_mariage_to_dict(mariages_file: str) -> dict:
|
2020-06-11 20:10:49 +02:00
|
|
|
id_to_mariage = {}
|
|
|
|
|
first_line = True
|
|
|
|
|
with open(mariages_file, 'r', encoding='utf-8') as mariages:
|
|
|
|
|
for line in mariages:
|
|
|
|
|
if first_line:
|
|
|
|
|
first_line = False
|
|
|
|
|
continue
|
|
|
|
|
line.strip()
|
|
|
|
|
infos = line.split(';')
|
|
|
|
|
if infos[3] == "NULL": # Union libre, not an event
|
|
|
|
|
continue
|
|
|
|
|
m_type = 'M'
|
|
|
|
|
if infos[6] == '0':
|
|
|
|
|
m_type = 'D'
|
|
|
|
|
if infos[6] == '2':
|
|
|
|
|
m_type = 'L'
|
|
|
|
|
m_date = infos[3].split('-')
|
|
|
|
|
m = Mariage(
|
|
|
|
|
int(infos[0]),
|
|
|
|
|
int(infos[1]),
|
|
|
|
|
int(infos[2]),
|
|
|
|
|
date(int(m_date[0]), int(m_date[1]), int(m_date[2])),
|
|
|
|
|
infos[4] == '1',
|
|
|
|
|
infos[5] == '1',
|
|
|
|
|
m_type
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if m.show_mariage():
|
|
|
|
|
id_to_mariage[int(infos[0])] = m
|
|
|
|
|
return id_to_mariage
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_significant_other(id_to_mariage: dict, uid: int) -> int:
|
|
|
|
|
for m in id_to_mariage.values():
|
|
|
|
|
if m.in_this_union(uid) and m.show_mariage():
|
|
|
|
|
return m.get_so(uid)
|
|
|
|
|
return -1
|
|
|
|
|
|
|
|
|
|
|
2020-06-11 21:27:15 +02:00
|
|
|
def create_events_from_span(start: date, end: date, members_file: str, mariages_file: str) -> list:
|
|
|
|
|
id_to_person = load_persons_to_dict(members_file)
|
|
|
|
|
id_to_mariage = load_mariage_to_dict(mariages_file)
|
2020-06-11 20:10:49 +02:00
|
|
|
cur_year = start.year
|
|
|
|
|
|
|
|
|
|
events = []
|
|
|
|
|
|
|
|
|
|
for p in id_to_person.values():
|
|
|
|
|
so = find_significant_other(id_to_mariage, p.uid())
|
|
|
|
|
if so == p.uid():
|
|
|
|
|
print(f"What ??? {so}")
|
|
|
|
|
if not p.is_dead():
|
|
|
|
|
birth = p.birth()
|
|
|
|
|
birth_in_year = date(cur_year, birth.month, birth.day)
|
|
|
|
|
if start <= birth_in_year <= end:
|
|
|
|
|
e = Event('N', birth, birth_in_year,
|
|
|
|
|
p, None if so == -1 else id_to_person[so])
|
|
|
|
|
events.append(e)
|
|
|
|
|
|
|
|
|
|
birth_in_year = date(cur_year + 1, birth.month, birth.day)
|
|
|
|
|
if start <= birth_in_year <= end:
|
|
|
|
|
e = Event('N', birth, birth_in_year,
|
|
|
|
|
p, None if so == -1 else id_to_person[so])
|
|
|
|
|
events.append(e)
|
|
|
|
|
else:
|
|
|
|
|
death = p.death()
|
|
|
|
|
death_in_year = date(cur_year, death.month, death.day)
|
|
|
|
|
if start <= death_in_year <= end:
|
|
|
|
|
e = Event('D', death, death_in_year,
|
|
|
|
|
p, None if so == -1 else id_to_person[so])
|
|
|
|
|
events.append(e)
|
|
|
|
|
|
|
|
|
|
death_in_year = date(cur_year + 1, death.month, death.day)
|
|
|
|
|
if start <= death_in_year <= end:
|
|
|
|
|
e = Event('D', death, death_in_year,
|
|
|
|
|
p, None if so == -1 else id_to_person[so])
|
|
|
|
|
events.append(e)
|
|
|
|
|
|
|
|
|
|
for m in id_to_mariage.values():
|
|
|
|
|
if not m.show_mariage():
|
|
|
|
|
continue
|
|
|
|
|
m_date = m.event_date()
|
|
|
|
|
date_in_year = date(cur_year, m_date.month, m_date.day)
|
|
|
|
|
if start <= date_in_year <= end:
|
|
|
|
|
e = Event('M', m_date, date_in_year,
|
|
|
|
|
id_to_person[m.id_a()], id_to_person[m.id_b()])
|
|
|
|
|
events.append(e)
|
|
|
|
|
|
|
|
|
|
date_in_year = date(cur_year + 1, m_date.month, m_date.day)
|
|
|
|
|
if start <= date_in_year <= end:
|
|
|
|
|
e = Event('M', m_date, date_in_year,
|
|
|
|
|
id_to_person[m.id_a()], id_to_person[m.id_b()])
|
|
|
|
|
events.append(e)
|
|
|
|
|
|
|
|
|
|
return events
|