From 9b318bf953c0322b86b962df7e1d581f9f35997c Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Fri, 23 Jun 2023 17:15:57 +0200 Subject: [PATCH] Add special _index, _404 and _default keys --- src/handler.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/handler.py b/src/handler.py index 2e3726d..8a2b374 100644 --- a/src/handler.py +++ b/src/handler.py @@ -37,17 +37,27 @@ def get_url_content(url): } 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): while args: arg = args.pop(0) if not arg: 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): continue 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): targets = get_url_content(url)