Skip to content

Commit 4e47ac9

Browse files
committed
Can globally specify default period and what value
1 parent 29c9607 commit 4e47ac9

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

sleep-inhibitor.conf

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77
# explicitly here if you want, e.g. for custom config file for testing.
88
# plugin_dir:
99
#
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).
13+
# period: 5
14+
#
15+
# Default global "what" value. Can be specified for each plugin, or if
16+
# not specified will the global default you define here. Takes a
17+
# colon-separated list of one or more operations to inhibit. See the
18+
# description of the --what option in the man page for systemd-inhibit.
19+
# Defaults to the default value of that systemd-inhibit option.
20+
# what:
21+
#
1022
# Plugins are defined following. You can define as many plugins as you
1123
# require. Custom plugins are specified with an absolute path to your
1224
# own executable file. Standard plugins (i.e. those included in the
@@ -17,13 +29,12 @@
1729
# plugins, or relative to the program distribution plugins/
1830
# directory for standard plugins.
1931
# args: Optional. Provides arguments to the above script if required.
20-
# period: Optional. Specifies period in minutes. Defaults to 5.
32+
# period: Optional. Specifies period in minutes. Defaults to global
33+
# value, see above.
2134
# name: Optional. Descriptive name for logging. Defaults to basename of
2235
# path.
2336
# what: Optional. Takes a colon-seperated list of one or more
24-
# operations to inhibit. See the description of the --what
25-
# option in the man page for systemd-inhibit. Defaults to the
26-
# default value of that systemd-inhibit option.
37+
# operations to inhibit. Defaults to global value, see above.
2738

2839
# Comment/delete out or edit the following examples, or add your own.
2940
plugins:

sleep_inhibitor.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
# Return code which indicates plugin wants to inhibit suspend
1616
SUSP_CODE = 254
1717

18-
# Default check period in minutes if not specified for plugin in config
19-
DEF_PERIOD = 5
20-
2118
# On some non-systemd systems, systemd-inhibit emulators are used
2219
# instead. The first found below is the one we use:
2320
SYSTEMD_SLEEP_PROGS = (
@@ -30,7 +27,8 @@ class Plugin:
3027
loglock = threading.Lock()
3128
threads = []
3229

33-
def __init__(self, index, prog, progname, conf, plugin_dir, inhibitor_prog):
30+
def __init__(self, index, prog, progname, def_period, def_what,
31+
conf, plugin_dir, inhibitor_prog):
3432
'Constructor'
3533
pathstr = conf.get('path')
3634
if not pathstr:
@@ -51,7 +49,7 @@ def __init__(self, index, prog, progname, conf, plugin_dir, inhibitor_prog):
5149
if not path.exists():
5250
sys.exit(f'{self.name}: "{path}" does not exist')
5351

54-
period = float(conf.get('period', DEF_PERIOD))
52+
period = float(conf.get('period', def_period))
5553
self.period = period * 60
5654
self.is_inhibiting = None
5755

@@ -63,7 +61,7 @@ def __init__(self, index, prog, progname, conf, plugin_dir, inhibitor_prog):
6361
# The normal periodic check command
6462
self.cmd = shlex.split(cmd)
6563

66-
whatval = conf.get('what')
64+
whatval = conf.get('what', def_what)
6765
what = f' --what="{whatval}"' if whatval else ''
6866

6967
# While inhibiting, we run outself again via systemd-inhibit to
@@ -111,7 +109,6 @@ def log(cls, msg):
111109

112110
def init():
113111
'Program initialisation'
114-
115112
# Process command line options
116113
opt = argparse.ArgumentParser(description=__doc__.strip())
117114
opt.add_argument('-c', '--config',
@@ -187,9 +184,14 @@ def init():
187184
# Work out plugin dir
188185
plugin_dir = args.plugin_dir or conf.get('plugin_dir', plugin_dir)
189186

187+
# Get some global defaults
188+
period = float(conf.get('period', 5))
189+
what = conf.get('what')
190+
190191
# Iterate to create each configured plugins
191192
for index, plugin in enumerate(plugins, 1):
192-
Plugin(index, prog, progname, plugin, plugin_dir, inhibitor_prog)
193+
Plugin(index, prog, progname, period, what, plugin, plugin_dir,
194+
inhibitor_prog)
193195

194196
def main():
195197
'Main entry'

0 commit comments

Comments
 (0)