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):
60 script = "person.py?ID=" + str(row[1])
62 return link_Template.substitute(script = script, text = name)
66 return row[0] + "," +str(row[1])
68 def print_tagged_name(relationship,row,newLine):
70 out = relationship + " not yet entered: " + row[1]
77 out = name_html(row,html) + ' '
79 out = relationship + ": " + name_html(row,html)
83 def ordinal_numbers(number):
85 if number % 10==1 and number/10 % 10 !=1:
86 out = str(number) +'st'
87 elif number % 10==2 and number/10 % 10 !=1:
88 out = str(number) +'nd'
89 elif number % 10==3 and number/10 % 10 !=1:
90 out = str(number) +'rd'
92 out = str(number) +'th'
95 def list_territories(newLine):
96 s = "SELECT DISTINCT territory"\
98 +" ORDER BY territory;"
101 for row in run_query(s,()):
102 out =out + terr_html(row[0],newLine) +newLine
105 def list_people(newLine):
106 s = "SELECT name,id,bornyear"\
108 +" ORDER BY bornyear;"
112 out = out + 'born in unknown year:' +newLine
113 for row in run_query(s,()):
114 if row[2]!=0 and row[2]/100==0:
115 out = out +newLine+ 'born in 1st century:' +newLine
117 if row[2]/100!=year/100:
118 century = row[2]/100 + 1
119 out = out +newLine+ 'born in '
121 out = out +ordinal_numbers(century) + ' century:' + newLine
123 out = out + name_html(row,newLine) +newLine
127 def count_names(newLine):
128 s = "SELECT firstName, count(*)"\
130 +" GROUP BY firstName"\
131 +" ORDER BY count(*) DESC;"
133 out = print_query(s,(),newLine)
138 def all_ancestors(personID,newLine):
140 s = "SELECT people.Name,parents.parentID FROM"\
141 +" parents LEFT JOIN people"\
142 +" ON parents.parentID = people.ID"\
143 +" WHERE parents.ID = ?"\
144 +" AND parents.parentID <> '.';"
147 ancestors = [personID]
148 allAncestors = [personID]
152 t = "SELECT name,id FROM people WHERE id==?"
155 out = "Ancestors of "
156 for row in run_query(t,id):
157 out = out + name_html(row,newLine)+newLine
159 while len(ancestors)>0:
162 thisout = newLine + parent_level(level,'parent') +':' + newLine
163 for ancestor in ancestors:
165 for row in run_query(s,id):
166 thisout = thisout + name_html(row,newLine)+newLine
167 if row[1] not in allAncestors and is_number(row[1])!=0:
169 allAncestors.append(row[1])
170 trackLevel.append(level)
176 return [out, allAncestors,trackLevel]
179 def common_ancestors(IDA, IDB,newLine):
180 out = 'Common ancestors of:' + newLine
182 s = "SELECT name,id FROM people WHERE id==?"
188 for row in run_query(s,t):
189 out = out + name_html(row,newLine)+newLine
196 related = 'No details held on one party'
198 return [out,[],related]
201 a = all_ancestors(IDA,newLine)
202 b = all_ancestors(IDB,newLine)
204 ancestorsB = set(b[1])
205 ancestorsA = set(a[1])
207 common = ancestorsA.intersection(ancestorsB)
208 common = list(common)
215 aLevels.append(a[2][i])
217 bLevels.append(b[2][i])
219 s = "SELECT Name, ID, bornyear"\
222 for i in range(len(common)):
228 s = s+") ORDER BY bornyear;"
232 related = names[0]+' and '+names[1]+' are not related'
233 out = out + newLine + related
234 return [out, common,related]
237 out = out + print_tagged_query('',s,common,newLine)
242 for i in range(len(common)):
243 if aLevels[i] == min(aLevels):
245 if bLevels[i] == min(bLevels):
250 s = "SELECT name, id"\
254 out = out + newLine + 'Most Recent Common Ancestors:' + newLine
257 out = out + print_tagged_query('',s,t,newLine)
259 out = out + 'and' + newLine
261 out = out + parent_level(aLevels[indexA[0]],'parent')
265 out = out + ' of ' + name_html([names[0],IDA],newLine)+newLine
270 out = out + print_tagged_query('',s,t,newLine)
272 out = out + 'and' + newLine
274 out = out + parent_level(bLevels[indexB[0]],'parent')
277 out = out + ' of ' + name_html([names[1],IDB],newLine)+newLine
280 al = aLevels[indexA[0]]
281 bl = bLevels[indexB[0]]
283 related = relationship(al,bl,names)
284 out = out+newLine + related
286 return [out,common,related]
288 def relationship(level1, level2,names):
290 if level1==0 and level2==0:
291 return names[0] + ' is ' +names[1]
293 return names[0] + ' is ' + names[1] + '\'s '+ parent_level(level2,'parent')
295 return names[1] + ' is ' + names[0] + '\'s '+ parent_level(level1,'parent')
299 remove = level1-level2
302 remove = level2-level1
306 uaLevel = parent_level(remove,'uncle or aunt')
308 return names[0] + ' is ' + names[1] + '\'s ' + uaLevel
311 return names[1] + ' is ' + names[0] + '\'s ' + uaLevel
313 c=ordinal_numbers(cousinNum)
319 rem = str(remove) + ' times'
321 r = names[0] +' and '+names[1] +' are ' + c + ' cousins '
323 r = r+ rem + ' removed'
327 def parent_level(level,type):
342 for i in range(2,level):
346 def rulers_of(aTerritory,newLine):
348 tq = "SELECT name, people.ID, startyear,stopyear,territory"\
349 +" FROM territories INNER JOIN people"\
350 +" ON people.ID = territories.ID"\
351 +" WHERE territory LIKE ?"\
352 +" ORDER BY territory,startyear,stopyear;"
359 for row in run_query(tq,(aTerritory+'%',)):
360 if row[4]!=last and last!='':
361 out = out + 'Rulers of '+terr_html(last,newLine) +':'+ newLine +thisT +newLine
364 thisT = thisT +name_html(row,newLine)
365 thisT = thisT +' from ' + str(row[2])+' to '+str(row[3]) + newLine
368 out = out + 'Rulers of '+terr_html(row[4],newLine) +':'+ newLine +thisT
372 def person_info(personID,newLine):
377 #Id, Name, Dates, Style, Style-Dates
378 s = "SELECT * FROM people WHERE ID = ?"
379 for row in run_query(s,t):
380 output = output + 'ID: '+str(row[0]) +newLine
381 output = output + print_tagged_name('Name',[row[1], row[0]],newLine) +newLine
382 output = output + 'Born: '+row[3] + newLine
383 output = output + 'Died: '+row[5] + newLine
385 s = "SELECT * FROM styles WHERE ID = ?"
386 for row in run_query(s,t):
387 output = output +newLine+ 'Style: '+row[1] + newLine
389 output = output + 'Territories:' + newLine
391 u = "SELECT * FROM territories"\
392 +" WHERE ID =? AND startYear =? AND stopYear=?"
393 v=(personID,row[3],row[5])
396 for r in run_query(u,v):
397 output = output + terr_html(r[1],newLine) +','
400 output = output[:-1] + newLine
402 output = output + 'From: '+row[2] + newLine
403 output = output + 'To: '+row[4] + newLine
405 output = output + newLine
407 s = "SELECT people.Name,parents.parentID FROM"\
408 +" parents LEFT JOIN people"\
409 +" ON parents.parentID = people.ID"\
410 +" WHERE parents.ID = ?"
411 for row in run_query(s,t):
412 output = output + print_tagged_name('Parent',row,newLine)
415 s = "SELECT people.NAME, marriages.IDb from"\
416 +" marriages LEFT JOIN people"\
417 +" ON people.ID = marriages.IDb"\
418 +" WHERE marriages.IDa = ?"
419 for row in run_query(s,t):
420 output = output + newLine
421 output = output + print_tagged_name('Spouse',row,newLine)
422 output = output + relationship_html(personID,row[1],newLine)
424 s = "SELECT people.NAME, marriages.IDa from"\
425 +" marriages LEFT JOIN people"\
426 +" ON people.ID = marriages.IDa"\
427 +" WHERE marriages.IDb = ?"
428 for row in run_query(s,t):
429 output = output + newLine
430 output = output + print_tagged_name('Spouse',row,newLine)
431 output = output + relationship_html(personID,row[1],newLine)
433 output = output + newLine
436 s = "Select people.NAME, people.ID from"\
437 +" people INNER JOIN parents"\
438 +" ON people.ID = parents.ID"\
439 +" WHERE parents.parentID = ?"
441 for row in run_query(s,t):
442 output = output + print_tagged_name('Child',row,newLine)
444 #find children's other parent
445 u = "Select people.NAME, parents.parentID FROM"\
446 +" parents LEFT JOIN people"\
447 +" ON people.ID = parents.parentID"\
448 +" WHERE parents.ID = ? AND parents.parentID <> ?"
452 for row in run_query(u,ids):
453 output = output + print_tagged_name('With',row,newLine)
455 output = output + newLine
461 conn = sqlite3.connect('/home/naath/familyTreeProject/familyTree/tree.db')
472 # [c, conn] = connect()