1
0
Fork 0

Improve output template internal formatting

* Allow slicing lists/strings using `field.start🔚step`
* A field can also be used as offset like `field1+num+field2`
* A default value can be given using `field|default`
* Capture all format strings and set it to `None` if invalid. This prevents invalid fields from causing errors
This commit is contained in:
pukkandan 2021-05-03 22:36:03 +05:30
commit e625be0d10
No known key found for this signature in database
GPG key ID: 0F00D95A001F4698
4 changed files with 71 additions and 32 deletions

View file

@ -6112,11 +6112,11 @@ def traverse_dict(dictn, keys, casesense=True):
key = key.lower()
dictn = dictn.get(key)
elif isinstance(dictn, (list, tuple, compat_str)):
key, n = int_or_none(key), len(dictn)
if key is not None and -n <= key < n:
dictn = dictn[key]
if ':' in key:
key = slice(*map(int_or_none, key.split(':')))
else:
dictn = None
key = int_or_none(key)
dictn = try_get(dictn, lambda x: x[key])
else:
return None
return dictn