Skip to content

Commit 6b652d2

Browse files
committed
Refactor internal BaseSearch.check_results
This makes it easier to add support for optional fields.
1 parent 8561fca commit 6b652d2

1 file changed

Lines changed: 65 additions & 53 deletions

File tree

code_annotations/base.py

Lines changed: 65 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -417,70 +417,82 @@ def check_results(self, all_results):
417417
"""
418418
self.echo.pprint(all_results, indent=3, verbosity_level=2)
419419

420-
group_children = self._get_group_children()
421-
422420
# Spin through the search results
423421
for filename in all_results:
424422
current_group = None
425-
found_group_members = []
426-
423+
found_group_tokens = []
427424
for annotation in all_results[filename]:
428-
self._check_results_choices(annotation)
429-
token = annotation['annotation_token']
425+
current_group = self.check_annotation(annotation, current_group, found_group_tokens)
430426

431-
# TODO: Clean this up into reasonable methods
432-
if current_group:
433-
if token not in self.config.groups[current_group]:
434-
self._add_annotation_error(
435-
annotation,
436-
'"{}" is not in the group that starts with "{}". Expecting one of: {}'.format(
437-
token,
438-
current_group,
439-
self.config.groups[current_group]
440-
)
441-
)
442-
current_group = None
443-
found_group_members = []
444-
elif token in found_group_members:
445-
self._add_annotation_error(
446-
annotation,
447-
f'"{token}" is already in the group that starts with "{current_group}"'
448-
)
449-
current_group = None
450-
found_group_members = []
451-
else:
452-
self.echo.echo_vv('Adding "{}", line {} to group {}'.format(
453-
token,
454-
annotation['line_number'],
455-
current_group
456-
))
457-
found_group_members.append(token)
458-
else:
459-
if token in group_children:
460-
current_group = self._get_group_for_token(token)
427+
if current_group:
428+
self.errors.append('File("{}") finished with an incomplete group {}!'.format(filename, current_group))
461429

462-
if not current_group: # pragma: no cover
463-
# If we get here there is a problem with check_results' group_children not matching up with
464-
# our config's groups. That puts us in an unknown state, so we should quit.
465-
raise Exception(
466-
f'group_children is out of sync with config.groups. {token} is not in a group!'
467-
)
430+
return not self.errors
468431

469-
found_group_members = [token]
470-
self.echo.echo_vv('Starting new group for "{}" token "{}", line {}'.format(
471-
current_group, token, annotation['line_number'])
472-
)
432+
def check_annotation(self, annotation, current_group, found_group_tokens):
433+
"""
434+
Check an annotation and add annotation errors when necessary.
473435
474-
# If we have all members, this group is done
475-
if current_group and len(found_group_members) == len(self.config.groups[current_group]):
476-
self.echo.echo_vv("Group complete!")
477-
current_group = None
478-
found_group_members = []
436+
Args:
437+
annotation (dict): in particular, every annotation contains 'annotation_token' and 'annotation_data' keys.
438+
current_group (str): None or the name of a group (from self.config.groups) to which preceding annotations
439+
belong.
440+
found_group_tokens (list): annotation tokens from the same group that were already found. This list is
441+
cleared in case of error or when creating a new group.
442+
443+
Return:
444+
current_group (str or None)
445+
"""
446+
self._check_results_choices(annotation)
447+
token = annotation['annotation_token']
479448

449+
if current_group:
450+
# Add to existing group
451+
if token not in self.config.groups[current_group]:
452+
# Check for token correctness
453+
self._add_annotation_error(
454+
annotation,
455+
'"{}" is not in the group that starts with "{}". Expecting one of: {}'.format(
456+
token,
457+
current_group,
458+
self.config.groups[current_group]
459+
)
460+
)
461+
current_group = None
462+
found_group_tokens.clear()
463+
elif token in found_group_tokens:
464+
# Check for duplicate tokens
465+
self._add_annotation_error(
466+
annotation,
467+
'"{}" is already in the group that starts with "{}"'.format(token, current_group)
468+
)
469+
current_group = None
470+
found_group_tokens.clear()
471+
else:
472+
# Token is correct
473+
self.echo.echo_vv('Adding "{}", line {} to group {}'.format(
474+
token,
475+
annotation['line_number'],
476+
current_group
477+
))
478+
found_group_tokens.append(token)
479+
else:
480+
current_group = self._get_group_for_token(token)
480481
if current_group:
481-
self.errors.append(f'File("{filename}") finished with an incomplete group {current_group}!')
482+
# Start a new group
483+
found_group_tokens.clear()
484+
found_group_tokens.append(token)
485+
self.echo.echo_vv('Starting new group for "{}" token "{}", line {}'.format(
486+
current_group, token, annotation['line_number'])
487+
)
482488

483-
return not self.errors
489+
# If we have all members, this group is done
490+
if current_group and len(found_group_tokens) == len(self.config.groups[current_group]):
491+
self.echo.echo_vv("Group complete!")
492+
current_group = None
493+
found_group_tokens.clear()
494+
495+
return current_group
484496

485497
def _add_annotation_error(self, annotation, message):
486498
"""

0 commit comments

Comments
 (0)