aboutsummaryrefslogtreecommitdiff
path: root/src/pydrilla
diff options
context:
space:
mode:
Diffstat (limited to 'src/pydrilla')
-rw-r--r--src/pydrilla/pydrilla.py37
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))