Skip to content

Commit 786dafb

Browse files
committed
Improve machinery to launch a notification service plugin standalone
The "--config" parameter optionally specifies a configuration file.
1 parent a586739 commit 786dafb

File tree

5 files changed

+41
-18
lines changed

5 files changed

+41
-18
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ in progress
88

99
- Rename repository default branch to "main"
1010
- Fix "http" service plugin
11+
- Improve machinery to launch a notification service plugin standalone.
12+
Now, it works without any ``mqttwarn.ini`` configuration file at all.
1113

1214
2021-06-12 0.24.0
1315
=================

README.rst

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,23 @@ To supply a different configuration file or log file, optionally use::
154154

155155
Running notification plugins
156156
============================
157-
For debugging or other purposes, you might want to directly run a notification plugin
158-
without the dispatching and transformation machinery of *mqttwarn*.
159-
We have you covered, just try this to launch the plugin standalone by passing essential information using JSON::
157+
For debugging, or other purposes, you might want to directly run an individual
158+
notification plugin without the dispatching and transformation machinery of
159+
*mqttwarn*.
160+
161+
We have you covered. To launch a plugin standalone, those commands will give
162+
you an idea how to pass relevant information on the command line using JSON::
160163

161164
# Launch "log" service plugin
162165
mqttwarn --plugin=log --data='{"message": "Hello world", "addrs": ["crit"]}'
163166

164167
# Launch "file" service plugin
165168
mqttwarn --plugin=file --data='{"message": "Hello world\n", "addrs": ["/tmp/mqttwarn.err"]}'
166169

170+
# Launch "pushover" service plugin
171+
mqttwarn --plugin=pushover --data='{"title": "About", "message": "Hello world", "addrs": ["userkey", "token"], "priority": 6}'
167172

168-
Please note this feature is a work in progress, so expect there to be dragons.
173+
The ``--config`` parameter can be used to optionally specify a configuration file.
169174

170175

171176
Running as system daemon

mqttwarn/commands.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from docopt import docopt
1212

1313
from mqttwarn import __version__
14-
from mqttwarn.configuration import load_configuration
14+
from mqttwarn.configuration import load_configuration, Config
1515
from mqttwarn.core import bootstrap, connect, cleanup, run_plugin
1616
from mqttwarn.util import get_resource_content
1717

@@ -25,7 +25,7 @@ def run():
2525
Usage:
2626
{program} [make-config]
2727
{program} [make-samplefuncs]
28-
{program} [--plugin=] [--data=]
28+
{program} [--config=] [--plugin=] [--data=]
2929
{program} --version
3030
{program} (-h | --help)
3131
@@ -68,21 +68,32 @@ def run():
6868
data = json.loads(options['--data'])
6969

7070
# Launch service plugin in standalone mode
71-
launch_plugin_standalone(plugin, data)
71+
launch_plugin_standalone(plugin, data, configfile=options.get("--config"))
7272

7373

7474
# Run mqttwarn in service mode when no command line arguments are given
7575
else:
7676
run_mqttwarn()
7777

7878

79-
def launch_plugin_standalone(plugin, data):
79+
def launch_plugin_standalone(plugin, data, configfile=None):
80+
8081
# Load configuration file
82+
does_not_exist = False
8183
scriptname = os.path.splitext(os.path.basename(sys.argv[0]))[0]
82-
config = load_configuration(name=scriptname)
84+
try:
85+
config = load_configuration(configfile=configfile, name=scriptname)
86+
except FileNotFoundError:
87+
does_not_exist = True
88+
config = Config()
89+
section = "config:{}".format(plugin)
90+
config.add_section(section)
8391

8492
# Setup logging
8593
setup_logging(config)
94+
if does_not_exist:
95+
logger.info('Configuration file "{}" does not exist, using default settings'.format(configfile))
96+
8697
logger.info('Running service plugin "{}" with data "{}"'.format(plugin, data))
8798

8899
# Launch service plugin

mqttwarn/configuration.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,19 @@ class Config(RawConfigParser):
2626
'NONE' : None,
2727
}
2828

29-
def __init__(self, configuration_file, defaults=None):
29+
def __init__(self, configuration_file=None, defaults=None):
3030

3131
defaults = defaults or {}
3232

33+
self.configuration_path = None
34+
3335
RawConfigParser.__init__(self)
34-
f = codecs.open(configuration_file, 'r', encoding='utf-8')
35-
self.read_file(f)
36-
f.close()
36+
if configuration_file is not None:
37+
f = codecs.open(configuration_file, 'r', encoding='utf-8')
38+
self.read_file(f)
39+
f.close()
3740

38-
self.configuration_path = os.path.dirname(configuration_file)
41+
self.configuration_path = os.path.dirname(configuration_file)
3942

4043
''' set defaults '''
4144
self.hostname = 'localhost'
@@ -48,8 +51,8 @@ def __init__(self, configuration_file, defaults=None):
4851
self.cleansession = False
4952
self.protocol = 3
5053

51-
self.logformat = '%(asctime)-15s %(levelname)-8s [%(name)-25s] %(message)s'
52-
self.logfile = None
54+
self.logformat = '%(asctime)-15s %(levelname)-8s [%(name)-26s] %(message)s'
55+
self.logfile = "stream://sys.stderr"
5356
self.loglevel = 'DEBUG'
5457

5558
self.functions = None
@@ -170,7 +173,7 @@ def config(self, section):
170173
Cannot use config.items() because I want each value to be
171174
retrieved with g() as above '''
172175

173-
d = None
176+
d = {}
174177
if self.has_section(section):
175178
d = dict((key, self.g(section, key))
176179
for (key) in self.options(section) if key not in ['targets', 'module'])
@@ -183,7 +186,7 @@ def load_configuration(configfile=None, name='mqttwarn'):
183186
configfile = os.getenv(name.upper() + 'INI', name + '.ini')
184187

185188
if not os.path.exists(configfile):
186-
raise ValueError('Configuration file "{}" does not exist'.format(configfile))
189+
raise FileNotFoundError('Configuration file "{}" does not exist'.format(configfile))
187190

188191
defaults = {
189192
'clientid': name,

mqttwarn/core.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,3 +751,5 @@ def run_plugin(config=None, name=None, data=None):
751751
module = service_plugins[name]['module']
752752
response = module.plugin(srv, item)
753753
logger.info('Plugin response: {}'.format(response))
754+
if response is False:
755+
sys.exit(1)

0 commit comments

Comments
 (0)