def _open_usb_handle(**kwargs):
  """Open a UsbHandle subclass, based on configuration.

  If configuration 'remote_usb' is set, use it to connect to remote usb,
  otherwise attempt to connect locally.'remote_usb' is set to usb type,
  EtherSync or other.

  Example of Cambrionix unit in config:
  remote_usb: ethersync
  ethersync:
       mac_addr: 78:a5:04:ca:91:66
       plug_port: 5

  Args:
    **kwargs: Arguments to pass to respective handle's Open() method.

  Returns:
    Instance of UsbHandle.
  """

  serial = None
  remote_usb = conf.remote_usb
  if remote_usb:
    if remote_usb.strip() == 'ethersync':
      device = conf.ethersync
      try:
        mac_addr = device['mac_addr']
        port = device['plug_port']
      except (__HOLE__,TypeError):
        raise ValueError('Ethersync needs mac_addr and plug_port to be set')
      else:
        ethersync = cambrionix.EtherSync(mac_addr)
        serial = ethersync.GetUSBSerial(port)

  return local_usb.LibUsbHandle.Open(serial_number=serial, **kwargs)


# pylint: disable=too-few-public-methods
dataset/ETHPy150Open google/openhtf/openhtf/plugs/usb/__init__.py/_open_usb_handle