Skip to content
This repository has been archived by the owner on Jan 16, 2019. It is now read-only.

Commit

Permalink
Merge pull request #65 from ktbyers/devel
Browse files Browse the repository at this point in the history
Add Python3 support to napalm-ios
  • Loading branch information
ktbyers authored Nov 16, 2016
2 parents 111b5b2 + 37581e0 commit 4afcacd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
language: python
python:
- 2.7
- 3.4
- 3.5
install:
- pip install -r requirements-dev.txt
- pip install .
Expand Down
36 changes: 19 additions & 17 deletions napalm_ios/ios.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
# the License.

from __future__ import print_function
from __future__ import unicode_literals

import re

from netmiko import ConnectHandler, FileTransfer
from netmiko import __version__ as netmiko_version
from napalm_base.base import NetworkDriver
from napalm_base.exceptions import ReplaceConfigException, MergeConfigException
from napalm_base.utils import py23_compat

# Easier to store these as constants
HOUR_SECONDS = 3600
Expand Down Expand Up @@ -584,10 +586,10 @@ def get_facts(self):
return {
'uptime': uptime,
'vendor': vendor,
'os_version': unicode(os_version),
'serial_number': unicode(serial_number),
'model': unicode(model),
'hostname': unicode(hostname),
'os_version': py23_compat.text_type(os_version),
'serial_number': py23_compat.text_type(serial_number),
'model': py23_compat.text_type(model),
'hostname': py23_compat.text_type(hostname),
'fqdn': fqdn,
'interface_list': interface_list
}
Expand Down Expand Up @@ -688,7 +690,7 @@ def get_interfaces(self):
match_mac = re.match(mac_regex, interface_output, flags=re.DOTALL)
group_mac = match_mac.groupdict()
mac_address = group_mac["mac_address"]
interface_list[interface]['mac_address'] = unicode(mac_address)
interface_list[interface]['mac_address'] = py23_compat.text_type(mac_address)
except AttributeError:
interface_list[interface]['mac_address'] = u'N/A'
try:
Expand Down Expand Up @@ -805,11 +807,11 @@ def get_interfaces_ip(self):
raise ValueError(u"Unexpected Response from the device")

# remove keys with no data
for key in interfaces.keys():
if not bool(interfaces[key]):
del interfaces[key]

return interfaces
new_interfaces = {}
for k, val in interfaces.items():
if val:
new_interfaces[k] = val
return new_interfaces

@staticmethod
def bgp_time_conversion(bgp_uptime):
Expand Down Expand Up @@ -901,7 +903,7 @@ def get_bgp_neighbors(self):
router_id = match.group(1)
local_as = int(match.group(2))
break
bgp_neighbor_data['global']['router_id'] = unicode(router_id)
bgp_neighbor_data['global']['router_id'] = py23_compat.text_type(router_id)
bgp_neighbor_data['global']['peers'] = {}

cmd_neighbor_table = 'show ip bgp summary | begin Neighbor'
Expand Down Expand Up @@ -944,7 +946,7 @@ def get_bgp_neighbors(self):
peer_dict['local_as'] = local_as
peer_dict['is_enabled'] = is_enabled
peer_dict['is_up'] = is_up
peer_dict['remote_id'] = unicode(remote_rid)
peer_dict['remote_id'] = py23_compat.text_type(remote_rid)

cmd_current_prefixes = 'show ip bgp neighbors {} | inc Prefixes Current'.format(peer_id)
# output: Prefixes Current: 0 0
Expand Down Expand Up @@ -1223,12 +1225,12 @@ def get_ntp_stats(self):
address_regex = re.match('(\W*)([0-9.*]*)', address)
try:
ntp_stats.append({
'remote': unicode(address_regex.group(2)),
'remote': py23_compat.text_type(address_regex.group(2)),
'synchronized': ('*' in address_regex.group(1)),
'referenceid': unicode(ref_clock),
'referenceid': py23_compat.text_type(ref_clock),
'stratum': int(st),
'type': u'-',
'when': unicode(when),
'when': py23_compat.text_type(when),
'hostpoll': int(poll),
'reachability': int(reach),
'delay': float(delay),
Expand Down Expand Up @@ -1404,8 +1406,8 @@ def ping(self, destination, source='', ttl=255, timeout=2, size=100, count=5):
})
results_array = []
for i in range(probes_received):
results_array.append({'ip_address': unicode(destination), 'rtt': 0.0})

results_array.append({'ip_address': py23_compat.text_type(destination),
'rtt': 0.0})
ping_dict['success'].update({'results': results_array})

return ping_dict
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
from setuptools import setup, find_packages
from pip.req import parse_requirements

__author__ = 'David Barroso <dbarrosop@dravetech.com>'
__author__ = 'Kirk Byers <ktbyers@twb-tech.com>'

install_reqs = parse_requirements('requirements.txt', session=uuid.uuid1())
reqs = [str(ir.req) for ir in install_reqs]

setup(
name="napalm-ios",
version="0.3.1",
version="0.4.0",
packages=find_packages(),
author="David Barroso",
author_email="dbarrosop@dravetech.com",
author="Kirk Byers",
author_email="ktbyers@twb-tech.com",
description="Network Automation and Programmability Abstraction Layer with Multivendor support",
classifiers=[
'Topic :: Utilities',
Expand Down
5 changes: 3 additions & 2 deletions test/unit/TestIOSDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import unittest
from napalm_ios import ios
from napalm_base.utils import py23_compat
from napalm_base.test.base import TestConfigNetworkDriver, TestGettersNetworkDriver
import re

Expand Down Expand Up @@ -159,7 +160,7 @@ def test_ios_only_bgp_time_conversion(self):
"never": -1,
}

for bgp_time, result in test_cases.iteritems():
for bgp_time, result in test_cases.items():
self.assertEqual(self.device.bgp_time_conversion(bgp_time), result)


Expand All @@ -176,7 +177,7 @@ def send_command_expect(self, command):
"""Fake execute a command in the device by just returning the content of a file."""
cmd = re.sub(r'[\[\]\*\^\+\s\|]', '_', command)
output = self.read_txt_file('ios/mock_data/{}.txt'.format(cmd))
return unicode(output)
return py23_compat.text_type(output)

def send_command(self, command):
"""Fake execute a command in the device by just returning the content of a file."""
Expand Down

0 comments on commit 4afcacd

Please sign in to comment.