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, text = name)
68 return row[0] + "," +str(row[1])
70 def print_tagged_name(relationship,row,newLine):
72 out = relationship + " not yet entered: " + row[1]
79 out = name_html(row,html) + ' '
81 out = relationship + ": " + name_html(row,html)
84 def month_numbers(monthN):
86 month ='unknown month'
112 month = 'Incorrectly entered month ' + str(monthN)
115 def ordinal_numbers(number):
117 if number % 10==1 and number/10 % 10 !=1:
118 out = str(number) +'st'
119 elif number % 10==2 and number/10 % 10 !=1:
120 out = str(number) +'nd'
121 elif number % 10==3 and number/10 % 10 !=1:
122 out = str(number) +'rd'
124 out = str(number) +'th'
127 def list_territories(newLine):
128 s = "SELECT DISTINCT territory"\
129 +" FROM territories"\
130 +" ORDER BY territory;"
133 for row in run_query(s,()):
134 out =out + terr_html(row[0],newLine) +newLine
137 def list_people(newLine):
138 s = "SELECT name,id,bornyear"\
140 +" ORDER BY bornyear;"
144 out = out + 'born in unknown year:' +newLine
145 for row in run_query(s,()):
146 if row[2]!=0 and row[2]/100==0:
147 out = out +newLine+ 'born in 1st century:' +newLine
149 if row[2]/100!=year/100:
150 century = row[2]/100 + 1
151 out = out +newLine+ 'born in '
153 out = out +ordinal_numbers(century) \
154 + ' century:' + newLine
156 out = out + name_html(row,newLine) +newLine
158 if row[2] == 0: #unknown year
160 t = (row[1],) #person ID
164 u = "SELECT diedyear FROM people WHERE ID = ?;"
167 for r in run_query(u,t):
169 out = out + "died: "\
170 + str(r[0]) + newLine
171 bornAfter = r[0] -100
174 u = "Select people.bornYear from"\
175 +" people INNER JOIN parents"\
176 +" ON people.ID = parents.ID"\
177 +" WHERE parents.parentID = ?"\
178 + " ORDER BY people.bornYear;"
182 for r in run_query(u,t):
184 hadChild.append(r[0])
188 out = out + "had children in: "
190 out = out + str(c) + ','
191 out = out[:-1] + newLine
193 bornBefore = hadChild[0]-12
195 bornAfter = hadChild[0]-100
199 out = out + "probably born "\
200 +"after " + str(bornAfter)
202 out = out + "probably born "\
203 +"betwen " + str(bornAfter)\
204 +" and " + str(bornBefore)
211 def count_names(newLine):
212 s = "SELECT firstName, count(*)"\
214 +" GROUP BY firstName"\
215 +" ORDER BY count(*) DESC;"
217 out = print_query(s,(),newLine)
222 def count_birth_month(newLine):
223 s = "SELECT bornMonth, count(*)"\
225 +" GROUP BY bornMonth"\
226 +" ORDER BY bornMonth;"
228 t = "SELECT * FROM people WHERE bornMonth = ?;"
231 for row in run_query(s,()):
232 month = month_numbers(row[0])
233 out = out + month + ': ' + str(row[1]) + newLine
237 out = out +print_query(t,u,newLine)
241 def count_death_month(newLine):
242 s = "SELECT diedMonth, count(*)"\
244 +" GROUP BY diedMonth"\
245 +" ORDER BY diedMonth;"
247 t = "SELECT * FROM people WHERE diedMonth = ?;"
250 for row in run_query(s,()):
251 month = month_numbers(row[0])
252 out = out + month + ': ' + str(row[1]) + newLine
256 out = out +print_query(t,u,newLine)
260 def count_age_at_child(newLine):
262 s = "select p1.bornYear - p2.bornYear as age, count(*)"\
264 +" parents INNER JOIN people p1"\
265 +" ON parents.ID = p1.ID"\
266 +" INNER JOIN people p2"\
267 +" ON parents.parentID = p2.ID"\
268 +" WHERE p1.bornYear <> 0 and p2.bornYear<>0"\
271 out = print_query(s,(),newLine)
274 def all_ancestors(personID,newLine):
276 s = "SELECT people.Name,parents.parentID FROM"\
277 +" parents LEFT JOIN people"\
278 +" ON parents.parentID = people.ID"\
279 +" WHERE parents.ID = ?"\
280 +" AND parents.parentID <> '.';"
283 ancestors = [personID]
284 allAncestors = [personID]
288 t = "SELECT name,id FROM people WHERE id==?"
291 out = "Ancestors of "
292 for row in run_query(t,id):
293 out = out + name_html(row,newLine)+newLine
295 while len(ancestors)>0:
298 thisout = newLine + parent_level(level,'parent') +':' + newLine
299 for ancestor in ancestors:
301 for row in run_query(s,id):
302 thisout = thisout + name_html(row,newLine)+newLine
303 if row[1] not in allAncestors and is_number(row[1])!=0:
305 allAncestors.append(row[1])
306 trackLevel.append(level)
312 return [out, allAncestors,trackLevel]
315 def common_ancestors(IDA, IDB,newLine):
316 out = 'Common ancestors of:' + newLine
318 s = "SELECT name,id FROM people WHERE id==?"
324 for row in run_query(s,t):
325 out = out + name_html(row,newLine)+newLine
332 related = 'No details held on one party'
334 return [out,[],related]
337 a = all_ancestors(IDA,newLine)
338 b = all_ancestors(IDB,newLine)
340 ancestorsB = set(b[1])
341 ancestorsA = set(a[1])
343 common = ancestorsA.intersection(ancestorsB)
344 common = list(common)
351 aLevels.append(a[2][i])
353 bLevels.append(b[2][i])
355 s = "SELECT Name, ID, bornyear"\
358 for i in range(len(common)):
364 s = s+") ORDER BY bornyear;"
368 related = names[0]+' and '+names[1]+' are not related'
369 out = out + newLine + related
370 return [out, common,related]
373 out = out + print_tagged_query('',s,common,newLine)
378 for i in range(len(common)):
379 if aLevels[i] == min(aLevels):
381 if bLevels[i] == min(bLevels):
386 s = "SELECT name, id"\
390 out = out + newLine + 'Most Recent Common Ancestors:' + newLine
393 out = out + print_tagged_query('',s,t,newLine)
395 out = out + 'and' + newLine
397 out = out + parent_level(aLevels[indexA[0]],'parent')
401 out = out + ' of ' + name_html([names[0],IDA],newLine)+newLine
406 out = out + print_tagged_query('',s,t,newLine)
408 out = out + 'and' + newLine
410 out = out + parent_level(bLevels[indexB[0]],'parent')
413 out = out + ' of ' + name_html([names[1],IDB],newLine)+newLine
416 al = aLevels[indexA[0]]
417 bl = bLevels[indexB[0]]
419 related = relationship(al,bl,names)
420 out = out+newLine + related
422 return [out,common,related]
424 def relationship(level1, level2,names):
426 if level1==0 and level2==0:
427 return names[0] + ' is ' +names[1]
429 return names[0] + ' is ' + names[1] + '\'s '+ parent_level(level2,'parent')
431 return names[1] + ' is ' + names[0] + '\'s '+ parent_level(level1,'parent')
435 remove = level1-level2
438 remove = level2-level1
442 uaLevel = parent_level(remove,'uncle or aunt')
444 return names[0] + ' is ' + names[1] + '\'s ' + uaLevel
447 return names[1] + ' is ' + names[0] + '\'s ' + uaLevel
449 c=ordinal_numbers(cousinNum)
455 rem = str(remove) + ' times'
457 r = names[0] +' and '+names[1] +' are ' + c + ' cousins '
459 r = r+ rem + ' removed'
463 def parent_level(level,type):
478 for i in range(2,level):
482 def rulers_of(aTerritory,newLine):
484 tq = "SELECT name, people.ID, startyear,stopyear,territory"\
485 +" FROM territories INNER JOIN people"\
486 +" ON people.ID = territories.ID"\
487 +" WHERE territory LIKE ?"\
488 +" ORDER BY territory,startyear,stopyear;"
495 for row in run_query(tq,(aTerritory+'%',)):
496 if row[4]!=last and last!='':
497 out = out + 'Rulers of '+terr_html(last,newLine) +':'+ newLine +thisT +newLine
500 thisT = thisT +name_html(row,newLine)
501 thisT = thisT +' from ' + str(row[2])+' to '+str(row[3]) + newLine
504 out = out + 'Rulers of '+terr_html(row[4],newLine) +':'+ newLine +thisT
508 def person_info(personID,newLine):
513 #Id, Name, Dates, Style, Style-Dates
514 s = "SELECT * FROM people WHERE ID = ?"
515 for row in run_query(s,t):
516 output = output + 'ID: '+str(row[0]) +newLine
517 output = output + print_tagged_name('Name',[row[1], row[0]],newLine) +newLine
518 output = output + 'Born: '+row[3] + newLine
520 output = output + 'Died: '+row[5] + newLine
522 s = "SELECT * FROM styles WHERE ID = ?"
523 for row in run_query(s,t):
524 output = output +newLine+ 'Style: '+row[1] + newLine
526 output = output + 'Territories:' + newLine
528 u = "SELECT * FROM territories"\
529 +" WHERE ID =? AND startYear =? AND stopYear=?"
530 v=(personID,row[3],row[5])
533 for r in run_query(u,v):
534 output = output + terr_html(r[1],newLine) +','
537 output = output[:-1] + newLine
539 output = output + 'From: '+row[2] + newLine
540 output = output + 'To: '+row[4] + newLine
542 s = "SELECT people.Name,consort "\
543 +"FROM consorts LEFT JOIN people"\
544 +" ON people.ID = consorts.consort"\
545 +" WHERE consorts.ID = ?"
546 for row in run_query(s,t):
547 output = output + print_tagged_name('Consort',row,newLine)
549 output = output + newLine
551 s = "SELECT people.Name,parents.parentID FROM"\
552 +" parents LEFT JOIN people"\
553 +" ON parents.parentID = people.ID"\
554 +" WHERE parents.ID = ?"
555 for row in run_query(s,t):
556 output = output + print_tagged_name('Parent',row,newLine)
559 s = "SELECT people.NAME, marriages.IDb from"\
560 +" marriages LEFT JOIN people"\
561 +" ON people.ID = marriages.IDb"\
562 +" WHERE marriages.IDa = ?"
563 for row in run_query(s,t):
564 output = output + newLine
565 output = output + print_tagged_name('Spouse',row,newLine)
566 output = output + relationship_html(personID,row[1],newLine)
568 s = "SELECT people.NAME, marriages.IDa from"\
569 +" marriages LEFT JOIN people"\
570 +" ON people.ID = marriages.IDa"\
571 +" WHERE marriages.IDb = ?"
572 for row in run_query(s,t):
573 output = output + newLine
574 output = output + print_tagged_name('Spouse',row,newLine)
575 output = output + relationship_html(personID,row[1],newLine)
577 output = output + newLine
580 s = "Select people.NAME, people.ID ,people.bornYear"\
581 +" FROM people INNER JOIN parents"\
582 +" ON people.ID = parents.ID"\
583 +" WHERE parents.parentID = ?"\
584 +" ORDER BY people.bornYear;"
586 for row in run_query(s,t):
587 output = output + print_tagged_name('Child',row,newLine)
589 #find children's other parent
590 u = "Select people.NAME, parents.parentID FROM"\
591 +" parents LEFT JOIN people"\
592 +" ON people.ID = parents.parentID"\
593 +" WHERE parents.ID = ? AND parents.parentID <> ?"
597 for r in run_query(u,ids):
598 output = output + print_tagged_name('With',r,newLine)
601 age = row[2]-bornYear
602 output = output[:-4] + " at the age of "+str(age) + newLine
604 output = output + newLine
610 conn = sqlite3.connect('/home/naath/familyTreeProject/familyTree/tree.db')
621 # [c, conn] = connect()