From c00ed7f9ef28f6aee946e168a535d7f637fe46ae Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Wed, 17 Nov 2021 12:17:59 +0100 Subject: add source index.json contents fetching --- src/pydrilla/pydrilla.py | 80 ++++++++++++++++++++--------------------------- src/test/test_pydrilla.py | 12 ++++++- 2 files changed, 45 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/pydrilla/pydrilla.py b/src/pydrilla/pydrilla.py index 3e5edc8..586be19 100644 --- a/src/pydrilla/pydrilla.py +++ b/src/pydrilla/pydrilla.py @@ -166,6 +166,7 @@ class VersionedContentItem: self.known_versions = [] def register_item(self, item): + '''Make item queryable by version. Perform sanity checks for uuid.''' if self.identifier is None: self.identifier = item['identifier'] self.uuid = item['uuid'] @@ -184,6 +185,22 @@ class VersionedContentItem: self.by_version[ver_str] = item self.known_versions.append(ver) + def get_by_ver(self, ver=None): + ''' + Find and return definition of the newest version of item. + + If ver is specified, instead find and return definition of that version + of the item (or None is absent). + ''' + ver = version_string(ver or self.known_versions[-1]) + + return self.by_version.get(ver) + + def get_all(self): + '''Return a list of all definitions of item, ordered by version.''' + return [self.by_version[version_string(ver)] + for ver in self.known_versions] + class PatternTreeNode: ''' "Pattern Tree" is how we refer to the data structure used for querying @@ -494,40 +511,6 @@ class Content: ver=version_string(mapping['version']) ) - def item_dict(self, type): - '''Obtain self.resources or self.mappings, depending on type.''' - return self.resources if type == 'resource' else self.mappings - - def find_item(self, type, identifier, ver=None): - ''' - Find and return definition of the newest version of resource/mapping - named by identifier. If no such resource/mapping exists, return None. - - If ver is specified, instead find and return definition of that version - of the item (or None is absent). - ''' - versioned_item = self.item_dict(type).get(identifier) - if not versioned_item: - return None - - ver = version_string(ver or versioned_item.known_versions[-1]) - - return versioned_item.by_version.get(ver) - - def get_item_all_versions(self, type, identifier): - ''' - Return a list of all definitions of given resource or mapping, ordered - by version. - - If no item of given type with such identifier exists, return []. - ''' - versioned_item = self.item_dict(type).get(identifier) - if not versioned_item: - return [] - - return [versioned_item.by_version[version_string(ver)] - for ver in versioned_item.known_versions] - def query(self, url, max=0): ''' Return return registered patterns and mappings (available as @@ -659,11 +642,12 @@ indexpage = j2env.get_template('index.html') def index(): return indexpage.render() -def get_item(identifier, item_type): +def get_resource_or_mapping(identifier, get_dict): ver = request.args.get('ver') + versioned_item = get_dict().get(identifier) if ver == 'all': - definition = content().get_item_all_versions(item_type, identifier) + definition = versioned_item.get_all() if versioned_item else [] else: if ver is not None: try: @@ -671,22 +655,26 @@ def get_item(identifier, item_type): except: abort(400) - definition = content().find_item(item_type, identifier, ver) + definition = versioned_item and versioned_item.get_by_ver(ver) if definition is None: abort(404) return json.dumps(definition) -for item_type in ['mapping', 'resource']: - def _get_item(identifier, item_type=item_type): - return get_item(identifier, item_type) - - bp.add_url_rule(f'/{item_type}s/', item_type, _get_item) - -@bp.route('/licenses/') -def get_license(identifier): - definition = content().licenses.get(identifier) +def get_license_or_source(identifier, get_dict): + definition = get_dict().get(identifier) if definition is None: abort(404) return json.dumps(definition) + +for item_type, get_dict, get_item in [ + ('resource', lambda: content().resources, get_resource_or_mapping), + ('mapping', lambda: content().mappings, get_resource_or_mapping), + ('license', lambda: content().licenses, get_license_or_source), + ('source', lambda: content().indexes, get_license_or_source) +]: + def _get_item(identifier, get_dict=get_dict, get_item=get_item): + return get_item(identifier, get_dict) + + bp.add_url_rule(f'/{item_type}s/', item_type, _get_item) diff --git a/src/test/test_pydrilla.py b/src/test/test_pydrilla.py index 89b499f..9ec6ba6 100644 --- a/src/test/test_pydrilla.py +++ b/src/test/test_pydrilla.py @@ -75,12 +75,22 @@ def test_api_basic(client): response = client.get('/licenses/CC0-1.0') definition = json.loads(response.data.decode()) - assert definition['long_name'] == 'Creative Commons Zero v1.0 Universal' + assert definition['type'] == 'license' + assert definition['long_name'] == 'Creative Commons Zero v1.0 Universal' assert definition['source_name'] == 'hello' response = client.get('/licenses/random-bad-identifier') assert response.status_code == 404 + response = client.get('/sources/hello') + definition = json.loads(response.data.decode()) + + assert definition['source_name'] == 'hello' + assert type(definition['schema_version']) is list + + response = client.get('/sources/random-bad-identifier') + assert response.status_code == 404 + def test_normalize_version(): assert pydrilla.normalize_version([4, 5, 3, 0, 0]) == [4, 5, 3] assert pydrilla.normalize_version([1, 0, 5, 0]) == [1, 0, 5] -- cgit v1.2.3