Skip to content

Commit

Permalink
Fix regression about plugins with Py-QGIS-Server
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed Jan 25, 2023
1 parent 978e1a3 commit c4977bd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

* Fix regression from the previous version about Py-QGIS-Server

## 1.3.0 - 2023-01-25

* Return the list of fonts installed on the server. It's useful for QGIS layouts.
Expand Down
73 changes: 46 additions & 27 deletions lizmap_server/server_info_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,64 @@
from lizmap_server.exception import ServiceError
from lizmap_server.tools import check_environment_variable, to_bool

LOGGER = logging.getLogger('Lizmap')
# FCGI or others
from qgis.utils import pluginMetadata, server_active_plugins

try:
# Py-QGIS-Server
# noinspection PyUnresolvedReferences
from pyqgisserver.plugins import plugin_list, plugin_metadata
if 'lizmap_server' not in plugin_list():
# Lizmap server is currently executed by the server but the list of plugin itself returned by Py-QGIS-Server
# does not contain 'lizmap_server'.
# Therefore, it means the administrator has installed Py-QGIS-Server, but he is not using it.
# We fall back on native QGIS server API.
# https://github.com/3liz/lizmap-web-client/issues/3437
raise ImportError

IS_PY_QGIS_SERVER = True

PY_QGIS_SERVER_INSTALLED = True
except ImportError:
# FCGI and others
from qgis.utils import pluginMetadata, server_active_plugins
IS_PY_QGIS_SERVER = False

def plugin_list() -> list:
""" To match Py-QGIS-Server API."""
return server_active_plugins
PY_QGIS_SERVER_INSTALLED = False

with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
from osgeo import gdal

LOGGER = logging.getLogger('Lizmap')


def is_py_qgis_server_used() -> bool:
""" Check if the plugin lizmap_server is found in Py-QGIS-Server plugins. """
if not PY_QGIS_SERVER_INSTALLED:
return False

def plugin_metadata_key(name: str, key: str, ) -> str:
if 'lizmap_server' in list(plugin_list()):
return True

# Py-QGIS-Server is installed
# Lizmap server is currently executed by the server but the list of plugin itself returned by Py-QGIS-Server
# does not contain 'lizmap_server'.
# Therefore, it means the administrator has installed Py-QGIS-Server, but he is not using it.
# We fall back on native QGIS server API.
# https://github.com/3liz/lizmap-web-client/issues/3437
return False


def plugins_installed(py_qgis_server: bool) -> list:
""" List of plugins according to the server FCGI or Py-QGIS-Server. """
if py_qgis_server:
# From Py-QGIS-Server API
return list(plugin_list())

# From QGIS native API
return server_active_plugins


def plugin_metadata_key(py_qgis_server: bool, name: str, key: str, ) -> str:
""" Return the version for a given plugin. """
unknown = 'unknown'
# it seems configparser is transforming all keys as lowercase...
if IS_PY_QGIS_SERVER:
if py_qgis_server:
# From Py-QGIS-Server API
metadata = plugin_metadata(name)
value = metadata['general'].get(key, None)
if value:
return value
return metadata['general'].get(key.lower(), unknown)
else:
# From QGIS native API
value = pluginMetadata(name, key)
if value not in ("__error__", ""):
return value
Expand All @@ -77,13 +94,13 @@ def plugin_metadata_key(name: str, key: str, ) -> str:
)


def py_qgis_server_info() -> PyQgisServer:
def py_qgis_server_info(py_qgis_server_used: bool) -> PyQgisServer:
""" Return the Py-QGIS-Server version or an empty string. """
version = 'not used'
build_id = None
commit_id = None
is_stable = False
if not IS_PY_QGIS_SERVER:
if not py_qgis_server_used:
return PyQgisServer(version, build_id, commit_id, is_stable)

# noinspection PyBroadException
Expand Down Expand Up @@ -124,13 +141,16 @@ def handleRequest(self, context):
if not check_environment_variable():
raise ServiceError("Bad request error", "Invalid request", 404)

is_py_qgis_server = is_py_qgis_server_used()
py_qgis_server_metadata = py_qgis_server_info(is_py_qgis_server)

# 'name' is not the folder name in the 'expected_list' variable, it can be different
keys = ('name', 'version', 'commitNumber', 'commitSha1', 'dateTime', 'repository')
plugins = dict()
for plugin in plugin_list():
for plugin in plugins_installed(is_py_qgis_server):
plugins[plugin] = dict()
for key in keys:
plugins[plugin][key] = plugin_metadata_key(plugin, key)
plugins[plugin][key] = plugin_metadata_key(is_py_qgis_server, plugin, key)

expected_list = (
'wfsOutputExtension',
Expand Down Expand Up @@ -166,7 +186,6 @@ def handleRequest(self, context):
except Exception:
tag = ""

py_qgis_server_metadata = py_qgis_server_info()
data = {
'qgis_server': {
'metadata': {
Expand All @@ -175,11 +194,11 @@ def handleRequest(self, context):
'name': qgis_version_split[1], # Hannover
'commit_id': commit_id, # 288d2cacb5 if it's a dev version
'version_int': Qgis.QGIS_VERSION_INT, # 31600
'py_qgis_server': IS_PY_QGIS_SERVER, # bool, # deprecated since 28/10/2022
'py_qgis_server': PY_QGIS_SERVER_INSTALLED, # bool, # deprecated since 28/10/2022
'py_qgis_server_version': py_qgis_server_metadata.version, # str, deprecated since 28/10/2022
},
'py_qgis_server': {
'found': IS_PY_QGIS_SERVER,
'found': is_py_qgis_server,
'version': py_qgis_server_metadata.version,
'build_id': py_qgis_server_metadata.build_id,
'commit_id': py_qgis_server_metadata.commit_id,
Expand Down

0 comments on commit c4977bd

Please sign in to comment.