improve downloader/postprocessor module loading
- handle arguments of any type without propagating an exception - prevent potential security risk through relative imports
This commit is contained in:
parent
712b58a93b
commit
e9ae6fd080
@ -1,11 +1,13 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2015 Mike Fährmann
|
||||
# Copyright 2015-2018 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
|
||||
# published by the Free Software Foundation.
|
||||
|
||||
"""Downloader modules"""
|
||||
|
||||
import importlib
|
||||
|
||||
|
||||
@ -14,13 +16,15 @@ def find(scheme):
|
||||
try:
|
||||
return _cache[scheme]
|
||||
except KeyError:
|
||||
klass = None
|
||||
try:
|
||||
module = importlib.import_module("."+scheme, __package__)
|
||||
klass = getattr(module, "Downloader")
|
||||
_cache[scheme] = klass
|
||||
return klass
|
||||
except ImportError:
|
||||
return None
|
||||
if "." not in scheme: # prevent relative imports
|
||||
module = importlib.import_module("." + scheme, __package__)
|
||||
klass = module.Downloader
|
||||
except (ImportError, AttributeError, TypeError):
|
||||
pass
|
||||
_cache[scheme] = klass
|
||||
return klass
|
||||
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
|
@ -6,6 +6,8 @@
|
||||
# it under the terms of the GNU General Public License version 2 as
|
||||
# published by the Free Software Foundation.
|
||||
|
||||
"""Post-processing modules"""
|
||||
|
||||
import importlib
|
||||
import logging
|
||||
|
||||
@ -17,13 +19,15 @@ def find(name):
|
||||
try:
|
||||
return _cache[name]
|
||||
except KeyError:
|
||||
klass = None
|
||||
try:
|
||||
module = importlib.import_module("."+name, __package__)
|
||||
cls = module.__postprocessor__
|
||||
_cache[name] = cls
|
||||
return cls
|
||||
except (ImportError, AttributeError):
|
||||
return None
|
||||
if "." not in name: # prevent relative imports
|
||||
module = importlib.import_module("." + name, __package__)
|
||||
klass = module.__postprocessor__
|
||||
except (ImportError, AttributeError, TypeError):
|
||||
pass
|
||||
_cache[name] = klass
|
||||
return klass
|
||||
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
|
Loading…
x
Reference in New Issue
Block a user