From: Frank Brehm Date: Thu, 16 Mar 2017 12:35:33 +0000 (+0100) Subject: Modified for first succesful execution X-Git-Tag: 0.1.2~261 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=3fa578c1d7f0ade519850cbf5fd13b50452ebe40;p=pixelpark%2Fadmin-tools.git Modified for first succesful execution --- diff --git a/pp_lib/du.py b/pp_lib/du.py index ad4199c..b81154b 100644 --- a/pp_lib/du.py +++ b/pp_lib/du.py @@ -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 diff --git a/pp_lib/format_du.py b/pp_lib/format_du.py index e607e36..b89c209 100644 --- a/pp_lib/format_du.py +++ b/pp_lib/format_du.py @@ -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 index 0000000..72e8ffc --- /dev/null +++ b/tmp/.gitignore @@ -0,0 +1 @@ +*