def ParseClientId(client_id, uuid=None):
  """Splits a client id string and converts all key/value pairs to a dict.

  Also, this truncates string values to 500 characters.

  Args:
    client_id: string client id with "|" as delimiter.
    uuid: optional string uuid to override the uuid in client_id.
  Returns:
    Dict. Client id string "foo=bar|key=|one=1" yields
        {'foo': 'bar', 'key': None, 'one': '1'}.
  """
  if client_id and client_id.find('\n') > -1:
    logging.warning(
        'ParseClientId: client_id has newline: %s',
        base64.b64encode(client_id))
    client_id = client_id.replace('\n', '_')

  # Convert str input to unicode.
  if type(client_id) is str:
    try:
      client_id = client_id.decode('utf-8')
    except UnicodeDecodeError:
      client_id = client_id.decode('utf-8', 'replace')
      logging.warning('UnicodeDecodeError on client_id: %s', client_id)

  out = KeyValueStringToDict(client_id)

  # If any required fields were not present in the client id string, add them.
  # Also cast all values to their defined output types.
  for field, value_type in CLIENT_ID_FIELDS.iteritems():
    if field not in out or out[field] is None:
      out[field] = None
    elif value_type is bool:
      out[field] = GetBoolValueFromString(out[field])
    elif value_type is str:
      # truncate str fields to 500 characters, the StringProperty limit.
      out[field] = out[field][:500]
    else:
      try:
        out[field] = value_type(out[field])
      except ValueError:
        logging.warning(
            'Error casting client id %s to defined type: %s', field, out[field])
        out[field] = None

  if out['track'] not in common.TRACKS:
    if out['track'] is not None:
      logging.warning('Invalid track requested: %s', out['track'])
    out['track'] = common.DEFAULT_TRACK

  if uuid:
    out['uuid'] = common.SanitizeUUID(uuid)

  return out
dataset/ETHPy150Open google/simian/src/simian/mac/munki/common.py/ParseClientId/original