diff options
Diffstat (limited to 'src/pydrilla/pydrilla.py')
-rw-r--r-- | src/pydrilla/pydrilla.py | 80 |
1 files changed, 34 insertions, 46 deletions
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/<string:identifier>', item_type, _get_item) - -@bp.route('/licenses/<string:identifier>') -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/<string:identifier>', item_type, _get_item) |