forked from AG_QGIS/Plugin_SN_Basis
140 lines
3.3 KiB
Python
140 lines
3.3 KiB
Python
|
|
"""
|
|||
|
|
sn_basis/functions/qgiscore_wrapper.py – zentrale QGIS-Core-Abstraktion
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
from typing import Type, Any
|
|||
|
|
|
|||
|
|
from sn_basis.functions.qt_wrapper import (
|
|||
|
|
QUrl,
|
|||
|
|
QEventLoop,
|
|||
|
|
QNetworkRequest,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# ---------------------------------------------------------
|
|||
|
|
# QGIS-Symbole (werden dynamisch gesetzt)
|
|||
|
|
# ---------------------------------------------------------
|
|||
|
|
|
|||
|
|
QgsProject: Type[Any]
|
|||
|
|
QgsVectorLayer: Type[Any]
|
|||
|
|
QgsNetworkAccessManager: Type[Any]
|
|||
|
|
Qgis: Type[Any]
|
|||
|
|
|
|||
|
|
QGIS_AVAILABLE = False
|
|||
|
|
|
|||
|
|
# ---------------------------------------------------------
|
|||
|
|
# Versuch: QGIS-Core importieren
|
|||
|
|
# ---------------------------------------------------------
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
from qgis.core import (
|
|||
|
|
QgsProject as _QgsProject,
|
|||
|
|
QgsVectorLayer as _QgsVectorLayer,
|
|||
|
|
QgsNetworkAccessManager as _QgsNetworkAccessManager,
|
|||
|
|
Qgis as _Qgis,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
QgsProject = _QgsProject
|
|||
|
|
QgsVectorLayer = _QgsVectorLayer
|
|||
|
|
QgsNetworkAccessManager = _QgsNetworkAccessManager
|
|||
|
|
Qgis = _Qgis
|
|||
|
|
|
|||
|
|
QGIS_AVAILABLE = True
|
|||
|
|
|
|||
|
|
# ---------------------------------------------------------
|
|||
|
|
# Mock-Modus
|
|||
|
|
# ---------------------------------------------------------
|
|||
|
|
|
|||
|
|
except Exception:
|
|||
|
|
QGIS_AVAILABLE = False
|
|||
|
|
|
|||
|
|
class _MockQgsProject:
|
|||
|
|
def __init__(self):
|
|||
|
|
self._variables = {}
|
|||
|
|
|
|||
|
|
@staticmethod
|
|||
|
|
def instance() -> "_MockQgsProject":
|
|||
|
|
return _MockQgsProject()
|
|||
|
|
|
|||
|
|
def read(self) -> bool:
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
QgsProject = _MockQgsProject
|
|||
|
|
|
|||
|
|
class _MockQgsVectorLayer:
|
|||
|
|
def __init__(self, *args, **kwargs):
|
|||
|
|
self._valid = True
|
|||
|
|
|
|||
|
|
def isValid(self) -> bool:
|
|||
|
|
return self._valid
|
|||
|
|
|
|||
|
|
def loadNamedStyle(self, path: str):
|
|||
|
|
return True, ""
|
|||
|
|
|
|||
|
|
def triggerRepaint(self) -> None:
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
QgsVectorLayer = _MockQgsVectorLayer
|
|||
|
|
|
|||
|
|
class _MockQgsNetworkAccessManager:
|
|||
|
|
@staticmethod
|
|||
|
|
def instance():
|
|||
|
|
return _MockQgsNetworkAccessManager()
|
|||
|
|
|
|||
|
|
def head(self, request: Any):
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
QgsNetworkAccessManager = _MockQgsNetworkAccessManager
|
|||
|
|
|
|||
|
|
class _MockQgis:
|
|||
|
|
class MessageLevel:
|
|||
|
|
Success = 0
|
|||
|
|
Info = 1
|
|||
|
|
Warning = 2
|
|||
|
|
Critical = 3
|
|||
|
|
|
|||
|
|
Qgis = _MockQgis
|
|||
|
|
|
|||
|
|
|
|||
|
|
# ---------------------------------------------------------
|
|||
|
|
# Netzwerk
|
|||
|
|
# ---------------------------------------------------------
|
|||
|
|
|
|||
|
|
class NetworkReply:
|
|||
|
|
"""
|
|||
|
|
Minimaler Wrapper für Netzwerkantworten.
|
|||
|
|
"""
|
|||
|
|
def __init__(self, error: int):
|
|||
|
|
self.error = error
|
|||
|
|
|
|||
|
|
|
|||
|
|
def network_head(url: str) -> NetworkReply | None:
|
|||
|
|
"""
|
|||
|
|
Führt einen HTTP-HEAD-Request aus.
|
|||
|
|
|
|||
|
|
Rückgabe:
|
|||
|
|
- NetworkReply(error=0) → erreichbar
|
|||
|
|
- NetworkReply(error!=0) → nicht erreichbar
|
|||
|
|
- None → Netzwerk nicht verfügbar / Fehler beim Request
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
if not QGIS_AVAILABLE:
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
if QUrl is None or QNetworkRequest is None:
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
manager = QgsNetworkAccessManager.instance()
|
|||
|
|
request = QNetworkRequest(QUrl(url))
|
|||
|
|
reply = manager.head(request)
|
|||
|
|
|
|||
|
|
# synchron warten (kurz)
|
|||
|
|
if QEventLoop is not None:
|
|||
|
|
loop = QEventLoop()
|
|||
|
|
reply.finished.connect(loop.quit)
|
|||
|
|
loop.exec()
|
|||
|
|
|
|||
|
|
return NetworkReply(error=reply.error())
|
|||
|
|
except Exception:
|
|||
|
|
return None
|