Commit 466c4274 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Fixed key pattern to allow entry_ids with - character.

parent de474348
Pipeline #72392 failed with stages
in 34 minutes and 45 seconds
......@@ -534,7 +534,7 @@ def read_archive(file_or_path: str, **kwargs) -> ArchiveReader:
return ArchiveReader(file_or_path, **kwargs)
__query_archive_key_pattern = re.compile(r'(\w+)(\[([-?0-9]*)(:([-?0-9]*))?\])?')
__query_archive_key_pattern = re.compile(r'^([\s\w\-]+)(\[([-?0-9]*)(:([-?0-9]*))?\])?$')
def query_archive(f_or_archive_reader: Union[str, ArchiveReader, BytesIO], query_dict: dict, **kwargs):
......
......@@ -42,8 +42,10 @@ from urllib.parse import urlparse
from bravado import requests_client as bravado_requests_client
import time
from keycloak import KeycloakOpenID
from io import StringIO
from nomad import config
from nomad import metainfo as mi
from nomad.datamodel import EntryArchive
# TODO this import is necessary to load all metainfo defintions that the parsers are using
......@@ -77,6 +79,32 @@ class KeycloakAuthenticator(bravado_requests_client.Authenticator):
return dict(Authorization='Bearer %s' % self.token['access_token'])
class ApiStatistics(mi.MSection):
nentries = mi.Quantity(
type=int,
description='Number queries entries')
last_response_nentries = mi.Quantity(
type=int,
description='Number of entries loaded in the last api call')
last_response_size = mi.Quantity(
type=int, unit=mi.units.bytes,
description='Bytes loaded in the last api call')
loaded_nentries = mi.Quantity(
type=int,
description='Number of downloaded entries')
def __repr__(self):
out = StringIO()
for quantity in self.m_def.all_quantities.values():
out.write('%s: %s\n' % (quantity.description, self.m_get(quantity)))
return out.getvalue()
class ArchiveQuery(Sequence):
def __init__(
self,
......@@ -106,6 +134,7 @@ class ArchiveQuery(Sequence):
self._total = -1
self._results: List[dict] = []
self._api_statistics = ApiStatistics()
@property
def authentication(self):
......@@ -162,6 +191,17 @@ class ArchiveQuery(Sequence):
self._results.append(archive)
self._api_statistics.last_response_size = len(response.content)
self._api_statistics.nentries = self._total
self._api_statistics.last_response_nentries = len(results)
self._api_statistics.loaded_nentries = len(self._results)
def __repr__(self):
if self._total == -1:
self.call_api()
return str(self._api_statistics)
def __getitem__(self, key):
if key >= self.__len__():
raise IndexError()
......
......@@ -257,6 +257,15 @@ def test_query(query, ref):
assert query_archive(f, query) == ref
@pytest.mark.parametrize('key', ['simple', ' fixedsize', 'z6qp-VxV5uacug_1xTBhm5xxU2yZ'])
def test_keys(key):
f = BytesIO()
write_archive(f, 1, [(key, dict(example='content'))])
packed_archive = f.getbuffer()
f = BytesIO(packed_archive)
assert key.strip() in query_archive(f, {key: '*'})
def test_read_springer():
springer = read_archive(config.normalize.springer_db_path)
with pytest.raises(KeyError):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment