10 def print_row(row,newLine):
13 out = out + str(item)+'|'
14 return out[:-1] + newLine
16 def print_query(s,t,newLine):
18 for row in run_query(s,t):
19 printMe = printMe + print_row(row,newLine)
29 def print_relation(relationship,s,t,newLine):
31 for row in run_query(s,t):
32 mine = mine + print_none_value(relationship,row,newLine)
35 def relationship_html(ID,ID2,newLine):
37 relationship = common_ancestors(ID,ID2,newLine)[2]
38 url="http://www.chiark.greenend.org.uk/ucgi/~naath/ancestors.py?ID="+ID+"&ID2="\
40 return relationship + ':<a href='+url+'> Common Ancestors</a>'+newLine
44 def terr_html(terr,newLine):
46 url = "http://www.chiark.greenend.org.uk/ucgi/~naath/territory.py?terr="
47 return '<a href ='+url + terr+'>'+terr+'</a>'
51 def row_html(row,html):
53 url = "http://www.chiark.greenend.org.uk/ucgi/~naath/person.py?ID=" + str(row[1])
55 return "<a href = "+url+">"+name+"</a>"
59 return row[0] + "," +str(row[1])
61 def print_none_value(relationship,row,newLine):
63 out = relationship + " not yet entered: " + row[1]
70 out = row_html(row,html) + ' '
72 out = relationship + ": " + row_html(row,html)
75 def list_territories(newLine):
76 s = "SELECT DISTINCT territory"\
78 +" ORDER BY territory;"
81 for row in run_query(s,()):
82 out =out + terr_html(row[0],newLine) +newLine
85 def list_people(newLine):
86 s = "SELECT name,id,bornyear"\
88 +" ORDER BY bornyear;"
92 out = out + 'born in unknown year:' +newLine
93 for row in run_query(s,()):
94 if row[2]!=0 and row[2]/100==0:
95 out = out +newLine+ 'born in 1st century:' +newLine
97 if row[2]/100!=year/100:
98 century = row[2]/100 + 1
99 out = out +newLine+ 'born in ' + str(century)
108 out = out + ' century:' + newLine
110 out = out + print_none_value('',row,newLine)
114 def count_names(newLine):
115 s = "SELECT firstName, count(*)"\
117 +" GROUP BY firstName"\
118 +" ORDER BY count(*) DESC;"
120 out = print_query(s,(),newLine)
125 def all_ancestors(personID,newLine):
127 s = "SELECT people.Name,parents.parentID FROM"\
128 +" parents LEFT JOIN people"\
129 +" ON parents.parentID = people.ID"\
130 +" WHERE parents.ID = ?"\
131 +" AND parents.parentID <> '.';"
134 ancestors = [personID]
135 allAncestors = [personID]
139 t = "SELECT name,id FROM people WHERE id==?"
142 out = "Ancestors of "
143 for row in run_query(t,id):
144 out = out + print_none_value('',row,newLine)
146 while len(ancestors)>0:
149 out = out+newLine + parent_level(level) +':' + newLine
150 for ancestor in ancestors:
152 for row in run_query(s,id):
153 out = out + print_none_value('',row,newLine)
154 if row[1] not in allAncestors and is_number(row[1])!=0:
156 allAncestors.append(row[1])
157 trackLevel.append(level)
161 return [out, allAncestors,trackLevel]
164 def common_ancestors(IDA, IDB,newLine):
165 out = 'Common ancestors of:' + newLine
167 s = "SELECT name,id FROM people WHERE id==?"
173 for row in run_query(s,t):
174 out = out + print_none_value('',row,newLine)
181 related = 'No details held on one party'
183 return [out,[],related]
186 a = all_ancestors(IDA,newLine)
187 b = all_ancestors(IDB,newLine)
189 ancestorsB = set(b[1])
190 ancestorsA = set(a[1])
192 common = ancestorsA.intersection(ancestorsB)
193 common = list(common)
200 aLevels.append(a[2][i])
202 bLevels.append(b[2][i])
204 s = "SELECT Name, ID, bornyear"\
207 for i in range(len(common)):
213 s = s+") ORDER BY bornyear;"
217 related = names[0]+' and '+names[1]+' are not related'
218 out = out + newLine + related
219 return [out, common,related]
222 out = out + print_relation('',s,common,newLine)
227 for i in range(len(common)):
228 if aLevels[i] == min(aLevels):
230 if bLevels[i] == min(bLevels):
235 s = "SELECT name, id"\
239 out = out + newLine + 'Most Recent Common Ancestors:' + newLine
242 out = out + print_relation('',s,t,newLine)
244 out = out + 'and' + newLine
246 out = out + parent_level(aLevels[indexA[0]])
250 out = out + ' of ' + print_none_value('',[names[0],IDA],newLine)
255 out = out + print_relation('',s,t,newLine)
257 out = out + 'and' + newLine
259 out = out + parent_level(bLevels[indexB[0]])
262 out = out + ' of ' + print_none_value('',[names[1],IDB],newLine)
265 al = aLevels[indexA[0]]
266 bl = bLevels[indexB[0]]
268 related = relationship(al,bl,names)
269 out = out+newLine + related
271 return [out,common,related]
273 def relationship(level1, level2,names):
275 if level1==0 and level2==0:
276 return names[0] + ' is ' +names[1]
278 return names[0] + ' is ' + names[1] + '\'s '+ parent_level(level2)
280 return names[1] + ' is ' + names[0] + '\'s '+ parent_level(level1)
285 return names[0] +' and '+names[1] +' are '+' siblings'
290 remove = level1-level2
293 remove = level2-level1
298 return names[0] + ' is ' + names[1] + '\'s uncle or aunt'
301 return names[1] + ' is ' + names[0] + '\'s uncle or aunt'
310 c = str(cousinNum)+'th'
317 rem = str(remove) + ' times'
319 r = names[0] +' and '+names[1] +' are ' + c + ' cousins '
321 r = r+ rem + ' removed'
325 def parent_level(level):
334 for i in range(2,level):
338 def rulers_of(aTerritory,newLine):
340 tq = "SELECT name, people.ID, startyear,stopyear,territory"\
341 +" FROM territories INNER JOIN people"\
342 +" ON people.ID = territories.ID"\
343 +" WHERE territory LIKE ?"\
344 +" ORDER BY territory,startyear,stopyear;"
351 for row in run_query(tq,(aTerritory+'%',)):
352 if row[4]!=last and last!='':
353 out = out + 'Rulers of '+terr_html(last,newLine) +':'+ newLine +thisT +newLine
356 thisT = thisT +print_none_value('',row,newLine)
357 thisT = thisT[:-4] + ' from ' + str(row[2])+' to '+str(row[3]) + newLine
360 out = out + 'Rulers of '+terr_html(row[4],newLine) +':'+ newLine +thisT
364 def person_info(personID,newLine):
369 #Id, Name, Dates, Style, Style-Dates
370 s = "SELECT * FROM people WHERE ID = ?"
371 for row in run_query(s,t):
372 output = output + 'ID: '+str(row[0]) +newLine
373 output = output + 'Name: '+print_none_value('',[row[1], row[0]],newLine) +newLine
374 output = output + 'Born: '+row[3] + newLine
375 output = output + 'Died: '+row[5] + newLine
377 s = "SELECT * FROM styles WHERE ID = ?"
378 for row in run_query(s,t):
379 output = output +newLine+ 'Style: '+row[1] + newLine
381 output = output + 'Territories:' + newLine
383 u = "SELECT * FROM territories"\
384 +" WHERE ID =? AND startYear =? AND stopYear=?"
385 v=(personID,row[3],row[5])
388 for r in run_query(u,v):
389 output = output + terr_html(r[1],newLine) +','
392 output = output[:-1] + newLine
394 output = output + 'From: '+row[2] + newLine
395 output = output + 'To: '+row[4] + newLine
397 output = output + newLine
399 s = "SELECT people.Name,parents.parentID FROM"\
400 +" parents LEFT JOIN people"\
401 +" ON parents.parentID = people.ID"\
402 +" WHERE parents.ID = ?"
403 for row in run_query(s,t):
404 output = output + print_none_value('Parent',row,newLine)
407 s = "SELECT people.NAME, marriages.IDb from"\
408 +" marriages LEFT JOIN people"\
409 +" ON people.ID = marriages.IDb"\
410 +" WHERE marriages.IDa = ?"
411 for row in run_query(s,t):
412 output = output + newLine
413 output = output + print_none_value('Spouse',row,newLine)
414 output = output + relationship_html(personID,row[1],newLine)
416 s = "SELECT people.NAME, marriages.IDa from"\
417 +" marriages LEFT JOIN people"\
418 +" ON people.ID = marriages.IDa"\
419 +" WHERE marriages.IDb = ?"
420 for row in run_query(s,t):
421 output = output + newLine
422 output = output + print_none_value('Spouse',row,newLine)
423 output = output + relationship_html(personID,row[1],newLine)
425 output = output + newLine
428 s = "Select people.NAME, people.ID from"\
429 +" people INNER JOIN parents"\
430 +" ON people.ID = parents.ID"\
431 +" WHERE parents.parentID = ?"
433 for row in run_query(s,t):
434 output = output + print_none_value('Child',row,newLine)
436 #find children's other parent
437 u = "Select people.NAME, parents.parentID FROM"\
438 +" parents LEFT JOIN people"\
439 +" ON people.ID = parents.parentID"\
440 +" WHERE parents.ID = ? AND parents.parentID <> ?"
444 for row in run_query(u,ids):
445 output = output + print_none_value('With',row,newLine)
447 output = output + newLine
453 conn = sqlite3.connect('/home/naath/familyTree/tree.db')
464 # [c, conn] = connect()