diff options
Diffstat (limited to 'python/vyos/template.py')
-rw-r--r-- | python/vyos/template.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/python/vyos/template.py b/python/vyos/template.py index 85e4d12b3..3fbb33acb 100644 --- a/python/vyos/template.py +++ b/python/vyos/template.py @@ -207,6 +207,11 @@ def network_from_ipv4(address): cidr_prefix = ip_interface(f'{address}/{netmask}').network return address_from_cidr(cidr_prefix) +@register_filter('is_interface') +def is_interface(interface): + """ Check if parameter is a valid local interface name """ + return os.path.exists(f'/sys/class/net/{interface}') + @register_filter('is_ip') def is_ip(addr): """ Check addr if it is an IPv4 or IPv6 address """ @@ -341,3 +346,18 @@ def get_dhcp_router(interface): if 'option routers' in line: (_, _, address) = line.split() return address.rstrip(';') + +@register_filter('natural_sort') +def natural_sort(iterable): + import re + from jinja2.runtime import Undefined + + if isinstance(iterable, Undefined) or iterable is None: + return list() + + def convert(text): + return int(text) if text.isdigit() else text.lower() + def alphanum_key(key): + return [convert(c) for c in re.split('([0-9]+)', str(key))] + + return sorted(iterable, key=alphanum_key) |