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) + ' century:' + newLine
155 out = out + name_html(row,newLine) +newLine
159 def count_names(newLine):
160 s = "SELECT firstName, count(*)"\
162 +" GROUP BY firstName"\
163 +" ORDER BY count(*) DESC;"
165 out = print_query(s,(),newLine)
170 def count_birth_month(newLine):
171 s = "SELECT bornMonth, count(*)"\
173 +" GROUP BY bornMonth"\
174 +" ORDER BY bornMonth;"
176 t = "SELECT * FROM people WHERE bornMonth = ?;"
179 for row in run_query(s,()):
180 month = month_numbers(row[0])
181 out = out + month + ': ' + str(row[1]) + newLine
185 out = out +print_query(t,u,newLine)
189 def count_death_month(newLine):
190 s = "SELECT diedMonth, count(*)"\
192 +" GROUP BY diedMonth"\
193 +" ORDER BY diedMonth;"
195 t = "SELECT * FROM people WHERE diedMonth = ?;"
198 for row in run_query(s,()):
199 month = month_numbers(row[0])
200 out = out + month + ': ' + str(row[1]) + newLine
204 out = out +print_query(t,u,newLine)
209 def all_ancestors(personID,newLine):
211 s = "SELECT people.Name,parents.parentID FROM"\
212 +" parents LEFT JOIN people"\
213 +" ON parents.parentID = people.ID"\
214 +" WHERE parents.ID = ?"\
215 +" AND parents.parentID <> '.';"
218 ancestors = [personID]
219 allAncestors = [personID]
223 t = "SELECT name,id FROM people WHERE id==?"
226 out = "Ancestors of "
227 for row in run_query(t,id):
228 out = out + name_html(row,newLine)+newLine
230 while len(ancestors)>0:
233 thisout = newLine + parent_level(level,'parent') +':' + newLine
234 for ancestor in ancestors:
236 for row in run_query(s,id):
237 thisout = thisout + name_html(row,newLine)+newLine
238 if row[1] not in allAncestors and is_number(row[1])!=0:
240 allAncestors.append(row[1])
241 trackLevel.append(level)
247 return [out, allAncestors,trackLevel]
250 def common_ancestors(IDA, IDB,newLine):
251 out = 'Common ancestors of:' + newLine
253 s = "SELECT name,id FROM people WHERE id==?"
259 for row in run_query(s,t):
260 out = out + name_html(row,newLine)+newLine
267 related = 'No details held on one party'
269 return [out,[],related]
272 a = all_ancestors(IDA,newLine)
273 b = all_ancestors(IDB,newLine)
275 ancestorsB = set(b[1])
276 ancestorsA = set(a[1])
278 common = ancestorsA.intersection(ancestorsB)
279 common = list(common)
286 aLevels.append(a[2][i])
288 bLevels.append(b[2][i])
290 s = "SELECT Name, ID, bornyear"\
293 for i in range(len(common)):
299 s = s+") ORDER BY bornyear;"
303 related = names[0]+' and '+names[1]+' are not related'
304 out = out + newLine + related
305 return [out, common,related]
308 out = out + print_tagged_query('',s,common,newLine)
313 for i in range(len(common)):
314 if aLevels[i] == min(aLevels):
316 if bLevels[i] == min(bLevels):
321 s = "SELECT name, id"\
325 out = out + newLine + 'Most Recent Common Ancestors:' + newLine
328 out = out + print_tagged_query('',s,t,newLine)
330 out = out + 'and' + newLine
332 out = out + parent_level(aLevels[indexA[0]],'parent')
336 out = out + ' of ' + name_html([names[0],IDA],newLine)+newLine
341 out = out + print_tagged_query('',s,t,newLine)
343 out = out + 'and' + newLine
345 out = out + parent_level(bLevels[indexB[0]],'parent')
348 out = out + ' of ' + name_html([names[1],IDB],newLine)+newLine
351 al = aLevels[indexA[0]]
352 bl = bLevels[indexB[0]]
354 related = relationship(al,bl,names)
355 out = out+newLine + related
357 return [out,common,related]
359 def relationship(level1, level2,names):
361 if level1==0 and level2==0:
362 return names[0] + ' is ' +names[1]
364 return names[0] + ' is ' + names[1] + '\'s '+ parent_level(level2,'parent')
366 return names[1] + ' is ' + names[0] + '\'s '+ parent_level(level1,'parent')
370 remove = level1-level2
373 remove = level2-level1
377 uaLevel = parent_level(remove,'uncle or aunt')
379 return names[0] + ' is ' + names[1] + '\'s ' + uaLevel
382 return names[1] + ' is ' + names[0] + '\'s ' + uaLevel
384 c=ordinal_numbers(cousinNum)
390 rem = str(remove) + ' times'
392 r = names[0] +' and '+names[1] +' are ' + c + ' cousins '
394 r = r+ rem + ' removed'
398 def parent_level(level,type):
413 for i in range(2,level):
417 def rulers_of(aTerritory,newLine):
419 tq = "SELECT name, people.ID, startyear,stopyear,territory"\
420 +" FROM territories INNER JOIN people"\
421 +" ON people.ID = territories.ID"\
422 +" WHERE territory LIKE ?"\
423 +" ORDER BY territory,startyear,stopyear;"
430 for row in run_query(tq,(aTerritory+'%',)):
431 if row[4]!=last and last!='':
432 out = out + 'Rulers of '+terr_html(last,newLine) +':'+ newLine +thisT +newLine
435 thisT = thisT +name_html(row,newLine)
436 thisT = thisT +' from ' + str(row[2])+' to '+str(row[3]) + newLine
439 out = out + 'Rulers of '+terr_html(row[4],newLine) +':'+ newLine +thisT
443 def person_info(personID,newLine):
448 #Id, Name, Dates, Style, Style-Dates
449 s = "SELECT * FROM people WHERE ID = ?"
450 for row in run_query(s,t):
451 output = output + 'ID: '+str(row[0]) +newLine
452 output = output + print_tagged_name('Name',[row[1], row[0]],newLine) +newLine
453 output = output + 'Born: '+row[3] + newLine
454 output = output + 'Died: '+row[5] + newLine
456 s = "SELECT * FROM styles WHERE ID = ?"
457 for row in run_query(s,t):
458 output = output +newLine+ 'Style: '+row[1] + newLine
460 output = output + 'Territories:' + newLine
462 u = "SELECT * FROM territories"\
463 +" WHERE ID =? AND startYear =? AND stopYear=?"
464 v=(personID,row[3],row[5])
467 for r in run_query(u,v):
468 output = output + terr_html(r[1],newLine) +','
471 output = output[:-1] + newLine
473 output = output + 'From: '+row[2] + newLine
474 output = output + 'To: '+row[4] + newLine
476 s = "SELECT people.Name,consort "\
477 +"FROM consorts LEFT JOIN people"\
478 +" ON people.ID = consorts.consort"\
479 +" WHERE consorts.ID = ?"
480 for row in run_query(s,t):
481 output = output + print_tagged_name('Consort',row,newLine)
483 output = output + newLine
485 s = "SELECT people.Name,parents.parentID FROM"\
486 +" parents LEFT JOIN people"\
487 +" ON parents.parentID = people.ID"\
488 +" WHERE parents.ID = ?"
489 for row in run_query(s,t):
490 output = output + print_tagged_name('Parent',row,newLine)
493 s = "SELECT people.NAME, marriages.IDb from"\
494 +" marriages LEFT JOIN people"\
495 +" ON people.ID = marriages.IDb"\
496 +" WHERE marriages.IDa = ?"
497 for row in run_query(s,t):
498 output = output + newLine
499 output = output + print_tagged_name('Spouse',row,newLine)
500 output = output + relationship_html(personID,row[1],newLine)
502 s = "SELECT people.NAME, marriages.IDa from"\
503 +" marriages LEFT JOIN people"\
504 +" ON people.ID = marriages.IDa"\
505 +" WHERE marriages.IDb = ?"
506 for row in run_query(s,t):
507 output = output + newLine
508 output = output + print_tagged_name('Spouse',row,newLine)
509 output = output + relationship_html(personID,row[1],newLine)
511 output = output + newLine
514 s = "Select people.NAME, people.ID from"\
515 +" people INNER JOIN parents"\
516 +" ON people.ID = parents.ID"\
517 +" WHERE parents.parentID = ?"
519 for row in run_query(s,t):
520 output = output + print_tagged_name('Child',row,newLine)
522 #find children's other parent
523 u = "Select people.NAME, parents.parentID FROM"\
524 +" parents LEFT JOIN people"\
525 +" ON people.ID = parents.parentID"\
526 +" WHERE parents.ID = ? AND parents.parentID <> ?"
530 for row in run_query(u,ids):
531 output = output + print_tagged_name('With',row,newLine)
533 output = output + newLine
539 conn = sqlite3.connect('/home/naath/familyTreeProject/familyTree/tree.db')
550 # [c, conn] = connect()