tools/metrics/histograms/update_histogram_enum.py
def ReadHistogramValues(filename, start_marker, end_marker, strip_k_prefix):
  """Creates a dictionary of enum values, read from a C++ file.

  Args:
      filename: The unix-style path (relative to src/) of the file to open.
      start_marker: A regex that signifies the start of the enum values.
      end_marker: A regex that signifies the end of the enum values.
      strip_k_prefix: Set to True if enum values are declared as kFoo and the
          'k' should be stripped.

  Returns:
      A dictionary from enum value to enum label.

  Raises:
      DuplicatedValue: An error when two enum labels share the same value.
  """
  # Read the file as a list of lines
  with io.open(path_util.GetInputFile(filename)) as f:
    content = f.readlines()

  START_REGEX = re.compile(start_marker)
  ITEM_REGEX = re.compile(r'^(\w+)')
  ITEM_REGEX_WITH_INIT = re.compile(r'(\w+)\s*=\s*(\d*)')
  WRAPPED_INIT = re.compile(r'(\d+)')
  END_REGEX = re.compile(end_marker)

  iterator = iter(content)
  # Find the start of the enum
  for line in iterator:
    if START_REGEX.match(line.strip()):
      break

  enum_value = 0
  result = {}
  for line in iterator:
    line = line.strip()
    # Exit condition: we reached last enum value
    if END_REGEX.match(line):
      break
    # Inside enum: generate new xml entry
    m = ITEM_REGEX_WITH_INIT.match(line)
    if m:
      label = m.group(1)
      if m.group(2):
        enum_value = int(m.group(2))
      else:
        # Enum name is so long that the value wrapped to the next line
        next_line = next(iterator).strip()
        enum_value = int(WRAPPED_INIT.match(next_line).group(1))
    else:
      m = ITEM_REGEX.match(line)
      if m:
        label = m.group(1)
      else:
        continue
    if strip_k_prefix:
      assert label.startswith('k'), "Enum " + label + " should start with 'k'."
      label = label[1:]
    # If two enum labels have the same value
    if enum_value in result:
      raise DuplicatedValue(result[enum_value], label)
    result[enum_value] = label
    enum_value += 1
  return result
Creates a dictionary of enum values, read from a C++ file.

  Args:
      filename: The unix-style path (relative to src/) of the file to open.
      start_marker: A regex that signifies the start of the enum values.
      end_marker: A regex that signifies the end of the enum values.
      strip_k_prefix: Set to True if enum values are declared as kFoo and the
          'k' should be stripped.

  Returns:
      A dictionary from enum value to enum label.

  Raises:
      DuplicatedValue: An error when two enum labels share the same value.
  
Creates a dictionary of enum values, read from a C++ file.
Creates a dictionary of enum values read from a C ++ file .
def ReadHistogramValues(filename, start_marker, end_marker, strip_k_prefix):
  with io.open(path_util.GetInputFile(filename)) as f:
    content = f.readlines()
  START_REGEX = re.compile(start_marker)
  ITEM_REGEX = re.compile(r'^(\w+)')
  ITEM_REGEX_WITH_INIT = re.compile(r'(\w+)\s*=\s*(\d*)')
  WRAPPED_INIT = re.compile(r'(\d+)')
  END_REGEX = re.compile(end_marker)
  iterator = iter(content)
  for line in iterator:
    if START_REGEX.match(line.strip()):
      break
  enum_value = 0
  result = {}
  for line in iterator:
    line = line.strip()
    if END_REGEX.match(line):
      break
    m = ITEM_REGEX_WITH_INIT.match(line)
    if m:
      label = m.group(1)
      if m.group(2):
        enum_value = int(m.group(2))
      else:
        next_line = next(iterator).strip()
        enum_value = int(WRAPPED_INIT.match(next_line).group(1))
    else:
      m = ITEM_REGEX.match(line)
      if m:
        label = m.group(1)
      else:
        continue
    if strip_k_prefix:
      assert label.startswith('k'), "Enum " + label + " should start with 'k'."
      label = label[1:]
    if enum_value in result:
      raise DuplicatedValue(result[enum_value], label)
    result[enum_value] = label
    enum_value += 1
  return result
def ReadHistogramValues ( filename , start_marker , end_marker , strip_k_prefix ) : with io . open ( path_util . GetInputFile ( filename ) ) as f : content = f . readlines ( ) START_REGEX = re . compile ( start_marker ) ITEM_REGEX = re . compile ( r'^(\w+)' ) ITEM_REGEX_WITH_INIT = re . compile ( r'(\w+)\s*=\s*(\d*)' ) WRAPPED_INIT = re . compile ( r'(\d+)' ) END_REGEX = re . compile ( end_marker ) iterator = iter ( content ) for line in iterator : if START_REGEX . match ( line . strip ( ) ) : break enum_value = 0 result = { } for line in iterator : line = line . strip ( ) if END_REGEX . match ( line ) : break m = ITEM_REGEX_WITH_INIT . match ( line ) if m : label = m . group ( 1 ) if m . group ( 2 ) : enum_value = int ( m . group ( 2 ) ) else : next_line = next ( iterator ) . strip ( ) enum_value = int ( WRAPPED_INIT . match ( next_line ) . group ( 1 ) ) else : m = ITEM_REGEX . match ( line ) if m : label = m . group ( 1 ) else : continue if strip_k_prefix : assert label . startswith ( 'k' ) , "Enum " + label + " should start with 'k'." label = label [ 1 : ] if enum_value in result : raise DuplicatedValue ( result [ enum_value ] , label ) result [ enum_value ] = label enum_value += 1 return result
Creates a dictionary of enum values, read from a C++ file.
Creates a dictionary of enum values read from a C ++ file .
"""Creates a dictionary of enum values, read from a C++ file.

  Args:
      filename: The unix-style path (relative to src/) of the file to open.
      start_marker: A regex that signifies the start of the enum values.
      end_marker: A regex that signifies the end of the enum values.
      strip_k_prefix: Set to True if enum values are declared as kFoo and the
          'k' should be stripped.

  Returns:
      A dictionary from enum value to enum label.

  Raises:
      DuplicatedValue: An error when two enum labels share the same value.
  """ # Read the file as a list of lines # Find the start of the enum # Exit condition: we reached last enum value # Inside enum: generate new xml entry # Enum name is so long that the value wrapped to the next line # If two enum labels have the same value
A dictionary from enum value to enum label.
A dictionary from enum value to enum label .
An error when two enum labels share the same value.
An error when two enum labels share the same value .
DuplicatedValue
filename
The unix-style path (relative to src/) of the file to open.
The unix - style path ( relative to src / ) of the file to open .
start_marker
A regex that signifies the start of the enum values.
A regex that signifies the start of the enum values .
end_marker
A regex that signifies the end of the enum values.
A regex that signifies the end of the enum values .
strip_k_prefix
Set to True if enum values are declared as kFoo and the
'k' should be stripped.
Set to True if enum values are declared as kFoo and the ' k ' should be stripped .