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
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
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)'})
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'