Commit f6441bc6 authored by Eliot Berriot's avatar Eliot Berriot

Added handling of error in autodiscovering

parent 438b96c9
from registries import Registry, meta_registry from registries import Registry, meta_registry
__version__ = "0.1.1" __version__ = "0.1.2"
\ No newline at end of file \ No newline at end of file
...@@ -77,15 +77,28 @@ class Registry(OrderedDict): ...@@ -77,15 +77,28 @@ class Registry(OrderedDict):
:param apps: an iterable of string, refering to python modules the registry will try to import via autodiscover :param apps: an iterable of string, refering to python modules the registry will try to import via autodiscover
""" """
for app in apps: for app in apps:
app_package = __import__(app)
try: try:
package = '{0}.{1}'.format(app, self.look_into) # try to import self.package inside current app package = '{0}.{1}'.format(app, self.look_into) # try to import self.package inside current app
#print(package) #print(package)
module = __import__(package) module = __import__(package)
if force_reload: if force_reload:
reload(module) reload(module)
except ImportError: except ImportError as exc:
# Module does not exist # From django's syncdb
pass # This is slightly hackish. We want to ignore ImportErrors
# if the module itself is missing -- but we don't
# want to ignore the exception if the module exists
# but raises an ImportError for some reason. The only way we
# can do this is to check the text of the exception. Note that
# we're a bit broad in how we check the text, because different
# Python implementations may not use the same text.
# CPython uses the text "No module named"
# PyPy uses "No module named myproject.myapp"
msg = exc.args[0]
if not msg.startswith('No module named') or self.look_into not in msg:
raise
class MetaRegistry(Registry): class MetaRegistry(Registry):
......
from test_registries import awesome_people
print(inexisting_var)
\ No newline at end of file
...@@ -86,6 +86,11 @@ class RegistryTest(unittest.TestCase): ...@@ -86,6 +86,11 @@ class RegistryTest(unittest.TestCase):
registry.clear() registry.clear()
def test_autodiscover_raises_an_error_if_there_is_an_error_in_imported_module(self):
with self.assertRaises(NameError):
registry = test_registries.awesome_people
registry.autodiscover(apps=('buggy_app',))
def test_meta_registry_can_autodiscovering_registries_and_trigger_their_autodiscover_method(self): def test_meta_registry_can_autodiscovering_registries_and_trigger_their_autodiscover_method(self):
registry = registries.meta_registry registry = registries.meta_registry
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment