-
Notifications
You must be signed in to change notification settings - Fork 11
/
NormaliseOpenQDA.py
executable file
·374 lines (327 loc) · 16.1 KB
/
NormaliseOpenQDA.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2016 Jonathan Schultz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
from builtins import chr
from sqlalchemy import *
from sqlalchemy import exc
import warnings
import sys
import os
import argparse
import uuid
import datetime
import urllib2
import webcolors
exec(open(os.path.dirname(os.path.realpath(__file__)) + os.path.sep + 'DataTypes.py').read())
try:
parser = argparse.ArgumentParser(description='Normalise an offloaded NVivo project.')
table_choices = ["", "skip", "merge", "overwrite", "replace"]
parser.add_argument('-n', '--nodes', choices=table_choices, default="merge",
help='Node action.')
parser.add_argument('-na', '--node-attributes', choices=table_choices, default="merge",
help='Node attribute table action.')
parser.add_argument('--sources', choices=table_choices, default="merge",
help='Source action.')
parser.add_argument('-sa', '--source-attributes', choices=table_choices, default="merge",
help='Source attribute action.')
parser.add_argument('-t', '--taggings', choices=table_choices, default="merge",
help='Tagging action.')
parser.add_argument('-u', '--users', choices=table_choices, default="merge",
help='User action.')
parser.add_argument('-U', '--user', type=str,
help='Username for fetching images')
parser.add_argument('-P', '--password', type=str,
help='Password for fetching images')
parser.add_argument('--url', type=str, default='http://localhost/images/',
help='URL for fetching images')
parser.add_argument('infile', type=str,
help='SQLAlchemy path of input OpenQDA database or "-" to create empty project.')
parser.add_argument('outfile', type=str, nargs='?',
help='SQLAlchemy path of output normalised database.')
args = parser.parse_args()
if args.infile != '-':
oqdadb = create_engine(args.infile)
oqdamd = MetaData(bind=oqdadb)
oqdamd.reflect(oqdadb)
else:
oqdadb = None
if args.outfile is None:
args.outfile = args.infile.rsplit('.',1)[0] + '.norm'
normdb = create_engine(args.outfile)
normmd = MetaData(bind=normdb)
normmd.reflect(normdb)
# Create the normalised database structure
normUser = normmd.tables.get('User')
if normUser == None:
normUser = Table('User', normmd,
Column('Id', UUID(), primary_key=True),
Column('Name', String(256)))
normProject = normmd.tables.get('Project')
if normProject == None:
normProject = Table('Project', normmd,
Column('Title', String(256), nullable=False),
Column('Description', String(2048)),
Column('CreatedBy', UUID(), ForeignKey("User.Id"), nullable=False),
Column('CreatedDate', DateTime, nullable=False),
Column('ModifiedBy', UUID(), ForeignKey("User.Id"), nullable=False),
Column('ModifiedDate', DateTime, nullable=False))
normSource = normmd.tables.get('Source')
if normSource == None:
normSource = Table('Source', normmd,
Column('Id', UUID(), primary_key=True),
Column('Category', UUID()),
Column('Name', String(256)),
Column('Description', String(512)),
Column('Color', Integer),
Column('Content', String(16384)),
Column('ObjectType', String(256)),
Column('SourceType', Integer),
Column('Object', LargeBinary, nullable=False),
Column('Thumbnail', LargeBinary, nullable=False),
#Column('Waveform', LargeBinary, nullable=False),
Column('CreatedBy', UUID(), ForeignKey("User.Id")),
Column('CreatedDate', DateTime),
Column('ModifiedBy', UUID(), ForeignKey("User.Id")),
Column('ModifiedDate', DateTime))
normSourceCategory = normmd.tables.get('SourceCategory')
if normSourceCategory == None:
normSourceCategory = Table('SourceCategory', normmd,
Column('Id', UUID(), primary_key=True),
Column('Name', String(256)),
Column('Description', String(512)),
Column('CreatedBy', UUID(), ForeignKey("User.Id")),
Column('CreatedDate', DateTime),
Column('ModifiedBy', UUID(), ForeignKey("User.Id")),
Column('ModifiedDate', DateTime))
normTagging = normmd.tables.get('Tagging')
if normTagging == None:
normTagging = Table('Tagging', normmd,
Column('Source', UUID(), ForeignKey("Source.Id")),
Column('Node', UUID(), ForeignKey("Node.Id")),
Column('Fragment', String(256)),
Column('Memo', String(256)),
Column('CreatedBy', UUID(), ForeignKey("User.Id")),
Column('CreatedDate', DateTime),
Column('ModifiedBy', UUID(), ForeignKey("User.Id")),
Column('ModifiedDate', DateTime))
normNode = normmd.tables.get('Node')
if normNode == None:
normNode = Table('Node', normmd,
Column('Id', UUID(), primary_key=True),
Column('Parent', UUID(), ForeignKey("Node.Id")),
Column('Category', UUID(), ForeignKey("NodeCategory.Id")),
Column('Name', String(256)),
Column('Description', String(512)),
Column('Color', Integer),
Column('Aggregate', Boolean),
Column('CreatedBy', UUID(), ForeignKey("User.Id")),
Column('CreatedDate', DateTime),
Column('ModifiedBy', UUID(), ForeignKey("User.Id")),
Column('ModifiedDate', DateTime))
normNodeCategory = normmd.tables.get('NodeCategory')
if normNodeCategory == None:
normNodeCategory = Table('NodeCategory', normmd,
Column('Id', UUID(), primary_key=True),
Column('Name', String(256)),
Column('Description', String(512)),
Column('CreatedBy', UUID(), ForeignKey("User.Id")),
Column('CreatedDate', DateTime),
Column('ModifiedBy', UUID(), ForeignKey("User.Id")),
Column('ModifiedDate', DateTime))
normSourceAttribute = normmd.tables.get('SourceAttribute')
if normSourceAttribute == None:
normSourceAttribute = Table('SourceAttribute', normmd,
Column('Source', UUID(), ForeignKey("Source.Id"), primary_key=True),
Column('Name', String(256), primary_key=True),
Column('Value', String(256)),
Column('Type', String(16)),
Column('Length', Integer),
Column('CreatedBy', UUID(), ForeignKey("User.Id")),
Column('CreatedDate', DateTime),
Column('ModifiedBy', UUID(), ForeignKey("User.Id")),
Column('ModifiedDate', DateTime))
normNodeAttribute = normmd.tables.get('NodeAttribute')
if normNodeAttribute == None:
normNodeAttribute = Table('NodeAttribute', normmd,
Column('Node', UUID(), ForeignKey("Node.Id"), primary_key=True),
Column('Name', String(256), primary_key=True),
Column('Value', String(256)),
Column('Type', String(16)),
Column('Length', Integer),
Column('CreatedBy', UUID(), ForeignKey("User.Id")),
Column('CreatedDate', DateTime),
Column('ModifiedBy', UUID(), ForeignKey("User.Id")),
Column('ModifiedDate', DateTime))
normmd.create_all(normdb)
if oqdadb == None:
sys.exit()
# OpenQDA tables
oqdaattributes = oqdamd.tables['attributes']
oqdacodes = oqdamd.tables['codes']
oqdaimageAttributes = oqdamd.tables['imageAttributes']
oqdaimageCoding = oqdamd.tables['imageCoding']
oqdaimages = oqdamd.tables['images']
# Users
if args.users != 'skip':
print("Normalising users", file=sys.stderr)
# Collect unique users from images and imageCoding tables
userlist = [row.owner for row in oqdadb.execute(select([oqdaimageCoding.c.owner]))] + \
[row.owner for row in oqdadb.execute(select([oqdaimages.c.owner]))]
users = {}
for user in set(userlist):
userid = uuid.uuid4()
users[user] = userid
normdb.execute(normUser.insert(), {
'Id' : userid,
'Name': user})
defaultuserid = users[userlist[1]]
# Project
#if args.project != 'skip':
if True:
print("Normalising project", file=sys.stderr)
# Get earliest and latest timestamp
dateset = set([row.date for row in oqdadb.execute(select([oqdaimageCoding.c.date]))] +
[row.date for row in oqdadb.execute(select([oqdaimages.c.date]))])
normdb.execute(normProject.insert(), {
'Title' : 'OpenQDA Project',
'Description' : 'Exported from ' + args.infile,
'CreatedBy' : defaultuserid,
'CreatedDate' : min(dateset),
'ModifiedBy' : defaultuserid,
'ModifiedDate': max(dateset)})
# Nodes
if args.nodes != 'skip':
print("Normalising nodes", file=sys.stderr)
sel = select([oqdacodes.c.id,
oqdacodes.c.name,
oqdacodes.c.memo,
oqdacodes.c.color])
codes = [dict(row) for row in oqdadb.execute(sel)]
codeuuid = {}
for code in codes:
code['uuid'] = uuid.uuid4()
code['Color'] = int(webcolors.name_to_hex(code['color'])[1:], 16)
code['CreatedBy'] = defaultuserid
code['CreatedDate'] = min(dateset)
code['ModifiedBy'] = defaultuserid
code['ModifiedDate'] = max(dateset)
codeuuid[code['id']] = code['uuid']
if len(codes) > 0:
normdb.execute(normNode.insert().values({
'Id' : bindparam('uuid'),
'Name' : bindparam('name'),
'Description': bindparam('memo')
}), codes)
# Sources
if args.sources != 'skip':
print("Normalising sources", file=sys.stderr)
# Create dummy source category
sourcecatuuid = uuid.uuid4()
normdb.execute(normSourceCategory.insert(), {
'Id' : sourcecatuuid,
'Name' : 'OpenQDA image',
'CreatedBy' : defaultuserid,
'CreatedDate' : min(dateset),
'ModifiedBy' : defaultuserid,
'ModifiedDate': max(dateset)})
sel = select([oqdaimages.c.id,
oqdaimages.c.name,
oqdaimages.c.owner,
oqdaimages.c.date,
oqdaimages.c.status,
oqdaimages.c.memo])
# Prepare http voodoo
if args.user != None:
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
top_level_url = args.url
password_mgr.add_password(None, top_level_url, args.user, args.password)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
sources = [dict(row) for row in oqdadb.execute(sel)]
sourceuuid = {}
for source in sources:
source['uuid'] = uuid.uuid4()
source['Category'] = sourcecatuuid
source['ObjectType'] = 'JPEG'
print("Downloading " + source['name'], file=sys.stderr)
if args.user != None:
opener.open(args.url + source['name'])
urllib2.install_opener(opener)
response = urllib2.urlopen(args.url + source['name'])
source['Object'] = response.read()
source['Thumbnail'] = ''
source['CreatedBy'] = users[source['owner']]
source['CreatedDate'] = source['date']
source['ModifiedBy'] = users[source['owner']]
source['ModifiedDate'] = source['date']
sourceuuid[source['id']] = source['uuid']
if len(sources) > 0:
normdb.execute(normSource.insert().values({
'Id' : bindparam('uuid'),
'Name' : bindparam('name'),
'Description': bindparam('memo')
}), sources)
# Source attributes
if args.source_attributes != 'skip':
print("Normalising source attributes", file=sys.stderr)
sel = select([oqdaimageAttributes.c.images_id,
oqdaimageAttributes.c.value,
oqdaattributes.c.name,
oqdaattributes.c.memo]).where(
oqdaattributes.c.id == oqdaimageAttributes.c.attributes_id)
attributes = [dict(row) for row in oqdadb.execute(sel)]
for attribute in attributes:
attribute['Source'] = sourceuuid[attribute['images_id']]
attribute['Type'] = 'Text'
attribute['Length'] = len(attribute['value'])
attribute['CreatedBy'] = defaultuserid
attribute['CreatedDate'] = min(dateset)
attribute['ModifiedBy'] = defaultuserid
attribute['ModifiedDate'] = max(dateset)
if len(attributes) > 0:
normdb.execute(normSourceAttribute.insert().values({
'Name' : bindparam('name'),
'Value': bindparam('value')
}), attributes)
# Tagging
if args.taggings != 'skip':
print("Normalising taggings", file=sys.stderr)
sel = select([oqdaimageCoding.c.images_id,
oqdaimageCoding.c.codes_id,
oqdaimageCoding.c.x1,
oqdaimageCoding.c.y1,
oqdaimageCoding.c.x2,
oqdaimageCoding.c.y2,
oqdaimageCoding.c.owner,
oqdaimageCoding.c.date,
oqdaimageCoding.c.memo])
taggings = [dict(row) for row in oqdadb.execute(sel)]
for tagging in taggings:
tagging['Fragment'] = str(tagging['x1']) + ':' + str(tagging['x2']) + ',' + str(tagging['y1']) + ':' + str(tagging['y2'])
tagging['Source'] = sourceuuid[tagging['images_id']]
tagging['Node'] = codeuuid[tagging['codes_id']]
tagging['CreatedBy'] = users[tagging['owner']]
tagging['CreatedDate'] = tagging['date']
tagging['ModifiedBy'] = users[tagging['owner']]
tagging['ModifiedDate'] = tagging['date']
if len(taggings) > 0:
normdb.execute(normTagging.insert().values({
'Memo': bindparam('memo')
}), taggings)
except exc.SQLAlchemyError:
raise