How to use the mqttwarn.util.Formatter function in mqttwarn

To help you get started, we’ve selected a few mqttwarn examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jpmens / mqttwarn / tests / test_util.py View on Github external
def test_formatter_json():
    result = Formatter().format("{foo!j}", **{u'foo': {u'bar': u'Räuber Hotzenplotz'}}).encode('utf-8')
    assert result == b'{"bar": "R\\u00e4uber Hotzenplotz"}'
github jpmens / mqttwarn / tests / test_util.py View on Github external
def test_formatter_basic():
    result = Formatter().format("{foo}", **{u'foo': u'Räuber Hotzenplotz'}).encode('utf-8')
    assert result == b'R\xc3\xa4uber Hotzenplotz'
github jpmens / mqttwarn / mqttwarn / core.py View on Github external
if orig_value is None:
        return None

    res = orig_value

    if function is not None:
        function_name = sanitize_function_name(function)
        if function_name is not None:
            try:
                res = context.invoker.datamap(function_name, transform_data)
                return res
            except Exception as e:
                logger.warning("Cannot invoke %s(): %s" % (function_name, e))

        try:
            res = Formatter().format(function, **transform_data)
        except Exception as e:
            logger.exception("Formatting message failed")

    if isinstance(res, str):
        res = res.replace("\\n", "\n")

    return res