Skip to content

Commit c61fb94

Browse files
committed
Improve time string parsing
1 parent fcd9294 commit c61fb94

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

sleep_inhibitor.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,29 @@
2121
'systemd-inhibit',
2222
)
2323

24+
TIMEMULTS = {'s': 1, 'm': 60, 'h': 3600}
25+
2426
def conv_to_secs(val):
2527
'Convert given time string to float seconds'
26-
if isinstance(val, str):
27-
if val.endswith('s'):
28-
num = float(val[:-1])
29-
elif val.endswith('m'):
30-
num = float(val[:-1]) * 60
31-
elif val.endswith('h'):
32-
num = float(val[:-1]) * 60 * 60
33-
else:
34-
sys.exit(f'Invalid time string "{val}".')
28+
# Default input time value (without qualifier) is minutes
29+
mult = isinstance(val, str) and TIMEMULTS.get(val[-1])
30+
if mult:
31+
valf = val[:-1]
3532
else:
36-
# Default time entry is minutes
37-
num = float(val) * 60
33+
mult = 60
34+
valf = val
35+
36+
try:
37+
valf = float(valf)
38+
except Exception:
39+
sys.exit(f'Invalid time string "{val}".')
3840

39-
return num
41+
return valf * mult
4042

4143
class Plugin:
4244
'Class to manage each plugin'
4345
def __init__(self, index, prog, progname, period, period_on,
44-
def_what, conf, plugin_dir, inhibitor_prog):
46+
def_what, conf, plugin_dir, inhibitor_prog):
4547
'Constructor'
4648
pathstr = conf.get('path')
4749
if not pathstr:

0 commit comments

Comments
 (0)