summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorViacheslav Hletenko <v.gletenko@vyos.io>2023-03-12 09:31:47 +0000
committerViacheslav Hletenko <v.gletenko@vyos.io>2023-03-12 09:31:47 +0000
commitf2f78ac796322a398c43a5c418d4563c4d34b23d (patch)
tree8d379f2e4f17345fc9eec7f9817829135c1fd911 /python
parenta3b16a483140bff9d0085351adabcb2e793cfe2b (diff)
downloadvyos-1x-f2f78ac796322a398c43a5c418d4563c4d34b23d.tar.gz
vyos-1x-f2f78ac796322a398c43a5c418d4563c4d34b23d.zip
T5058: Add template filter range_to_regex
Add template filter 'range_to_regex' Convert range of numbers or list of ranges to regex % range_to_regex('11-12') '(1[1-2])' % range_to_regex(['11-12', '14-15']) '(1[1-2]|1[4-5])'
Diffstat (limited to 'python')
-rw-r--r--python/vyos/template.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/python/vyos/template.py b/python/vyos/template.py
index 4778bf1c8..c98d6874e 100644
--- a/python/vyos/template.py
+++ b/python/vyos/template.py
@@ -362,3 +362,28 @@ def get_ipv4(interface):
""" Get interface IPv4 addresses"""
from vyos.ifconfig import Interface
return Interface(interface).get_addr_v4()
+
+@register_filter('range_to_regex')
+def range_to_regex(num_range):
+ """Convert range of numbers or list of ranges
+ to regex
+ % range_to_regex('11-12')
+ '(1[1-2])'
+ % range_to_regex(['11-12', '14-15'])
+ '(1[1-2]|1[4-5])'
+ """
+ from vyos.range_regex import range_to_regex
+ if isinstance(num_range, list):
+ data = []
+ for entry in num_range:
+ if '-' not in entry:
+ data.append(entry)
+ else:
+ data.append(range_to_regex(entry))
+ return f'({"|".join(data)})'
+
+ if '-' not in num_range:
+ return num_range
+
+ regex = range_to_regex(num_range)
+ return f'({regex})'