Add TiB to disk units, add SI unit option for disk space

This commit is contained in:
Florian Eich 2020-12-02 19:08:45 +01:00
parent 2de39be731
commit 3c08eafa4a
2 changed files with 21 additions and 8 deletions

View file

@ -8,6 +8,7 @@ Parameters:
* disk.path: Path to calculate disk usage from (defaults to /) * disk.path: Path to calculate disk usage from (defaults to /)
* disk.open: Which application / file manager to launch (default xdg-open) * disk.open: Which application / file manager to launch (default xdg-open)
* disk.format: Format string, tags {path}, {used}, {left}, {size} and {percent} (defaults to '{path} {used}/{size} ({percent:05.02f}%)') * disk.format: Format string, tags {path}, {used}, {left}, {size} and {percent} (defaults to '{path} {used}/{size} ({percent:05.02f}%)')
* disk.system: Unit system to use - SI (KB, MB, ...) or IEC (KiB, MiB, ...) (defaults to 'IEC')
""" """
import os import os
@ -25,6 +26,7 @@ class Module(core.module.Module):
self._path = self.parameter("path", "/") self._path = self.parameter("path", "/")
self._format = self.parameter("format", "{used}/{size} ({percent:05.02f}%)") self._format = self.parameter("format", "{used}/{size} ({percent:05.02f}%)")
self._system = self.parameter("system", "IEC")
self._used = 0 self._used = 0
self._left = 0 self._left = 0
@ -38,9 +40,9 @@ class Module(core.module.Module):
) )
def diskspace(self, widget): def diskspace(self, widget):
used_str = util.format.byte(self._used) used_str = util.format.byte(self._used, sys=self._system)
size_str = util.format.byte(self._size) size_str = util.format.byte(self._size, sys=self._system)
left_str = util.format.byte(self._left) left_str = util.format.byte(self._left, sys=self._system)
percent_str = self._percent percent_str = self._percent
return self._format.format( return self._format.format(

View file

@ -71,22 +71,33 @@ def astemperature(val, unit="metric"):
return "{}°{}".format(int(val), __UNITS.get(unit.lower(), __UNITS["default"])) return "{}°{}".format(int(val), __UNITS.get(unit.lower(), __UNITS["default"]))
def byte(val, fmt="{:.2f}"): def byte(val, fmt="{:.2f}", sys="IEC"):
"""Returns a byte representation of the input value """Returns a byte representation of the input value
:param val: value to format, must be convertible into a float :param val: value to format, must be convertible into a float
:param fmt: optional output format string, defaults to {:.2f} :param fmt: optional output format string, defaults to {:.2f}
:param sys: optional unit system specifier - SI (kilo, Mega, Giga, ...) or
IEC (kibi, Mebi, Gibi, ...) - defaults to IEC
:return: byte representation (e.g. <X> KiB, GiB, etc.) of the input value :return: byte representation (e.g. <X> KiB, GiB, etc.) of the input value
:rtype: string :rtype: string
""" """
if sys == "IEC":
div = 1024.0
units = ["", "Ki", "Mi", "Gi", "Ti"]
final = "TiB"
elif sys == "SI":
div = 1000.0
units = ["", "K", "M", "G", "T"]
final = "TB"
val = float(val) val = float(val)
for unit in ["", "Ki", "Mi", "Gi"]: for unit in units:
if val < 1024.0: if val < div:
return "{}{}B".format(fmt, unit).format(val) return "{}{}B".format(fmt, unit).format(val)
val /= 1024.0 val /= div
return "{}GiB".format(fmt).format(val * 1024.0) return "{}{}".format(fmt).format(val * div, final)
__seconds_pattern = re.compile(r"(([\d\.?]+)h)?(([\d\.]+)m)?([\d\.]+)?s?") __seconds_pattern = re.compile(r"(([\d\.?]+)h)?(([\d\.]+)m)?([\d\.]+)?s?")