[core/config] Allow string formatted intervals

Allow users to specify intervals such as '5m', for convenience
This commit is contained in:
tobi-wan-kenobi 2020-04-02 16:54:57 +02:00
parent 18154dd74f
commit d0200b656d
4 changed files with 33 additions and 4 deletions

View file

@ -1,6 +1,7 @@
import argparse import argparse
import util.store 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 <module>:<alias> to provide an alias in case you want to load the same module multiple times, but specify different parameters.' 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 <module>:<alias> 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 <module>.<key>=<value>' PARAMETER_HELP = 'Provide configuration parameters in the form of <module>.<key>=<value>'
@ -32,8 +33,8 @@ class Config(util.store.Store):
def modules(self): def modules(self):
return [item for sub in self._args.modules for item in sub] return [item for sub in self._args.modules for item in sub]
def interval(self): def interval(self, default=1):
return float(self.get('interval', 1)) return util.format.seconds(self.get('interval', default))
def debug(self): def debug(self):
return self._args.debug return self._args.debug

View file

@ -3,4 +3,4 @@
- theme.minwidth - theme.minwidth
- scrolling decorator - scrolling decorator
- theme.exclude - theme.exclude
- per module update interval - per module update interval -> nice string format

View file

@ -76,4 +76,16 @@ class format(unittest.TestCase):
self.assertEqual('00:20m', duration(20, unit=True)) self.assertEqual('00:20m', duration(20, unit=True))
self.assertEqual('00:20m', duration(20, compact=True, 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 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -1,3 +1,4 @@
import re
def asbool(val): def asbool(val):
if val is None: if val is None:
@ -15,7 +16,6 @@ def asint(val, minimum=None, maximum=None):
val = max(val, minimum if minimum else val) val = max(val, minimum if minimum else val)
return val return val
def aslist(val): def aslist(val):
if val is None: if val is None:
return [] return []
@ -30,6 +30,22 @@ def byte(val, fmt='{:.2f}'):
val /= 1024.0 val /= 1024.0
return '{}GiB'.format(fmt).format(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): def duration(duration, compact=False, unit=False):
duration = int(duration) duration = int(duration)
minutes, seconds = divmod(duration, 60) minutes, seconds = divmod(duration, 60)