]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
cephfs-shell: extend to_bytes()
authorRishabh Dave <ridave@redhat.com>
Thu, 15 Aug 2019 06:22:12 +0000 (11:52 +0530)
committerRishabh Dave <ridave@redhat.com>
Fri, 13 Sep 2019 05:15:42 +0000 (10:45 +0530)
Make to_bytes() handle lists, None as well as other simple types as
well. In case of None, to_bytes() returns None instead of returning
b'None'.

Signed-off-by: Rishabh Dave <ridave@redhat.com>
src/tools/cephfs/cephfs-shell

index dd86cb4830a9ec57d5e3f01e31a8a2ba8f682cac..67337c7398f71f56a7600c703c9de6bcbbc31660 100755 (executable)
@@ -102,9 +102,21 @@ def get_chunks(file_size):
     yield(chunk_start, final_chunk_size)
 
 
-def to_bytes(string):
-    return bytes(string, encoding='utf-8')
-
+def to_bytes(param):
+    # don't convert as follows as it can lead unusable results like coverting
+    # [1, 2, 3, 4] to '[1, 2, 3, 4]' -
+    # str(param).encode('utf-8')
+    if isinstance(param, bytes):
+        return param
+    elif isinstance(param, str):
+        return bytes(param, encoding='utf-8')
+    elif isinstance(param, list):
+        return [i.encode('utf-8') if isinstance(i, str) else to_bytes(i) for \
+                i in param]
+    elif isinstance(param, int) or isinstance(param, float):
+        return str(param).encode('utf-8')
+    elif param is None:
+        return None
 
 def ls(path, opts=''):
     # opts tries to be like /bin/ls opts