From 7ff09813a4ec183356c4f4a0576a6353071490d3 Mon Sep 17 00:00:00 2001 From: Mircea Ulinic Date: Tue, 7 Nov 2017 14:51:31 +0000 Subject: [PATCH 1/4] Final release: 1.0.0 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index bbf86e24..7a12bc66 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ setup( name="napalm-base", - version='0.25.0', + version='1.0.0', packages=find_packages(), author="David Barroso, Kirk Byers, Mircea Ulinic", author_email="dbarrosop@dravetech.com, ping@mirceaulinic.net, ktbyers@twb-tech.com", From 4fb2e267e07e3be84549ed147a230fcb589a6ebc Mon Sep 17 00:00:00 2001 From: Mircea Ulinic Date: Wed, 8 Nov 2017 15:39:39 +0000 Subject: [PATCH 2/4] Import get_network_driver from the new code base When napalm>=2.0.0 is installed, will import everything from the new codebase, otherwise it will fallback and use the features defined as previously. --- napalm_base/__init__.py | 162 +++++++++++++++++++++++----------------- 1 file changed, 92 insertions(+), 70 deletions(-) diff --git a/napalm_base/__init__.py b/napalm_base/__init__.py index bce3d114..0a75cd31 100644 --- a/napalm_base/__init__.py +++ b/napalm_base/__init__.py @@ -20,10 +20,11 @@ # Python std lib import sys -import inspect -import importlib import pkg_resources +# Import local modules +from napalm_base.base import NetworkDriver + # Verify Python Version that is running try: if not(sys.version_info.major == 2 and sys.version_info.minor == 7) and \ @@ -32,11 +33,16 @@ except AttributeError: raise RuntimeError('NAPALM requires Python 2.7 or Python3') -# NAPALM base -from napalm_base.base import NetworkDriver -from napalm_base.exceptions import ModuleImportError -from napalm_base.mock import MockDriver -from napalm_base.utils import py23_compat +# Try to import napalm +try: + import napalm + HAS_NAPALM = True + try: + NAPALM_MAJOR = napalm.__version__.split('.')[0] + except AttributeError: + NAPALM_MAJOR = 0 +except ImportError: + HAS_NAPALM = False try: __version__ = pkg_resources.get_distribution('napalm-base').version @@ -44,71 +50,87 @@ __version__ = "Not installed" -__all__ = [ - 'get_network_driver', # export the function - 'NetworkDriver' # also export the base class -] - +if HAS_NAPALM and NAPALM_MAJOR >= 2: + # If napalm >= 2.0.0 is installed, then import get_network_driver + from napalm import get_network_driver +else: + # Import std lib + import inspect + import importlib + + # Import local modules + from napalm_base.exceptions import ModuleImportError + from napalm_base.mock import MockDriver + from napalm_base.utils import py23_compat + + def get_network_driver(module_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]. + NAPALM community supports a list of devices and provides the corresponding libraries; for + full reference please refer to the `Supported Network Operation Systems`_ paragraph on + `Read the Docs`_. + + .. _`Supported Network Operation Systems`: \ + http://napalm.readthedocs.io/en/latest/#supported-network-operating-systems + .. _`Read the Docs`: \ + http://napalm.readthedocs.io/ + + module_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. + + Example: + + .. code-block:: python + + >>> get_network_driver('junos') + + >>> get_network_driver('IOS-XR') + + >>> get_network_driver('napalm_eos') + + >>> get_network_driver('wrong') + napalm_base.exceptions.ModuleImportError: Cannot import "napalm_wrong". Is the library \ + installed? + """ + if module_name == "mock": + return MockDriver + + if not (isinstance(module_name, py23_compat.string_types) and len(module_name) > 0): + raise ModuleImportError('Please provide a valid driver name.') + + try: + # Only lowercase allowed + module_name = module_name.lower() + # Try to not raise error when users requests IOS-XR for e.g. + module_install_name = module_name.replace('-', '') + # Can also request using napalm_[SOMETHING] + if 'napalm_' not in module_install_name and prepend is True: + module_install_name = 'napalm_{name}'.format(name=module_install_name) + module = importlib.import_module(module_install_name) + except ImportError: + raise ModuleImportError( + 'Cannot import "{install_name}". Is the library installed?'.format( + install_name=module_install_name + ) + ) -def get_network_driver(module_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]. - NAPALM community supports a list of devices and provides the corresponding libraries; for - full reference please refer to the `Supported Network Operation Systems`_ paragraph on - `Read the Docs`_. - - .. _`Supported Network Operation Systems`: \ - http://napalm.readthedocs.io/en/latest/#supported-network-operating-systems - .. _`Read the Docs`: \ - http://napalm.readthedocs.io/ - - :param module_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. - - Example:: - - .. code-block:: python - - >>> get_network_driver('junos') - - >>> get_network_driver('IOS-XR') - - >>> get_network_driver('napalm_eos') - - >>> get_network_driver('wrong') - napalm_base.exceptions.ModuleImportError: Cannot import "napalm_wrong". Is the library \ - installed? - """ - if module_name == "mock": - return MockDriver - - if not (isinstance(module_name, py23_compat.string_types) and len(module_name) > 0): - raise ModuleImportError('Please provide a valid driver name.') + for name, obj in inspect.getmembers(module): + if inspect.isclass(obj) and issubclass(obj, NetworkDriver): + return obj - try: - # Only lowercase allowed - module_name = module_name.lower() - # Try to not raise error when users requests IOS-XR for e.g. - module_install_name = module_name.replace('-', '') - # Can also request using napalm_[SOMETHING] - if 'napalm_' not in module_install_name and prepend is True: - module_install_name = 'napalm_{name}'.format(name=module_install_name) - module = importlib.import_module(module_install_name) - except ImportError: + # looks like you don't have any Driver class in your module... raise ModuleImportError( - 'Cannot import "{install_name}". Is the library installed?'.format( - install_name=module_install_name - ) - ) + 'No class inheriting "napalm_base.base.NetworkDriver" found in "{install_name}".' + .format(install_name=module_install_name)) - for name, obj in inspect.getmembers(module): - if inspect.isclass(obj) and issubclass(obj, NetworkDriver): - return obj - # looks like you don't have any Driver class in your module... - raise ModuleImportError( - 'No class inheriting "napalm_base.base.NetworkDriver" found in "{install_name}".' - .format(install_name=module_install_name)) +__all__ = [ + 'get_network_driver', # export the function + 'NetworkDriver' # also export the base class +] From 819f48d6d8acd7bd274adcc5a6eb1e34aea5f3ba Mon Sep 17 00:00:00 2001 From: Mircea Ulinic Date: Wed, 8 Nov 2017 15:44:25 +0000 Subject: [PATCH 3/4] napalm major version as int --- napalm_base/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/napalm_base/__init__.py b/napalm_base/__init__.py index 0a75cd31..e04cbaae 100644 --- a/napalm_base/__init__.py +++ b/napalm_base/__init__.py @@ -38,7 +38,7 @@ import napalm HAS_NAPALM = True try: - NAPALM_MAJOR = napalm.__version__.split('.')[0] + NAPALM_MAJOR = int(napalm.__version__.split('.')[0]) except AttributeError: NAPALM_MAJOR = 0 except ImportError: From 16d20f9127cc864c95f585fd7aa73584e2dd35c7 Mon Sep 17 00:00:00 2001 From: Mircea Ulinic Date: Wed, 15 Nov 2017 16:24:38 +0000 Subject: [PATCH 4/4] Import NetworkDriver from napalm.base when napalm>=2.0.0 is installed Otherwise, couple together the local napalm_base get_network_driver and NetworkDriver. --- napalm_base/__init__.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/napalm_base/__init__.py b/napalm_base/__init__.py index e04cbaae..6a82fb41 100644 --- a/napalm_base/__init__.py +++ b/napalm_base/__init__.py @@ -22,9 +22,6 @@ import sys import pkg_resources -# Import local modules -from napalm_base.base import NetworkDriver - # Verify Python Version that is running try: if not(sys.version_info.major == 2 and sys.version_info.minor == 7) and \ @@ -53,6 +50,7 @@ if HAS_NAPALM and NAPALM_MAJOR >= 2: # If napalm >= 2.0.0 is installed, then import get_network_driver from napalm import get_network_driver + from napalm.base import NetworkDriver else: # Import std lib import inspect @@ -62,6 +60,7 @@ from napalm_base.exceptions import ModuleImportError from napalm_base.mock import MockDriver from napalm_base.utils import py23_compat + from napalm_base.base import NetworkDriver def get_network_driver(module_name, prepend=True): """