Skip to content

Commit 9d16a14

Browse files
committed
Add ability to print summary to file
1 parent 3eb6101 commit 9d16a14

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

spy.py

+33-15
Original file line numberDiff line numberDiff line change
@@ -480,32 +480,47 @@ def parse_json_object(devices: Dict[str, Device], data: str, live: bool = False)
480480
dev.counter = cnt + 1
481481

482482

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)
485485
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)
487487
for vdata in dev.vdata:
488-
print(vdata.string())
488+
print(vdata.string(), file=ofile)
489489

490-
print("ID Hashes:")
490+
print("ID Hashes:", file=ofile)
491491
for addr, dev in devices.items():
492492
if dev.hashes:
493-
print(f"{addr}:")
493+
print(f"{addr}:", file=ofile)
494494
for h in dev.hashes:
495-
print(f"\t{h.print()}")
495+
print(f"\t{h.print()}", file=ofile)
496496

497-
print("Idents:")
497+
print("Idents:", file=ofile)
498498
for addr, dev in devices.items():
499499
if dev.idents:
500500
identstr = ",".join(
501501
[f"{binascii.hexlify(bytearray(x))}" for x in dev.idents]
502502
)
503503
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,
505506
)
506507

507508

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):
509524
"""
510525
Start listening on UNIX socket for bluewalker to connect and read data
511526
from the socket.
@@ -534,10 +549,10 @@ def from_unix_socket(name: str, live: bool):
534549

535550
conn.close()
536551
sock.close()
537-
print_summary(devices)
552+
summary(devices, oname)
538553

539554

540-
def from_file(name: str, live: bool):
555+
def from_file(name: str, live: bool, oname: Optional[str] = None):
541556

542557
if not os.path.exists(name):
543558
print(f"Error: file {name} not found")
@@ -548,7 +563,7 @@ def from_file(name: str, live: bool):
548563
for line in f:
549564
parse_json_object(devices, line, live)
550565

551-
print_summary(devices)
566+
summary(devices, oname)
552567

553568

554569
if __name__ == "__main__":
@@ -563,10 +578,13 @@ def from_file(name: str, live: bool):
563578
action="store_true",
564579
default=False,
565580
)
581+
parser.add_argument(
582+
"--output", help="Name of a file to write output to", action="store",
583+
)
566584
args = parser.parse_args()
567585
if args.unix != "":
568-
from_unix_socket(args.unix, args.live)
586+
from_unix_socket(args.unix, args.live, args.output)
569587
elif args.file != "":
570-
from_file(args.file, args.live)
588+
from_file(args.file, args.live, args.output)
571589
else:
572590
print("use --unix <path> to specify the unix socket to listen on")

0 commit comments

Comments
 (0)