Skip to content

Commit ebf787b

Browse files
committed
Improve launching service plugins interactively from the command line
Some service plugins need section-wide top-level configuration settings. Now, there are two options "--config" and "--options" to be able to obtain all relevant bits of information from the command line.
1 parent 3d86589 commit ebf787b

File tree

6 files changed

+54
-22
lines changed

6 files changed

+54
-22
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ in progress
1212
- Use STDERR as default log target
1313
- Stop including the "tests" folder into the sdist package
1414
- Add "mqttwarn-contrib" package to the list of "extra" dependencies
15+
- Improve launching service plugins interactively from the command line
16+
Now, there are two options "--config" and "--options" to be able to
17+
obtain all relevant bits of information from the command line.
1518

1619

1720
2021-06-18 0.25.0

HANDBOOK.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,9 +638,10 @@ targets = {
638638
'speaker' : ['Living Room'],
639639
}
640640
641+
# Command line test
642+
# mqttwarn --plugin=chromecast --options='{"message": "Hello world", "addrs": ["Living Room"]}'
641643
# echo 'Hello world' | mosquitto_pub -t 'chromecast/say' -l
642644
# echo '{"message": "Hello world", "addrs": ["Living Room"]}' | mosquitto_pub -t 'chromecast/say' -l
643-
# command line test; mqttwarn --plugin=chromecast --data='{"message": "Hello world", "addrs": ["Living Room"]}'
644645
[chromecast/say]
645646
targets = chromecast:speaker
646647

README.rst

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,20 @@ We have you covered. To launch a plugin standalone, those commands will give
162162
you an idea how to pass relevant information on the command line using JSON::
163163

164164
# Launch "log" service plugin
165-
mqttwarn --plugin=log --data='{"message": "Hello world", "addrs": ["crit"]}'
165+
mqttwarn --plugin=log --options='{"message": "Hello world", "addrs": ["crit"]}'
166166

167167
# Launch "file" service plugin
168-
mqttwarn --plugin=file --data='{"message": "Hello world\n", "addrs": ["/tmp/mqttwarn.err"]}'
168+
mqttwarn --plugin=file --options='{"message": "Hello world\n", "addrs": ["/tmp/mqttwarn.err"]}'
169169

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

173-
The ``--config`` parameter can be used to optionally specify a configuration file.
173+
# Launch "cloudflare_zone" service plugin from "mqttwarn-contrib", passing "--config" parameters via command line
174+
mqttwarn --plugin=mqttwarn_contrib.services.cloudflare_zone --config='{"auth-email": "foo", "auth-key": "bar"}' --options='{"addrs": ["0815", "www.example.org", ""], "message": "192.168.0.1"}'
175+
176+
177+
Also, the ``--config-file`` parameter can be used to optionally specify the
178+
path to a configuration file.
174179

175180

176181
Running as system daemon

doc/backlog.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Goals for 0.21.0
5050
Goals for 1.0.0
5151
***************
5252
- [o] Make mqttwarn completely unicode-safe
53-
- [o] Make "mqttwarn --plugin=log --data=" obtain JSON data from STDIN
53+
- [o] Make "mqttwarn --plugin=log --options=" obtain JSON data from STDIN
5454
- [o] Translate documentation into reStructuredText format,
5555
render it using Sphinx and optionally publish to readthedocs.org.
5656
- [o] Add support for Python 3

mqttwarn/commands.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,27 @@ def run():
2525
Usage:
2626
{program} [make-config]
2727
{program} [make-samplefuncs]
28-
{program} [--config=] [--plugin=] [--data=]
28+
{program} [--config=] [--config-file=] [--plugin=] [--options=]
2929
{program} --version
3030
{program} (-h | --help)
3131
32-
Configuration file options:
33-
make-config Will dump configuration file content to STDOUT,
32+
No options:
33+
mqttwarn will start as a service.
34+
35+
Interactive options:
36+
[--config=] Use configuration settings from JSON string
37+
[--config-file=] Use configuration settings from JSON file
38+
[--plugin=] The plugin name to load. This can either be a
39+
full qualified Python package/module name or a
40+
path to a Python file.
41+
[--options=] Configuration options to propagate to the plugin
42+
entrypoint.
43+
44+
Bootstrapping options:
45+
make-config Dump configuration file blueprint to STDOUT,
3446
suitable for redirecting into a configuration file.
47+
make-samplefuncs Dump blueprint for custom functions file to STDOUT,
48+
suitable for redirecting into a `samplefuncs.py` file.
3549
3650
Miscellaneous options:
3751
--version Show version information
@@ -61,24 +75,27 @@ def run():
6175
payload = get_resource_content('mqttwarn.examples', 'basic/samplefuncs.py')
6276
print(payload)
6377

64-
elif options['--plugin'] and options['--data']:
78+
elif options['--plugin'] and options['--options']:
6579

6680
# Decode arguments
67-
plugin = options['--plugin']
68-
data = json.loads(options['--data'])
81+
arg_plugin = options['--plugin']
82+
arg_options = json.loads(options['--options'])
83+
arg_config = None
84+
if "--config" in options and options['--config'] is not None:
85+
arg_config = json.loads(options['--config'])
6986

7087
# Launch service plugin in standalone mode
71-
launch_plugin_standalone(plugin, data, configfile=options.get("--config"))
88+
launch_plugin_standalone(arg_plugin, arg_options, configfile=options.get("--config-file"), config_more=arg_config)
7289

7390

7491
# Run mqttwarn in service mode when no command line arguments are given
7592
else:
7693
run_mqttwarn()
7794

7895

79-
def launch_plugin_standalone(plugin, data, configfile=None):
96+
def launch_plugin_standalone(plugin, options, configfile=None, config_more=None):
8097

81-
# Load configuration file
98+
# Optionally load configuration file
8299
does_not_exist = False
83100
scriptname = os.path.splitext(os.path.basename(sys.argv[0]))[0]
84101
try:
@@ -89,15 +106,21 @@ def launch_plugin_standalone(plugin, data, configfile=None):
89106
section = "config:{}".format(plugin)
90107
config.add_section(section)
91108

109+
# Optionally add additional config settings from command line.
110+
if config_more is not None:
111+
section = "config:{}".format(plugin)
112+
for key, value in config_more.items():
113+
config.set(section, key, value)
114+
92115
# Setup logging
93116
setup_logging(config)
94117
if does_not_exist:
95118
logger.info('Configuration file "{}" does not exist, using default settings'.format(configfile))
96119

97-
logger.info('Running service plugin "{}" with data "{}"'.format(plugin, data))
120+
logger.info('Running service plugin "{}" with options "{}"'.format(plugin, options))
98121

99122
# Launch service plugin
100-
run_plugin(config=config, name=plugin, data=data)
123+
run_plugin(config=config, name=plugin, options=options)
101124

102125

103126
def run_mqttwarn():

mqttwarn/core.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ def bootstrap(config=None, scriptname=None):
713713
SCRIPTNAME = scriptname
714714

715715

716-
def run_plugin(config=None, name=None, data=None):
716+
def run_plugin(config=None, name=None, options=None):
717717
"""
718718
Run service plugins directly without the
719719
dispatching and transformation machinery.
@@ -723,9 +723,9 @@ def run_plugin(config=None, name=None, data=None):
723723
the innards of mqttwarn interact so it might also please
724724
newcomers as a "learning the guts of mqttwarn" example.
725725
726-
:param config: The configuration object
727-
:param name: The name of the service plugin, e.g. "log" or "file"
728-
:param data: The data to be converged into an appropriate item Struct object instance
726+
:param config: The configuration object
727+
:param name: The name of the service plugin, e.g. "log" or "file"
728+
:param options: The data to be converged into an appropriate item Struct object instance
729729
"""
730730

731731
# Bootstrap mqttwarn core
@@ -741,7 +741,7 @@ def run_plugin(config=None, name=None, data=None):
741741
srv = make_service(mqttc=None, name=service_logger_name)
742742

743743
# Build a mimikry item instance for feeding to the service plugin
744-
item = Struct(**data)
744+
item = Struct(**options)
745745
# TODO: Read configuration optionally from data.
746746
item.config = config.config('config:' + name)
747747
item.service = srv

0 commit comments

Comments
 (0)