use __import__() to dynamically load modules

This commit is contained in:
Mike Fährmann
2021-03-01 01:25:46 +01:00
parent 69ea781d32
commit 8821dceb79
5 changed files with 22 additions and 26 deletions

View File

@@ -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
# --------------------------------------------------------------------