5 from string import Template
8 link_Template= Template("<a href = http://www.chiark.greenend.org.uk/ucgi/~naath/$script>$text</a>")
14 def print_row(row,newLine):
17 out = out + str(item)+'|'
18 return out[:-1] + newLine
20 def print_query(s,t,newLine):
22 for row in run_query(s,t):
23 printMe = printMe + print_row(row,newLine)
33 def print_tagged_query(relationship,s,t,newLine):
35 for row in run_query(s,t):
36 mine = mine + print_tagged_name(relationship,row,newLine)
40 def relationship_html(ID,ID2,newLine):
42 relationship = common_ancestors(ID,ID2,newLine)[2]
43 script = "ancestors.py?ID="+str(ID)+"&ID2="+str(ID2)
44 url = link_Template.substitute(script = script,text = "Common ancestors")
45 return relationship + ' '+url + newLine
49 def terr_html(terr,newLine):
51 return link_Template.substitute(script = "territory.py?terr="+terr, text=terr)
54 def name_html(row,html):
64 script = "person.py?ID=" + str(row[1])
66 return link_Template.substitute(script = script\
69 return row[0] + "," +str(row[1])
71 def print_age_count(row,newLine):
73 script = "age.py?age="+str(row[0])
74 link = link_Template.substitute(script = \
75 script, text = row[0])
76 return str(row[1]) + ' people had children at age '\
79 return print_row(row,newLine)
82 def print_tagged_name(relationship,row,newLine):
84 out = relationship + " not yet entered: " + row[1]
91 out = name_html(row,html) + ' '
93 out = relationship + ": " + name_html(row,html)
96 def month_numbers(monthN):
98 month ='unknown month'
124 month = 'Incorrectly entered month ' + str(monthN)
127 def ordinal_numbers(number):
129 if number % 10==1 and number/10 % 10 !=1:
130 out = str(number) +'st'
131 elif number % 10==2 and number/10 % 10 !=1:
132 out = str(number) +'nd'
133 elif number % 10==3 and number/10 % 10 !=1:
134 out = str(number) +'rd'
136 out = str(number) +'th'
139 def list_territories(newLine):
140 s = "SELECT DISTINCT territory"\
141 +" FROM territories"\
142 +" ORDER BY territory;"
145 for row in run_query(s,()):
146 out =out + terr_html(row[0],newLine) +newLine
150 def list_people(newLine):
151 s = "SELECT name,id,bornyear"\
153 +" ORDER BY bornyear;"
157 out = out + 'born in unknown year:' +newLine
158 for row in run_query(s,()):
159 if row[2]!=0 and row[2]/100==0:
160 out = out +newLine+ 'born in 1st century:' +newLine
162 if row[2]/100!=year/100:
163 century = row[2]/100 + 1
164 out = out +newLine+ 'born in '
166 out = out +ordinal_numbers(century) \
167 + ' century:' + newLine
169 out = out + name_html(row,newLine) +newLine
171 if row[2] == 0: #unknown year
173 t = (row[1],) #person ID
177 u = "SELECT diedyear FROM people WHERE ID = ?;"
180 for r in run_query(u,t):
182 out = out + "died: "\
183 + str(r[0]) + newLine
184 bornAfter = r[0] -100
187 u = "Select people.bornYear from"\
188 +" people INNER JOIN parents"\
189 +" ON people.ID = parents.ID"\
190 +" WHERE parents.parentID = ?"\
191 + " ORDER BY people.bornYear;"
195 for r in run_query(u,t):
197 hadChild.append(r[0])
201 out = out + "had children in: "
203 out = out + str(c) + ','
204 out = out[:-1] + newLine
206 bornBefore = hadChild[0]-12
208 bornAfter = hadChild[0]-100
212 out = out + "probably born "\
213 +"after " + str(bornAfter)
215 out = out + "probably born "\
216 +"betwen " + str(bornAfter)\
217 +" and " + str(bornBefore)
224 def count_names(newLine):
225 s = "SELECT firstName, count(*)"\
227 +" GROUP BY firstName"\
228 +" ORDER BY count(*) DESC;"
230 out = print_query(s,(),newLine)
235 def count_birth_month(newLine):
236 s = "SELECT bornMonth, count(*)"\
238 +" GROUP BY bornMonth"\
239 +" ORDER BY bornMonth;"
241 t = "SELECT * FROM people WHERE bornMonth = ?;"
244 for row in run_query(s,()):
245 month = month_numbers(row[0])
246 out = out + month + ': ' + str(row[1]) + newLine
250 out = out +print_query(t,u,newLine)
254 def count_death_month(newLine):
255 s = "SELECT diedMonth, count(*)"\
257 +" GROUP BY diedMonth"\
258 +" ORDER BY diedMonth;"
260 t = "SELECT * FROM people WHERE diedMonth = ?;"
263 for row in run_query(s,()):
264 month = month_numbers(row[0])
265 out = out + month + ': ' + str(row[1]) + newLine
269 out = out +print_query(t,u,newLine)
273 def count_age_at_child(newLine):
275 s = "select p1.bornYear - p2.bornYear as age, count(*)"\
277 +" parents INNER JOIN people p1"\
278 +" ON parents.ID = p1.ID"\
279 +" INNER JOIN people p2"\
280 +" ON parents.parentID = p2.ID"\
281 +" WHERE p1.bornYear <> 0 and p2.bornYear<>0"\
285 for row in run_query(s,()):
286 out = out + print_age_count(row,newLine)
290 def people_had_child_at_age(age,newLine):
292 s = "select p1.bornYear - p2.bornYear as age, p1.name, p1.ID"\
293 +",p2.name,p2.ID FROM"\
294 +" parents INNER JOIN people p1"\
295 +" ON parents.ID = p1.ID"\
296 +" INNER JOIN people p2"\
297 +" ON parents.parentID = p2.ID"\
298 +" WHERE age = ? AND p1.bornYear<>0 AND p2.bornYear<>0"
303 out = 'At age ' + str(age) + ' :'
304 for row in run_query(s,t):
306 out =out + name_html([row[3],row[4]],newLine) + ' had '\
307 +name_html([row[1],row[2]],newLine)
312 def all_ancestors(personID,newLine):
314 s = "SELECT people.Name,parents.parentID FROM"\
315 +" parents LEFT JOIN people"\
316 +" ON parents.parentID = people.ID"\
317 +" WHERE parents.ID = ?"\
318 +" AND parents.parentID <> '.';"
321 ancestors = [personID]
322 allAncestors = [personID]
326 t = "SELECT name,id FROM people WHERE id==?"
329 out = "Ancestors of "
330 for row in run_query(t,id):
331 out = out + name_html(row,newLine)+newLine
333 while len(ancestors)>0:
336 thisout = newLine + parent_level(level,'parent') +':' + newLine
337 for ancestor in ancestors:
339 for row in run_query(s,id):
340 thisout = thisout + name_html(row,newLine)+newLine
341 if row[1] not in allAncestors and is_number(row[1])!=0:
343 allAncestors.append(row[1])
344 trackLevel.append(level)
350 return [out, allAncestors,trackLevel]
353 def common_ancestors(IDA, IDB,newLine):
354 out = 'Common ancestors of:' + newLine
356 s = "SELECT name,id FROM people WHERE id==?"
362 for row in run_query(s,t):
363 out = out + name_html(row,newLine)+newLine
370 related = 'No details held on one party'
372 return [out,[],related]
375 a = all_ancestors(IDA,newLine)
376 b = all_ancestors(IDB,newLine)
378 ancestorsB = set(b[1])
379 ancestorsA = set(a[1])
381 common = ancestorsA.intersection(ancestorsB)
382 common = list(common)
389 aLevels.append(a[2][i])
391 bLevels.append(b[2][i])
393 s = "SELECT Name, ID, bornyear"\
396 for i in range(len(common)):
402 s = s+") ORDER BY bornyear;"
406 related = names[0]+' and '+names[1]+' are not related'
407 out = out + newLine + related
408 return [out, common,related]
411 out = out + print_tagged_query('',s,common,newLine)
416 for i in range(len(common)):
417 if aLevels[i] == min(aLevels):
419 if bLevels[i] == min(bLevels):
424 s = "SELECT name, id"\
428 out = out + newLine + 'Most Recent Common Ancestors:' + newLine
431 out = out + print_tagged_query('',s,t,newLine)
433 out = out + 'and' + newLine
435 out = out + parent_level(aLevels[indexA[0]],'parent')
439 out = out + ' of ' + name_html([names[0],IDA],newLine)+newLine
444 out = out + print_tagged_query('',s,t,newLine)
446 out = out + 'and' + newLine
448 out = out + parent_level(bLevels[indexB[0]],'parent')
451 out = out + ' of ' + name_html([names[1],IDB],newLine)+newLine
454 al = aLevels[indexA[0]]
455 bl = bLevels[indexB[0]]
457 related = relationship(al,bl,names)
458 out = out+newLine + related
460 return [out,common,related]
462 def relationship(level1, level2,names):
464 if level1==0 and level2==0:
465 return names[0] + ' is ' +names[1]
467 return names[0] + ' is ' + names[1] + '\'s '+ parent_level(level2,'parent')
469 return names[1] + ' is ' + names[0] + '\'s '+ parent_level(level1,'parent')
473 remove = level1-level2
476 remove = level2-level1
480 uaLevel = parent_level(remove,'uncle or aunt')
482 return names[0] + ' is ' + names[1] + '\'s ' + uaLevel
485 return names[1] + ' is ' + names[0] + '\'s ' + uaLevel
487 c=ordinal_numbers(cousinNum)
493 rem = str(remove) + ' times'
495 r = names[0] +' and '+names[1] +' are ' + c + ' cousins '
497 r = r+ rem + ' removed'
501 def parent_level(level,type):
516 for i in range(2,level):
520 def rulers_of(aTerritory,newLine):
522 tq = "SELECT name, people.ID, startyear,stopyear,territory"\
523 +" FROM territories INNER JOIN people"\
524 +" ON people.ID = territories.ID"\
525 +" WHERE territory LIKE ?"\
526 +" ORDER BY territory,startyear,stopyear;"
533 for row in run_query(tq,(aTerritory+'%',)):
534 if row[4]!=last and last!='':
535 out = out + 'Rulers of '+terr_html(last,newLine) +':'+ newLine +thisT +newLine
538 thisT = thisT +name_html(row,newLine)
539 thisT = thisT +' from ' + str(row[2])+' to '+str(row[3]) + newLine
542 out = out + 'Rulers of '+terr_html(row[4],newLine) +':'+ newLine +thisT
546 def person_info(personID,newLine):
551 #Id, Name, Dates, Style, Style-Dates
552 s = "SELECT * FROM people WHERE ID = ?"
553 for row in run_query(s,t):
554 output = output + 'ID: '+str(row[0]) +newLine
555 output = output + print_tagged_name('Name',[row[1], row[0]],newLine) +newLine
556 output = output + 'Born: '+row[3] + newLine
558 output = output + 'Died: '+row[5] + newLine
560 s = "SELECT * FROM styles WHERE ID = ?"
561 for row in run_query(s,t):
562 output = output +newLine+ 'Style: '+row[1] + newLine
564 output = output + 'Territories:' + newLine
566 u = "SELECT * FROM territories"\
567 +" WHERE ID =? AND startYear =? AND stopYear=?"
568 v=(personID,row[3],row[5])
571 for r in run_query(u,v):
572 output = output + terr_html(r[1],newLine) +','
575 output = output[:-1] + newLine
577 output = output + 'From: '+row[2] + newLine
578 output = output + 'To: '+row[4] + newLine
580 s = "SELECT people.Name,consort "\
581 +"FROM consorts LEFT JOIN people"\
582 +" ON people.ID = consorts.consort"\
583 +" WHERE consorts.ID = ?"
584 for row in run_query(s,t):
585 output = output + print_tagged_name('Consort',row,newLine)
587 output = output + newLine
589 s = "SELECT people.Name,parents.parentID FROM"\
590 +" parents LEFT JOIN people"\
591 +" ON parents.parentID = people.ID"\
592 +" WHERE parents.ID = ?"
593 for row in run_query(s,t):
594 output = output + print_tagged_name('Parent',row,newLine)
597 s = "SELECT people.NAME, marriages.IDb from"\
598 +" marriages LEFT JOIN people"\
599 +" ON people.ID = marriages.IDb"\
600 +" WHERE marriages.IDa = ?"
601 for row in run_query(s,t):
602 output = output + newLine
603 output = output + print_tagged_name('Spouse',row,newLine)
604 output = output + relationship_html(personID,row[1],newLine)
606 s = "SELECT people.NAME, marriages.IDa from"\
607 +" marriages LEFT JOIN people"\
608 +" ON people.ID = marriages.IDa"\
609 +" WHERE marriages.IDb = ?"
610 for row in run_query(s,t):
611 output = output + newLine
612 output = output + print_tagged_name('Spouse',row,newLine)
613 output = output + relationship_html(personID,row[1],newLine)
615 output = output + newLine
618 s = "Select people.NAME, people.ID ,people.bornYear"\
619 +" FROM people INNER JOIN parents"\
620 +" ON people.ID = parents.ID"\
621 +" WHERE parents.parentID = ?"\
622 +" ORDER BY people.bornYear;"
624 for row in run_query(s,t):
625 output = output + print_tagged_name('Child',row,newLine)
627 #find children's other parent
628 u = "Select people.NAME, parents.parentID FROM"\
629 +" parents LEFT JOIN people"\
630 +" ON people.ID = parents.parentID"\
631 +" WHERE parents.ID = ? AND parents.parentID <> ?"
635 for r in run_query(u,ids):
636 output = output + print_tagged_name('With',r,newLine)
639 age = row[2]-bornYear
640 output = output[:-4] + " at the age of "+str(age) + newLine
642 output = output + newLine
648 conn = sqlite3.connect('/home/naath/familyTreeProject/familyTree/tree.db')
659 # [c, conn] = connect()