diff options
author | Wojtek Kosior <koszko@koszko.org> | 2021-11-17 21:12:22 +0100 |
---|---|---|
committer | Wojtek Kosior <koszko@koszko.org> | 2021-11-17 21:12:22 +0100 |
commit | a6721b316fca0f7a460fa2e62ed679238f97a10e (patch) | |
tree | 08600f8fb34a5f888a27f43480b1dae89e422d0f /src/pydrilla | |
parent | c6e5d1ece9c9ddf738e4124135f33651c94baa7c (diff) | |
download | haketilo-hydrilla-a6721b316fca0f7a460fa2e62ed679238f97a10e.tar.gz haketilo-hydrilla-a6721b316fca0f7a460fa2e62ed679238f97a10e.zip |
enable querying of patterns by url
Diffstat (limited to 'src/pydrilla')
-rw-r--r-- | src/pydrilla/pydrilla.py | 37 |
1 files changed, 26 insertions, 11 deletions
diff --git a/src/pydrilla/pydrilla.py b/src/pydrilla/pydrilla.py index dc472a4..af4ff1c 100644 --- a/src/pydrilla/pydrilla.py +++ b/src/pydrilla/pydrilla.py @@ -511,27 +511,36 @@ class Content: ver=version_string(mapping['version']) ) - def query(self, url, max=0): + def query(self, url): ''' - Return return registered patterns and mappings (available as - MappingItems) that match url. The maximum number of items yielded may be - limited by using the optional max argument. Its default value, 0, causes - no limit to be imposed. + Return a list of registered mappings that match url. If multiple versions of a mapping are applicable, only the most recent is included in the result. ''' deco = DeconstructedUrl(url) + mappings = {} + domain_tree = self.patterns_by_proto.get(deco.proto) \ or PatternTreeNode() + + def process_item(item): + if url[-1] != '/' and item.pattern[-1] == '/': + return + + identifier = item.mapping['identifier'] + + if identifier not in mappings or \ + item.mapping['version'] > mappings[identifier]['version']: + mappings[identifier] = item.mapping + for path_tree in domain_tree.search(deco.domain): - for item in path_tree.search(deco.path): - if url[-1] == '/' or item.pattern[-1] != '/': - yield item - max -= 1 - if max == 0: - return + for item_list in path_tree.search(deco.path): + for item in item_list: + process_item(item) + + return list(mappings.values()) def load_content_from_subdir(subdir_path, source_name, content): index_path = subdir_path / 'index.json' @@ -683,3 +692,9 @@ for item_type, get_dict, get_item in [ return get_item(identifier, get_dict) bp.add_url_rule(f'/{item_type}s/<string:identifier>', item_type, _get_item) + +@bp.route('/query') +def query(): + url = request.args['url'] + + return json.dumps(content().query(url)) |