import collections


def parse_bv_keywords(section):
  """Grab the keywords in a BrainVision section, and create a dictionary.

  Args:
    section: a section starts with [xxx], and contains lines of key=value

  Returns:
    A dictionary of keys and values from this section.
  """
  section = section.split(']', 1)[1]
  # Use OrderedDict so the channel list stays in order
  section_dict = collections.OrderedDict(())
  for key_value in section.split('\n'):
    if not key_value or key_value[0] == ';':
      continue
    if '=' in key_value:
      key, value = key_value.split('=', 1)
      key = key.strip()
      value = value.strip()
      try:
        value = int(value) if value.isdigit() else float(value)
      except ValueError:
        # Leave value as a str.
        pass
      section_dict[key] = value
  return section_dict