@@ -480,32 +480,47 @@ def parse_json_object(devices: Dict[str, Device], data: str, live: bool = False)
480
480
dev .counter = cnt + 1
481
481
482
482
483
- def print_summary (devices : Dict [str , Device ]):
484
- print ("Summary of received data:" )
483
+ def print_summary (devices : Dict [str , Device ], ofile = None ):
484
+ print ("Summary of received data:" , file = ofile )
485
485
for addr , dev in devices .items ():
486
- print (f"{ addr } (last update @{ dev .last_update .isoformat ()} ) :" )
486
+ print (f"{ addr } (last update @{ dev .last_update .isoformat ()} ) :" , file = ofile )
487
487
for vdata in dev .vdata :
488
- print (vdata .string ())
488
+ print (vdata .string (), file = ofile )
489
489
490
- print ("ID Hashes:" )
490
+ print ("ID Hashes:" , file = ofile )
491
491
for addr , dev in devices .items ():
492
492
if dev .hashes :
493
- print (f"{ addr } :" )
493
+ print (f"{ addr } :" , file = ofile )
494
494
for h in dev .hashes :
495
- print (f"\t { h .print ()} " )
495
+ print (f"\t { h .print ()} " , file = ofile )
496
496
497
- print ("Idents:" )
497
+ print ("Idents:" , file = ofile )
498
498
for addr , dev in devices .items ():
499
499
if dev .idents :
500
500
identstr = "," .join (
501
501
[f"{ binascii .hexlify (bytearray (x ))} " for x in dev .idents ]
502
502
)
503
503
print (
504
- f"\t { identstr } -> { addr } ({ dev .vdata [0 ].created .isoformat ()} - { dev .last_update .isoformat ()} )"
504
+ f"\t { identstr } -> { addr } ({ dev .vdata [0 ].created .isoformat ()} - { dev .last_update .isoformat ()} )" ,
505
+ file = ofile ,
505
506
)
506
507
507
508
508
- def from_unix_socket (name : str , live : bool ):
509
+ def summary (devices : Dict [str , Device ], oname : Optional [str ] = None ):
510
+ ofile = None
511
+ try :
512
+ if oname is not None :
513
+ ofile = open (oname , "w" )
514
+ except OSError as err :
515
+ print (f"Error: unable to open output file: { str (err )} , writing to stdout" )
516
+
517
+ print_summary (devices , ofile = ofile )
518
+
519
+ if ofile is not None :
520
+ ofile .close ()
521
+
522
+
523
+ def from_unix_socket (name : str , live : bool , oname : Optional [str ] = None ):
509
524
"""
510
525
Start listening on UNIX socket for bluewalker to connect and read data
511
526
from the socket.
@@ -534,10 +549,10 @@ def from_unix_socket(name: str, live: bool):
534
549
535
550
conn .close ()
536
551
sock .close ()
537
- print_summary (devices )
552
+ summary (devices , oname )
538
553
539
554
540
- def from_file (name : str , live : bool ):
555
+ def from_file (name : str , live : bool , oname : Optional [ str ] = None ):
541
556
542
557
if not os .path .exists (name ):
543
558
print (f"Error: file { name } not found" )
@@ -548,7 +563,7 @@ def from_file(name: str, live: bool):
548
563
for line in f :
549
564
parse_json_object (devices , line , live )
550
565
551
- print_summary (devices )
566
+ summary (devices , oname )
552
567
553
568
554
569
if __name__ == "__main__" :
@@ -563,10 +578,13 @@ def from_file(name: str, live: bool):
563
578
action = "store_true" ,
564
579
default = False ,
565
580
)
581
+ parser .add_argument (
582
+ "--output" , help = "Name of a file to write output to" , action = "store" ,
583
+ )
566
584
args = parser .parse_args ()
567
585
if args .unix != "" :
568
- from_unix_socket (args .unix , args .live )
586
+ from_unix_socket (args .unix , args .live , args . output )
569
587
elif args .file != "" :
570
- from_file (args .file , args .live )
588
+ from_file (args .file , args .live , args . output )
571
589
else :
572
590
print ("use --unix <path> to specify the unix socket to listen on" )
0 commit comments