-
Notifications
You must be signed in to change notification settings - Fork 0
/
rm-unset-living.py
312 lines (228 loc) · 8.58 KB
/
rm-unset-living.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/python3
'''
Update a RootsMagic database file (v7/v8/v9) so that anyone above a set limit of
descendant generations is set to not-living. The limit is set below.
A list of the people being changed is printed to standard out.
Give the name of the database file as the program parameter.
Please run first on a test database or a copy of the true database.
This code is released under the MIT License: https://opensource.org/licenses/MIT
Copyright (c) 2021 John A. Andrea
Code is provided AS IS.
No support, discussion, maintenance, etc. is included or implied.
v2.2
'''
import sys
import os
import sqlite3
import datetime
import argparse
def get_program_options():
results = dict()
# defaults
results['sql-out'] = False
results['dry-run'] = False
results['verbose'] = False
results['max-age'] = 120
results['max-gen'] = 4
results['infile'] = None
arg_help = 'Unset the living flag based on age and descendent generations.'
parser = argparse.ArgumentParser( description=arg_help )
parser.add_argument('infile', type=argparse.FileType('r') )
arg_help = 'Do not change the database. Default: not selected'
parser.add_argument( '--dry-run', action='store_true', help=arg_help )
arg_help = 'Show everyone regardless of changes. Default: not selected'
parser.add_argument( '--verbose', action='store_true', help=arg_help )
arg_help = 'Output SQL statements for the updates for later database changes.'
parser.add_argument( '--sql-out', action='store_true', help=arg_help )
arg_help = 'Consider anyone deceased who was born or died this many years ago.'
arg_help += ' Default: ' + str(results['max-age'])
parser.add_argument( '--max-age', default=results['max-age'], type=int, help=arg_help )
arg_help = 'Consider anyone deceased who has more that this many generations of descendents.'
arg_help += ' Default: ' + str( results['max-gen'] )
parser.add_argument( '--max-gen', default=results['max-gen'], type=int, help=arg_help )
args = parser.parse_args()
results['sql-out'] = args.sql_out
results['dry-run'] = args.dry_run
results['verbose'] = args.verbose
results['max-age'] = args.max_age
results['max-gen'] = args.max_gen
results['infile'] = args.infile.name
# this selection forces other behaviour
if results['sql-out']:
results['verbose'] = False
results['dry-run'] = True
return results
def show_sql( p, name, count ):
print( '' )
print( '--', name, 'gen count', count )
print( 'update PersonTable set Living=0 where PersonID=', p, ';' )
def change_db_flag( db_file, id_list ):
try:
conn = sqlite3.connect( db_file )
cur = conn.cursor()
sql = 'update PersonTable set Living=0 where PersonID=?'
cur.executemany( sql, id_list )
cur.close()
conn.commit()
except Exception as e:
print( 'DBError:', e, str(e), file=sys.stderr )
finally:
if conn:
conn.close()
def from_name_table( db_file ):
data = dict()
try:
conn = sqlite3.connect( db_file )
cur = conn.cursor()
sql = 'select OwnerId, Surname, Given, BirthYear, DeathYear'
sql += ' from NameTable'
sql += ' where IsPrimary > 0'
cur.execute( sql )
for row in cur:
data[row[0]] = { 'surname':row[1], 'given':row[2], 'birth':row[3], 'death':row[4] }
cur.close()
except Exception as e:
print( 'DBError:', e, str(e), file=sys.stderr )
finally:
if conn:
conn.close()
return data
def from_family_table( db_file ):
data = dict()
try:
conn = sqlite3.connect( db_file )
cur = conn.cursor()
sql = 'select FamilyId, FatherId, MotherId'
sql += ' from FamilyTable'
cur.execute( sql )
for row in cur:
data[row[0]] = { 'husb': row[1], 'wife': row[2], 'children': [] }
cur.close()
# add all the children for each family
cur = conn.cursor()
sql = 'select FamilyId, ChildId'
sql += ' from ChildTable'
cur.execute( sql )
for row in cur:
data[row[0]]['children'].append( row[1] )
cur.close()
except Exception as e:
print( 'DBError:', e, str(e), file=sys.stderr )
finally:
if conn:
conn.close()
return data
def from_people_table( db_file ):
data = dict()
try:
conn = sqlite3.connect( db_file )
cur = conn.cursor()
sql = 'select PersonId, Living'
sql += ' from PersonTable'
cur.execute( sql )
for row in cur:
p_id = row[0]
data[p_id] = { 'living': row[1] }
# additional items not from the database
# initialize each count to None which will be tested differently than zero
# when the counting is performed
data[p_id]['gen-count'] = None
data[p_id]['families'] = []
data[p_id]['too-old'] = False
cur.close()
except Exception as e:
print( 'DBError:', e, str(e), file=sys.stderr )
finally:
if conn:
conn.close()
return data
def set_child_of_family():
# set the family links for each person
global people
global families
for f in families:
for c in families[f]['children']:
people[c]['child-of'] = f
for partner in ['husb','wife']:
partner_id = families[f][partner]
if partner_id in people:
people[partner_id]['families'].append( f )
def count_generations( p ):
global people
global families
result = 0
for f in people[p]['families']:
for c in families[f]['children']:
child_count = people[c]['gen-count']
if child_count is None:
child_count = count_generations( c )
people[c]['gen-count'] = child_count
result = max( result, 1 + child_count )
return result
def check_age( p, max_age, this_year ):
global names
result = False
if p in names:
if names[p]['death']:
# if there is a death...
result = True
if not result:
if names[p]['birth']:
result = ( this_year - names[p]['birth'] ) > max_age
return result
options = get_program_options()
db_file = options['infile']
if db_file.lower().endswith( '.rmgc' ) or db_file.lower().endswith( '.rmtree' ):
if os.path.isfile( db_file ):
this_year = datetime.datetime.now().year
people = from_people_table( db_file )
names = from_name_table( db_file )
families = from_family_table( db_file )
if options['verbose']:
print( 'people count', len(people) )
set_child_of_family()
for p in people:
if people[p]['gen-count'] is None:
people[p]['gen-count'] = count_generations( p )
people[p]['too-old'] = check_age( p, options['max-age'], this_year )
# track the ids which need to be updated
to_change = []
for p in people:
count = people[p]['gen-count']
name = names[p]['surname'] + ', ' + names[p]['given']
birth = names[p]['birth']
death = names[p]['death']
if birth or death:
name += ' ('
if birth:
name += str(birth)
name += '-'
if death:
name += str(death)
name += ')'
if options['verbose']:
print( 'id', p, name, '=', count )
if people[p]['living']:
if count > options['max-gen'] or people[p]['too-old']:
# needs to be a tuple for the database action
to_change.append( (p,) )
if options['sql-out']:
show_sql( p, name, count )
else:
if not options['verbose']:
print( 'id', p, name, '=', count )
print( ' to be changed' )
if not options['sql-out']:
if options['verbose']:
print( 'changes', len( to_change ) )
if options['dry-run']:
print( 'No changes - dry run' )
else:
if to_change:
change_db_flag( db_file, to_change )
else:
print( 'File not found:', db_file, file=sys.stderr )
sys.exit( 1 )
else:
print( 'Given file does not match RM name types:', db_file, file=sys.stderr )
sys.exit( 1 )