Skip to content

Commit

Permalink
cached function
Browse files Browse the repository at this point in the history
  • Loading branch information
sulincix committed Jul 8, 2024
1 parent 2a21114 commit 47d78e8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/common/cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cache_fn = {}
def cached(func):
def wrapper(*args, **kwargs):
if func in cache_fn:
return cache_fn[func]
else:
ret = func(*args, **kwargs)
cache_fn[func] = ret
return ret
return wrapper
11 changes: 11 additions & 0 deletions src/common/detect.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
from file import readfile
from cache import cached

@cached
def get_device_type():
chassis_names = [
"Other (1)",
Expand Down Expand Up @@ -46,6 +48,7 @@ def get_device_type():
return chassis_names[type]
return chassis_names[1]

@cached
def is_laptop():
if os.path.isdir("/proc/pmu"):
return "Battery" in open("/proc/pmu/info","r").read()
Expand All @@ -69,6 +72,7 @@ def is_laptop():
return False


@cached
def is_support_deep():
return "deep" in readfile("/sys/power/mem_sleep")

Expand All @@ -78,26 +82,32 @@ def which(command):
return "{}/{}".format(dir,command)
return None

@cached
def is_live():
return "boot=live" in readfile("/proc/cmdline")

@cached
def is_virtual_machine():
cpuinfo = readfile("/proc/cpuinfo").split("\n")
for line in cpuinfo:
if line.startswith("flags"):
return "hypervisor" in line
return False

@cached
def is_chroot():
return readfile("/proc/1/mountinfo") != readfile("/proc/self/mountinfo")

@cached
def is_docker():
return "docker" in readfile("/proc/1/cgroup")

@cached
def is_root():
return os.getuid() == 0

acpi_support = None
@cached
def is_acpi_supported():
global acpi_support
if acpi_support == None:
Expand All @@ -108,5 +118,6 @@ def is_acpi_supported():
acpi_support = False
return acpi_support

@cached
def is_oem_available():
return os.path.exists("/sys/firmware/acpi/MSDM")
3 changes: 3 additions & 0 deletions src/common/power.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import sys
from file import *
from cache import cached

def get_ac_online():
if not os.path.exists("/sys/class/power_supply/"):
return True
Expand All @@ -23,6 +25,7 @@ def get_ac_online():
return True
return True

@cached
def get_acpi_power_devices():
devices = []
for interface in os.listdir("/sys/bus/acpi/devices/"):
Expand Down

0 comments on commit 47d78e8

Please sign in to comment.