Source code for devctrl.devices.device_select

from .device_info import DeviceInfo


[docs] def select_device_interactive(dev_info: DeviceInfo, prompt="Select a device for <name>: ") -> tuple[str, str]: """ Select a device interactively from the command line :param dev_info: A DeviceInfo object. The list of devices will be determined from by calling `dev_info.device_type.list_devices()` :param prompt: A prompt to display. The following expressions will be substituted: - '<key>' - '<name>' - '<device_type>' - '<connect_priority>' - '<global_varname>' - '<N>': number of devices found :return: The type and name of the selected device. These can be passed to the `connect_device` method of the device type to actually connect a device. """ type_devices = dev_info.device_type.list_devices() substs = { '<key>': dev_info.key, '<name>': dev_info.name, '<device_type>': dev_info.device_type.get_device_type_name(), '<connect_priority>': str(dev_info.connect_priority), '<global_varname>': dev_info.global_varname, '<N>': str(len(type_devices)), } for k, v in substs.items(): prompt = prompt.replace(k, v) for i, (t,v) in enumerate(type_devices): print(f"{i+1:02}: {t} - {v}") while len(type_devices) > 0: try: instr = int(input(prompt)) - 1 if instr < 0 or instr >= len(type_devices): raise ValueError return type_devices[instr] except ValueError: print(f"Enter a number between 1 and {len(type_devices)}") continue raise Exception("No devices found")