From e84389accc35c8f0e650c0164410a09b8468c07c Mon Sep 17 00:00:00 2001 From: infeeeee Date: Mon, 23 Oct 2023 03:02:21 +0200 Subject: [PATCH] More readable os detection --- IoTuring/Entity/Deployments/Lock/Lock.py | 6 +-- IoTuring/Entity/Deployments/Notify/Notify.py | 4 +- IoTuring/Entity/Deployments/Power/Power.py | 16 +++--- IoTuring/Entity/Deployments/Volume/Volume.py | 4 +- IoTuring/MyApp/App.py | 4 -- .../SystemConsts/OperatingSystemDetection.py | 49 ++++++++++--------- IoTuring/MyApp/SystemConsts/consts.py | 3 -- 7 files changed, 41 insertions(+), 45 deletions(-) delete mode 100644 IoTuring/MyApp/SystemConsts/consts.py diff --git a/IoTuring/Entity/Deployments/Lock/Lock.py b/IoTuring/Entity/Deployments/Lock/Lock.py index b9cd5ee8b..bc170c32e 100644 --- a/IoTuring/Entity/Deployments/Lock/Lock.py +++ b/IoTuring/Entity/Deployments/Lock/Lock.py @@ -6,13 +6,13 @@ KEY_LOCK = 'lock' commands = { - 'Windows': { + OsD.WINDOWS: { 'base': 'rundll32.exe user32.dll,LockWorkStation' }, - 'macOS': { + OsD.MACOS: { 'base': 'pmset displaysleepnow' }, - 'Linux': { + OsD.LINUX: { 'gnome': 'gnome-screensaver-command -l', 'cinnamon': 'cinnamon-screensaver-command -a', 'i3': 'i3lock', diff --git a/IoTuring/Entity/Deployments/Notify/Notify.py b/IoTuring/Entity/Deployments/Notify/Notify.py index edb1f7ded..5c09098ad 100644 --- a/IoTuring/Entity/Deployments/Notify/Notify.py +++ b/IoTuring/Entity/Deployments/Notify/Notify.py @@ -14,8 +14,8 @@ supports_win = False commands = { - OsD.OS_FIXED_VALUE_LINUX: 'notify-send "{}" "{}" --icon="ICON_PATH"', - OsD.OS_FIXED_VALUE_MACOS: 'osascript -e \'display notification "{}" with title "{}"\'' + OsD.LINUX: 'notify-send "{}" "{}" --icon="ICON_PATH"', + OsD.MACOS: 'osascript -e \'display notification "{}" with title "{}"\'' } diff --git a/IoTuring/Entity/Deployments/Power/Power.py b/IoTuring/Entity/Deployments/Power/Power.py index 58c0391e2..2c2f5ab15 100644 --- a/IoTuring/Entity/Deployments/Power/Power.py +++ b/IoTuring/Entity/Deployments/Power/Power.py @@ -9,21 +9,21 @@ KEY_SLEEP = 'sleep' commands_shutdown = { - 'Windows': 'shutdown /s /t 0', - 'macOS': 'sudo shutdown -h now', - 'Linux': 'poweroff' + OsD.WINDOWS: 'shutdown /s /t 0', + OsD.MACOS: 'sudo shutdown -h now', + OsD.LINUX: 'poweroff' } commands_reboot = { - 'Windows': 'shutdown /r', - 'macOS': 'sudo reboot', - 'Linux': 'reboot' + OsD.WINDOWS: 'shutdown /r', + OsD.MACOS: 'sudo reboot', + OsD.LINUX: 'reboot' } commands_sleep = { - 'Windows': 'rundll32.exe powrprof.dll,SetSuspendState 0,1,0', - 'Linux': 'systemctl suspend', + OsD.WINDOWS: 'rundll32.exe powrprof.dll,SetSuspendState 0,1,0', + OsD.LINUX: 'systemctl suspend', 'Linux_X11': 'xset dpms force standby', } diff --git a/IoTuring/Entity/Deployments/Volume/Volume.py b/IoTuring/Entity/Deployments/Volume/Volume.py index 5d14a29a8..8d5df3496 100644 --- a/IoTuring/Entity/Deployments/Volume/Volume.py +++ b/IoTuring/Entity/Deployments/Volume/Volume.py @@ -16,8 +16,8 @@ value_type=ValueFormatterOptions.TYPE_PERCENTAGE, decimals=0) commands = { - OsD.OS_FIXED_VALUE_LINUX: 'pactl set-sink-volume @DEFAULT_SINK@ {}%', - OsD.OS_FIXED_VALUE_MACOS: 'osascript -e "set volume output volume {}"' + OsD.LINUX: 'pactl set-sink-volume @DEFAULT_SINK@ {}%', + OsD.MACOS: 'osascript -e "set volume output volume {}"' } diff --git a/IoTuring/MyApp/App.py b/IoTuring/MyApp/App.py index 743de742b..102dc944f 100644 --- a/IoTuring/MyApp/App.py +++ b/IoTuring/MyApp/App.py @@ -1,8 +1,4 @@ -import inspect -from IoTuring.Logger.Logger import Logger from importlib_metadata import metadata -import os - class App(): METADATA = metadata('IoTuring') diff --git a/IoTuring/MyApp/SystemConsts/OperatingSystemDetection.py b/IoTuring/MyApp/SystemConsts/OperatingSystemDetection.py index 7dc499060..232e2c373 100644 --- a/IoTuring/MyApp/SystemConsts/OperatingSystemDetection.py +++ b/IoTuring/MyApp/SystemConsts/OperatingSystemDetection.py @@ -2,42 +2,45 @@ import os import psutil import shutil -from IoTuring.MyApp.SystemConsts import consts class OperatingSystemDetection(): OS_NAME = platform.system() - from .consts import OS_FIXED_VALUE_LINUX, OS_FIXED_VALUE_MACOS, OS_FIXED_VALUE_WINDOWS + # Fixed list: + MACOS = "macOS" + WINDOWS = "Windows" + LINUX = "Linux" - @staticmethod - def GetOs() -> str: - if OperatingSystemDetection.IsMacos(): - return OperatingSystemDetection.OS_FIXED_VALUE_MACOS - elif OperatingSystemDetection.IsLinux(): - return OperatingSystemDetection.OS_FIXED_VALUE_LINUX - elif OperatingSystemDetection.IsWindows(): - return OperatingSystemDetection.OS_FIXED_VALUE_WINDOWS + + @classmethod + def GetOs(cls) -> str: + if cls.IsMacos(): + return cls.MACOS + elif cls.IsLinux(): + return cls.LINUX + elif cls.IsWindows(): + return cls.WINDOWS else: raise Exception( - f"Operating system not in the fixed list. Please open a Git issue and warn about this: OS = {OperatingSystemDetection.OS_NAME}") + f"Operating system not in the fixed list. Please open a Git issue and warn about this: OS = {cls.OS_NAME}") - @staticmethod - def IsLinux() -> bool: - return bool(OperatingSystemDetection.OS_NAME == 'Linux') + @classmethod + def IsLinux(cls) -> bool: + return bool(cls.OS_NAME == cls.LINUX) - @staticmethod - def IsWindows() -> bool: - return bool(OperatingSystemDetection.OS_NAME == 'Windows') + @classmethod + def IsWindows(cls) -> bool: + return bool(cls.OS_NAME == cls.WINDOWS) - @staticmethod - def IsMacos() -> bool: - return bool(OperatingSystemDetection.OS_NAME == 'Darwin') # It's macOS + @classmethod + def IsMacos(cls) -> bool: + return bool(cls.OS_NAME == cls.MACOS) - @staticmethod - def GetEnv(envvar) -> str: + @classmethod + def GetEnv(cls, envvar) -> str: """Get envvar, also from different tty on linux""" env_value = "" - if OperatingSystemDetection.IsLinux(): + if cls.IsLinux(): e = os.environ.get(envvar) if not e: try: diff --git a/IoTuring/MyApp/SystemConsts/consts.py b/IoTuring/MyApp/SystemConsts/consts.py deleted file mode 100644 index a57448ca1..000000000 --- a/IoTuring/MyApp/SystemConsts/consts.py +++ /dev/null @@ -1,3 +0,0 @@ -OS_FIXED_VALUE_MACOS = "macOS" -OS_FIXED_VALUE_WINDOWS = "Windows" -OS_FIXED_VALUE_LINUX = "Linux"