Beispiel #1
0
def _process_regexps(data):
    placeholders = {
        name[2:]: regex
        for name, regex in data.items() if name.startswith('__')
    }
    regexps = {}
    for name, regex in data.items():
        if name.startswith('__'):
            continue
        try:
            regex = combine_placeholders(regex, placeholders)
        except KeyError as exc:
            raise ConfigError(
                'placeholder does not exist: {}'.format(exc)) from exc
        try:
            compiled = re.compile(regex)
        except re.error as exc:
            raise ConfigError(
                'regex could not be compiled: {} ({})\n{}'.format(
                    name, exc, regex))
        if set(compiled.groupindex) < {'source', 'message'}:
            raise ConfigError(
                "regex must contain named groups 'source' and 'message': {}".
                format(name))
        regexps[name] = compiled
    return regexps
Beispiel #2
0
def _process_regexps(data):
    placeholders = {name[2:]: regex for name, regex in data.items() if name.startswith("__")}
    regexps = {}
    for name, regex in data.items():
        if name.startswith("__"):
            continue
        try:
            regex = combine_placeholders(regex, placeholders)
        except KeyError as exc:
            raise ConfigError("placeholder does not exist: {}".format(exc)) from exc
        try:
            compiled = re.compile(regex)
        except re.error as exc:
            raise ConfigError("regex could not be compiled: {} ({})\n{}".format(name, exc, regex))
        if set(compiled.groupindex) < {"source", "message"}:
            raise ConfigError("regex must contain named groups 'source' and 'message': {}".format(name))
        regexps[name] = compiled
    return regexps
Beispiel #3
0
def test_combine_placeholders_cycle():
    with pytest.raises(ValueError):
        util.combine_placeholders('test%(a)', {'a': '%(a)'})
    with pytest.raises(ValueError):
        util.combine_placeholders('test%(a)', {'a': '%(b)', 'b': '%(a)'})
Beispiel #4
0
def test_combine_placeholders():
    assert util.combine_placeholders('test', {'test': 'xxx'}) == 'test'
    assert util.combine_placeholders('test%(test)test', {'test': 'xxx'}) == 'testxxxtest'
    assert util.combine_placeholders('test%(a)%(b)', {'a': '1%(b)', 'b': '2'}) == 'test122'