@@ -26,6 +26,7 @@ def parse_arguments():
26
26
help = 'output filename' )
27
27
parser .add_argument ('--show' , action = 'store_true' , help = 'Simply show the plan node' )
28
28
parser .add_argument ('--raw' , action = 'store_true' , help = 'With --show, also show raw JSON' )
29
+ parser .add_argument ('--convert' , help = 'Convert JSON to text format' )
29
30
30
31
args , unparsed = parser .parse_known_args ()
31
32
return args , unparsed
@@ -286,17 +287,17 @@ def build_dataframe(args):
286
287
store .close ()
287
288
288
289
289
- def print_line (line ):
290
+ def print_line (line , fp_out ):
290
291
"""line: list of str"""
291
292
if len (line ) == 0 :
292
293
return
293
294
# Indent
294
- print (" " , end = "" )
295
+ print (" " , end = "" , file = fp_out )
295
296
text = " " .join (line )
296
- print (text )
297
+ print (text , file = fp_out )
297
298
298
299
299
- def show_list (L ):
300
+ def show_list (L , fp_out ):
300
301
"""
301
302
Show list entries in indented 70-character lines,
302
303
ending on blank line
@@ -312,47 +313,82 @@ def show_list(L):
312
313
n = len (s ) + 1
313
314
c += n
314
315
if c > limit :
315
- print_line (line )
316
+ print_line (line , fp_out )
316
317
line .clear ()
317
318
c = len (s )
318
319
line .append (s )
319
320
320
- print_line (line )
321
- print ("" )
321
+ print_line (line , fp_out )
322
+ print ("" , file = fp_out )
322
323
323
324
324
- def show_node (subtree ):
325
+ def show_node (subtree , fp_out ):
325
326
"""Write out the given plan subtree"""
326
327
for partition in ["val" , "train" ]:
327
328
partition_sets = len (subtree [partition ])
328
- print ("%s_sets: %i" % (partition , partition_sets ))
329
+ print ("%s_sets: %i" % (partition , partition_sets ), file = fp_out )
329
330
for index in range (0 , len (subtree [partition ])):
330
- print ("index: %i" % index )
331
+ print ("index: %i" % index , file = fp_out )
331
332
dataset = subtree [partition ][index ]
332
333
for key in ["CELL" , "DRUG" ]:
333
334
if key not in dataset :
334
335
continue
335
336
partition_keys = dataset [key ]
336
337
print ("%s %ss: count: %i" %
337
- (partition , key , len (partition_keys )))
338
- show_list (partition_keys )
338
+ (partition , key , len (partition_keys )),
339
+ file = fp_out )
340
+ show_list (partition_keys , fp_out )
339
341
340
342
341
- def show (args ):
343
+ def show (args , fp_out ):
342
344
"""Simply show the entry for this node"""
343
345
if args .node is None :
344
346
print ("Provide a node to show!" )
345
347
exit (1 )
346
348
# Get the plan subtree for the given node
347
349
subtree = read_plan (args .plan , args .node )
348
350
if args .raw :
349
- print (str (subtree ))
350
- print ("node: " + args .node )
351
- show_node (subtree )
351
+ print (str (subtree ), file = fp_out )
352
+ print ("node: " + args .node , file = fp_out )
353
+ show_node (subtree , fp_out )
354
+
355
+
356
+ def show_metadata (node , fp_out ):
357
+ print ("metadata:" , file = fp_out )
358
+ for key in node :
359
+ print ("%s: %s" % (key , str (node [key ])), file = fp_out )
360
+ print ("" , file = fp_out )
361
+
362
+
363
+ def convert_all (tree , fp_out ):
364
+ for node in tree .keys ():
365
+ if node == "1" :
366
+ show_metadata (tree [node ], fp_out )
367
+ continue
368
+ print ("node: " + node , file = fp_out )
369
+ show_node (tree [node ], fp_out )
370
+
371
+
372
+ def convert (args ):
373
+ output = args .convert
374
+ if output == "-" :
375
+ print ("converting to: stdout" )
376
+ fp_out = sys .stdout
377
+ else :
378
+ print ("converting to: " + output )
379
+ fp_out = open (output , "w" )
380
+ # Get the full tree:
381
+ tree = read_plan (args .plan , None )
382
+ convert_all (tree , fp_out )
383
+ if output != "-" :
384
+ fp_out .close ()
385
+
352
386
353
387
if __name__ == '__main__' :
354
388
parsed , unparsed = parse_arguments ()
355
389
if parsed .show :
356
- show (parsed )
390
+ show (parsed , sys .stdout )
391
+ elif parsed .convert is not None :
392
+ convert (parsed )
357
393
else :
358
394
build_dataframe (parsed )
0 commit comments