Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Hvornum <anton@hvornum.se>2021-09-06 18:01:31 +0200
committerGitHub <noreply@github.com>2021-09-06 18:01:31 +0200
commit6aa028d29d5313b15c020ad614a670c372f4aa65 (patch)
tree21691fc895b13c0b2e77baed3100cbe5d09b6cf8
parent6ead7679073f9a8c50ceb42d0d15b6bfbbbef903 (diff)
parent0a8fe402a4a739fb87bad317f066c142b5ad88df (diff)
Merge pull request #626 from conqp/refactor_meminfo
Refactor meminfo() to allow direct key access
-rw-r--r--archinstall/lib/hardware.py35
1 files changed, 23 insertions, 12 deletions
diff --git a/archinstall/lib/hardware.py b/archinstall/lib/hardware.py
index d1f160c7..b58e45af 100644
--- a/archinstall/lib/hardware.py
+++ b/archinstall/lib/hardware.py
@@ -1,7 +1,7 @@
import json
import os
from pathlib import Path
-from typing import Iterator, Optional
+from typing import Iterator, Optional, Union
from .general import SysCommand
from .networking import list_interfaces, enrich_iface_types
@@ -58,6 +58,7 @@ AVAILABLE_GFX_DRIVERS = {
}
CPUINFO = Path("/proc/cpuinfo")
+MEMINFO = Path("/proc/meminfo")
def cpuinfo() -> Iterator[dict[str, str]]:
@@ -75,6 +76,24 @@ def cpuinfo() -> Iterator[dict[str, str]]:
cpu[key.strip()] = value.strip()
+def meminfo(key: Optional[str] = None) -> Union[dict[str, int], int]:
+ """Returns a dict with memory info if called with no args
+ or the value of the given key of said dict.
+ """
+ mem_info = {}
+
+ with MEMINFO.open() as file:
+ mem_info = {
+ (columns := line.strip().split())[0].rstrip(':'): int(columns[1])
+ for line in file
+ }
+
+ if key is None:
+ return mem_info
+
+ return mem_info.get(key)
+
+
def has_wifi() -> bool:
return 'WIRELESS' in enrich_iface_types(list_interfaces().values()).values()
@@ -134,24 +153,16 @@ def product_name() -> Optional[str]:
return product.read().strip()
-def mem_info():
- # This implementation is from https://stackoverflow.com/a/28161352
- return {
- i.split()[0].rstrip(':'): int(i.split()[1])
- for i in open('/proc/meminfo').readlines()
- }
-
-
def mem_available() -> Optional[str]:
- return mem_info()['MemAvailable']
+ return meminfo('MemAvailable')
def mem_free() -> Optional[str]:
- return mem_info()['MemFree']
+ return meminfo('MemFree')
def mem_total() -> Optional[str]:
- return mem_info()['MemTotal']
+ return meminfo('MemTotal')
def virtualization() -> Optional[str]: