mirror of https://github.com/scrapy/scrapy.git
parent
27f5f35134
commit
c7f78a8305
|
|
@ -11,7 +11,7 @@ from zope.interface.verify import verifyObject
|
|||
|
||||
from scrapy.interfaces import IAddon
|
||||
from scrapy.utils.conf import build_component_list
|
||||
from scrapy.utils.misc import load_object
|
||||
from scrapy.utils.misc import load_module_or_object
|
||||
|
||||
|
||||
@zope.interface.implementer(IAddon)
|
||||
|
|
@ -252,8 +252,8 @@ class AddonManager(Mapping):
|
|||
"""
|
||||
if isinstance(path, str):
|
||||
try:
|
||||
obj = load_object(path)
|
||||
except (ValueError, NameError, ImportError):
|
||||
obj = load_module_or_object(path)
|
||||
except NameError:
|
||||
raise NameError(f"Could not find add-on '{path}'")
|
||||
else:
|
||||
obj = path
|
||||
|
|
|
|||
|
|
@ -72,6 +72,22 @@ def load_object(path: Union[str, Callable]) -> Any:
|
|||
return obj
|
||||
|
||||
|
||||
def load_module_or_object(path):
|
||||
"""Load python module or (non-module) object from given path.
|
||||
|
||||
Path can be both a Python or a file path.
|
||||
"""
|
||||
try:
|
||||
return import_module(path)
|
||||
except ImportError:
|
||||
pass
|
||||
try:
|
||||
return load_object(path)
|
||||
except (ValueError, NameError, ImportError):
|
||||
pass
|
||||
raise NameError(f"Could not load '{path}'")
|
||||
|
||||
|
||||
def walk_modules(path):
|
||||
"""Loads a module and all its submodules from the given module path and
|
||||
returns them. If *any* module throws an exception while importing, that
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ from scrapy.item import Field, Item
|
|||
from scrapy.utils.misc import (
|
||||
arg_to_iter,
|
||||
create_instance,
|
||||
load_module_or_object,
|
||||
load_object,
|
||||
rel_has_nofollow,
|
||||
set_environ,
|
||||
|
|
@ -30,17 +31,17 @@ class UtilsMiscTestCase(unittest.TestCase):
|
|||
obj = load_object("scrapy.utils.misc.load_object")
|
||||
self.assertIs(obj, load_object)
|
||||
|
||||
def test_load_object_module(self):
|
||||
testmod = load_object(__name__ + ".testmod")
|
||||
self.assertTrue(hasattr(testmod, "TESTVAR"))
|
||||
obj = load_object("scrapy.utils.misc.load_object")
|
||||
self.assertIs(obj, load_object)
|
||||
|
||||
def test_load_object_exceptions(self):
|
||||
self.assertRaises(ImportError, load_object, "nomodule999.mod.function")
|
||||
self.assertRaises(NameError, load_object, "scrapy.utils.misc.load_object999")
|
||||
self.assertRaises(TypeError, load_object, {})
|
||||
|
||||
def test_load_module_or_object(self):
|
||||
testmod = load_module_or_object(__name__ + ".testmod")
|
||||
self.assertTrue(hasattr(testmod, "TESTVAR"))
|
||||
obj = load_object("scrapy.utils.misc.load_object")
|
||||
self.assertIs(obj, load_object)
|
||||
|
||||
def test_walk_modules(self):
|
||||
mods = walk_modules("tests.test_utils_misc.test_walk_modules")
|
||||
expected = [
|
||||
|
|
|
|||
Loading…
Reference in New Issue