Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _is_windows(self) -> bool:
return self.platform == Platform.WINDOWS
def _is_linux(self) -> bool:
return self.platform == Platform.LINUX
def normalize_platform(name: PlatformType) -> Platform:
if isinstance(name, Platform):
return name
if name:
name = name.strip().lower()
if name == "posix":
return Platform.POSIX
if name == "auto":
name = platform.system().lower()
if name in ["linux"]:
return Platform.LINUX
if name and name.startswith("win"):
return Platform.WINDOWS
if name in ["mac", "macos", "darwin"]:
return Platform.MACOS
return Platform.UNIVERSAL
def normalize_platform(name: PlatformType) -> Platform:
if isinstance(name, Platform):
return name
if name:
name = name.strip().lower()
if name == "posix":
return Platform.POSIX
if name == "auto":
name = platform.system().lower()
if name in ["linux"]:
return Platform.LINUX
if name and name.startswith("win"):
return Platform.WINDOWS
if name in ["mac", "macos", "darwin"]:
return Platform.MACOS
return Platform.UNIVERSAL
name = name.strip().lower()
if name == "posix":
return Platform.POSIX
if name == "auto":
name = platform.system().lower()
if name in ["linux"]:
return Platform.LINUX
if name and name.startswith("win"):
return Platform.WINDOWS
if name in ["mac", "macos", "darwin"]:
return Platform.MACOS
return Platform.UNIVERSAL
def __validate_win_filepath(self, unicode_filepath: str) -> None:
match = _RE_INVALID_WIN_PATH.findall(unicode_filepath)
if match:
raise InvalidCharError(
self._ERROR_MSG_TEMPLATE.format(
invalid=findall_to_str(match), value=repr(unicode_filepath)
),
platform=Platform.WINDOWS,
)
_drive, value = self.__split_drive(unicode_filepath)
if value:
match_reserved = self._RE_NTFS_RESERVED.search(value)
if match_reserved:
reserved_name = match_reserved.group()
raise ReservedNameError(
"'{}' is a reserved name".format(reserved_name),
reusable_name=False,
reserved_name=reserved_name,
platform=self.platform,
)
def _get_sanitize_regexp(self) -> Pattern:
if self.platform in [Platform.UNIVERSAL, Platform.WINDOWS]:
return _RE_INVALID_WIN_PATH
return _RE_INVALID_PATH
def _is_macos(self) -> bool:
return self.platform == Platform.MACOS
from pathlib import Path
from typing import Any, List, Optional, Union, cast
@enum.unique
class Platform(enum.Enum):
POSIX = "POSIX"
UNIVERSAL = "universal"
LINUX = "Linux"
WINDOWS = "Windows"
MACOS = "macOS"
PathType = Union[str, Path]
PlatformType = Union[str, Platform, None]
def is_pathlike_obj(value: PathType) -> bool:
return isinstance(value, Path)
def validate_pathtype(text: PathType, error_msg: Optional[str] = None) -> None:
from .error import ErrorReason, ValidationError
if _is_not_null_string(text) or is_pathlike_obj(text):
return
if is_null_string(text):
if not error_msg:
error_msg = "the value must be a not empty"