예제 #1
0
def device_list(  # pylint: disable=too-many-branches
    serial, logical, mdns, json_output
):
    if not logical and not mdns:
        serial = True
    data = {}
    if serial:
        data["serial"] = util.get_serial_ports()
    if logical:
        data["logical"] = util.get_logical_devices()
    if mdns:
        data["mdns"] = util.get_mdns_services()

    single_key = list(data)[0] if len(list(data)) == 1 else None

    if json_output:
        return click.echo(
            dump_json_to_unicode(data[single_key] if single_key else data)
        )

    titles = {
        "serial": "Serial Ports",
        "logical": "Logical Devices",
        "mdns": "Multicast DNS Services",
    }

    for key, value in data.items():
        if not single_key:
            click.secho(titles[key], bold=True)
            click.echo("=" * len(titles[key]))

        if key == "serial":
            for item in value:
                click.secho(item["port"], fg="cyan")
                click.echo("-" * len(item["port"]))
                click.echo("Hardware ID: %s" % item["hwid"])
                click.echo("Description: %s" % item["description"])
                click.echo("")

        if key == "logical":
            for item in value:
                click.secho(item["path"], fg="cyan")
                click.echo("-" * len(item["path"]))
                click.echo("Name: %s" % item["name"])
                click.echo("")

        if key == "mdns":
            for item in value:
                click.secho(item["name"], fg="cyan")
                click.echo("-" * len(item["name"]))
                click.echo("Type: %s" % item["type"])
                click.echo("IP: %s" % item["ip"])
                click.echo("Port: %s" % item["port"])
                if item["properties"]:
                    click.echo(
                        "Properties: %s"
                        % (
                            "; ".join(
                                [
                                    "%s=%s" % (k, v)
                                    for k, v in item["properties"].items()
                                ]
                            )
                        )
                    )
                click.echo("")

        if single_key:
            click.echo("")

    return True
예제 #2
0
def device_list(  # pylint: disable=too-many-branches
        serial, logical, mdns, json_output):
    if not logical and not mdns:
        serial = True
    data = {}
    if serial:
        data['serial'] = util.get_serial_ports()
    if logical:
        data['logical'] = util.get_logical_devices()
    if mdns:
        data['mdns'] = util.get_mdns_services()

    single_key = data.keys()[0] if len(data.keys()) == 1 else None

    if json_output:
        return click.echo(json.dumps(data[single_key] if single_key else data))

    titles = {
        "serial": "Serial Ports",
        "logical": "Logical Devices",
        "mdns": "Multicast DNS Services"
    }

    for key, value in data.iteritems():
        if not single_key:
            click.secho(titles[key], bold=True)
            click.echo("=" * len(titles[key]))

        if key == "serial":
            for item in value:
                click.secho(item['port'], fg="cyan")
                click.echo("-" * len(item['port']))
                click.echo("Hardware ID: %s" % item['hwid'])
                click.echo("Description: %s" % item['description'])
                click.echo("")

        if key == "logical":
            for item in value:
                click.secho(item['path'], fg="cyan")
                click.echo("-" * len(item['path']))
                click.echo("Name: %s" % item['name'])
                click.echo("")

        if key == "mdns":
            for item in value:
                click.secho(item['name'], fg="cyan")
                click.echo("-" * len(item['name']))
                click.echo("Type: %s" % item['type'])
                click.echo("IP: %s" % item['ip'])
                click.echo("Port: %s" % item['port'])
                if item['properties']:
                    click.echo("Properties: %s" % ("; ".join([
                        "%s=%s" % (k, v)
                        for k, v in item['properties'].iteritems()
                    ])))
                click.echo("")

        if single_key:
            click.echo("")

    return True