37
37
from nacc .uds3 import filters
38
38
from nacc .uds3 import packet as uds3_packet
39
39
from nacc .uds3 import Field
40
+ from nacc .logger import configure_logging
41
+ from report_handler .report_handler import ReportHandler
42
+ import logging
40
43
41
44
42
45
def check_blanks (packet : uds3_packet .Packet , options : argparse .Namespace ) \
@@ -178,7 +181,8 @@ def check_for_bad_characters(field: Field) -> typing.List:
178
181
return incompatible
179
182
180
183
181
- def check_redcap_event (options , record , out = sys .stdout , err = sys .stderr ) -> bool :
184
+ def check_redcap_event (
185
+ options , record , out = sys .stdout , err = sys .stderr ) -> bool :
182
186
"""
183
187
Determines if the record's redcap_event_name and filled forms match the
184
188
options flag
@@ -303,8 +307,21 @@ def check_redcap_event(options, record, out=sys.stdout, err=sys.stderr) -> bool:
303
307
if followup_match in ['' , '0' ]:
304
308
return False
305
309
except KeyError :
306
- print ("Could not find a REDCap field for TFP Z1X form." ,
307
- file = err )
310
+ print (
311
+ "Could not find a REDCap field for TFP Z1X form." ,
312
+ file = err )
313
+ logging .error (
314
+ "Could not find a REDCap field for TFP Z1X form" ,
315
+ extra = {
316
+ "report_handler" : {
317
+ "data" : {
318
+ "ptid" : record ['ptid' ],
319
+ "error" : "Could not find a REDCap field for TFP Z1X form"
320
+ },
321
+ "sheet" : 'ERROR'
322
+ }
323
+ },
324
+ )
308
325
return False
309
326
elif options .tfp3 :
310
327
event_name = 'tele'
@@ -419,9 +436,8 @@ def set_to_zero_if_blank(*field_names):
419
436
# B8 3.
420
437
try :
421
438
if packet ['CVDSIGNS' ] == 1 :
422
- set_to_zero_if_blank (
423
- 'CORTDEF' , 'SIVDFIND' , 'CVDMOTL' , 'CVDMOTR' , 'CORTVISL' ,
424
- 'CORTVISR' , 'SOMATL' , 'SOMATR' )
439
+ set_to_zero_if_blank ('CORTDEF' , 'SIVDFIND' , 'CVDMOTL' , 'CVDMOTR' ,
440
+ 'CORTVISL' , 'CORTVISR' , 'SOMATL' , 'SOMATR' )
425
441
except KeyError :
426
442
pass
427
443
@@ -440,15 +456,15 @@ def set_to_zero_if_blank(*field_names):
440
456
try :
441
457
if packet ['DEMENTED' ] == 1 :
442
458
set_to_zero_if_blank (
443
- 'AMNDEM' , 'PCA' , 'PPASYN' , 'FTDSYN' , 'LBDSYN' , 'NAMNDEM' )
459
+ 'AMNDEM' , 'PCA' , 'PPASYN' , 'FTDSYN' , 'LBDSYN' , 'NAMNDEM' )
444
460
except KeyError :
445
461
pass
446
462
447
463
# D1 5.
448
464
try :
449
465
if packet ['DEMENTED' ] == 0 :
450
466
set_to_zero_if_blank (
451
- 'MCIAMEM' , 'MCIAPLUS' , 'MCINON1' , 'MCINON2' , 'IMPNOMCI' )
467
+ 'MCIAMEM' , 'MCIAPLUS' , 'MCINON1' , 'MCINON2' , 'IMPNOMCI' )
452
468
except KeyError :
453
469
pass
454
470
@@ -470,6 +486,13 @@ def set_to_zero_if_blank(*field_names):
470
486
except KeyError :
471
487
pass
472
488
489
+ # NP v11 19(r, s, t)
490
+ try :
491
+ set_to_zero_if_blank (
492
+ 'NPPDXR' , 'NPPDXS' , 'NPPDXT' )
493
+ except KeyError :
494
+ pass
495
+
473
496
474
497
def convert (fp , options , out = sys .stdout , err = sys .stderr ):
475
498
"""
@@ -512,8 +535,9 @@ def convert(fp, options, out=sys.stdout, err=sys.stderr):
512
535
if not event_match :
513
536
continue
514
537
515
- print ("[START] ptid : " + str (record ['ptid' ]) + " visit " +
516
- str (record ['visitnum' ]), file = err )
538
+ print ("[START] ptid : " + str (record ['ptid' ]) +
539
+ " visit " + str (record ['visitnum' ]), file = err )
540
+ logging .info ('[START] ptid: {}' .format (record ['ptid' ]))
517
541
try :
518
542
if options .lbd and options .ivp :
519
543
packet = lbd_ivp_builder .build_lbd_ivp_form (record )
@@ -548,14 +572,23 @@ def convert(fp, options, out=sys.stdout, err=sys.stderr):
548
572
elif options .m :
549
573
packet = m_builder .build_uds3_m_form (record )
550
574
551
- except Exception :
575
+ except Exception as e :
552
576
if 'ptid' in record :
553
- print ("[SKIP] Error for ptid : " + str (record ['ptid' ]) +
554
- " visit " + str (record ['visitnum' ]), file = err )
577
+ print ("[SKIP] Error for ptid : " + str (record ['ptid' ]),
578
+ file = err )
579
+ logging .error (
580
+ '[SKIP] Error for ptid : {}' .format (record ['ptid' ]),
581
+ extra = {
582
+ "report_handler" : {
583
+ "data" : {"ptid" : record ['ptid' ], "error" : str (traceback .format_exc ())},
584
+ "sheet" : "SKIP"
585
+ }
586
+ }
587
+ )
555
588
traceback .print_exc ()
556
589
continue
557
590
558
- if not (options .np or options . np10 or options .m or options .lbd or
591
+ if not (options .np10 or options .m or options .lbd or
559
592
options .lbdsv or options .ftld or options .csf or options .cv ):
560
593
set_blanks_to_zero (packet )
561
594
@@ -565,17 +598,35 @@ def convert(fp, options, out=sys.stdout, err=sys.stderr):
565
598
warnings = []
566
599
try :
567
600
warnings += check_blanks (packet , options )
568
- except KeyError :
601
+ except KeyError as e :
569
602
print ("[SKIP] Error for ptid : " + str (record ['ptid' ]) +
570
603
" visit " + str (record ['visitnum' ]), file = err )
604
+ logging .error (
605
+ '[SKIP] Error for ptid : {}' .format (record ['ptid' ]),
606
+ extra = {
607
+ "report_handler" : {
608
+ "data" : {"ptid" : record ['ptid' ], "error" : str (traceback .format_exc ())},
609
+ "sheet" : "SKIP"
610
+ }
611
+ }
612
+ )
571
613
traceback .print_exc ()
572
614
continue
573
615
574
616
try :
575
617
warnings += check_characters (packet )
576
- except KeyError :
618
+ except KeyError as e :
577
619
print ("[SKIP] Error for ptid : " + str (record ['ptid' ]) +
578
620
" visit " + str (record ['visitnum' ]), file = err )
621
+ logging .error (
622
+ '[SKIP] Error for ptid : {}' .format (record ['ptid' ]),
623
+ extra = {
624
+ "report_handler" : {
625
+ "data" : {"ptid" : record ['ptid' ], "error" : str (traceback .format_exc ())},
626
+ "sheet" : "SKIP"
627
+ }
628
+ }
629
+ )
579
630
traceback .print_exc ()
580
631
continue
581
632
@@ -585,6 +636,15 @@ def convert(fp, options, out=sys.stdout, err=sys.stderr):
585
636
warn = "\n " .join (map (str , warnings ))
586
637
warn = warn .replace ("\\ " , "" )
587
638
print (warn , file = err )
639
+ logging .error (
640
+ '[SKIP] Error for ptid : {}' .format (record ['ptid' ]),
641
+ extra = {
642
+ "report_handler" : {
643
+ "data" : {"ptid" : record ['ptid' ], "error" : "," .join (map (str , warnings ))},
644
+ "sheet" : "SKIP"
645
+ }
646
+ }
647
+ )
588
648
continue
589
649
590
650
if not options .np and not options .np10 and not options .m and not \
@@ -596,9 +656,19 @@ def convert(fp, options, out=sys.stdout, err=sys.stderr):
596
656
597
657
try :
598
658
print (form , file = out )
599
- except AssertionError :
600
- print ("[SKIP] Error for ptid : " + str (record ['ptid' ]) +
601
- " visit " + str (record ['visitnum' ]), file = err )
659
+ except AssertionError as e :
660
+ print ("[SKIP] Error for ptid assertion: " +
661
+ str (record ['ptid' ]),
662
+ file = err )
663
+ logging .error (
664
+ '[SKIP] Error for ptid : {}' .format (record ['ptid' ]),
665
+ extra = {
666
+ "report_handler" : {
667
+ "data" : {"ptid" : record ['ptid' ], "error" : str (e )},
668
+ "sheet" : "SKIP"
669
+ }
670
+ }
671
+ )
602
672
traceback .print_exc ()
603
673
continue
604
674
@@ -700,21 +770,31 @@ def main():
700
770
701
771
fp = sys .stdin if options .file is None else open (options .file , 'r' )
702
772
773
+ report_handler = ReportHandler ()
774
+ configure_logging (options , [report_handler ])
775
+
703
776
# Default option is to print out directly to the command terminal.
704
777
# If you want to print the output to a specific file, then redirect
705
778
# stdout to that filename.
706
779
output = sys .stdout
707
780
708
- if options .filter :
709
- if options .filter == "getPtid" :
710
- filters .filter_extract_ptid (
711
- fp , options .ptid , options .vnum , options .vtype , output )
781
+ try :
782
+ if options .filter :
783
+ if options .filter == "getPtid" :
784
+ filters .filter_extract_ptid (
785
+ fp , options .ptid , options .vnum , options .vtype , output )
786
+ else :
787
+ filter_method = 'filter_' + filters_names [options .filter ]
788
+ filter_func = getattr (filters , filter_method )
789
+ filter_func (fp , options .filter_meta , output )
712
790
else :
713
- filter_method = 'filter_' + filters_names [options .filter ]
714
- filter_func = getattr (filters , filter_method )
715
- filter_func (fp , options .filter_meta , output )
716
- else :
717
- convert (fp , options )
791
+ convert (fp , options )
792
+ logging .info ('Nacculator Ended' )
793
+ except Exception as e :
794
+ print (
795
+ f"An exception occurred in main(): { str (e ), str (e .__cause__ ), str (e .__context__ ), str (e .__traceback__ ), str (e .with_traceback ())} " )
796
+ finally :
797
+ report_handler .write_report ("logs" )
718
798
719
799
720
800
if __name__ == '__main__' :
0 commit comments