[PATCH 13/24] Rename convert_human_to_bytes(). Add bytes_to_human()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



x_to_y already connotes conversion, so omit the "convert_" prefix for
brevity.

Add a corresponding bytes_to_human(), which targetcli uses.

Signed-off-by: Andy Grover <agrover@xxxxxxxxxx>
---
 rtslib/tcm.py   |    6 +++---
 rtslib/utils.py |   21 ++++++++++++++-------
 2 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/rtslib/tcm.py b/rtslib/tcm.py
index 3a19712..326b8ad 100644
--- a/rtslib/tcm.py
+++ b/rtslib/tcm.py
@@ -24,7 +24,7 @@ from target import LUN, TPG, Target, FabricModule
 from node import CFSNode
 from utils import fread, fwrite, RTSLibError, list_scsi_hbas, generate_wwn
 from utils import convert_scsi_path_to_hctl, convert_scsi_hctl_to_path
-from utils import convert_human_to_bytes, is_dev_in_use, get_block_type
+from utils import human_to_bytes, is_dev_in_use, get_block_type
 from utils import is_disk_partition, get_disk_size
 
 class Backstore(CFSNode):
@@ -705,7 +705,7 @@ class RDMCPStorageObject(StorageObject):
 
     def _configure(self, size, wwn):
         self._check_self()
-        size = convert_human_to_bytes(size)
+        size = human_to_bytes(size)
         # convert to 4k pages
         size = round(float(size)/4096)
         if size == 0:
@@ -835,7 +835,7 @@ class FileIOStorageObject(StorageObject):
             if size is None:
                 raise RTSLibError("The size parameter is mandatory "
                                   + "when using a file.")
-            size = convert_human_to_bytes(size)
+            size = human_to_bytes(size)
             self._control("fd_dev_name=%s,fd_dev_size=%d" % (dev, size))
         else:
             # it is a block device or a disk partition
diff --git a/rtslib/utils.py b/rtslib/utils.py
index bd1c8a3..3dad99a 100644
--- a/rtslib/utils.py
+++ b/rtslib/utils.py
@@ -487,7 +487,7 @@ def convert_scsi_hctl_to_path(host, controller, target, lun):
     else:
         return ''
 
-def convert_human_to_bytes(hsize, kilo=1024):
+def human_to_bytes(hsize, kilo=1024):
     '''
     This function converts human-readable amounts of bytes to bytes.
     It understands the following units :
@@ -504,13 +504,13 @@ def convert_human_to_bytes(hsize, kilo=1024):
     Example:
 
     >>> import rtslib.utils as utils
-    >>> utils.convert_human_to_bytes("1k")
+    >>> utils.human_to_bytes("1k")
     1024
-    >>> utils.convert_human_to_bytes("1k", 1000)
+    >>> utils.human_to_bytes("1k", 1000)
     1000
-    >>> utils.convert_human_to_bytes("1MB")
+    >>> utils.human_to_bytes("1MB")
     1048576
-    >>> utils.convert_human_to_bytes("12kB")
+    >>> utils.human_to_bytes("12kB")
     12288
 
     @param hsize: The human-readable version of the Bytes amount to convert
@@ -535,8 +535,15 @@ def convert_human_to_bytes(hsize, kilo=1024):
     else:
         size = int(size[:-1])
 
-    size = size * int(kilo) ** power
-    return size
+    return size * int(kilo) ** power
+
+def bytes_to_human(size):
+    if not size:
+        return ""
+    for x in ['bytes','K','M','G','T']:
+        if size < 1024.0:
+            return "(%3.1f%s) " % (size, x)
+        size /= 1024.0
 
 def generate_wwn(wwn_type):
     '''
-- 
1.7.1

--
To unsubscribe from this list: send the line "unsubscribe target-devel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux SCSI]     [Kernel Newbies]     [Linux SCSI Target Infrastructure]     [Share Photos]     [IDE]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux ATA RAID]     [Linux IIO]     [Device Mapper]

  Powered by Linux