Skip to content

OSHelpers

psengine.helpers.helpers.OSHelpers

Helpers for OS related functions.

mkdir staticmethod

mkdir(path: str | Path) -> Path

Safely create a directory.

PARAMETER DESCRIPTION
path

Path to directory to create.

TYPE: str | Path

RAISES DESCRIPTION
ValueError

If path is not a string or is empty.

WriteFileError

If the directory is not writable.

RETURNS DESCRIPTION
Path

Path to the directory created.

Source code in psengine/helpers/helpers.py
@staticmethod
def mkdir(
    path: Annotated[str | Path, Doc('Path to directory to create.')],
) -> Annotated[Path, Doc('Path to the directory created.')]:
    """Safely create a directory.

    Raises:
        ValueError: If path is not a string or is empty.
        WriteFileError: If the directory is not writable.
    """
    if path == '':
        raise ValueError('path cannot be empty')

    path = Path(path)
    LOG.debug(f'Creating directory: {path.as_posix()}')
    if not path.is_absolute():
        path = Path(sys.path[0]) / path
    if path.is_dir() and os.access(path, os.W_OK):
        return path
    try:
        path.mkdir(parents=True, exist_ok=True)
    except PermissionError as err:
        raise WriteFileError(f'Directory {path} is not writable') from err
    # In case it already exists, check if it is writable
    if not os.access(path, os.W_OK):
        raise WriteFileError(f'Directory {path} is not writable')
    return path

os_platform staticmethod

os_platform() -> str | None

Get the OS platform information, for example: macOS-13.0-x86_64-i386-64bit.

RETURNS DESCRIPTION
str | None

OS platform info string, or None if unavailable.

Source code in psengine/helpers/helpers.py
@staticmethod
def os_platform() -> Annotated[
    str | None, Doc('OS platform info string, or None if unavailable.')
]:
    """Get the OS platform information, for example: `macOS-13.0-x86_64-i386-64bit`."""
    return platform.platform(aliased=True, terse=False) or None