#!/usr/bin/env python3

import json
import platform


def make_sys_report(anonymous=False, skipUsb=False, skipPackages=False):
    def get_usb():
        try:
            import usb.core
        except ImportError:
            yield "NoLib"
            return
        speeds = ["Unknown", "Low", "Full", "High", "Super", "SuperPlus"]
        format_hex = lambda val: f"{val:#0{6}x}"
        try:
            for dev in usb.core.find(find_all=True):
                yield {
                    "port": dev.port_number,
                    "vendor_id": format_hex(dev.idVendor),
                    "product_id": format_hex(dev.idProduct),
                    "speed": speeds[dev.speed] if dev.speed < len(speeds) else dev.speed
                }
        except usb.core.NoBackendError:
            yield "No USB backend found"

    result = {
        "architecture": ' '.join(platform.architecture()).strip(),
        "machine": platform.machine(),
        "platform": platform.platform(),
        "processor": platform.processor(),
        "python_build": ' '.join(platform.python_build()).strip(),
        "python_compiler": platform.python_compiler(),
        "python_implementation": platform.python_implementation(),
        "python_version": platform.python_version(),
        "release": platform.release(),
        "system": platform.system(),
        "version": platform.version(),
        "win32_ver": ' '.join(platform.win32_ver()).strip(),
    }

    if not skipPackages:
        from pip._internal.operations.freeze import freeze
        result["packages"] = list(freeze(local_only=True))
    if not skipUsb:
        result["usb"] = list(get_usb())
    if not anonymous:
        result["uname"] = ' '.join(platform.uname()).strip(),
    return result


if __name__ == "__main__":
    data = make_sys_report()
    with open("log_system_information.json", "w") as f:
        json.dump(data, f, indent=4)

    print(json.dumps(data, indent=4))
    print("System info gathered successfully - saved as \"log_system_information.json\"")

[((29, 19, 29, 37), 'platform.machine', 'platform.machine', ({}, {}), '()', False, 'import platform\n'), ((30, 20, 30, 39), 'platform.platform', 'platform.platform', ({}, {}), '()', False, 'import platform\n'), ((31, 21, 31, 41), 'platform.processor', 'platform.processor', ({}, {}), '()', False, 'import platform\n'), ((33, 27, 33, 53), 'platform.python_compiler', 'platform.python_compiler', ({}, {}), '()', False, 'import platform\n'), ((34, 33, 34, 65), 'platform.python_implementation', 'platform.python_implementation', ({}, {}), '()', False, 'import platform\n'), ((35, 26, 35, 51), 'platform.python_version', 'platform.python_version', ({}, {}), '()', False, 'import platform\n'), ((36, 19, 36, 37), 'platform.release', 'platform.release', ({}, {}), '()', False, 'import platform\n'), ((37, 18, 37, 35), 'platform.system', 'platform.system', ({}, {}), '()', False, 'import platform\n'), ((38, 19, 38, 37), 'platform.version', 'platform.version', ({}, {}), '()', False, 'import platform\n'), ((55, 8, 55, 36), 'json.dump', 'json.dump', (), '', False, 'import json\n'), ((57, 10, 57, 36), 'json.dumps', 'json.dumps', (), '', False, 'import json\n'), ((44, 34, 44, 57), 'pip._internal.operations.freeze.freeze', 'freeze', (), '', False, 'from pip._internal.operations.freeze import freeze\n'), ((28, 33, 28, 56), 'platform.architecture', 'platform.architecture', ({}, {}), '()', False, 'import platform\n'), ((32, 33, 32, 56), 'platform.python_build', 'platform.python_build', ({}, {}), '()', False, 'import platform\n'), ((39, 30, 39, 50), 'platform.win32_ver', 'platform.win32_ver', ({}, {}), '()', False, 'import platform\n'), ((48, 35, 48, 51), 'platform.uname', 'platform.uname', ({}, {}), '()', False, 'import platform\n')]