summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@vyos.io>2022-10-28 10:38:26 -0400
committerDaniil Baturin <daniil@vyos.io>2022-10-28 10:38:26 -0400
commit3f75a38abe3e97ff243d01931a4defe8f4eef98a (patch)
tree58dd9083318f2c07262a7f058ea4a5908e95cfbd
parentf35195945daba0a81a93b74b280591dd955c193a (diff)
downloadvyos-1x-3f75a38abe3e97ff243d01931a4defe8f4eef98a.tar.gz
vyos-1x-3f75a38abe3e97ff243d01931a4defe8f4eef98a.zip
T4779: add vyos.util.human_to_bytes
-rw-r--r--python/vyos/util.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py
index e4e2a44ec..a80584c5a 100644
--- a/python/vyos/util.py
+++ b/python/vyos/util.py
@@ -574,6 +574,37 @@ def bytes_to_human(bytes, initial_exponent=0):
size_string = "{0:.2f} {1}".format(value, suffix)
return size_string
+def human_to_bytes(value):
+ """ Converts a data amount with a unit suffix to bytes, like 2K to 2048 """
+
+ from re import match as re_match
+
+ res = re_match(r'^\s*(\d+(?:\.\d+)?)\s*([a-zA-Z]+)\s*$', value)
+
+ if not res:
+ raise ValueError(f"'{value}' is not a valid data amount")
+ else:
+ amount = float(res.group(1))
+ unit = res.group(2).lower()
+
+ if unit == 'b':
+ res = amount
+ elif (unit == 'k') or (unit == 'kb'):
+ res = amount * 1024
+ elif (unit == 'm') or (unit == 'mb'):
+ res = amount * 1024**2
+ elif (unit == 'g') or (unit == 'gb'):
+ res = amount * 1024**3
+ elif (unit == 't') or (unit == 'tb'):
+ res = amount * 1024**4
+ else:
+ raise ValueError(f"Unsupported data unit '{unit}'")
+
+ # There cannot be fractional bytes, so we convert them to integer.
+ # However, truncating causes problems with conversion back to human unit,
+ # so we round instead -- that seems to work well enough.
+ return round(res)
+
def get_cfg_group_id():
from grp import getgrnam
from vyos.defaults import cfg_group