-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change how platform support is defined
- Loading branch information
Showing
19 changed files
with
289 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,103 @@ | ||
from __future__ import annotations | ||
|
||
import platform | ||
import typing | ||
import warnings | ||
|
||
from .constants import PlatformName | ||
from .constants import IdentifiedPlatformType, PlatformType | ||
|
||
if typing.TYPE_CHECKING: | ||
from typing import Callable, Tuple | ||
|
||
PlatformFunc = Callable[[IdentifiedPlatformType], bool] | ||
|
||
|
||
def get_current_platform() -> PlatformName: | ||
def get_current_platform() -> IdentifiedPlatformType: | ||
# Ref: https://docs.python.org/3/library/platform.html#platform.system | ||
system = platform.system() | ||
if system == "Windows": | ||
return PlatformName.WINDOWS | ||
return IdentifiedPlatformType.WINDOWS | ||
elif system == "Darwin": | ||
return PlatformName.MACOS | ||
return IdentifiedPlatformType.MACOS | ||
elif system == "Linux": | ||
return PlatformName.LINUX | ||
return IdentifiedPlatformType.LINUX | ||
elif system == "FreeBSD": | ||
return IdentifiedPlatformType.FREEBSD | ||
|
||
# LATER: This should be improved in https://github.com/fohrloop/wakepy/issues/378 | ||
warnings.warn( | ||
f"Could not detect current platform! platform.system() returned {system}" | ||
) | ||
return PlatformName.OTHER | ||
return IdentifiedPlatformType.UNKNOWN | ||
|
||
|
||
CURRENT_PLATFORM: IdentifiedPlatformType = get_current_platform() | ||
|
||
|
||
def get_platform_supported( | ||
platform: IdentifiedPlatformType, supported_platforms: Tuple[PlatformType, ...] | ||
) -> bool | None: | ||
"""Checks if method is supported by the platform | ||
TODO: Update the docstring. | ||
Parameters | ||
---------- | ||
platform: Method | ||
The method which platform support to check. | ||
supported_platforms: | ||
The platform to check against. | ||
Returns | ||
------- | ||
is_supported: bool | ||
If platform is supported, returns True. If the support is unknown, | ||
returns None, and if the platform is not supported, returns False. | ||
""" | ||
for supported_platform in supported_platforms: | ||
func = PLATFORM_INFO_FUNCS[supported_platform] | ||
if func(platform) is True: | ||
return True | ||
if is_unknown(platform): | ||
return None | ||
return False | ||
|
||
|
||
def is_windows(current_platform: IdentifiedPlatformType) -> bool: | ||
return current_platform == IdentifiedPlatformType.WINDOWS | ||
|
||
|
||
def is_linux(current_platform: IdentifiedPlatformType) -> bool: | ||
return current_platform == IdentifiedPlatformType.LINUX | ||
|
||
|
||
def is_freebsd(current_platform: IdentifiedPlatformType) -> bool: | ||
return current_platform == IdentifiedPlatformType.FREEBSD | ||
|
||
|
||
def is_macos(current_platform: IdentifiedPlatformType) -> bool: | ||
return current_platform == IdentifiedPlatformType.MACOS | ||
|
||
|
||
def is_bsd(current_platform: IdentifiedPlatformType) -> bool: | ||
return is_freebsd(current_platform) | ||
|
||
|
||
def is_unknown(current_platform: IdentifiedPlatformType) -> bool: | ||
return current_platform == IdentifiedPlatformType.UNKNOWN | ||
|
||
|
||
def is_unix_like_foss(current_platform: IdentifiedPlatformType) -> bool: | ||
return is_bsd(current_platform) or is_linux(current_platform) | ||
|
||
|
||
CURRENT_PLATFORM = get_current_platform() | ||
PLATFORM_INFO_FUNCS: dict[PlatformType, PlatformFunc] = { | ||
PlatformType.WINDOWS: is_windows, | ||
PlatformType.LINUX: is_linux, | ||
PlatformType.MACOS: is_macos, | ||
PlatformType.FREEBSD: is_freebsd, | ||
PlatformType.UNKNOWN: is_unknown, | ||
PlatformType.BSD: is_bsd, | ||
PlatformType.ANY: lambda _: True, | ||
PlatformType.UNIX_LIKE_FOSS: is_unix_like_foss, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.