@@ -362,6 +362,20 @@ def _check_results_choices(self, annotation):
362362 'No choices found for "{}". Expected one of {}.' .format (token , self .config .choices [token ])
363363 )
364364
365+ def _get_group_children (self ):
366+ """
367+ Create a list of all annotation tokens that are part of a group.
368+
369+ Returns:
370+ List of annotation tokens that are configured to be in groups
371+ """
372+ group_children = []
373+
374+ for group in self .config .groups :
375+ group_children .extend (self .config .groups [group ])
376+
377+ return group_children
378+
365379 def _get_group_for_token (self , token ):
366380 """
367381 Find out which group, if any, an annotation token belongs to.
@@ -391,12 +405,7 @@ def check_results(self, all_results):
391405 if self .config .verbosity >= 2 :
392406 pprint .pprint (all_results , indent = 3 )
393407
394- # This is used to quickly find out if a token is a member of a group
395- group_children = []
396-
397- # Build a big list of all tokens that are part of a group
398- for group in self .config .groups :
399- group_children .extend (self .config .groups [group ])
408+ group_children = self ._get_group_children ()
400409
401410 # Spin through the search results
402411 for filename in all_results :
@@ -434,12 +443,6 @@ def check_results(self, all_results):
434443 current_group
435444 ))
436445 found_group_members .append (token )
437-
438- # If we have all members, this group is done
439- if len (found_group_members ) == len (self .config .groups [current_group ]):
440- self .echo .echo_vv ("Group complete!" )
441- current_group = None
442- found_group_members = []
443446 else :
444447 if token in group_children :
445448 current_group = self ._get_group_for_token (token )
@@ -456,6 +459,12 @@ def check_results(self, all_results):
456459 current_group , token , annotation ['line_number' ])
457460 )
458461
462+ # If we have all members, this group is done
463+ if current_group and len (found_group_members ) == len (self .config .groups [current_group ]):
464+ self .echo .echo_vv ("Group complete!" )
465+ current_group = None
466+ found_group_members = []
467+
459468 if current_group :
460469 self .errors .append ('File finished with an incomplete group {}!' .format (current_group ))
461470
@@ -494,6 +503,73 @@ def search(self):
494503 """
495504 pass # pragma: no cover
496505
506+ def _format_results_for_report (self , all_results ):
507+ """
508+ Format the given results dict for reporting purposes.
509+
510+ Args:
511+ all_results: Dict of all results found in a search
512+
513+ Returns:
514+ Dict of results arranged for reporting
515+ """
516+ group_children = self ._get_group_children ()
517+ formatted_results = {}
518+ current_group_id = 0
519+
520+ for filename in all_results :
521+ self .echo .echo_vv ("report_format: formatting {}" .format (filename ))
522+ formatted_results [filename ] = []
523+ current_group = None
524+
525+ found_group_members = []
526+
527+ for annotation in all_results [filename ]:
528+ token = annotation ['annotation_token' ]
529+ self .echo .echo_vvv ("report_format: formatting annotation token {}" .format (token ))
530+
531+ if current_group :
532+ if token not in self .config .groups [current_group ]:
533+ self .echo .echo_vv (
534+ "report_format: {} is not a group member, finishing group id {}" .format (
535+ token ,
536+ current_group_id
537+ )
538+ )
539+ current_group = None
540+ found_group_members = []
541+ formatted_results [filename ].append (annotation )
542+ else :
543+ self .echo .echo_vv ("report_format: Adding {} to group id {}" .format (
544+ token ,
545+ current_group_id
546+ ))
547+ annotation ['report_group_id' ] = current_group_id
548+ formatted_results [filename ].append (annotation )
549+ found_group_members .append (token )
550+ else :
551+ if token in group_children :
552+ current_group = self ._get_group_for_token (token )
553+ current_group_id += 1
554+ found_group_members = [token ]
555+ annotation ['report_group_id' ] = current_group_id
556+ formatted_results [filename ].append (annotation )
557+
558+ self .echo .echo_vv ('Starting group id {} for "{}" token "{}", line {}' .format (
559+ current_group_id , current_group , token , annotation ['line_number' ])
560+ )
561+ else :
562+ self .echo .echo_vv ('Adding single token {}.' .format (token ))
563+ formatted_results [filename ].append (annotation )
564+
565+ # If we have all members, this group is done
566+ if current_group and len (found_group_members ) == len (self .config .groups [current_group ]):
567+ self .echo .echo_vv ("report_format: Group complete!" )
568+ current_group = None
569+ found_group_members = []
570+
571+ return formatted_results
572+
497573 def report (self , all_results ):
498574 """
499575 Genrates the YAML report of all search results.
@@ -509,6 +585,8 @@ def report(self, all_results):
509585 now = datetime .datetime .now ()
510586 report_filename = os .path .join (self .config .report_path , '{}.yaml' .format (now .strftime ('%Y-%d-%m-%H-%M-%S' )))
511587
588+ formatted_results = self ._format_results_for_report (all_results )
589+
512590 self .echo ("Generating report to {}" .format (report_filename ))
513591
514592 try :
@@ -518,6 +596,6 @@ def report(self, all_results):
518596 raise
519597
520598 with open (report_filename , 'w+' ) as report_file :
521- yaml .dump (all_results , report_file , default_flow_style = False )
599+ yaml .dump (formatted_results , report_file , default_flow_style = False )
522600
523601 return report_filename
0 commit comments