Merge pull request #672 from conqp/hardware_refactoring

Hardware refactoring
This commit is contained in:
Anton Hvornum 2021-10-27 11:56:51 +02:00 committed by GitHub
commit ea1cf70ae3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 10 deletions

View File

@ -1,4 +1,5 @@
import os import os
from functools import partial
from pathlib import Path from pathlib import Path
from typing import Iterator, Optional, Union from typing import Iterator, Optional, Union
@ -75,12 +76,10 @@ def cpuinfo() -> Iterator[dict[str, str]]:
cpu[key.strip()] = value.strip() cpu[key.strip()] = value.strip()
def meminfo(key: Optional[str] = None) -> Union[dict[str, int], int]: def meminfo(key: Optional[str] = None) -> Union[dict[str, int], Optional[int]]:
"""Returns a dict with memory info if called with no args """Returns a dict with memory info if called with no args
or the value of the given key of said dict. or the value of the given key of said dict.
""" """
mem_info = {}
with MEMINFO.open() as file: with MEMINFO.open() as file:
mem_info = { mem_info = {
(columns := line.strip().split())[0].rstrip(':'): int(columns[1]) (columns := line.strip().split())[0].rstrip(':'): int(columns[1])
@ -97,11 +96,11 @@ def has_wifi() -> bool:
return 'WIRELESS' in enrich_iface_types(list_interfaces().values()).values() return 'WIRELESS' in enrich_iface_types(list_interfaces().values()).values()
def has_amd_cpu() -> bool: def has_cpu_vendor(vendor_id: str) -> bool:
return any(cpu.get("vendor_id") == "AuthenticAMD" for cpu in cpuinfo()) return any(cpu.get("vendor_id") == vendor_id for cpu in cpuinfo())
def has_intel_cpu() -> bool: has_amd_cpu = partial(has_cpu_vendor, "AuthenticAMD")
return any(cpu.get("vendor_id") == "GenuineIntel" for cpu in cpuinfo()) has_intel_cpu = partial(has_cpu_vendor, "GenuineIntel")
def has_uefi() -> bool: def has_uefi() -> bool:
return os.path.isdir('/sys/firmware/efi') return os.path.isdir('/sys/firmware/efi')
@ -152,15 +151,15 @@ def product_name() -> Optional[str]:
return product.read().strip() return product.read().strip()
def mem_available() -> Optional[str]: def mem_available() -> Optional[int]:
return meminfo('MemAvailable') return meminfo('MemAvailable')
def mem_free() -> Optional[str]: def mem_free() -> Optional[int]:
return meminfo('MemFree') return meminfo('MemFree')
def mem_total() -> Optional[str]: def mem_total() -> Optional[int]:
return meminfo('MemTotal') return meminfo('MemTotal')