From d0200b656d696189c496493554d667f5d9828563 Mon Sep 17 00:00:00 2001 From: tobi-wan-kenobi Date: Thu, 2 Apr 2020 16:54:57 +0200 Subject: [PATCH] [core/config] Allow string formatted intervals Allow users to specify intervals such as '5m', for convenience --- core/config.py | 5 +++-- doc/FEATURES.md | 2 +- tests/util/test_format.py | 12 ++++++++++++ util/format.py | 18 +++++++++++++++++- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/core/config.py b/core/config.py index 85c6190..cae5f5d 100644 --- a/core/config.py +++ b/core/config.py @@ -1,6 +1,7 @@ import argparse import util.store +import util.format MODULE_HELP = 'Specify a space-separated list of modules to load. The order of the list determines their order in the i3bar (from left to right). Use : to provide an alias in case you want to load the same module multiple times, but specify different parameters.' PARAMETER_HELP = 'Provide configuration parameters in the form of .=' @@ -32,8 +33,8 @@ class Config(util.store.Store): def modules(self): return [item for sub in self._args.modules for item in sub] - def interval(self): - return float(self.get('interval', 1)) + def interval(self, default=1): + return util.format.seconds(self.get('interval', default)) def debug(self): return self._args.debug diff --git a/doc/FEATURES.md b/doc/FEATURES.md index c7caff6..8894013 100644 --- a/doc/FEATURES.md +++ b/doc/FEATURES.md @@ -3,4 +3,4 @@ - theme.minwidth - scrolling decorator - theme.exclude -- per module update interval +- per module update interval -> nice string format diff --git a/tests/util/test_format.py b/tests/util/test_format.py index 885bcb3..f31df19 100644 --- a/tests/util/test_format.py +++ b/tests/util/test_format.py @@ -76,4 +76,16 @@ class format(unittest.TestCase): self.assertEqual('00:20m', duration(20, unit=True)) self.assertEqual('00:20m', duration(20, compact=True, unit=True)) + def test_seconds(self): + self.assertEqual(10, seconds(10)) + self.assertEqual(10, seconds('10')) + + self.assertEqual(300, seconds('5m')) + self.assertEqual(320, seconds('5m20s')) + + self.assertEqual(4*3600, seconds('4h')) + self.assertEqual(4*3600 + 5*60 + 22, seconds('4h5m22s')) + + self.assertEqual(4*3600 + 5*60, seconds('4h5m')) + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/util/format.py b/util/format.py index 5ed29f6..ef64343 100644 --- a/util/format.py +++ b/util/format.py @@ -1,3 +1,4 @@ +import re def asbool(val): if val is None: @@ -15,7 +16,6 @@ def asint(val, minimum=None, maximum=None): val = max(val, minimum if minimum else val) return val - def aslist(val): if val is None: return [] @@ -30,6 +30,22 @@ def byte(val, fmt='{:.2f}'): val /= 1024.0 return '{}GiB'.format(fmt).format(val*1024.0) +__seconds_pattern = re.compile('(([\d\.?]+)h)?(([\d\.]+)m)?([\d\.]+)?s?') +def seconds(duration): + if isinstance(duration, int) or isinstance(duration, float): + return float(duration) + + matches = __seconds_pattern.match(duration) + result = 0.0 + if matches.group(2): + result += float(matches.group(2))*3600 # hours + if matches.group(4): + result += float(matches.group(4))*60 # minutes + if matches.group(5): + result += float(matches.group(5)) # seconds + + return result + def duration(duration, compact=False, unit=False): duration = int(duration) minutes, seconds = divmod(duration, 60)