Files
Plugin_SN_Basis/modules/linkpruefer.py

135 lines
3.8 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.
"""
sn_basis/modules/linkpruefer.py Prüfung von URLs und lokalen Links.
Verwendet Wrapper und gibt pruef_ergebnis an den Pruefmanager zurück.
"""
from pathlib import Path
from sn_basis.functions import (
file_exists,
join_path,
network_head,
)
from sn_basis.modules.pruef_ergebnis import pruef_ergebnis, PruefAktion
class Linkpruefer:
"""
Prüft URLs und lokale Pfade.
Die eigentliche Nutzerinteraktion übernimmt der Pruefmanager.
"""
def __init__(self, basis_pfad: str | None = None):
"""
basis_pfad: optionaler Basisordner für relative Pfade.
"""
self.basis = basis_pfad
# ---------------------------------------------------------
# Hilfsfunktionen
# ---------------------------------------------------------
def _pfad(self, relativer_pfad: str) -> Path:
"""
Erzeugt einen OSunabhängigen Pfad relativ zum Basisverzeichnis.
"""
if not self.basis:
return Path(relativer_pfad)
return join_path(self.basis, relativer_pfad)
def _ist_url(self, text: str) -> bool:
"""
Einfache URL-Erkennung.
"""
return text.startswith("http://") or text.startswith("https://")
# ---------------------------------------------------------
# Hauptfunktion
# ---------------------------------------------------------
def pruefe(self, eingabe: str) -> pruef_ergebnis:
"""
Prüft einen Link (URL oder lokalen Pfad).
Rückgabe: pruef_ergebnis
"""
if not eingabe:
return pruef_ergebnis(
ok=False,
meldung="Es wurde kein Link angegeben.",
aktion="leer",
kontext=None,
)
# -----------------------------------------------------
# 1. Fall: URL
# -----------------------------------------------------
if self._ist_url(eingabe):
return self._pruefe_url(eingabe)
# -----------------------------------------------------
# 2. Fall: lokaler Pfad
# -----------------------------------------------------
return self._pruefe_dateipfad(eingabe)
# ---------------------------------------------------------
# URLPrüfung
# ---------------------------------------------------------
def _pruefe_url(self, url: str) -> pruef_ergebnis:
"""
Prüft eine URL über einen HEADRequest.
"""
reply = network_head(url)
if reply is None:
return pruef_ergebnis(
ok=False,
meldung=f"Die URL '{url}' konnte nicht geprüft werden.",
aktion="netzwerkfehler",
kontext=url,
)
if reply.error != 0:
return pruef_ergebnis(
ok=False,
meldung=f"Die URL '{url}' ist nicht erreichbar.",
aktion="url_nicht_erreichbar",
kontext=url,
)
return pruef_ergebnis(
ok=True,
meldung="URL ist erreichbar.",
aktion="ok",
kontext=url,
)
# ---------------------------------------------------------
# Lokale Datei/Pfadprüfung
# ---------------------------------------------------------
def _pruefe_dateipfad(self, eingabe: str) -> pruef_ergebnis:
"""
Prüft einen lokalen Pfad.
"""
pfad = self._pfad(eingabe)
if not file_exists(pfad):
return pruef_ergebnis(
ok=False,
meldung=f"Der Pfad '{eingabe}' wurde nicht gefunden.",
aktion="pfad_nicht_gefunden",
kontext=pfad,
)
return pruef_ergebnis(
ok=True,
meldung="Dateipfad ist gültig.",
aktion="ok",
kontext=pfad,
)