diff options
author | Alexandru Avadanii <Alexandru.Avadanii@enea.com> | 2018-02-22 01:31:50 +0100 |
---|---|---|
committer | Alexandru Avadanii <Alexandru.Avadanii@enea.com> | 2018-02-22 01:48:39 +0100 |
commit | ffc697113c99fa1e955bdce068d67ca153898566 (patch) | |
tree | 19d9c19ec7d1c9d277ffcee310d68f49a4aa851c | |
parent | d085c0ec73dfd804f226fb383509f6592c36236b (diff) |
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 <Alexandru.Avadanii@enea.com>
-rw-r--r-- | config/utils/gen_config_lib.py | 19 |
1 files changed, 19 insertions, 0 deletions
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: |