Skip to content

Commit

Permalink
don't hide relevant import errors (#635)
Browse files Browse the repository at this point in the history
* don't hide relevant import errors

* original name

* py2 compliance

* now back to py3 compatibility....
  • Loading branch information
dbarrosop committed Feb 3, 2018
1 parent de9457a commit f32ad84
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions napalm/base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
]


def get_network_driver(module_name, prepend=True):
def get_network_driver(name, prepend=True):
"""
Searches for a class derived form the base NAPALM class NetworkDriver in a specific library.
The library name must repect the following pattern: napalm_[DEVICE_OS].
Expand All @@ -48,7 +48,7 @@ def get_network_driver(module_name, prepend=True):
.. _`Read the Docs`: \
http://napalm.readthedocs.io/
:param module_name: the name of the device operating system or the name of the library.
:param name: the name of the device operating system or the name of the library.
:return: the first class derived from NetworkDriver, found in the library.
:raise ModuleImportError: when the library is not installed or a derived class from \
NetworkDriver was not found.
Expand All @@ -67,16 +67,16 @@ def get_network_driver(module_name, prepend=True):
napalm.base.exceptions.ModuleImportError: Cannot import "napalm_wrong". Is the library \
installed?
"""
if module_name == "mock":
if name == "mock":
return MockDriver

if not (isinstance(module_name, py23_compat.string_types) and len(module_name) > 0):
if not (isinstance(name, py23_compat.string_types) and len(name) > 0):
raise ModuleImportError('Please provide a valid driver name.')

# Only lowercase allowed
module_name = module_name.lower()
name = name.lower()
# Try to not raise error when users requests IOS-XR for e.g.
module_install_name = module_name.replace('-', '')
module_install_name = name.replace('-', '')
community_install_name = "napalm_{name}".format(name=module_install_name)
custom_install_name = "custom_napalm.{name}".format(name=module_install_name)
# Can also request using napalm_[SOMETHING]
Expand All @@ -87,12 +87,22 @@ def get_network_driver(module_name, prepend=True):
try:
module = importlib.import_module(module_name)
break
except ImportError:
pass
except ImportError as e:
try:
# gotta love py23 differences
message = e.message
except AttributeError:
message = e.msg
if "No module named" in message:
# py2 doesn't have ModuleNotFoundError exception
failed_module = message.split()[-1]
if failed_module.replace("'", "") in module_name:
continue
raise e
else:
raise ModuleImportError(
'Cannot import "{install_name}". Is the library installed?'.format(
install_name=module_install_name
install_name=name
)
)

Expand Down

0 comments on commit f32ad84

Please sign in to comment.