Skip to content

Commit a586739

Browse files
committed
Docs: Improve documentation about how relative file paths are resolved
Both when loading external service plugins and when loading custom functions, relative paths are resolved from the folder of the "mqttwarn.ini" configuration file.
1 parent 065002f commit a586739

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

HANDBOOK.md

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3063,13 +3063,22 @@ Below are a number of example scenarios where custom functions are being used.
30633063
Consider the following configuration snippet in addition to the configuration
30643064
of the `mqtt` service shown above:
30653065
3066+
Add this to a custom Python file, like, e.g. `/etc/mqttwarn/myfunctions.py`.
30663067
```python
30673068
def lookup_data(data, srv=None):
30683069
if type(data) == dict and 'fruit' in data:
30693070
return "Ananas"
30703071
return None
30713072
```
30723073
3074+
Register that file by saying:
3075+
3076+
```ini
3077+
[defaults]
3078+
; path to file containing self-defined functions for formatmap and datamap
3079+
functions = 'myfunctions.py'
3080+
```
3081+
30733082
Then, in the section defining the topic we listen on:
30743083
30753084
```ini
@@ -3079,17 +3088,24 @@ Then, in the section defining the topic we listen on:
30793088
format = lookup_data()
30803089
```
30813090
3082-
We've replaced the `formatmap` entry for the topic by a function which you
3083-
define within the _functions_ file you configure as `functions` in `mqttwarn.ini` configuration file. These functions
3084-
are invoked with decoded JSON `data` passed to them as a _dict_. The string returned
3085-
by the function returned string replaces the outgoing `message`:
3091+
Here, we replaced the `formatmap` entry for the topic by a custom function.
3092+
3093+
These functions are invoked with decoded JSON `data` passed to them as a
3094+
_dict_. The string returned by the function returned string replaces the
3095+
outgoing `message`:
30863096
30873097
```
30883098
in/a1 {"fruit":"pineapple", "price": 131, "tst" : "1391779336"}
30893099
out/food Ananas
30903100
out/fruit/pineapple Ananas
30913101
```
30923102
3103+
You can define custom functions in a Python file which you configure as
3104+
`functions` in the `[default]` section of the `mqttwarn.ini` configuration
3105+
file, as outlined above. When relative file names are given, they will be
3106+
resolved from the directory of the `mqttwarn.ini` file, which is, by default,
3107+
the `/etc/mqttwarn` folder.
3108+
30933109
If a function operating on a message (i.e. within `format =`) returns `None` or an empty string, the target notification is suppressed.
30943110
30953111
The optional `srv` is an object with some helper functions. In particular, these allow us to use _mqttwarn_'s logging and MQTT publish functions, as in this example:
@@ -3412,7 +3428,12 @@ plugins, there are two options.
34123428
### Service plugin from package
34133429
34143430
This configuration snippet outlines how to load a custom plugin from a Python
3415-
module referenced in "dotted" notation.
3431+
module referenced in "dotted" notation. Modules will be searched for in all
3432+
directories listed in [`sys.path`]. Custom directories can be added by using the
3433+
[`PYTHONPATH`] environment variable.
3434+
3435+
[`sys.path`]: https://docs.python.org/3/library/sys.html#sys.path
3436+
[`PYTHONPATH`]: https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPATH
34163437
34173438
```ini
34183439
[defaults]
@@ -3433,7 +3454,9 @@ targets = {
34333454
### Service plugin from file
34343455
34353456
This configuration snippet outlines how to load a custom plugin from a Python
3436-
file referenced by file name.
3457+
file referenced by file name. When relative file names are given, they will be
3458+
resolved from the directory of the `mqttwarn.ini` file, which is, by default,
3459+
the `/etc/mqttwarn` folder.
34373460
34383461
```ini
34393462
[defaults]

0 commit comments

Comments
 (0)