示例#1
0
def get_system_info() -> Tuple[List[str], List[str]]:
    info_lines: List[str] = []

    def info(name: str, value: str, *format_args: object) -> None:
        line = bright_colored(name + ":", Fore.YELLOW) + " " + value
        if format_args:
            line = line % format_args
        info_lines.append(line)

    username = getuser()
    hostname = _get_hostname()

    info_lines.append(
        bright_colored(username, Fore.BLUE) + "@" +
        bright_colored(hostname, Fore.RED))
    info_lines.append("")

    distro_id, distro_name, distro_version, distro_codename = _get_distro_info(
    )
    info("OS", " ".join([distro_name, distro_version, distro_codename]))

    logo_path = os.path.join(os.path.dirname(__file__), "logos", distro_id)
    with open(logo_path) as logo_file:
        logo_lines = logo_file.read().splitlines()

    info("Kernel", "%s %s", platform.system(), platform.release())

    info("Uptime", humanize_timedelta(_get_uptime()))

    users_info = _get_users()
    if users_info:
        info("Users", users_info)

    shell = _get_shell()
    if shell is not None:
        info("Shell", shell)

    info_lines.append("")

    cpu_usage_info = _get_cpu_usage()
    if cpu_usage_info is not None:
        info("CPU Usage", "%s", cpu_usage_info)
    info("Memory", "%s / %s (%s)", *_get_memory())

    for disk_info in _get_disks():
        info("Disk (%s)", "%s / %s (%s)", *disk_info)

    battery_info = _get_battery()
    if battery_info is not None:
        info("Battery", "%s (%s)", *battery_info)

    info_lines.append("")

    for local_ip_address in _get_local_ipv4_addresses():
        info("Local IPv4 Address (%s)", "%s", *local_ip_address)

    return logo_lines, info_lines
示例#2
0
def _get_battery():
    if not hasattr(psutil, "sensors_battery"):
        return None

    try:
        battery = psutil.sensors_battery()
    except Exception as e:
        print(e)
        return None

    if battery is None:
        return None

    percent = battery.percent
    if battery.power_plugged:
        status = "charging" if percent < 100 else "fully charged"
    else:
        status = "%s left" % humanize_timedelta(timedelta(seconds=battery.secsleft))
    return colorize_percent(percent, critical=10, warning=20, inverse=True), status