From 8821dceb791957a7c353bfdd850874ee94f7f9ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Mon, 1 Mar 2021 01:25:46 +0100 Subject: [PATCH] use __import__() to dynamically load modules --- gallery_dl/downloader/__init__.py | 16 +++++++--------- gallery_dl/extractor/__init__.py | 6 +++--- gallery_dl/postprocessor/__init__.py | 14 ++++++-------- test/test_downloader.py | 8 ++++---- test/test_postprocessor.py | 4 ++-- 5 files changed, 22 insertions(+), 26 deletions(-) diff --git a/gallery_dl/downloader/__init__.py b/gallery_dl/downloader/__init__.py index 6fb09e1b..e1b936e4 100644 --- a/gallery_dl/downloader/__init__.py +++ b/gallery_dl/downloader/__init__.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2015-2019 Mike Fährmann +# Copyright 2015-2021 Mike Fährmann # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as @@ -8,8 +8,6 @@ """Downloader modules""" -import importlib - modules = [ "http", "text", @@ -24,22 +22,22 @@ def find(scheme): except KeyError: pass - klass = None + cls = None if scheme == "https": scheme = "http" if scheme in modules: # prevent unwanted imports try: - module = importlib.import_module("." + scheme, __package__) + module = __import__(scheme, globals(), None, (), 1) except ImportError: pass else: - klass = module.__downloader__ + cls = module.__downloader__ if scheme == "http": - _cache["http"] = _cache["https"] = klass + _cache["http"] = _cache["https"] = cls else: - _cache[scheme] = klass - return klass + _cache[scheme] = cls + return cls # -------------------------------------------------------------------- diff --git a/gallery_dl/extractor/__init__.py b/gallery_dl/extractor/__init__.py index 476cbdd3..62233940 100644 --- a/gallery_dl/extractor/__init__.py +++ b/gallery_dl/extractor/__init__.py @@ -7,7 +7,6 @@ # published by the Free Software Foundation. import re -import importlib modules = [ "2chan", @@ -185,11 +184,12 @@ def _list_classes(): """Yield all available extractor classes""" yield from _cache + globals_ = globals() for module_name in _module_iter: - module = importlib.import_module("."+module_name, __package__) + module = __import__(module_name, globals_, None, (), 1) yield from add_module(module) - globals()["_list_classes"] = lambda : _cache + globals_["_list_classes"] = lambda : _cache def _get_classes(module): diff --git a/gallery_dl/postprocessor/__init__.py b/gallery_dl/postprocessor/__init__.py index faa4d6c9..ee490e79 100644 --- a/gallery_dl/postprocessor/__init__.py +++ b/gallery_dl/postprocessor/__init__.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2018-2020 Mike Fährmann +# Copyright 2018-2021 Mike Fährmann # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as @@ -8,8 +8,6 @@ """Post-processing modules""" -import importlib - modules = [ "classify", "compare", @@ -28,16 +26,16 @@ def find(name): except KeyError: pass - klass = None + cls = None if name in modules: # prevent unwanted imports try: - module = importlib.import_module("." + name, __package__) + module = __import__(name, globals(), None, (), 1) except ImportError: pass else: - klass = module.__postprocessor__ - _cache[name] = klass - return klass + cls = module.__postprocessor__ + _cache[name] = cls + return cls # -------------------------------------------------------------------- diff --git a/test/test_downloader.py b/test/test_downloader.py index 99cfb629..42b5c727 100644 --- a/test/test_downloader.py +++ b/test/test_downloader.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -# Copyright 2018-2020 Mike Fährmann +# Copyright 2018-2021 Mike Fährmann # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as @@ -74,7 +74,7 @@ class TestDownloaderModule(unittest.TestCase): self.assertEqual(downloader.find(1234) , None) self.assertEqual(downloader.find(None) , None) - @patch("importlib.import_module") + @patch("builtins.__import__") def test_cache(self, import_module): import_module.return_value = MockDownloaderModule() downloader.find("http") @@ -86,14 +86,14 @@ class TestDownloaderModule(unittest.TestCase): downloader.find("ytdl") self.assertEqual(import_module.call_count, 3) - @patch("importlib.import_module") + @patch("builtins.__import__") def test_cache_http(self, import_module): import_module.return_value = MockDownloaderModule() downloader.find("http") downloader.find("https") self.assertEqual(import_module.call_count, 1) - @patch("importlib.import_module") + @patch("builtins.__import__") def test_cache_https(self, import_module): import_module.return_value = MockDownloaderModule() downloader.find("https") diff --git a/test/test_postprocessor.py b/test/test_postprocessor.py index 4e98a97c..6bf887c4 100644 --- a/test/test_postprocessor.py +++ b/test/test_postprocessor.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -# Copyright 2019-2020 Mike Fährmann +# Copyright 2019-2021 Mike Fährmann # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as @@ -53,7 +53,7 @@ class TestPostprocessorModule(unittest.TestCase): self.assertEqual(postprocessor.find(1234) , None) self.assertEqual(postprocessor.find(None) , None) - @patch("importlib.import_module") + @patch("builtins.__import__") def test_cache(self, import_module): import_module.return_value = MockPostprocessorModule()