Beispiel #1
0
def xform(function, orig_value, transform_data):
    ''' Attempt transformation on orig_value.
        1st. function()
        2nd. inline {xxxx}
        '''

    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 = cf.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).encode('utf-8')
        except Exception as e:
            logger.warning("Cannot format message: %s" % e)

    if isinstance(res, str):
        res = res.replace("\\n", "\n")
    return res
Beispiel #2
0
def xform(function, orig_value, transform_data):
    """
    Attempt transformation on orig_value.

    - 1st. function()
    - 2nd. inline {xxxx}
    """

    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
Beispiel #3
0
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"}'
Beispiel #4
0
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"}'
Beispiel #5
0
def test_formatter_basic():
    result = Formatter().format("{foo}", **{u'foo': u'Räuber Hotzenplotz'}).encode('utf-8')
    assert result == b'R\xc3\xa4uber Hotzenplotz'
Beispiel #6
0
def test_formatter_basic():
    result = Formatter().format("{foo}", **{
        u"foo": u"Räuber Hotzenplotz"
    }).encode("utf-8")
    assert result == b"R\xc3\xa4uber Hotzenplotz"