Skip to content

Commit 5ee8848

Browse files
committed
Allow different period while ON/INHIBIT checking
1 parent 5469779 commit 5ee8848

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ inhibit sleep or not until at least the next check period.
3131
The latest version of this document and code is available at
3232
https://github.com/bulletmark/sleep-inhibitor.
3333

34-
:warning: **Warning**: Unfortunately this program is currently
34+
:warning: **Warning**: Unfortunately this program is currently slightly
3535
handicapped due to [this systemd
3636
issue](https://github.com/systemd/systemd/issues/14812). Until this
37-
issue is addressed, your system may not automatically [re-]suspend after
38-
it has been inhibited, even though _sleep-inhibitor_ has removed the
39-
inhibit.
37+
issue is addressed, your system may not automatically [re-]suspend if
38+
still idle after it has been inhibited, even though _sleep-inhibitor_
39+
has removed the inhibit.
4040

4141
### Motivation
4242

sleep-inhibitor.conf

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,27 @@
55
# determined (e.g. at either /usr/share/sleep-inhibitor/plugins or
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.
8+
#
89
# plugin_dir:
910
#
10-
# Default global check period in minutes. Can be specified
11-
# for each plugin, or if not specified will the global default you
12-
# define here (or is 5 mins if not specified at all).
11+
# Default global check period in minutes. Can be specified for each
12+
# plugin, or if not specified will be the global default you define
13+
# here, or is 5 mins if not specified at all.
14+
#
1315
# period: 5
1416
#
17+
# Default global check period, while ON/INHIBITING, in minutes. Can be
18+
# specified for each plugin, or if not specified will be the global
19+
# default you define here, or is "period" above if not specified at all.
20+
#
21+
# period_on:
22+
#
1523
# Default global "what" value. Can be specified for each plugin, or if
1624
# not specified will the global default you define here. Takes a
1725
# colon-separated list of one or more operations to inhibit. See the
1826
# description of the --what option in the man page for systemd-inhibit.
1927
# Defaults to the default value of that systemd-inhibit option.
28+
#
2029
# what:
2130
#
2231
# Plugins are defined following. You can define as many plugins as you
@@ -29,11 +38,13 @@
2938
# plugins, or relative to the program distribution plugins/
3039
# directory for standard plugins.
3140
# args: Optional. Provides arguments to the above script if required.
32-
# period: Optional. Specifies period in minutes. Defaults to global
33-
# value, see above.
41+
# period: Optional. Specifies period to check in minutes. Defaults to
42+
# global value, see above.
43+
# period_on: Optional. Specifies period to check (while ON/INHIBITING)
44+
# in minutes. Defaults to above "period" value, then global value.
3445
# name: Optional. Descriptive name for logging. Defaults to basename of
3546
# path.
36-
# what: Optional. Takes a colon-seperated list of one or more
47+
# what: Optional. Takes a colon-separated list of one or more
3748
# operations to inhibit. Defaults to global value, see above.
3849

3950
# Comment/delete out or edit the following examples, or add your own.

sleep_inhibitor.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ class Plugin:
2727
loglock = threading.Lock()
2828
threads = []
2929

30-
def __init__(self, index, prog, progname, def_period, def_what,
31-
conf, plugin_dir, inhibitor_prog):
30+
def __init__(self, index, prog, progname, def_period, def_period_on,
31+
def_what, conf, plugin_dir, inhibitor_prog):
3232
'Constructor'
3333
pathstr = conf.get('path')
3434
if not pathstr:
3535
sys.exit(f'Plugin #{index}: path must be defined')
3636

3737
path = Path(pathstr)
38-
name = conf.get('name', path.name)
38+
name = conf.get('name', path.stem)
3939
self.name = f'Plugin {name}'
4040

4141
if not path.is_absolute():
@@ -49,7 +49,15 @@ def __init__(self, index, prog, progname, def_period, def_what,
4949
if not path.exists():
5050
sys.exit(f'{self.name}: "{path}" does not exist')
5151

52-
period = float(conf.get('period', def_period))
52+
per = conf.get('period')
53+
if per is None:
54+
period = def_period
55+
period_on_def = def_period_on
56+
else:
57+
period = float(per)
58+
period_on_def = period
59+
60+
period_on = float(conf.get('period_on', period_on_def))
5361
self.period = period * 60
5462
self.is_inhibiting = None
5563

@@ -68,9 +76,9 @@ def __init__(self, index, prog, progname, def_period, def_what,
6876
# run the plugin in a loop which keeps the inhibit on while the
6977
# inhibit state is returned.
7078
self.icmd = shlex.split(f'{inhibitor_prog}{what} --who="{progname}" '
71-
f'--why="{self.name}" {prog} -s {self.period} -i "{cmd}"')
79+
f'--why="{self.name}" {prog} -s {period_on * 60} -i "{cmd}"')
7280

73-
print(f'{self.name} [{path}] configured @ {period} minutes')
81+
print(f'{self.name} [{path}] configured @ {period}/{period_on} minutes')
7482

7583
# Each plugin periodic check runs in it's own thread
7684
thread = threading.Thread(target=self.run)
@@ -186,12 +194,13 @@ def init():
186194

187195
# Get some global defaults
188196
period = float(conf.get('period', 5))
197+
period_on = float(conf.get('period_on', period))
189198
what = conf.get('what')
190199

191200
# Iterate to create each configured plugins
192201
for index, plugin in enumerate(plugins, 1):
193-
Plugin(index, prog, progname, period, what, plugin, plugin_dir,
194-
inhibitor_prog)
202+
Plugin(index, prog, progname, period, period_on, what, plugin,
203+
plugin_dir, inhibitor_prog)
195204

196205
def main():
197206
'Main entry'

0 commit comments

Comments
 (0)