tools/traffic_annotation/scripts/extractor.py
def extract_annotations(file_path: Path) -> List[Annotation]:
  """Extracts and returns annotations from the file at |file_path|."""
  if file_path.suffix not in LANGUAGE_MAPPING:
    raise ValueError("Unrecognized extension '{}' for file '{}'.".format(
        file_path.suffix, str(file_path)))

  language = LANGUAGE_MAPPING[file_path.suffix]

  contents = file_path.read_text(encoding="utf-8")

  defs = []

  # Check for function calls (e.g. DefineNetworkTrafficAnnotation(...))
  for re_match in language.call_detection_regex.finditer(contents):
    if is_inside_comment(re_match.string, re_match.start()):
      continue
    line_number = get_line_number_at(contents, re_match.start())
    annotation = Annotation(language, file_path, line_number,
                            AnnotationType.COMPLETE)
    annotation.parse_definition(re_match)
    defs.append(annotation)

  # Check for test annotations (e.g. TRAFFIC_ANNOTATION_FOR_TESTS)
  for re_match in TEST_ANNOTATION_REGEX.finditer(contents):
    if is_inside_comment(re_match.string, re_match.start()):
      continue
    line_number = get_line_number_at(contents, re_match.start())

    is_partial = bool(re_match.group(1))
    if is_partial:
      type_name = AnnotationType.PARTIAL
      unique_id = 'test_partial'
      extra_id = 'test'
    else:
      type_name = AnnotationType.COMPLETE
      unique_id = 'test'
      extra_id = ''

    annotation = Annotation(
        language,
        file_path,
        line_number,
        type_name=type_name,
        unique_id=unique_id,
        extra_id=extra_id,
        text='Traffic annotation for unit, browser and other tests')
    defs.append(annotation)

  # Check for MISSING_TRAFFIC_ANNOTATION.
  for re_match in MISSING_ANNOTATION_REGEX.finditer(contents):
    if is_inside_comment(re_match.string, re_match.start()):
      continue
    line_number = get_line_number_at(contents, re_match.start())

    annotation = Annotation(language,
                            file_path,
                            line_number,
                            type_name=AnnotationType.COMPLETE,
                            unique_id='missing',
                            text='Function called without traffic annotation.')
    defs.append(annotation)

  # Check for NO_TRAFFIC_ANNOTATION_YET.
  for re_match in NO_ANNOTATION_REGEX.finditer(contents):
    if is_inside_comment(re_match.string, re_match.start()):
      continue
    line_number = get_line_number_at(contents, re_match.start())

    annotation = Annotation(language,
                            file_path,
                            line_number,
                            type_name=AnnotationType.COMPLETE,
                            unique_id='undefined',
                            text='Nothing here yet.')
    defs.append(annotation)

  return defs
Extracts and returns annotations from the file at |file_path|.
Extracts and returns annotations from the file at |file_path|.
Extracts and returns annotations from the file at |file_path| .
def extract_annotations(file_path: Path) -> List[Annotation]:
  if file_path.suffix not in LANGUAGE_MAPPING:
    raise ValueError("Unrecognized extension '{}' for file '{}'.".format(
        file_path.suffix, str(file_path)))
  language = LANGUAGE_MAPPING[file_path.suffix]
  contents = file_path.read_text(encoding="utf-8")
  defs = []
  for re_match in language.call_detection_regex.finditer(contents):
    if is_inside_comment(re_match.string, re_match.start()):
      continue
    line_number = get_line_number_at(contents, re_match.start())
    annotation = Annotation(language, file_path, line_number,
                            AnnotationType.COMPLETE)
    annotation.parse_definition(re_match)
    defs.append(annotation)
  for re_match in TEST_ANNOTATION_REGEX.finditer(contents):
    if is_inside_comment(re_match.string, re_match.start()):
      continue
    line_number = get_line_number_at(contents, re_match.start())
    is_partial = bool(re_match.group(1))
    if is_partial:
      type_name = AnnotationType.PARTIAL
      unique_id = 'test_partial'
      extra_id = 'test'
    else:
      type_name = AnnotationType.COMPLETE
      unique_id = 'test'
      extra_id = ''
    annotation = Annotation(
        language,
        file_path,
        line_number,
        type_name=type_name,
        unique_id=unique_id,
        extra_id=extra_id,
        text='Traffic annotation for unit, browser and other tests')
    defs.append(annotation)
  for re_match in MISSING_ANNOTATION_REGEX.finditer(contents):
    if is_inside_comment(re_match.string, re_match.start()):
      continue
    line_number = get_line_number_at(contents, re_match.start())
    annotation = Annotation(language,
                            file_path,
                            line_number,
                            type_name=AnnotationType.COMPLETE,
                            unique_id='missing',
                            text='Function called without traffic annotation.')
    defs.append(annotation)
  for re_match in NO_ANNOTATION_REGEX.finditer(contents):
    if is_inside_comment(re_match.string, re_match.start()):
      continue
    line_number = get_line_number_at(contents, re_match.start())
    annotation = Annotation(language,
                            file_path,
                            line_number,
                            type_name=AnnotationType.COMPLETE,
                            unique_id='undefined',
                            text='Nothing here yet.')
    defs.append(annotation)
  return defs
def extract_annotations ( file_path : Path ) -> List [ Annotation ] : if file_path . suffix not in LANGUAGE_MAPPING : raise ValueError ( "Unrecognized extension '{}' for file '{}'." . format ( file_path . suffix , str ( file_path ) ) ) language = LANGUAGE_MAPPING [ file_path . suffix ] contents = file_path . read_text ( encoding = "utf-8" ) defs = [ ] for re_match in language . call_detection_regex . finditer ( contents ) : if is_inside_comment ( re_match . string , re_match . start ( ) ) : continue line_number = get_line_number_at ( contents , re_match . start ( ) ) annotation = Annotation ( language , file_path , line_number , AnnotationType . COMPLETE ) annotation . parse_definition ( re_match ) defs . append ( annotation ) for re_match in TEST_ANNOTATION_REGEX . finditer ( contents ) : if is_inside_comment ( re_match . string , re_match . start ( ) ) : continue line_number = get_line_number_at ( contents , re_match . start ( ) ) is_partial = bool ( re_match . group ( 1 ) ) if is_partial : type_name = AnnotationType . PARTIAL unique_id = 'test_partial' extra_id = 'test' else : type_name = AnnotationType . COMPLETE unique_id = 'test' extra_id = '' annotation = Annotation ( language , file_path , line_number , type_name = type_name , unique_id = unique_id , extra_id = extra_id , text = 'Traffic annotation for unit, browser and other tests' ) defs . append ( annotation ) for re_match in MISSING_ANNOTATION_REGEX . finditer ( contents ) : if is_inside_comment ( re_match . string , re_match . start ( ) ) : continue line_number = get_line_number_at ( contents , re_match . start ( ) ) annotation = Annotation ( language , file_path , line_number , type_name = AnnotationType . COMPLETE , unique_id = 'missing' , text = 'Function called without traffic annotation.' ) defs . append ( annotation ) for re_match in NO_ANNOTATION_REGEX . finditer ( contents ) : if is_inside_comment ( re_match . string , re_match . start ( ) ) : continue line_number = get_line_number_at ( contents , re_match . start ( ) ) annotation = Annotation ( language , file_path , line_number , type_name = AnnotationType . COMPLETE , unique_id = 'undefined' , text = 'Nothing here yet.' ) defs . append ( annotation ) return defs
Extracts and returns annotations from the file at |file_path|.
Extracts and returns annotations from the file at |file_path| .
"""Extracts and returns annotations from the file at |file_path|.""" # Check for function calls (e.g. DefineNetworkTrafficAnnotation(...)) # Check for test annotations (e.g. TRAFFIC_ANNOTATION_FOR_TESTS) # Check for MISSING_TRAFFIC_ANNOTATION. # Check for NO_TRAFFIC_ANNOTATION_YET.
file_path
Path