From ffc697113c99fa1e955bdce068d67ca153898566 Mon Sep 17 00:00:00 2001 From: Alexandru Avadanii Date: Thu, 22 Feb 2018 01:31:50 +0100 Subject: [PATCH] gen_config_lib: Add storage_size_num filter New filter for converting PDF disk_capacity strings into a numeric only string (to bypass scientific notation), parsable as float. Change-Id: I779bec7db03ef1b3ce1bfd6ca239a3e7b4021eb2 Signed-off-by: Alexandru Avadanii --- config/utils/gen_config_lib.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/config/utils/gen_config_lib.py b/config/utils/gen_config_lib.py index e75387fb..1e7229be 100644 --- a/config/utils/gen_config_lib.py +++ b/config/utils/gen_config_lib.py @@ -18,6 +18,7 @@ def load_custom_filters(environment): # TODO deprecate ipaddr_index and netmask for the better ipnet ones filter_list = { 'dpkg_arch': filter_dpkg_arch, + 'storage_size_num': filter_storage_size_num, 'ipnet_hostaddr': filter_ipnet_hostaddr, 'ipnet_hostmin': filter_ipnet_hostmin, 'ipnet_hostmax': filter_ipnet_hostmax, @@ -52,6 +53,24 @@ def filter_dpkg_arch(arch, to_dpkg=True): return arch_dpkg_table[arch] +def filter_storage_size_num(size_str): + """Convert human-readable size string to a string convertible to float""" + + # pattern: '^[1-9][\d\.]*[MGT]B?$', multiplier=1000 (not KiB) + if size_str.endswith('B'): + size_str = size_str[:-1] + try: + size_num = 1000000 + for multiplier in ['M', 'G', 'T']: + if size_str.endswith(multiplier): + return '{:.2f}'.format(size_num * float(size_str[:-1])) + size_num = size_num * 1000 + return '{:.2f}'.format(float(size_str)) + except ValueError as ex: + logging.error(size_str + " is not a valid size string") + raise + + def filter_ipnet_hostaddr(network_cidr, index): """Return the host IP address on given index from an IP network""" try: -- 2.16.6