diff options
author | Viacheslav Hletenko <v.gletenko@vyos.io> | 2022-08-04 13:04:30 +0000 |
---|---|---|
committer | Viacheslav Hletenko <v.gletenko@vyos.io> | 2022-08-04 13:04:30 +0000 |
commit | e2259e25029a142ba607b5865337b38ff8a482aa (patch) | |
tree | 1bce98e60fd375f34686173fc2fb993303cf52a9 | |
parent | 993961f60ead2a18912eb577b1152463d4eb8b4e (diff) | |
download | vyos-1x-e2259e25029a142ba607b5865337b38ff8a482aa.tar.gz vyos-1x-e2259e25029a142ba607b5865337b38ff8a482aa.zip |
utils: T4594: Add convert_data util
Convert multiple types of data to types usable in CLI
For example 'vici' returns values in bytestring/bytes and we can
decode them all at once
-rw-r--r-- | python/vyos/util.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py index b86b1949c..8df9ef7d6 100644 --- a/python/vyos/util.py +++ b/python/vyos/util.py @@ -800,6 +800,32 @@ def dict_search_recursive(dict_object, key, path=[]): for x in dict_search_recursive(j, key, new_path): yield x +def convert_data(data): + """Convert multiple types of data to types usable in CLI + + Args: + data (str | bytes | list | OrderedDict): input data + + Returns: + str | list | dict: converted data + """ + from collections import OrderedDict + + if isinstance(data, str): + return data + if isinstance(data, bytes): + return data.decode() + if isinstance(data, list): + list_tmp = [] + for item in data: + list_tmp.append(convert_data(item)) + return list_tmp + if isinstance(data, OrderedDict): + dict_tmp = {} + for key, value in data.items(): + dict_tmp[key] = convert_data(value) + return dict_tmp + def get_bridge_fdb(interface): """ Returns the forwarding database entries for a given interface """ if not os.path.exists(f'/sys/class/net/{interface}'): |