Compile the regex's used in ASes

pull/4/merge
Erik Johnston 8 years ago
parent dc56a6b8c8
commit 650f0e69f2
  1. 14
      synapse/appservice/__init__.py
  2. 4
      tests/appservice/test_appservice.py

@ -124,22 +124,18 @@ class ApplicationService(object):
raise ValueError( raise ValueError(
"Expected bool for 'exclusive' in ns '%s'" % ns "Expected bool for 'exclusive' in ns '%s'" % ns
) )
if not isinstance(regex_obj.get("regex"), basestring): regex = regex_obj.get("regex")
if isinstance(regex, basestring):
regex_obj["regex"] = re.compile(regex)
else:
raise ValueError( raise ValueError(
"Expected string for 'regex' in ns '%s'" % ns "Expected string for 'regex' in ns '%s'" % ns
) )
return namespaces return namespaces
def _matches_regex(self, test_string, namespace_key, return_obj=False): def _matches_regex(self, test_string, namespace_key, return_obj=False):
if not isinstance(test_string, basestring):
logger.error(
"Expected a string to test regex against, but got %s",
test_string
)
return False
for regex_obj in self.namespaces[namespace_key]: for regex_obj in self.namespaces[namespace_key]:
if re.match(regex_obj["regex"], test_string): if regex_obj["regex"].match(test_string):
if return_obj: if return_obj:
return regex_obj return regex_obj
return True return True

@ -19,10 +19,12 @@ from twisted.internet import defer
from mock import Mock from mock import Mock
from tests import unittest from tests import unittest
import re
def _regex(regex, exclusive=True): def _regex(regex, exclusive=True):
return { return {
"regex": regex, "regex": re.compile(regex),
"exclusive": exclusive "exclusive": exclusive
} }

Loading…
Cancel
Save