summaryrefslogtreecommitdiff
path: root/python/vyos/utils/convert.py
diff options
context:
space:
mode:
authorChristian Breunig <christian@breunig.cc>2023-07-15 20:12:56 +0200
committerChristian Breunig <christian@breunig.cc>2023-07-15 20:13:12 +0200
commit5f77ccf91eb402c548fc91b2e080a4b2b86f4181 (patch)
tree9b926dedc07ef547ad8bbe539f89990249552414 /python/vyos/utils/convert.py
parent9285b9a571ee944daf6f17847a62f115146834a4 (diff)
downloadvyos-1x-5f77ccf91eb402c548fc91b2e080a4b2b86f4181.tar.gz
vyos-1x-5f77ccf91eb402c548fc91b2e080a4b2b86f4181.zip
T5195: vyos.util -> vyos.utils package refactoring part #2
Diffstat (limited to 'python/vyos/utils/convert.py')
-rw-r--r--python/vyos/utils/convert.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/python/vyos/utils/convert.py b/python/vyos/utils/convert.py
index 975c67e0a..ec2333ef0 100644
--- a/python/vyos/utils/convert.py
+++ b/python/vyos/utils/convert.py
@@ -143,3 +143,33 @@ def mac_to_eui64(mac, prefix=None):
return str(net[euil])
except: # pylint: disable=bare-except
return
+
+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 base64 import b64encode
+ from collections import OrderedDict
+
+ if isinstance(data, str):
+ return data
+ if isinstance(data, bytes):
+ try:
+ return data.decode()
+ except UnicodeDecodeError:
+ return b64encode(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