22
22
'systemd-inhibit' ,
23
23
)
24
24
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
+
25
45
class Plugin :
26
46
'Class to manage each plugin'
27
47
loglock = threading .Lock ()
@@ -49,15 +69,14 @@ def __init__(self, index, prog, progname, def_period, def_period_on,
49
69
if not path .exists ():
50
70
sys .exit (f'{ self .name } : "{ path } " does not exist' )
51
71
52
- per = conf . get ( 'period' )
53
- if per is None :
72
+ period = gettime ( conf , 'period' )
73
+ if period is None :
54
74
period = def_period
55
75
period_on_def = def_period_on
56
76
else :
57
- period = float (per )
58
77
period_on_def = period
59
78
60
- period_on = float (conf . get ( 'period_on' , period_on_def ) )
79
+ period_on = gettime (conf , 'period_on' , period_on_def )
61
80
self .period = period * 60
62
81
self .is_inhibiting = None
63
82
@@ -78,7 +97,9 @@ def __init__(self, index, prog, progname, def_period, def_period_on,
78
97
self .icmd = shlex .split (f'{ inhibitor_prog } { what } --who="{ progname } " '
79
98
f'--why="{ self .name } " { prog } -s { period_on * 60 } -i "{ cmd } "' )
80
99
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' )
82
103
83
104
# Each plugin periodic check runs in it's own thread
84
105
thread = threading .Thread (target = self .run )
@@ -193,8 +214,8 @@ def init():
193
214
plugin_dir = args .plugin_dir or conf .get ('plugin_dir' , plugin_dir )
194
215
195
216
# 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 )
198
219
what = conf .get ('what' )
199
220
200
221
# Iterate to create each configured plugins
0 commit comments