2025-12-19 14:29:52 +01:00
|
|
|
|
"""
|
|
|
|
|
|
sn_basis/functions/os_wrapper.py – Betriebssystem-Abstraktion
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
import platform
|
|
|
|
|
|
from typing import Union
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
|
# OS-Erkennung
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
_SYSTEM = platform.system().lower()
|
|
|
|
|
|
|
|
|
|
|
|
if _SYSTEM.startswith("win"):
|
|
|
|
|
|
OS_NAME = "windows"
|
|
|
|
|
|
elif _SYSTEM.startswith("darwin"):
|
|
|
|
|
|
OS_NAME = "macos"
|
|
|
|
|
|
else:
|
|
|
|
|
|
OS_NAME = "linux"
|
|
|
|
|
|
|
|
|
|
|
|
IS_WINDOWS = OS_NAME == "windows"
|
|
|
|
|
|
IS_LINUX = OS_NAME == "linux"
|
|
|
|
|
|
IS_MACOS = OS_NAME == "macos"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
|
# OS-Eigenschaften
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
PATH_SEPARATOR = "\\" if IS_WINDOWS else "/"
|
|
|
|
|
|
LINE_SEPARATOR = "\r\n" if IS_WINDOWS else "\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
|
# Pfad-Utilities
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
_PathLike = Union[str, Path]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def normalize_path(path: _PathLike) -> Path:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Normalisiert einen Pfad OS-unabhängig.
|
|
|
|
|
|
"""
|
|
|
|
|
|
try:
|
|
|
|
|
|
return Path(path).expanduser().resolve()
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
return Path(path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_home_dir() -> Path:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Liefert das Home-Verzeichnis des aktuellen Users.
|
|
|
|
|
|
"""
|
|
|
|
|
|
return Path.home()
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-03-12 16:14:02 +01:00
|
|
|
|
def is_absolute_path(path: _PathLike) -> bool:
|
|
|
|
|
|
"""Prüft, ob ein Pfad absolut ist."""
|
|
|
|
|
|
try:
|
|
|
|
|
|
return Path(path).is_absolute()
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def basename(path: _PathLike) -> str:
|
|
|
|
|
|
"""Gibt den finalen Namen des Pfades zurück (Dateiname oder Ordner)."""
|
|
|
|
|
|
try:
|
|
|
|
|
|
return Path(path).name
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-12-19 14:29:52 +01:00
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
|
# Dateisystem-Eigenschaften
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
def is_case_sensitive_fs() -> bool:
|
|
|
|
|
|
"""
|
|
|
|
|
|
Gibt zurück, ob das Dateisystem case-sensitiv ist.
|
|
|
|
|
|
"""
|
|
|
|
|
|
# Windows ist immer case-insensitive
|
|
|
|
|
|
if IS_WINDOWS:
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
# macOS meist case-insensitive, aber nicht garantiert
|
|
|
|
|
|
if IS_MACOS:
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
# Linux praktisch immer case-sensitiv
|
|
|
|
|
|
return True
|
2026-03-12 16:14:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def path_suffix(path: _PathLike) -> str:
|
|
|
|
|
|
"""Gibt die Dateiendung eines Pfades zurück (inklusive Punkt)."""
|
|
|
|
|
|
try:
|
|
|
|
|
|
return Path(path).suffix
|
|
|
|
|
|
except Exception:
|
|
|
|
|
|
return ""
|