forked from AG_QGIS/Plugin_SN_Basis
116 lines
2.9 KiB
Python
116 lines
2.9 KiB
Python
"""
|
||
variable_wrapper.py – QGIS-Variablen-Abstraktion
|
||
"""
|
||
|
||
from typing import Any
|
||
|
||
from sn_basis.functions.qgiscore_wrapper import QgsProject
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# Versuch: QgsExpressionContextUtils importieren
|
||
# ---------------------------------------------------------
|
||
|
||
try:
|
||
from qgis.core import QgsExpressionContextUtils
|
||
|
||
_HAS_QGIS_VARIABLES = True
|
||
|
||
# ---------------------------------------------------------
|
||
# Mock-Modus
|
||
# ---------------------------------------------------------
|
||
|
||
except Exception:
|
||
_HAS_QGIS_VARIABLES = False
|
||
|
||
class _MockVariableStore:
|
||
global_vars: dict[str, str] = {}
|
||
project_vars: dict[str, str] = {}
|
||
|
||
class QgsExpressionContextUtils:
|
||
@staticmethod
|
||
def setGlobalVariable(name: str, value: str) -> None:
|
||
_MockVariableStore.global_vars[name] = value
|
||
|
||
@staticmethod
|
||
def globalScope():
|
||
class _Scope:
|
||
def variable(self, name: str) -> str:
|
||
return _MockVariableStore.global_vars.get(name, "")
|
||
|
||
return _Scope()
|
||
|
||
@staticmethod
|
||
def setProjectVariable(project: Any, name: str, value: str) -> None:
|
||
_MockVariableStore.project_vars[name] = value
|
||
|
||
@staticmethod
|
||
def projectScope(project: Any):
|
||
class _Scope:
|
||
def variable(self, name: str) -> str:
|
||
return _MockVariableStore.project_vars.get(name, "")
|
||
|
||
return _Scope()
|
||
|
||
|
||
# ---------------------------------------------------------
|
||
# Öffentliche API
|
||
# ---------------------------------------------------------
|
||
|
||
def get_variable(key: str, scope: str = "project") -> str:
|
||
"""
|
||
Liest eine QGIS-Variable.
|
||
|
||
:param key: Variablenname ohne Prefix
|
||
:param scope: 'project' oder 'global'
|
||
"""
|
||
var_name = f"sn_{key}"
|
||
|
||
if scope == "project":
|
||
project = QgsProject.instance()
|
||
return (
|
||
QgsExpressionContextUtils
|
||
.projectScope(project)
|
||
.variable(var_name)
|
||
or ""
|
||
)
|
||
|
||
if scope == "global":
|
||
return (
|
||
QgsExpressionContextUtils
|
||
.globalScope()
|
||
.variable(var_name)
|
||
or ""
|
||
)
|
||
|
||
raise ValueError("Scope muss 'project' oder 'global' sein.")
|
||
|
||
|
||
def set_variable(key: str, value: str, scope: str = "project") -> None:
|
||
"""
|
||
Setzt eine QGIS-Variable.
|
||
|
||
:param key: Variablenname ohne Prefix
|
||
:param value: Wert
|
||
:param scope: 'project' oder 'global'
|
||
"""
|
||
var_name = f"sn_{key}"
|
||
|
||
if scope == "project":
|
||
project = QgsProject.instance()
|
||
QgsExpressionContextUtils.setProjectVariable(
|
||
project,
|
||
var_name,
|
||
value,
|
||
)
|
||
return
|
||
|
||
if scope == "global":
|
||
QgsExpressionContextUtils.setGlobalVariable(
|
||
var_name,
|
||
value,
|
||
)
|
||
return
|
||
|
||
raise ValueError("Scope muss 'project' oder 'global' sein.")
|