mirror of
https://gitlab.com/kupfer/kupferbootstrap.git
synced 2025-02-23 05:35:44 -05:00
84 lines
3.1 KiB
Python
84 lines
3.1 KiB
Python
import click
|
|
import logging
|
|
|
|
from json import dumps as json_dump
|
|
from typing import Optional
|
|
|
|
from config.state import config
|
|
from utils import colors_supported, color_str
|
|
|
|
from .device import get_devices, get_profile_device
|
|
|
|
|
|
@click.command(name='devices')
|
|
@click.option('-j', '--json', is_flag=True, help='output machine-parsable JSON format')
|
|
@click.option(
|
|
'--force-parse-deviceinfo/--no-parse-deviceinfo',
|
|
is_flag=True,
|
|
default=None,
|
|
help="Force or disable deviceinfo parsing. The default is to try but continue if it fails.",
|
|
)
|
|
@click.option(
|
|
'--download-packages/--no-download-packages',
|
|
is_flag=True,
|
|
default=False,
|
|
help='Download packages while trying to parse deviceinfo',
|
|
)
|
|
@click.option('--output-file', type=click.Path(exists=False, file_okay=True), help="Dump JSON to file")
|
|
def cmd_devices(
|
|
json: bool = False,
|
|
force_parse_deviceinfo: Optional[bool] = True,
|
|
download_packages: bool = False,
|
|
output_file: Optional[str] = None,
|
|
):
|
|
'list the available devices and descriptions'
|
|
devices = get_devices()
|
|
if not devices:
|
|
raise Exception("No devices found!")
|
|
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 = ['']
|
|
json_output = {}
|
|
interactive_json = json and not output_file
|
|
if output_file:
|
|
json = True
|
|
use_colors = colors_supported(False if interactive_json else config.runtime.colors)
|
|
for name in sorted(devices.keys()):
|
|
prefix = ''
|
|
suffix = ''
|
|
device = devices[name]
|
|
assert device
|
|
if force_parse_deviceinfo in [None, True]:
|
|
try:
|
|
device.parse_deviceinfo(try_download=download_packages)
|
|
except Exception as ex:
|
|
if not force_parse_deviceinfo:
|
|
logging.debug(f"Failed to parse deviceinfo for extended description, not a problem: {ex}")
|
|
else:
|
|
raise ex
|
|
|
|
if json:
|
|
json_output[name] = device.get_summary().toDict()
|
|
if interactive_json:
|
|
continue
|
|
if profile_device and profile_device.name == device.name:
|
|
prefix = color_str('>>> ', bold=True, fg="bright_green", use_colors=use_colors)
|
|
suffix = '\n\n'
|
|
suffix += color_str('Currently selected by profile', bold=True, use_colors=use_colors) + " "
|
|
suffix += color_str(f'"{config.file.profiles.current}"', bold=True, fg="bright_green", use_colors=use_colors)
|
|
snippet = f'{device.nice_str(colors=use_colors, newlines=True)}{suffix}'
|
|
# prefix each line in the snippet
|
|
snippet = '\n'.join([f'{prefix}{line}' for line in snippet.split('\n')])
|
|
output.append(f"{snippet}\n")
|
|
if interactive_json:
|
|
output = ['\n' + json_dump(json_output, indent=4)]
|
|
if output_file:
|
|
with open(output_file, 'w') as fd:
|
|
fd.write(json_dump(json_output))
|
|
for line in output:
|
|
print(line)
|