5 from string import Template
8 link_Template= Template("<a href = http://www.chiark.greenend.org.uk/ucgi/~naath/$script>$text</a>")
13 def print_row(row,newLine):
16 out = out + str(item)+'|'
17 return out[:-1] + newLine
19 def print_query(s,t,newLine):
21 for row in run_query(s,t):
22 printMe = printMe + print_row(row,newLine)
32 def print_tagged_query(relationship,s,t,newLine):
34 for row in run_query(s,t):
35 mine = mine + print_tagged_name(relationship,row,newLine)
39 def relationship_html(ID,ID2,newLine):
41 relationship = common_ancestors(ID,ID2,newLine)[2]
42 script = "ancestors.py?ID="+str(ID)+"&ID2="+str(ID2)
43 url = link_Template.substitute(script = script,text = "Common ancestors")
44 return relationship + ' '+url + newLine
48 def terr_html(terr,newLine):
50 return link_Template.substitute(script = "territory.py?terr="+terr, text=terr)
53 def name_html(row,html):
63 script = "person.py?ID=" + str(row[1])
65 return link_Template.substitute(script = script\
68 return row[0] + "," +str(row[1])
76 def print_age_child_count(row,newLine):
78 script = "age.py?age="+str(row[0])
79 link = link_Template.substitute(script = \
80 script, text = row[0])
81 out = str(row[1])+print_people(row[1])
83 out = out + 'had children at age '+ link + newLine
86 return print_row(row,newLine)
88 def print_age_death_count(row,newLine):
90 script = "ageDeath.py?age="+str(row[0])
91 link = link_Template.substitute(script = script,text = row[0])
92 out = str(row[1])+print_people(row[1])
93 out = out + "died at age " + link + newLine
96 return print_row(row,newLine)
98 def print_name_count(row,newLine):
100 script = "name.py?name=" + row[0]
101 link = link_Template.substitute(script =\
102 script, text = row[0])
103 return str(row[1]) + " people called "+link + newLine
105 return print_row(row,newLine)
107 def print_tagged_name(relationship,row,newLine):
109 out = relationship + " not yet entered: " + row[1]
111 if newLine == '<br>':
115 if relationship =='':
116 out = name_html(row,html) + ' '
118 out = relationship + ": " + name_html(row,html)
121 def month_numbers(monthN):
123 month ='unknown month'
149 month = 'Incorrectly entered month ' + str(monthN)
152 def ordinal_numbers(number):
154 if number % 10==1 and number/10 % 10 !=1:
155 out = str(number) +'st'
156 elif number % 10==2 and number/10 % 10 !=1:
157 out = str(number) +'nd'
158 elif number % 10==3 and number/10 % 10 !=1:
159 out = str(number) +'rd'
161 out = str(number) +'th'
164 def list_territories(newLine):
165 s = "SELECT DISTINCT territory"\
166 +" FROM territories"\
167 +" ORDER BY territory;"
170 for row in run_query(s,()):
171 out =out + terr_html(row[0],newLine) +newLine
174 def list_people_parents():
175 s = "SELECT name,id"\
180 for row in run_query(s,()):
181 t = "SELECT parentid"\
185 u = "SELECT name,id"\
190 for r in run_query(t,(row[1],)):
193 for q in run_query(u,(r[0],)):
194 parents.append(q[0] + ' ' + str(q[1]))
197 parents.append(r[0] + ' p' +\
201 v = "SELECT name,idb"\
202 +" FROM marriages LEFT JOIN people"\
205 for r in run_query(v,(row[1],)):
207 spouses.append(r[0]+ ' '+str(r[1]))
209 spouses.append(r[1] + ' s' +\
215 output.append([myName+ ' '+ myID,parents,spouses])
219 def list_people(newLine):
220 s = "SELECT name,id,bornyear"\
222 +" ORDER BY bornyear;"
226 out = out + 'born in unknown year:' +newLine
227 for row in run_query(s,()):
228 if row[2]!=0 and row[2]/100==0:
229 out = out +newLine+ 'born in 1st century:' +newLine
231 if row[2]/100!=year/100:
232 century = row[2]/100 + 1
233 out = out +newLine+ 'born in '
235 out = out +ordinal_numbers(century) \
236 + ' century:' + newLine
238 out = out + name_html(row,newLine) +newLine
240 if row[2] == 0: #unknown year
242 t = (row[1],) #person ID
246 u = "SELECT diedyear FROM people WHERE ID = ?;"
249 for r in run_query(u,t):
251 out = out + "died: "\
252 + str(r[0]) + newLine
253 bornAfter = r[0] -100
256 u = "Select people.bornYear from"\
257 +" people INNER JOIN parents"\
258 +" ON people.ID = parents.ID"\
259 +" WHERE parents.parentID = ?"\
260 + " ORDER BY people.bornYear;"
264 for r in run_query(u,t):
266 hadChild.append(r[0])
270 out = out + "had children in: "
272 out = out + str(c) + ','
273 out = out[:-1] + newLine
275 bornBefore = hadChild[0]-12
277 bornAfter = hadChild[0]-100
279 u = "Select styles.startYear, styles.style from"\
280 +" people INNER JOIN styles"\
281 +" ON people.ID = styles.ID"\
282 +" WHERE people.ID = ? and"\
283 +" styles.startYear <>0"\
284 +" ORDER BY styles.startYear;"
286 for r in run_query(u,t):
287 out = out + r[1] + " from " + str(r[0])\
290 bornAfter = r[0] -100
295 out = out + "probably born "\
296 +"after " + str(bornAfter)
298 out = out + "probably born "\
299 +"betwen " + str(bornAfter)\
300 +" and " + str(bornBefore)
306 def count_names(newLine):
307 s = "SELECT firstName, count(*)"\
309 +" GROUP BY firstName"\
310 +" ORDER BY count(*) DESC;"
313 for row in run_query(s,()):
314 out = out + print_name_count(row,newLine)
318 def people_with_name(name,newLine):
319 s = "SELECT Name, ID"\
321 +" WHERE Name LIKE ?;"
327 for row in run_query(s,t):
328 out = out + name_html(row,newLine) + newLine
332 def count_birth_month(newLine):
333 s = "SELECT bornMonth, count(*)"\
335 +" GROUP BY bornMonth"\
336 +" ORDER BY bornMonth;"
338 t = "SELECT * FROM people WHERE bornMonth = ?;"
341 for row in run_query(s,()):
342 month = month_numbers(row[0])
343 out = out + month + ': ' + str(row[1]) + newLine
347 out = out +print_query(t,u,newLine)
351 def count_death_month(newLine):
352 s = "SELECT diedMonth, count(*)"\
354 +" GROUP BY diedMonth"\
355 +" ORDER BY diedMonth;"
357 t = "SELECT * FROM people WHERE diedMonth = ?;"
360 for row in run_query(s,()):
361 month = month_numbers(row[0])
362 out = out + month + ': ' + str(row[1]) + newLine
366 out = out +print_query(t,u,newLine)
370 def count_age_at_child(newLine):
372 s = "select p1.bornYear - p2.bornYear as age, count(*)"\
374 +" parents INNER JOIN people p1"\
375 +" ON parents.ID = p1.ID"\
376 +" INNER JOIN people p2"\
377 +" ON parents.parentID = p2.ID"\
378 +" WHERE p1.bornYear <> 0 and p2.bornYear<>0"\
382 for row in run_query(s,()):
383 out = out + print_age_child_count(row,newLine)
387 def people_had_child_at_age(age,newLine):
389 s = "select p1.bornYear - p2.bornYear as age, p1.name, p1.ID"\
390 +",p2.name,p2.ID FROM"\
391 +" parents INNER JOIN people p1"\
392 +" ON parents.ID = p1.ID"\
393 +" INNER JOIN people p2"\
394 +" ON parents.parentID = p2.ID"\
395 +" WHERE age = ? AND p1.bornYear<>0 AND p2.bornYear<>0"
400 out = 'At age ' + str(age) + ' :'
401 for row in run_query(s,t):
403 out =out + name_html([row[3],row[4]],newLine) + ' had '\
404 +name_html([row[1],row[2]],newLine)
408 def count_age_at_death(newLine):
409 s = "select diedYear-bornYear as age,count(*)"\
411 +" WHERE diedYear<>0 AND bornYear<>0"\
414 for row in run_query(s,()):
415 out = out + print_age_death_count(row,newLine)
418 def people_died_at_age(age,newLine):
419 s = "SELECT diedYear-bornYear as age, name,ID"\
421 +" WHERE age = ? AND bornYear<>0 AND diedYear<>0;"
424 out = 'These people died at age ' +str(age) + ' :'
425 for row in run_query(s,t):
427 out = out + name_html([row[1],row[2]],newLine)
430 def all_ancestors(personID,newLine):
432 s = "SELECT people.Name,parents.parentID FROM"\
433 +" parents LEFT JOIN people"\
434 +" ON parents.parentID = people.ID"\
435 +" WHERE parents.ID = ?"\
436 +" AND parents.parentID <> '.';"
439 ancestors = [personID]
440 allAncestors = [personID]
444 t = "SELECT name,id FROM people WHERE id==?"
447 out = "Ancestors of "
448 for row in run_query(t,id):
449 out = out + name_html(row,newLine)+newLine
451 while len(ancestors)>0:
454 thisout = newLine + parent_level(level,'parent') +':' + newLine
455 for ancestor in ancestors:
457 for row in run_query(s,id):
458 thisout = thisout + \
459 name_html(row,newLine)+newLine
460 if row[1] not in allAncestors and \
461 is_number(row[1])!=0:
463 allAncestors.append(row[1])
464 trackLevel.append(level)
469 image = "<img src = ancestorGraph.py?id="+str(personID)+">"
470 out = out+newLine + image+newLine
471 return [out, allAncestors,trackLevel]
474 def common_ancestors(IDA, IDB,newLine):
475 out = 'Common ancestors of:' + newLine
477 s = "SELECT name,id FROM people WHERE id==?"
483 for row in run_query(s,t):
484 out = out + name_html(row,newLine)+newLine
491 related = 'No details held on one party'
493 return [out,[],related]
496 a = all_ancestors(IDA,newLine)
497 b = all_ancestors(IDB,newLine)
499 ancestorsB = set(b[1])
500 ancestorsA = set(a[1])
502 common = ancestorsA.intersection(ancestorsB)
503 common = list(common)
510 aLevels.append(a[2][i])
512 bLevels.append(b[2][i])
514 s = "SELECT Name, ID, bornyear"\
517 for i in range(len(common)):
523 s = s+") ORDER BY bornyear;"
527 related = names[0]+' and '+names[1]+' are not related'
528 out = out + newLine + related
529 return [out, common,related]
532 out = out + print_tagged_query('',s,common,newLine)
537 for i in range(len(common)):
538 if aLevels[i] == min(aLevels):
540 if bLevels[i] == min(bLevels):
545 s = "SELECT name, id"\
549 out = out + newLine + 'Most Recent Common Ancestors:' + newLine
553 mrca.append(common[a])
554 out = out + print_tagged_query('',s,t,newLine)
556 out = out + 'and' + newLine
558 out = out + parent_level(aLevels[indexA[0]],'parent')
562 out = out + ' of ' + name_html([names[0],IDA],newLine)+newLine
567 # out = out + print_tagged_query('',s,t,newLine)
569 # out = out + 'and' + newLine
571 out = out + parent_level(bLevels[indexB[0]],'parent')
574 out = out + ' of ' + name_html([names[1],IDB],newLine)+newLine
577 al = aLevels[indexA[0]]
578 bl = bLevels[indexB[0]]
580 related = relationship(al,bl,names)
581 out = out+newLine + related
584 image = "<img src = jointAncestorGraph.py?id="+str(IDA)\
585 +"&id2="+str(IDB) + "&LA=" + str(min(aLevels)) \
586 +"&LB=" + str(min(bLevels))+">"
590 out = out+newLine + image+newLine
592 return [out,common,related]
594 def relationship(level1, level2,names):
596 if level1==0 and level2==0:
597 return names[0] + ' is ' +names[1]
599 return names[0] + ' is ' + names[1] + '\'s '+ parent_level(level2,'parent')
601 return names[1] + ' is ' + names[0] + '\'s '+ parent_level(level1,'parent')
605 remove = level1-level2
608 remove = level2-level1
612 uaLevel = parent_level(remove,'uncle or aunt')
614 return names[0] + ' is ' + names[1] + '\'s ' + uaLevel
617 return names[1] + ' is ' + names[0] + '\'s ' + uaLevel
619 c=ordinal_numbers(cousinNum)
625 rem = str(remove) + ' times'
627 r = names[0] +' and '+names[1] +' are ' + c + ' cousins '
629 r = r+ rem + ' removed'
633 def parent_level(level,type):
648 for i in range(2,level):
652 def rulers_of(aTerritory,newLine):
654 tq = "SELECT name, people.ID, startyear,stopyear,territory"\
655 +" FROM territories INNER JOIN people"\
656 +" ON people.ID = territories.ID"\
657 +" WHERE territory LIKE ?"\
658 +" ORDER BY territory,startyear,stopyear;"
665 for row in run_query(tq,(aTerritory+'%',)):
666 if row[4]!=last and last!='':
667 out = out + 'Rulers of '+terr_html(last,newLine) +':'+ newLine +thisT +newLine
670 thisT = thisT +name_html(row,newLine)
671 thisT = thisT +' from ' + str(row[2])+' to '+str(row[3]) + newLine
674 out = out + 'Rulers of '+terr_html(row[4],newLine) +':'+ newLine +thisT
678 def person_info(personID,newLine):
683 #Id, Name, Dates, Style, Style-Dates
684 s = "SELECT * FROM people WHERE ID = ?"
685 for row in run_query(s,t):
686 output = output + 'ID: '+str(row[0]) +newLine
687 output = output + print_tagged_name('Name',[row[1], row[0]],newLine) +newLine
689 output = output + 'Born: '+row[3] + newLine
691 output = output + 'Died: '+row[5] + ", aged " \
692 +str(row[6]-row[4]) +newLine
694 s = "SELECT * FROM styles WHERE ID = ?"
695 for row in run_query(s,t):
696 output = output +newLine+ 'Style: '+row[1] + newLine
698 output = output + 'Territories:' + newLine
700 u = "SELECT * FROM territories"\
701 +" WHERE ID =? AND startYear =? AND stopYear=?"
702 v=(personID,row[3],row[5])
705 for r in run_query(u,v):
706 output = output + terr_html(r[1],newLine) +','
709 output = output[:-1] + newLine
711 output = output + 'From: '+row[2] + newLine
712 output = output + 'To: '+row[4] + newLine
714 s = "SELECT people.Name,consort "\
715 +"FROM consorts LEFT JOIN people"\
716 +" ON people.ID = consorts.consort"\
717 +" WHERE consorts.ID = ?"
718 for row in run_query(s,t):
719 output = output + print_tagged_name('Consort',row,newLine)
721 output = output + newLine
723 s = "SELECT people.Name,parents.parentID FROM"\
724 +" parents LEFT JOIN people"\
725 +" ON parents.parentID = people.ID"\
726 +" WHERE parents.ID = ?"
729 for row in run_query(s,t):
730 output = output + print_tagged_name('Parent',row,newLine)
732 parents.append(row[0] + ' ' + row[1])
734 parents.append(row[1] + ' p' + personID)
737 s = "SELECT people.NAME, marriages.IDb from"\
738 +" marriages LEFT JOIN people"\
739 +" ON people.ID = marriages.IDb"\
740 +" WHERE marriages.IDa = ?"\
743 for row in run_query(s,t):
745 spouses.append(row[0] + ' '+str(row[1]))
747 spouses.append(row[1] + ' s' + personID)
748 output = output + newLine
749 output = output + print_tagged_name('Spouse',row,newLine)
750 output = output + relationship_html(personID,row[1],newLine)
752 s = "SELECT people.NAME, marriages.IDa from"\
753 +" marriages LEFT JOIN people"\
754 +" ON people.ID = marriages.IDa"\
755 +" WHERE marriages.IDb = ?"\
757 for row in run_query(s,t):
759 spouses.append(row[0] + ' '+str(row[1]))
761 spouses.append(row[1] + ' s' + personID)
762 output = output + newLine
763 output = output + print_tagged_name('Spouse',row,newLine)
764 output = output + relationship_html(personID,row[1],newLine)
766 output = output + newLine
769 s = "Select people.NAME, people.ID ,people.bornYear"\
770 +" FROM people INNER JOIN parents"\
771 +" ON people.ID = parents.ID"\
772 +" WHERE parents.parentID = ?"\
773 +" ORDER BY people.bornYear;"
777 for row in run_query(s,t):
778 output = output + print_tagged_name('Child',row,newLine)
779 children.append(row[0] + ' ' + str(row[1]))
781 #find children's other parent
782 u = "Select people.NAME, parents.parentID FROM"\
783 +" parents INNER JOIN people"\
784 +" ON people.ID = parents.parentID"\
785 +" WHERE parents.ID = ? AND parents.parentID <>?;"
790 for r in run_query(u,ids):
791 output = output + print_tagged_name('With',r,newLine)
793 ops.append(r[0] + ' ' + str(r[1]))
795 ops.append('?' + ' s' + personID)
798 if row[2] !=0 and bornYear != 0:
799 age = row[2]-bornYear
800 output = output[:-4] + " at the age of "+str(age) + newLine
803 Self = name +' ' + str(personID)
805 image = "smallGraph.py?Self="+Self
807 image = image + '&p='+p
809 image = image + '&c='+c
812 image = image + '&op='+op
815 image = image + '&s='+str(s)
817 image = image.replace(' ','%20')
818 image ="<img src ="+ image + '>'
821 output = newLine+ output+ image+newLine
829 conn = sqlite3.connect('/home/naath/familyTreeProject/familyTree/tree.db')
840 # [c, conn] = connect()