Add special _index, _404 and _default keys

This commit is contained in:
Tobias Reisinger 2023-06-23 17:15:57 +02:00
parent 856298a67f
commit 9b318bf953
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE

View file

@ -37,17 +37,27 @@ def get_url_content(url):
} }
return targets return targets
def get_dict_entry_fallback(d, key, fallback):
if key and key in d:
return d[key]
if fallback in d:
return d[fallback]
return d.get('_default', None)
def get_entry(args, targets): def get_entry(args, targets):
while args: while args:
arg = args.pop(0) arg = args.pop(0)
if not arg: if not arg:
continue continue
targets = targets.get(arg, None)
# Fallback to _404 if the key is not found
targets = get_dict_entry_fallback(targets, arg, '_404')
if isinstance(targets, dict): if isinstance(targets, dict):
continue continue
return targets, args return targets, args
return None, args
# Try to match an index entry
return get_dict_entry_fallback(targets, '', '_index'), args
def get_entry_for_url(url, args): def get_entry_for_url(url, args):
targets = get_url_content(url) targets = get_url_content(url)