]> Frank Brehm's Git Trees - pixelpark/admin-tools.git/commitdiff
Modified for first succesful execution
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 16 Mar 2017 12:35:33 +0000 (13:35 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 16 Mar 2017 12:35:33 +0000 (13:35 +0100)
pp_lib/du.py
pp_lib/format_du.py
tmp/.gitignore [new file with mode: 0644]

index ad4199ce3b3a658635c76e7cb5af723b99e016b7..b81154b76cf4d694fa5b4fee81100ebbef214bc2 100644 (file)
@@ -25,7 +25,7 @@ from .errors import PpError
 from .obj import PpBaseObjectError, PpBaseObject
 
 
-__version__ = '0.3.1'
+__version__ = '0.4.1'
 
 LOG = logging.getLogger(__name__)
 
@@ -72,10 +72,14 @@ class DuEntry(PpBaseObject):
     if six.PY2:
         kilo = long(1024)
 
+    human_limit = 1.5
+
     factor = {}
+    hlimits = {}
     for unit in DU_UNIT_EXP.keys():
         exp = DU_UNIT_EXP[unit]
         factor[unit] = kilo ** exp
+        hlimits[unit] = human_limit * float(factor[unit])
 
     locale_conv = locale.localeconv()
     dp = '.'
@@ -191,25 +195,32 @@ class DuEntry(PpBaseObject):
                 unit_show = ''
                 precision = 0
         else:
+            # Human readable
             unit_show = 'K'
             size_float = float(self.size_kb)
-            prec = 0
-            for u in ('T', 'G', 'M'):
-                limit = 1.5 * float(self.factor[u])
-                sz_f = float(self.size_kb) / float(self.factor[u])
-                if sz_f >= limit:
-                    unit_show = u
-                    size_float = sz_f
-                    if precision < 1:
-                        prec = 1
-                    else:
-                        prec = precision
-                    break
-            if unit_show == 'K':
-                precision = 0
-            else:
-                precision = prec
-
+            precision = 0
+            if self.verbose > 2:
+                LOG.debug("Checking size_float {s} for factors\n{f}".format(
+                    s=size_float, f=pp(self.hlimits)))
+
+            if size_float > self.hlimits['T']:
+                unit_show = 'T'
+                size_float = size_float / float(self.factor['T'])
+                precision = 1
+            elif size_float > self.hlimits['G']:
+                unit_show = 'G'
+                size_float = size_float / float(self.factor['G'])
+                precision = 1
+            elif size_float > self.hlimits['M']:
+                unit_show = 'M'
+                size_float = size_float / float(self.factor['M'])
+                precision = 1
+            if self.verbose > 2:
+                LOG.debug("Computed size_float: {s} {u}".format(
+                    s=size_float, u=unit_show))
+
+        if unit_show != '':
+            unit_show = ' ' + unit_show
         template = "{{:,.{:d}f}}".format(precision) + unit_show
         size_show = template.format(size_float)
 
@@ -220,10 +231,33 @@ class DuEntry(PpBaseObject):
         return size_show
 
     # -------------------------------------------------------------------------
-    def to_str(self, unit='K', precision=0, size_with=5):
+    def to_str(self, unit='K', precision=0, size_width=None):
+
+        width = 16
+        unit = unit.upper()
+        if size_width is None:
+            if unit == 'K':
+                width = 16
+            elif unit == 'M':
+                width = 13
+                if precision:
+                    width += 1 + precision
+            elif unit == 'G':
+                width = 9
+                if precision:
+                    width += 1 + precision
+            elif unit == 'T':
+                width = 5
+                if precision:
+                    width += 1 + precision
+            else:
+                # Human
+                width = 9
+        else:
+            width = size_width
 
         size_show = self.size_str(unit=unit, precision=precision)
-        line = "{0:>{w}s}      {1}".format(size_show, self.path, w=size_with)
+        line = "{0:>{w}s}      {1}".format(size_show, self.path, w=width)
 
         return line
 
index e607e3631d0baed4f839c64a46302719b2616021..b89c2093d596821acf291a69bd669ac43c0279af 100644 (file)
@@ -34,7 +34,7 @@ try:
 except ImportError:
     from .global_version import __version__ as my_version
 
-__version__ = '0.4.1'
+__version__ = '0.4.2'
 LOG = logging.getLogger(__name__)
 
 
@@ -215,7 +215,7 @@ class FormatDuApp(PpApplication):
             return
 
         LOG.debug("Entry:\n{}".format(pp(entry.as_dict())))
-        LOG.debug("Entry: {}".format(entry.to_str(unit='M', precision=1, size_with=10)))
+        print(entry.to_str(unit=self.unit, precision=self.precision))
 
 # =============================================================================
 
diff --git a/tmp/.gitignore b/tmp/.gitignore
new file mode 100644 (file)
index 0000000..72e8ffc
--- /dev/null
@@ -0,0 +1 @@
+*