2020-04-02 16:54:57 +02:00
|
|
|
import re
|
2020-02-23 13:43:44 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-02-23 13:43:44 +01:00
|
|
|
def asbool(val):
|
2020-05-05 19:55:37 +02:00
|
|
|
"""Converts a value into a boolean
|
|
|
|
|
|
|
|
:param val: value to convert; accepts a wide range of
|
|
|
|
possible representations, such as yes, no, true, false, on, off
|
|
|
|
|
|
|
|
:return: True of val maps to true, False otherwise
|
|
|
|
:rtype: boolean
|
|
|
|
"""
|
2020-02-23 13:43:44 +01:00
|
|
|
if val is None:
|
|
|
|
return False
|
|
|
|
if isinstance(val, bool):
|
|
|
|
return val
|
|
|
|
val = str(val).strip().lower()
|
2020-05-03 11:15:52 +02:00
|
|
|
return val in ("t", "true", "y", "yes", "on", "1")
|
|
|
|
|
2020-02-23 13:43:44 +01:00
|
|
|
|
|
|
|
def asint(val, minimum=None, maximum=None):
|
2020-05-05 19:55:37 +02:00
|
|
|
"""Converts a value into an integer
|
|
|
|
|
|
|
|
:param val: value to convert
|
|
|
|
:param minimum: if specified, this determines the lower
|
|
|
|
boundary for the returned value, defaults to None
|
|
|
|
:param maximum: if specified, this determines the upper
|
|
|
|
boundary for the returned value, defaults to None
|
|
|
|
|
|
|
|
:return: integer representation of value
|
|
|
|
:rtype: integer
|
|
|
|
"""
|
2020-02-23 13:43:44 +01:00
|
|
|
if val is None:
|
|
|
|
val = 0
|
|
|
|
val = int(val)
|
|
|
|
val = min(val, maximum if maximum else val)
|
|
|
|
val = max(val, minimum if minimum else val)
|
|
|
|
return val
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-02-23 13:43:44 +01:00
|
|
|
def aslist(val):
|
2020-05-05 19:55:37 +02:00
|
|
|
"""Converts a comma-separated value string into a list
|
|
|
|
|
|
|
|
:param val: value to convert, either a single value or a comma-separated string
|
|
|
|
|
|
|
|
:return: list representation of the value passed in
|
|
|
|
:rtype: list of strings
|
|
|
|
"""
|
2020-02-23 13:43:44 +01:00
|
|
|
if val is None:
|
|
|
|
return []
|
|
|
|
if isinstance(val, list):
|
|
|
|
return val
|
2020-05-03 11:15:52 +02:00
|
|
|
return str(val).replace(" ", "").split(",")
|
|
|
|
|
|
|
|
|
|
|
|
__UNITS = {"metric": "C", "kelvin": "K", "imperial": "F", "default": "C"}
|
|
|
|
|
2020-02-23 13:43:44 +01:00
|
|
|
|
2020-05-05 19:55:37 +02:00
|
|
|
def astemperature(val, unit="metric"):
|
|
|
|
"""Returns a temperature representation of the input value
|
|
|
|
|
|
|
|
:param val: value to format, must be convertible into an integer
|
|
|
|
:param unit: unit of the input value, supported units are:
|
|
|
|
metric, kelvin, imperial, defaults to metric
|
|
|
|
|
|
|
|
:return: temperature representation of the input value
|
|
|
|
:rtype: string
|
|
|
|
"""
|
2020-05-08 21:01:45 +02:00
|
|
|
return "{}°{}".format(int(val), __UNITS.get(unit, __UNITS["default"]))
|
2020-04-04 07:47:53 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
|
|
|
def byte(val, fmt="{:.2f}"):
|
2020-05-05 19:55:37 +02:00
|
|
|
"""Returns a byte representation of the input value
|
|
|
|
|
|
|
|
:param val: value to format, must be convertible into a float
|
|
|
|
:param fmt: optional output format string, defaults to {:.2f}
|
|
|
|
|
|
|
|
:return: byte representation (e.g. <X> KiB, GiB, etc.) of the input value
|
|
|
|
:rtype: string
|
|
|
|
"""
|
|
|
|
|
|
|
|
val = float(val)
|
2020-05-03 11:15:52 +02:00
|
|
|
for unit in ["", "Ki", "Mi", "Gi"]:
|
2020-03-06 14:52:16 +01:00
|
|
|
if val < 1024.0:
|
2020-05-03 11:15:52 +02:00
|
|
|
return "{}{}B".format(fmt, unit).format(val)
|
2020-03-06 14:52:16 +01:00
|
|
|
val /= 1024.0
|
2020-05-03 11:15:52 +02:00
|
|
|
return "{}GiB".format(fmt).format(val * 1024.0)
|
|
|
|
|
|
|
|
|
|
|
|
__seconds_pattern = re.compile("(([\d\.?]+)h)?(([\d\.]+)m)?([\d\.]+)?s?")
|
|
|
|
|
2020-03-06 14:52:16 +01:00
|
|
|
|
2020-04-02 16:54:57 +02:00
|
|
|
def seconds(duration):
|
2020-05-05 19:55:37 +02:00
|
|
|
"""Returns a time duration (in seconds) representation of the input value
|
|
|
|
|
|
|
|
:param duration: value to format (e.g. 5h30m2s)
|
|
|
|
|
|
|
|
:return: duration in seconds of the input value
|
|
|
|
:rtype: float
|
|
|
|
"""
|
2020-04-02 16:54:57 +02:00
|
|
|
if isinstance(duration, int) or isinstance(duration, float):
|
|
|
|
return float(duration)
|
|
|
|
|
|
|
|
matches = __seconds_pattern.match(duration)
|
|
|
|
result = 0.0
|
|
|
|
if matches.group(2):
|
2020-05-03 11:15:52 +02:00
|
|
|
result += float(matches.group(2)) * 3600 # hours
|
2020-04-02 16:54:57 +02:00
|
|
|
if matches.group(4):
|
2020-05-03 11:15:52 +02:00
|
|
|
result += float(matches.group(4)) * 60 # minutes
|
2020-04-02 16:54:57 +02:00
|
|
|
if matches.group(5):
|
2020-05-03 11:15:52 +02:00
|
|
|
result += float(matches.group(5)) # seconds
|
2020-04-02 16:54:57 +02:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-03-07 13:33:28 +01:00
|
|
|
def duration(duration, compact=False, unit=False):
|
2020-05-05 19:55:37 +02:00
|
|
|
"""Returns a time duration string representing the input value
|
|
|
|
|
|
|
|
:param duration: value to format, must be convertible into an into
|
|
|
|
:param compact: whether to show also seconds, defaults to False
|
|
|
|
:param unit: whether to display he unit, defaults to False
|
|
|
|
|
|
|
|
:return: duration representation (e.g. 5:02s) of the input value
|
|
|
|
:rtype: string
|
|
|
|
"""
|
2020-03-06 20:57:32 +01:00
|
|
|
duration = int(duration)
|
2020-04-19 14:07:15 +02:00
|
|
|
if duration < 0:
|
2020-05-03 11:15:52 +02:00
|
|
|
return "n/a"
|
2020-03-06 20:57:32 +01:00
|
|
|
minutes, seconds = divmod(duration, 60)
|
|
|
|
hours, minutes = divmod(minutes, 60)
|
2020-05-03 11:15:52 +02:00
|
|
|
suf = "m"
|
|
|
|
res = "{:02d}:{:02d}".format(minutes, seconds)
|
2020-03-06 20:57:32 +01:00
|
|
|
if hours > 0:
|
2020-03-07 13:33:28 +01:00
|
|
|
if compact:
|
2020-05-03 11:15:52 +02:00
|
|
|
res = "{:02d}:{:02d}".format(hours, minutes)
|
2020-03-06 20:57:32 +01:00
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
res = "{:02d}:{}".format(hours, res)
|
|
|
|
suf = "h"
|
|
|
|
|
|
|
|
return "{}{}".format(res, suf if unit else "")
|
2020-03-06 20:57:32 +01:00
|
|
|
|
|
|
|
|
2020-02-23 13:43:44 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|