Skip to content

crossplatform

crossplatform

Contain platform specific implementations of common actions like opening programs.

System

Bases: Enum

Enumeration of system names created to avoid typos.

Source code in cssfinder/crossplatform.py
class System(Enum):
    """Enumeration of system names created to avoid typos."""

    Win32 = "Windows"
    MacOS = "Darwin"
    Linux = "Linux"
    Other = "Other"

    @classmethod
    def _missing_(cls, value: Any) -> System:  # noqa: ARG003
        return cls.Other

Priority

Bases: Enum

Process priority constants.

Source code in cssfinder/crossplatform.py
class Priority(Enum):  # type: ignore[no-redef]
    """Process priority constants."""

    IDLE = 19
    BELOW_NORMAL = 10
    NORMAL = 0
    ABOVE_NORMAL = -7
    HIGH = -15
    REALTIME = -20

IoPriority

Bases: Enum

Process I/O niceness.

Not available on MacOS.

Source code in cssfinder/crossplatform.py
class IoPriority(Enum):  # type: ignore[no-redef]
    """Process I/O niceness.

    Not available on MacOS.

    """

    HIGH = 0
    NORMAL = 1
    LOW = 2
    NONE = 3

open_file_explorer

open_file_explorer(path: Path) -> None

Open file explorer application specific to platform.

Source code in cssfinder/crossplatform.py
def open_file_explorer(path: Path) -> None:
    """Open file explorer application specific to platform."""
    subprocess.Popen(["xdg-open", path])

open_terminal

open_terminal(path: Path) -> None

Open terminal application specific to platform.

Source code in cssfinder/crossplatform.py
def open_terminal(path: Path) -> None:
    """Open terminal application specific to platform."""
    terminal = os.environ.get("TERMINAL", "x-terminal-emulator")
    subprocess.Popen([terminal, "--working-directory", str(path)])

set_priority

set_priority(
    pid: int, priority: Priority, io_priority: IoPriority
) -> None

Set process priority. Implemented for win32, linux and macOS, noop elsewhere.

Can raise psutil.AccessDenied. io_priority is noop on macOS.

Source code in cssfinder/crossplatform.py
def set_priority(pid: int, priority: Priority, io_priority: IoPriority) -> None:
    """Set process priority. Implemented for win32, linux and macOS, noop elsewhere.

    Can raise psutil.AccessDenied. io_priority is noop on macOS.

    """
    process = psutil.Process(pid)
    process.nice(priority.value)

    if IS_MAC:
        return

    if IS_LINUX:
        if io_priority == IoPriority.HIGH:
            process.ionice(io_priority.value, value=0)
            return

        if io_priority == IoPriority.NORMAL:
            process.ionice(io_priority.value, value=0)

    if IS_WIN32:
        process.ionice(io_priority.value)