Skip to content

Commit

Permalink
More readable os detection
Browse files Browse the repository at this point in the history
  • Loading branch information
infeeeee committed Oct 23, 2023
1 parent 3df7a0c commit e84389a
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 45 deletions.
6 changes: 3 additions & 3 deletions IoTuring/Entity/Deployments/Lock/Lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions IoTuring/Entity/Deployments/Notify/Notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "{}"\''
}


Expand Down
16 changes: 8 additions & 8 deletions IoTuring/Entity/Deployments/Power/Power.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}

Expand Down
4 changes: 2 additions & 2 deletions IoTuring/Entity/Deployments/Volume/Volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}"'
}


Expand Down
4 changes: 0 additions & 4 deletions IoTuring/MyApp/App.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import inspect
from IoTuring.Logger.Logger import Logger
from importlib_metadata import metadata
import os


class App():
METADATA = metadata('IoTuring')
Expand Down
49 changes: 26 additions & 23 deletions IoTuring/MyApp/SystemConsts/OperatingSystemDetection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 0 additions & 3 deletions IoTuring/MyApp/SystemConsts/consts.py

This file was deleted.

0 comments on commit e84389a

Please sign in to comment.