precompile regular expressions

This commit is contained in:
Mike Fährmann
2016-08-23 16:36:39 +02:00
parent 57a616a36f
commit 6d401b1118

View File

@@ -64,7 +64,7 @@ modules = [
def find(url): def find(url):
"""Find suitable extractor for the given url""" """Find suitable extractor for the given url"""
for pattern, klass in _list_patterns(): for pattern, klass in _list_patterns():
match = re.match(pattern, url) match = pattern.match(url)
if match: if match:
return klass(match) return klass(match)
return None return None
@@ -84,19 +84,17 @@ _module_iter = iter(modules)
def _list_patterns(): def _list_patterns():
"""Yield all available (pattern, class) tuples""" """Yield all available (pattern, class) tuples"""
for entry in _cache: yield from _cache
yield entry
for module_name in _module_iter: for module_name in _module_iter:
module = importlib.import_module("."+module_name, __package__) module = importlib.import_module("."+module_name, __package__)
tuples = [ tuples = [
(pattern, klass) (re.compile(pattern), klass)
for klass in _get_classes(module) for klass in _get_classes(module)
for pattern in klass.pattern for pattern in klass.pattern
] ]
_cache.extend(tuples) _cache.extend(tuples)
for etuple in tuples: yield from tuples
yield etuple
def _get_classes(module): def _get_classes(module):
"""Return a list of all extractor classes in a module""" """Return a list of all extractor classes in a module"""