summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorChristian Breunig <christian@breunig.cc>2023-02-09 07:04:47 +0100
committerGitHub <noreply@github.com>2023-02-09 07:04:47 +0100
commitcd78fe90771147f31fef252dc9b4f4061f325930 (patch)
treeee63a94cbbe16c04dcdb1f34132427aa54027ae8 /python
parent1042fc32c371a74f048ffaf9a551b5d13c227f45 (diff)
parent89191003df19e81869882a7eb673499b36ce1e63 (diff)
downloadvyos-1x-cd78fe90771147f31fef252dc9b4f4061f325930.tar.gz
vyos-1x-cd78fe90771147f31fef252dc9b4f4061f325930.zip
Merge pull request #1807 from dmbaturin/T4988-new-jinja-filters
vyos.template: T4988: add bytes and seconds conversion filters
Diffstat (limited to 'python')
-rw-r--r--python/vyos/template.py20
1 files changed, 19 insertions, 1 deletions
diff --git a/python/vyos/template.py b/python/vyos/template.py
index 15240f815..6367f51e5 100644
--- a/python/vyos/template.py
+++ b/python/vyos/template.py
@@ -1,4 +1,4 @@
-# Copyright 2019-2022 VyOS maintainers and contributors <maintainers@vyos.io>
+# Copyright 2019-2023 VyOS maintainers and contributors <maintainers@vyos.io>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
@@ -158,6 +158,24 @@ def force_to_list(value):
else:
return [value]
+@register_filter('seconds_to_human')
+def seconds_to_human(seconds, separator=""):
+ """ Convert seconds to human-readable values like 1d6h15m23s """
+ from vyos.util import seconds_to_human
+ return seconds_to_human(seconds, separator=separator)
+
+@register_filter('bytes_to_human')
+def bytes_to_human(bytes, initial_exponent=0, precision=2):
+ """ Convert bytes to human-readable values like 1.44M """
+ from vyos.util import bytes_to_human
+ return bytes_to_human(bytes, initial_exponent=initial_exponent, precision=precision)
+
+@register_filter('human_to_bytes')
+def human_to_bytes(value):
+ """ Convert a data amount with a unit suffix to bytes, like 2K to 2048 """
+ from vyos.util import human_to_bytes
+ return human_to_bytes(value)
+
@register_filter('ip_from_cidr')
def ip_from_cidr(prefix):
""" Take an IPv4/IPv6 CIDR host and strip cidr mask.