Skip to content

Commit eff3fc7

Browse files
committed
Allow periods to be specified in seconds or hours
1 parent 5ee8848 commit eff3fc7

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
setup(
1313
name=name,
14-
version='1.15',
14+
version='1.16',
1515
description='Program to run plugins to inhibit system '
1616
'sleep/suspend/hibernate',
1717
long_description=here.joinpath('README.md').read_text(),

sleep-inhibitor.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
# /usr/local/share/sleep-inhibitor/plugins) but you can define it
77
# explicitly here if you want, e.g. for custom config file for testing.
88
#
9+
# Note period values in this file are specified in minutes by default.
10+
# E.g. "2" is 2 minutes. However, you can append an "s", "m", or "h" to
11+
# specify in seconds, minutes, or hours, e.g. "2s" is 2 seconds, "2m" is
12+
# 2 minutes (i.e. same as "2"), or "2h" is 2 hours.
13+
#
914
# plugin_dir:
1015
#
1116
# Default global check period in minutes. Can be specified for each

sleep_inhibitor.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@
2222
'systemd-inhibit',
2323
)
2424

25+
def gettime(conf, field, default=None):
26+
'Read time value from given conf.'
27+
val = conf.get(field, default)
28+
if val is None:
29+
return None
30+
31+
if isinstance(val, str):
32+
if val.endswith('s'):
33+
num = float(val[:-1]) / 60
34+
elif val.endswith('m'):
35+
num = float(val[:-1])
36+
elif val.endswith('h'):
37+
num = float(val[:-1]) * 60
38+
else:
39+
sys.exit(f'Invalid time value "{field}: {val}".')
40+
else:
41+
num = float(val)
42+
43+
return num
44+
2545
class Plugin:
2646
'Class to manage each plugin'
2747
loglock = threading.Lock()
@@ -49,15 +69,14 @@ def __init__(self, index, prog, progname, def_period, def_period_on,
4969
if not path.exists():
5070
sys.exit(f'{self.name}: "{path}" does not exist')
5171

52-
per = conf.get('period')
53-
if per is None:
72+
period = gettime(conf, 'period')
73+
if period is None:
5474
period = def_period
5575
period_on_def = def_period_on
5676
else:
57-
period = float(per)
5877
period_on_def = period
5978

60-
period_on = float(conf.get('period_on', period_on_def))
79+
period_on = gettime(conf, 'period_on', period_on_def)
6180
self.period = period * 60
6281
self.is_inhibiting = None
6382

@@ -78,7 +97,9 @@ def __init__(self, index, prog, progname, def_period, def_period_on,
7897
self.icmd = shlex.split(f'{inhibitor_prog}{what} --who="{progname}" '
7998
f'--why="{self.name}" {prog} -s {period_on * 60} -i "{cmd}"')
8099

81-
print(f'{self.name} [{path}] configured @ {period}/{period_on} minutes')
100+
per = round(period, 3)
101+
per_on = round(period_on, 3)
102+
print(f'{self.name} [{path}] configured @ {per}/{per_on} minutes')
82103

83104
# Each plugin periodic check runs in it's own thread
84105
thread = threading.Thread(target=self.run)
@@ -193,8 +214,8 @@ def init():
193214
plugin_dir = args.plugin_dir or conf.get('plugin_dir', plugin_dir)
194215

195216
# Get some global defaults
196-
period = float(conf.get('period', 5))
197-
period_on = float(conf.get('period_on', period))
217+
period = gettime(conf, 'period', 5)
218+
period_on = gettime(conf, 'period_on', period)
198219
what = conf.get('what')
199220

200221
# Iterate to create each configured plugins

0 commit comments

Comments
 (0)