Files
Plugin_SN_Basis/functions/syswrapper.py

186 lines
4.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
snbasis/functions/syswrapper.py zentrale OS-/Dateisystem-Abstraktion
Robust, testfreundlich, mock-fähig.
"""
import os
import tempfile
import pathlib
import sys
# ---------------------------------------------------------
# DateisystemFunktionen
# ---------------------------------------------------------
def file_exists(path: str) -> bool:
"""Prüft, ob eine Datei existiert."""
try:
return os.path.exists(path)
except Exception:
return False
def is_file(path: str) -> bool:
"""Prüft, ob ein Pfad eine Datei ist."""
try:
return os.path.isfile(path)
except Exception:
return False
def is_dir(path: str) -> bool:
"""Prüft, ob ein Pfad ein Verzeichnis ist."""
try:
return os.path.isdir(path)
except Exception:
return False
def join_path(*parts) -> str:
"""Verbindet Pfadbestandteile OSunabhängig."""
try:
return os.path.join(*parts)
except Exception:
# Fallback: naive Verkettung
return "/".join(str(p) for p in parts)
# ---------------------------------------------------------
# Pfad und Systemfunktionen
# ---------------------------------------------------------
def get_temp_dir() -> str:
"""Gibt das temporäre Verzeichnis zurück."""
try:
return tempfile.gettempdir()
except Exception:
return "/tmp"
def get_plugin_root() -> str:
"""
Ermittelt den PluginRootPfad.
Annahme: syswrapper liegt in sn_basis/funktions/
→ also zwei Ebenen hoch.
"""
try:
here = pathlib.Path(__file__).resolve()
return str(here.parent.parent)
except Exception:
# Fallback: aktuelles Arbeitsverzeichnis
return os.getcwd()
# ---------------------------------------------------------
# DateiI/O (optional, aber nützlich)
# ---------------------------------------------------------
def read_file(path: str, mode="r"):
"""Liest eine Datei ein. Gibt None zurück, wenn Fehler auftreten."""
try:
with open(path, mode) as f:
return f.read()
except Exception:
return None
def write_file(path: str, data, mode="w"):
"""Schreibt Daten in eine Datei. Gibt True/False zurück."""
try:
with open(path, mode) as f:
f.write(data)
return True
except Exception:
return False
# ---------------------------------------------------------
# MockModus (optional erweiterbar)
# ---------------------------------------------------------
class FakeFileSystem:
"""
Minimaler MockDateisystemErsatz.
Wird nicht automatisch aktiviert, aber kann in Tests gepatcht werden.
"""
files = {}
@classmethod
def add_file(cls, path, content=""):
cls.files[path] = content
@classmethod
def exists(cls, path):
return path in cls.files
@classmethod
def read(cls, path):
return cls.files.get(path, None)
# ---------------------------------------------------------
# BetriebssystemErkennung
# ---------------------------------------------------------
import platform
def get_os() -> str:
"""
Gibt das Betriebssystem zurück:
- 'windows'
- 'linux'
- 'mac'
"""
system = platform.system().lower()
if "windows" in system:
return "windows"
if "darwin" in system:
return "mac"
if "linux" in system:
return "linux"
return "unknown"
def is_windows() -> bool:
return get_os() == "windows"
def is_linux() -> bool:
return get_os() == "linux"
def is_mac() -> bool:
return get_os() == "mac"
# ---------------------------------------------------------
# PfadNormalisierung
# ---------------------------------------------------------
def normalize_path(path: str) -> str:
"""
Normalisiert Pfade OSunabhängig:
- ersetzt Backslashes durch Slashes
- entfernt doppelte Slashes
- löst relative Pfade auf
"""
try:
p = pathlib.Path(path).resolve()
return str(p)
except Exception:
# Fallback: einfache Normalisierung
return path.replace("\\", "/").replace("//", "/")
def add_to_sys_path(path: str) -> None:
"""
Fügt einen Pfad sicher zum Python-Importpfad hinzu.
"""
try:
if path not in sys.path:
sys.path.insert(0, path)
except Exception:
pass