Skip to content

Commit

Permalink
Fix managment apis
Browse files Browse the repository at this point in the history
    - Follow backport for qgis/QGIS#45439
  • Loading branch information
dmarteau committed Jan 19, 2022
1 parent 2bea038 commit c40bb43
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* Fix api managment:
- Follow backport for https://github.com/qgis/QGIS/issues/45439
- Fix regression from static cache implementation

## 1.7.14 - 2022-01-18

* Define explicit `CACHE_DEFAULT_HANDLER` configuration option
Expand Down
6 changes: 1 addition & 5 deletions pyqgisserver/management/apis/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

from .handler import RequestHandler, register_handlers

from qgis.core import Qgis

LOGGER = logging.getLogger('SRVLOG')


Expand Down Expand Up @@ -49,11 +47,9 @@ def get(self, key: str=None) -> None:
def register( serverIface ):
""" Register plugins api handlers
"""
# XXX See https://github.com/qgis/QGIS/issues/45439
prefix = '/cache' if Qgis.QGIS_VERSION_INT < 32200 else ''
register_handlers(serverIface, "/cache","CacheManagment",
[
(rf'{prefix}/(?P<key>.+)$', CacheCollection),
(r'/(?P<key>.+)$', CacheCollection),
(r'/', CacheCollection),
])

Expand Down
22 changes: 12 additions & 10 deletions pyqgisserver/management/apis/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
from pyqgisserver.plugins import plugin_list, plugin_metadata, failed_plugins
from .handler import RequestHandler, register_handlers

from qgis.core import Qgis

LOGGER = logging.getLogger('SRVLOG')


Expand All @@ -38,22 +36,26 @@ def get(self, name: Optional[str]=None) -> None:
raise HTTPError(404)
self.write({'name': name, 'status': 'loaded', 'metadata': metadata })
else:
def link(name):
return self.public_url(f"/{name}")
def _link(name, status):
return {
'href': self.public_url(f"/{name}"),
'status': status,
'name': name,
'type': 'application/json',
'title': f'Details for plugin {name}',
}
# List all loaded plugins
plugins = [{ 'name': n, 'status': 'loaded', 'link': link(n) } for n in plugin_list()]
plugins.extend([{ 'name' : n, 'status': 'failed', 'link': link(n) } for n in failed_plugins])
self.write({'plugins': plugins })
plugins = [_link(n,'loaded') for n in plugin_list()]
plugins.extend([_link(n,'failed') for n in failed_plugins])
self.write({'links': plugins })


def register( serverIface ):
""" Register plugins api handlers
"""
# XXX See https://github.com/qgis/QGIS/issues/45439
prefix = '/plugins' if Qgis.QGIS_VERSION_INT < 32200 else ''
register_handlers(serverIface, "/plugins","PluginsManagment",
[
(rf'{prefix}/(?P<name>[^\/]+)/?$', PluginCollection),
(r'/(?P<name>[^\/]+)/?$', PluginCollection),
(r'/', PluginCollection),
])

Expand Down
1 change: 0 additions & 1 deletion pyqgisserver/management/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class _ReportHandler(_PoolHandler):
async def get(self) -> Awaitable[None]:
""" Return worker reports
"""
# Broadcast 'restart' to workers
req = self.request
reports = await self._poolserver.get_reports()
for w in reports:
Expand Down
20 changes: 15 additions & 5 deletions pyqgisserver/qgscache/cachemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from collections import namedtuple, OrderedDict
from pathlib import Path
from datetime import datetime
from enum import Enum
from itertools import chain

from ..utils.lru import lrucache
from ..config import confservice
Expand Down Expand Up @@ -63,6 +65,12 @@ def _merge_qs( query1: str, query2: str ) -> str:
return '&'.join('%s=%s' % (k,v[0]) for k,v in params_2.items())


class CacheType(Enum):
LRU = 'lru'
STATIC = 'static'
ALL = 'all'


@componentmanager.register_factory(CACHE_MANAGER_CONTRACTID)
class QgsCacheManager:
""" Handle Qgis project cache
Expand Down Expand Up @@ -111,11 +119,13 @@ def clear(self) -> None:
self._lru_cache.clear()
self._static_cache.clear()

def lru_items(self) -> Sequence[Tuple[str,CacheDetails]]:
return self._lru_cache.items()

def static_items(self) -> Sequence[Tuple[str,CacheDetails]]:
return self._static_cache.items()
def items(self, cachetype: CacheType = CacheType.ALL) -> Sequence[Tuple[str,CacheDetails]]:
if cachetype == CacheType.ALL:
return chain(self._lru_cache.items(), self._static_cache.items())
elif cachetype == CacheType.LRU:
return self._lru_cache.items()
elif cachetype == CacheType.STATIC:
return self._static_cache.items()

def remove_entry(self, key: str) -> None:
""" Remove cache entry
Expand Down
3 changes: 2 additions & 1 deletion tests/unittests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from pathlib import Path
from pyqgisserver.qgscache.cachemanager import (QgsCacheManager,
CacheType,
PathNotAllowedError,
preload_projects_file)
from pyqgisserver.config import confservice
Expand Down Expand Up @@ -175,7 +176,7 @@ def test_preload_projects(data) -> None:
# raster_layer.qgs (invalid layer)

# Ensure that items are in static cache
items = list(k for k,_ in cacheservice.static_items())
items = list(k for k,_ in cacheservice.items(CacheType.STATIC))
assert "file:france_parts.qgs" in items
assert "project_simple.qgs" in items

Expand Down

0 comments on commit c40bb43

Please sign in to comment.