1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-25 12:12:34 +01:00

implement alternative constructor for extractors

This commit is contained in:
Mike Fährmann 2019-02-09 14:39:38 +01:00
parent abbd45d0f4
commit 9a9cd32461
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
2 changed files with 29 additions and 9 deletions

View File

@ -41,6 +41,13 @@ class Extractor():
self._timeout = self.config("timeout", 30)
self._verify = self.config("verify", True)
@classmethod
def from_url(cls, url):
if isinstance(cls.pattern, str):
cls.pattern = re.compile(cls.pattern)
match = cls.pattern.match(url)
return cls(match) if match else None
def __iter__(self):
return self.items()

View File

@ -30,21 +30,20 @@ class FakeExtractor(Extractor):
class TestExtractor(unittest.TestCase):
VALID_URIS = (
"https://example.org/file.jpg",
"tumblr:foobar",
"oauth:flickr",
"test:pixiv:",
"recursive:https://example.org/document.html",
)
def setUp(self):
extractor._cache.clear()
extractor._module_iter = iter(extractor.modules)
def test_find(self):
valid_uris = (
"https://example.org/file.jpg",
"tumblr:foobar",
"oauth:flickr",
"test:pixiv:",
"recursive:https://example.org/document.html",
)
for uri in valid_uris:
for uri in self.VALID_URIS:
result = extractor.find(uri)
self.assertIsInstance(result, Extractor, uri)
@ -96,6 +95,20 @@ class TestExtractor(unittest.TestCase):
self.assertIsNone(extractor.find(test_uri))
self.assertIsNone(extractor.find(fake_uri))
def test_from_url(self):
for uri in self.VALID_URIS:
cls = extractor.find(uri).__class__
extr = cls.from_url(uri)
self.assertIs(type(extr), cls)
self.assertIsInstance(extr, Extractor)
for not_found in ("", "/tmp/file.ext"):
self.assertIsNone(FakeExtractor.from_url(not_found))
for invalid in (None, [], {}, 123, b"test:"):
with self.assertRaises(TypeError):
FakeExtractor.from_url(invalid)
def test_unique_pattern_matches(self):
test_urls = []