12
12
# TODO: run_info
13
13
14
14
import glob
15
+ import itertools
15
16
import os
16
17
import sqlite3
17
18
@@ -90,7 +91,7 @@ def __init__(self, basename=None, suffix=None, warn=None, debug=None):
90
91
self ._has_lines = False
91
92
self ._has_arcs = False
92
93
93
- self ._context_id = 0
94
+ self ._current_context_id = None
94
95
95
96
def _choose_filename (self ):
96
97
self .filename = self ._basename
@@ -104,6 +105,7 @@ def _reset(self):
104
105
self ._db = None
105
106
self ._file_map = {}
106
107
self ._have_used = False
108
+ self ._current_context_id = None
107
109
108
110
def _create_db (self ):
109
111
if self ._debug and self ._debug .should ('dataio' ):
@@ -178,17 +180,28 @@ def _file_id(self, filename, add=False):
178
180
self ._file_map [filename ] = cur .lastrowid
179
181
return self ._file_map .get (filename )
180
182
183
+ def _context_id (self , context ):
184
+ """Get the id for a context."""
185
+ assert context is not None
186
+ self ._start_using ()
187
+ with self ._connect () as con :
188
+ row = con .execute ("select id from context where context = ?" , (context ,)).fetchone ()
189
+ if row is not None :
190
+ return row [0 ]
191
+ else :
192
+ return None
193
+
181
194
def set_context (self , context ):
182
195
"""Set the current context for future `add_lines` etc."""
183
196
self ._start_using ()
184
197
context = context or ""
185
198
with self ._connect () as con :
186
199
row = con .execute ("select id from context where context = ?" , (context ,)).fetchone ()
187
200
if row is not None :
188
- self ._context_id = row [0 ]
201
+ self ._current_context_id = row [0 ]
189
202
else :
190
203
cur = con .execute ("insert into context (context) values (?)" , (context ,))
191
- self ._context_id = cur .lastrowid
204
+ self ._current_context_id = cur .lastrowid
192
205
193
206
def add_lines (self , line_data ):
194
207
"""Add measured line data.
@@ -204,10 +217,12 @@ def add_lines(self, line_data):
204
217
))
205
218
self ._start_using ()
206
219
self ._choose_lines_or_arcs (lines = True )
220
+ if self ._current_context_id is None :
221
+ self .set_context ("" )
207
222
with self ._connect () as con :
208
223
for filename , linenos in iitems (line_data ):
209
224
file_id = self ._file_id (filename , add = True )
210
- data = [(file_id , self ._context_id , lineno ) for lineno in linenos ]
225
+ data = [(file_id , self ._current_context_id , lineno ) for lineno in linenos ]
211
226
con .executemany (
212
227
"insert or ignore into line (file_id, context_id, lineno) values (?, ?, ?)" ,
213
228
data ,
@@ -227,10 +242,12 @@ def add_arcs(self, arc_data):
227
242
))
228
243
self ._start_using ()
229
244
self ._choose_lines_or_arcs (arcs = True )
245
+ if self ._current_context_id is None :
246
+ self .set_context ("" )
230
247
with self ._connect () as con :
231
248
for filename , arcs in iitems (arc_data ):
232
249
file_id = self ._file_id (filename , add = True )
233
- data = [(file_id , self ._context_id , fromno , tono ) for fromno , tono in arcs ]
250
+ data = [(file_id , self ._current_context_id , fromno , tono ) for fromno , tono in arcs ]
234
251
con .executemany (
235
252
"insert or ignore into arc (file_id, context_id, fromno, tono) values (?, ?, ?, ?)" ,
236
253
data ,
@@ -306,19 +323,23 @@ def update(self, other_data, aliases=None):
306
323
307
324
# lines
308
325
if other_data ._has_lines :
309
- for filename in other_data .measured_files ():
310
- lines = set (other_data .lines (filename ))
311
- filename = aliases .map (filename )
312
- lines .update (self .lines (filename ) or ())
313
- self .add_lines ({filename : lines })
326
+ for context in other_data .measured_contexts ():
327
+ self .set_context (context )
328
+ for filename in other_data .measured_files ():
329
+ lines = set (other_data .lines (filename , context = context ))
330
+ filename = aliases .map (filename )
331
+ lines .update (self .lines (filename , context = context ) or ())
332
+ self .add_lines ({filename : lines })
314
333
315
334
# arcs
316
335
if other_data ._has_arcs :
317
- for filename in other_data .measured_files ():
318
- arcs = set (other_data .arcs (filename ))
319
- filename = aliases .map (filename )
320
- arcs .update (self .arcs (filename ) or ())
321
- self .add_arcs ({filename : arcs })
336
+ for context in other_data .measured_contexts ():
337
+ self .set_context (context )
338
+ for filename in other_data .measured_files ():
339
+ arcs = set (other_data .arcs (filename , context = context ))
340
+ filename = aliases .map (filename )
341
+ arcs .update (self .arcs (filename , context = context ) or ())
342
+ self .add_arcs ({filename : arcs })
322
343
323
344
# file_tracers
324
345
for filename in other_data .measured_files ():
@@ -407,12 +428,11 @@ def file_tracer(self, filename):
407
428
return row [0 ] or ""
408
429
return "" # File was measured, but no tracer associated.
409
430
410
- def lines (self , filename ):
431
+ def lines (self , filename , context = None ):
411
432
self ._start_using ()
412
433
if self .has_arcs ():
413
- arcs = self .arcs (filename )
434
+ arcs = self .arcs (filename , context = context )
414
435
if arcs is not None :
415
- import itertools
416
436
all_lines = itertools .chain .from_iterable (arcs )
417
437
return list (set (l for l in all_lines if l > 0 ))
418
438
@@ -421,18 +441,28 @@ def lines(self, filename):
421
441
if file_id is None :
422
442
return None
423
443
else :
424
- linenos = con .execute ("select lineno from line where file_id = ?" , (file_id ,))
444
+ query = "select lineno from line where file_id = ?"
445
+ data = [file_id ]
446
+ if context is not None :
447
+ query += " and context_id = ?"
448
+ data += [self ._context_id (context )]
449
+ linenos = con .execute (query , data )
425
450
return [lineno for lineno , in linenos ]
426
451
427
- def arcs (self , filename ):
452
+ def arcs (self , filename , context = None ):
428
453
self ._start_using ()
429
454
with self ._connect () as con :
430
455
file_id = self ._file_id (filename )
431
456
if file_id is None :
432
457
return None
433
458
else :
434
- arcs = con .execute ("select fromno, tono from arc where file_id = ?" , (file_id ,))
435
- return [pair for pair in arcs ]
459
+ query = "select fromno, tono from arc where file_id = ?"
460
+ data = [file_id ]
461
+ if context is not None :
462
+ query += " and context_id = ?"
463
+ data += [self ._context_id (context )]
464
+ arcs = con .execute (query , data )
465
+ return list (arcs )
436
466
437
467
def run_infos (self ):
438
468
return [] # TODO
0 commit comments