def __determine_device_id(ctx_params):
# All attached devices
devices = Adb.get_devices_as_list()
if not devices:
raise click.ClickException("No devices attached")
device_param = ctx_params["device"]
index_param = ctx_params["index"]
if device_param is not None and index_param is not None:
# Only one of the two options can be passed
raise click.BadOptionUsage(
"Only one of the options '--device' or '--index' is allowed")
elif device_param is not None:
# Only return the device id when the device is attached
if __is_device_id_attached(devices, device_param):
return device_param
else:
raise click.ClickException(
"The device '%s' isn't attached" % device_param)
elif index_param is not None:
# Get the device id from the index
# Only return the device id when the device is attached
try:
return devices[index_param][0]
except IndexError:
raise click.ClickException(
"No device with the index '%s' available" % index_param)
# When no option is provided, then read the local and global config
device_id = Config.read_value(Config.SECTION_DEVICE,
Config.KEY_DEFAULT)
if device_id and __is_device_id_attached(devices, device_id):
# Only return the device id when the device is attached
return device_id
if len(devices) == 1:
# Last resort: Return the only attached device
return devices[0][0]
else:
raise click.ClickException("Can't determine the best matching device")
评论列表
文章目录