import re
import json


def _DeviceSpecsToTFCRunTarget(device_specs):
  """Convert device specs to TFC run target format."""
  groups = []
  for spec in device_specs:
    attrs = []
    for match in re.finditer(r'([^\s:]+):(\S+)', spec):
      key = match.group(1)
      value = match.group(2)
      attrs.append({'name': key, 'value': value})
    groups.append({'run_targets': [{'name': '*', 'device_attributes': attrs}]})
  obj = {'host': {'groups': groups}}
  return json.dumps(obj)