2022-10-08 02:17:04 +02:00
|
|
|
import click
|
2023-01-02 03:54:31 +01:00
|
|
|
from json import dumps as json_dump
|
2023-01-02 02:45:26 +01:00
|
|
|
import logging
|
2022-10-08 02:17:04 +02:00
|
|
|
|
2023-01-02 02:45:26 +01:00
|
|
|
from config.state import config
|
|
|
|
|
|
|
|
from .device import get_devices, get_profile_device
|
2022-10-08 02:17:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
@click.command(name='devices')
|
2023-01-02 03:54:31 +01:00
|
|
|
@click.option('--json', is_flag=True, help='output machine-parsable JSON format')
|
|
|
|
def cmd_devices(json: bool = False):
|
2022-10-08 02:17:04 +02:00
|
|
|
'list the available devices and descriptions'
|
|
|
|
devices = get_devices()
|
|
|
|
if not devices:
|
|
|
|
raise Exception("No devices found!")
|
2023-01-02 02:45:26 +01:00
|
|
|
profile_device = None
|
|
|
|
try:
|
|
|
|
dev = get_profile_device()
|
|
|
|
assert dev
|
|
|
|
profile_device = dev
|
|
|
|
except Exception as ex:
|
|
|
|
logging.debug(f"Failed to get profile device for visual highlighting, not a problem: {ex}")
|
|
|
|
output = ['']
|
2023-01-02 03:54:31 +01:00
|
|
|
json_output = {}
|
2023-01-02 02:45:26 +01:00
|
|
|
for name in sorted(devices.keys()):
|
|
|
|
prefix = ''
|
|
|
|
suffix = ''
|
|
|
|
device = devices[name]
|
|
|
|
assert device
|
|
|
|
try:
|
|
|
|
device.parse_deviceinfo(try_download=False)
|
|
|
|
except Exception as ex:
|
|
|
|
logging.debug(f"Failed to parse deviceinfo for extended description, not a problem: {ex}")
|
2023-01-02 03:54:31 +01:00
|
|
|
if json:
|
|
|
|
json_output[name] = device.get_summary().toDict()
|
|
|
|
continue
|
2023-01-02 02:45:26 +01:00
|
|
|
if profile_device and profile_device.name == device.name:
|
|
|
|
prefix = '>>> '
|
|
|
|
suffix = f'\n\nCurrently selected by profile "{config.file.profiles.current}"'
|
|
|
|
snippet = f'{device}{suffix}'
|
|
|
|
# prefix each line in the snippet
|
|
|
|
snippet = '\n'.join([f'{prefix}{line}' for line in snippet.split('\n')])
|
|
|
|
output.append(f"{snippet}\n")
|
2023-01-02 03:54:31 +01:00
|
|
|
if json:
|
|
|
|
output = ['\n' + json_dump(json_output, indent=4)]
|
2023-01-02 02:45:26 +01:00
|
|
|
for line in output:
|
|
|
|
print(line)
|