diff --git a/IoTuring/Configurator/ConfiguratorIO.py b/IoTuring/Configurator/ConfiguratorIO.py index c2becf3d0..acba8faae 100644 --- a/IoTuring/Configurator/ConfiguratorIO.py +++ b/IoTuring/Configurator/ConfiguratorIO.py @@ -8,12 +8,9 @@ from IoTuring.MyApp.SystemConsts import OperatingSystemDetection as OsD # macOS dep (in PyObjC) -try: - from AppKit import * # type:ignore - from Foundation import * # type:ignore - macos_support = True -except: - macos_support = False +if OsD.IsMacos(): + from AppKit import * + from Foundation import * CONFIG_PATH_ENV_VAR = "IOTURING_CONFIG_DIR" @@ -80,7 +77,7 @@ def getFolderPath(self) -> Path: if envvarPath and len(envvarPath) > 0: folderPath = Path(envvarPath) else: - if OsD.IsMacos() and macos_support: + if OsD.IsMacos() and 'AppKit' in sys.modules and 'Foundation' in sys.modules: folderPath = self.macOSFolderPath().joinpath(self.directoryName) elif OsD.IsWindows(): folderPath = self.windowsFolderPath().joinpath(self.directoryName) diff --git a/IoTuring/Entity/Deployments/ActiveWindow/ActiveWindow.py b/IoTuring/Entity/Deployments/ActiveWindow/ActiveWindow.py index 4f4366433..356808df9 100644 --- a/IoTuring/Entity/Deployments/ActiveWindow/ActiveWindow.py +++ b/IoTuring/Entity/Deployments/ActiveWindow/ActiveWindow.py @@ -4,20 +4,16 @@ from IoTuring.MyApp.SystemConsts import OperatingSystemDetection as OsD from IoTuring.MyApp.SystemConsts import DesktopEnvironmentDetection as De +import sys # Windows dep -try: - from win32gui import GetWindowText, GetForegroundWindow # type: ignore - windows_support = True -except BaseException: - windows_support = False +if OsD.IsWindows(): + from win32gui import GetWindowText, GetForegroundWindow # macOS dep -try: - from AppKit import NSWorkspace # type: ignore - macos_support = True -except BaseException: - macos_support = False +if OsD.IsMacos(): + from AppKit import NSWorkspace + KEY = 'active_window' @@ -85,11 +81,12 @@ def CheckSystemSupport(cls): elif not OsD.CommandExists("xprop"): raise Exception("No xprop command found!") - elif OsD.IsWindows() or OsD.IsMacos(): - - if (OsD.IsWindows() and not windows_support) or\ - (OsD.IsMacos() and not macos_support): - raise Exception("Unsatisfied dependencies for this entity") + elif OsD.IsWindows(): + if 'win32gui' not in sys.modules: + raise Exception("Unsatisfied dependencies (win32gui) for this entity") + elif OsD.IsMacos(): + if 'AppKit' not in sys.modules: + raise Exception("Unsatisfied dependencies (AppKit) for this entity") else: raise cls.UnsupportedOsException() diff --git a/IoTuring/Entity/Deployments/Notify/Notify.py b/IoTuring/Entity/Deployments/Notify/Notify.py index 7b3105088..b7ff4d53a 100644 --- a/IoTuring/Entity/Deployments/Notify/Notify.py +++ b/IoTuring/Entity/Deployments/Notify/Notify.py @@ -6,12 +6,11 @@ import os import json +import sys -supports_win = True -try: - import tinyWinToast.tinyWinToast as twt # type: ignore -except: - supports_win = False + +if OsD.IsWindows(): + import tinyWinToast.tinyWinToast as twt commands = { OsD.LINUX: 'notify-send "{}" "{}" --icon="{}"', # title, message, icon path @@ -137,7 +136,7 @@ def ConfigurationPreset(cls) -> MenuPreset: @classmethod def CheckSystemSupport(cls): if OsD.IsWindows(): - if not supports_win: + if 'tinyWinToast' not in sys.modules: raise Exception( 'Notify not available, have you installed \'tinyWinToast\' on pip ?') diff --git a/IoTuring/Settings/Deployments/LogSettings/LogSettings.py b/IoTuring/Settings/Deployments/LogSettings/LogSettings.py index 72ddcaf7a..4b0405f64 100644 --- a/IoTuring/Settings/Deployments/LogSettings/LogSettings.py +++ b/IoTuring/Settings/Deployments/LogSettings/LogSettings.py @@ -9,14 +9,12 @@ from IoTuring.Logger.LogLevel import LogLevel from IoTuring.Logger import consts +import sys # macOS dep (in PyObjC) -try: - from AppKit import * # type:ignore - from Foundation import * # type:ignore - macos_support = True -except: - macos_support = False +if OsD.IsMacos(): + from AppKit import * + from Foundation import * CONFIG_KEY_CONSOLE_LOG_LEVEL = "console_log_level" @@ -91,7 +89,7 @@ def GetDefaultLogPath() -> str: "Logger").joinpath(consts.LOGS_FOLDER) base_path = None - if OsD.IsMacos() and macos_support: + if OsD.IsMacos() and 'AppKit' in sys.modules and 'Foundation' in sys.modules: base_path = \ Path(NSSearchPathForDirectoriesInDomains( # type: ignore NSLibraryDirectory, # type: ignore