Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade for python3 #45

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pycsco.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
MANIFEST.in
README.md
setup.cfg
setup.py
pycsco/__init__.py
Expand Down
3 changes: 2 additions & 1 deletion pycsco.egg-info/requires.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
xmltodict>=0.9.2
gtextfsm==0.2.1
textfsm==1.1.0
scp
paramiko
pyyaml
24 changes: 13 additions & 11 deletions pycsco/lib/ipaddr.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ def __str__(self):
return '%s' % self._string_from_ip_int(self._ip)

def __hash__(self):
return hash(hex(long(self._ip)))
return hash(hex(int(self._ip)))

def _get_address_key(self):
return (self._version, self)
Expand Down Expand Up @@ -1132,7 +1132,7 @@ def _string_from_ip_int(self, ip_int):

"""
octets = []
for _ in xrange(4):
for _ in range(4):
octets.insert(0, str(ip_int & 0xFF))
ip_int >>= 8
return '.'.join(octets)
Expand Down Expand Up @@ -1240,7 +1240,7 @@ def __init__(self, address):
_BaseV4.__init__(self, address)

# Efficient constructor from integer.
if isinstance(address, (int, long)):
if isinstance(address, int):
self._ip = address
if address < 0 or address > self._ALL_ONES:
raise AddressValueError(address)
Expand Down Expand Up @@ -1319,7 +1319,7 @@ def __init__(self, address, strict=False):
_BaseV4.__init__(self, address)

# Constructing from an integer or packed bytes.
if isinstance(address, (int, long, Bytes)):
if isinstance(address, (int, Bytes)):
self.ip = IPv4Address(address)
self._ip = self.ip._ip
self._prefixlen = self._max_prefixlen
Expand Down Expand Up @@ -1413,7 +1413,7 @@ def _ip_int_from_string(self, ip_str):
# This indicates that a run of zeroes has been skipped.
try:
skip_index, = (
[i for i in xrange(1, len(parts) - 1) if not parts[i]] or
[i for i in range(1, len(parts) - 1) if not parts[i]] or
[None])
except ValueError:
# Can't have more than one '::'
Expand Down Expand Up @@ -1447,12 +1447,14 @@ def _ip_int_from_string(self, ip_str):

try:
# Now, parse the hextets into a 128-bit integer.
ip_int = 0L
for i in xrange(parts_hi):
#ip_int = 0L

ip_int = 0
for i in range(parts_hi):
ip_int <<= 16
ip_int |= self._parse_hextet(parts[i])
ip_int <<= 16 * parts_skipped
for i in xrange(-parts_lo, 0):
for i in range(-parts_lo, 0):
ip_int <<= 16
ip_int |= self._parse_hextet(parts[i])
return ip_int
Expand Down Expand Up @@ -1573,7 +1575,7 @@ def _explode_shorthand_ip_string(self):

ip_int = self._ip_int_from_string(ip_str)
parts = []
for i in xrange(self._HEXTET_COUNT):
for i in range(self._HEXTET_COUNT):
parts.append('%04x' % (ip_int & 0xFFFF))
ip_int >>= 16
parts.reverse()
Expand Down Expand Up @@ -1753,7 +1755,7 @@ def __init__(self, address):
_BaseV6.__init__(self, address)

# Efficient constructor from integer.
if isinstance(address, (int, long)):
if isinstance(address, int):
self._ip = address
if address < 0 or address > self._ALL_ONES:
raise AddressValueError(address)
Expand Down Expand Up @@ -1828,7 +1830,7 @@ def __init__(self, address, strict=False):
_BaseV6.__init__(self, address)

# Constructing from an integer or packed bytes.
if isinstance(address, (int, long, Bytes)):
if isinstance(address, (int, Bytes)):
self.ip = IPv6Address(address)
self._ip = self.ip._ip
self._prefixlen = self._max_prefixlen
Expand Down
10 changes: 5 additions & 5 deletions pycsco/nxos/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import yaml
import json
from os.path import expanduser
from nxapi import NXAPI
from error import CLIError
from .nxapi import NXAPI
from .error import CLIError
except ImportError as e:
print '***************************'
print e
print '***************************'
print ('***************************')
print (e)
print ('***************************')


class Auth():
Expand Down
47 changes: 26 additions & 21 deletions pycsco/nxos/nxapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@
# Copyright (C) 2013 Cisco Systems Inc.
# All rights reserved
try:
import urllib2
import urllib3
import contextlib
import base64
import socket
import httplib
from httplib import HTTPConnection, HTTPS_PORT
import http.client
import urllib.request
from urllib.request import urlopen, Request
from http.client import HTTPConnection, HTTPS_PORT
import ssl
import io
import json
import requests

except ImportError as e:
print '***************************'
print e
print '***************************'
print ('***************************')
print (e)
print ('***************************')


class HTTPSConnection(HTTPConnection):
Expand Down Expand Up @@ -123,27 +129,26 @@ def __init__(
self.username = username
self.password = password
self.url = url
self.base64_str = base64.encodestring('%s:%s' % (username,
password)).replace('\n', '')
self.base64_str = base64.encodestring(('%s:%s' % (username,
password)).encode()).decode('utf-8').replace('\n','')

def get_resp(
self,
req_str,
cookie,
timeout,
):

req = urllib2.Request(self.url, req_str)
req_str = req_str.encode('utf8')
req = Request(self.url, req_str)
req.add_header('Authorization', 'Basic %s' % self.base64_str)
req.add_header('Cookie', '%s' % cookie)
try:
with contextlib.closing(urllib2.urlopen(req,
timeout=timeout)) as resp:
resp_str = resp.read()
resp_headers = resp.info()
return (resp_headers, resp_str)
except socket.timeout, e:
print 'Req timeout'
with contextlib.closing(urlopen(req, timeout=timeout)) as resp:
resp_str = resp.read()
resp_headers = resp.info()
return (resp_headers, resp_str)
except socket.timeout as e:
print('Req timeout')
raise


Expand All @@ -169,17 +174,17 @@ def get_resp(
timeout,
):

req = urllib2.Request(self.url, req_str)
req = urllib.request.Request(self.url, req_str)
req.add_header('Authorization', 'Basic %s' % self.base64_str)
req.add_header('Cookie', '%s' % cookie)
try:
with contextlib.closing(urllib2.urlopen(req,
with contextlib.closing(urllib.request.urlopen(req,
timeout=timeout)) as resp:
resp_str = resp.read()
resp_headers = resp.info()
return (resp_headers, resp_str)
except socket.timeout, e:
print 'Req timeout'
except socket.timeout as e:
print ('Req timeout')
raise


Expand Down
6 changes: 3 additions & 3 deletions pycsco/nxos/utils/aaa.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import xmltodict
import re
except ImportError as e:
print '*' * 30
print e
print '*' * 30
print(('*' * 30))
print (e)
print(('*' * 30))

__all__ = ['get_aaa_server_info', 'config_aaa_server',
'default_aaa_server', 'get_aaa_host_info',
Expand Down
8 changes: 4 additions & 4 deletions pycsco/nxos/utils/fhrp.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import xmltodict
from pycsco.nxos.device import Device
except ImportError as e:
print '*' * 30
print e
print '*' * 30
print(('*' * 30))
print (e)
print(('*' * 30))

__all__ = []

Expand Down Expand Up @@ -181,4 +181,4 @@ def get_commands_remove_vrrp(group):
test = get_vrrp_existing(device, interface)

import json
print json.dumps(test, indent=4)
print((json.dumps(test, indent=4)))
38 changes: 19 additions & 19 deletions pycsco/nxos/utils/mcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
try:
import xmltodict
except ImportError as e:
print '*' * 30
print e
print '*' * 30
print(('*' * 30))
print (e)
print(('*' * 30))

__all__ = ['get_igmp_defaults', 'get_igmp_global', 'get_igmp_snooping',
'get_igmp_snooping_defaults', 'get_igmp_interface',
Expand All @@ -44,7 +44,7 @@ def get_igmp_defaults():
args = dict(flush_routes=flush_routes,
enforce_rtr_alert=enforce_rtr_alert)

default = dict((param, value) for (param, value) in args.iteritems()
default = dict((param, value) for (param, value) in args.items()
if value is not None)

return default
Expand All @@ -65,7 +65,7 @@ def config_igmp(delta):
'enforce_rtr_alert': 'ip igmp enforce-router-alert'
}
commands = []
for key, value in delta.iteritems():
for key, value in delta.items():
if value:
command = CMDS.get(key)
else:
Expand Down Expand Up @@ -159,7 +159,7 @@ def get_igmp_snooping(device):

existing2.update(existing)

for k, v in existing2.iteritems():
for k, v in existing2.items():
if v in ['true', 'enabled']:
existing2[k] = True
elif v in ['false', 'disabled']:
Expand Down Expand Up @@ -187,7 +187,7 @@ def get_igmp_snooping_defaults():
report_supp=report_supp, v3_report_supp=v3_report_supp,
group_timeout=group_timeout)

default = dict((param, value) for (param, value) in args.iteritems()
default = dict((param, value) for (param, value) in args.items()
if value is not None)

return default
Expand All @@ -213,7 +213,7 @@ def config_igmp_snooping(delta, existing, default=False):

commands = []
command = None
for k, v in delta.iteritems():
for k, v in delta.items():
if v:
# this next check is funky & used when defaulting the group timeout
# funky because there is technically no default, so we just need to
Expand Down Expand Up @@ -303,7 +303,7 @@ def get_igmp_interface(device, interface):
new_staticoif = []
temp = {}
for counter, data in enumerate(staticoif):
for k, v in data.iteritems():
for k, v in data.items():
if v:
temp[k] = v
if temp:
Expand Down Expand Up @@ -353,7 +353,7 @@ def config_igmp_interface(delta, found_both, found_prefix):
commands = []
command = None

for k, v in delta.iteritems():
for k, v in delta.items():
if k in ['source', 'oif_source'] or found_both or found_prefix:
pass
elif k == 'prefix':
Expand Down Expand Up @@ -389,7 +389,7 @@ def config_default_igmp_interface(existing, delta, found_both, found_prefix):

commands = []
proposed = get_igmp_interface_defaults()
delta = dict(set(proposed.iteritems()).difference(existing.iteritems()))
delta = dict(set(proposed.items()).difference(iter(existing.items())))
if delta:
command = config_igmp_interface(delta, found_both, found_prefix)

Expand Down Expand Up @@ -428,7 +428,7 @@ def get_igmp_interface_defaults():
group_timeout=group_timeout, report_llg=report_llg,
immediate_leave=immediate_leave)

default = dict((param, value) for (param, value) in args.iteritems()
default = dict((param, value) for (param, value) in args.items()
if value is not None)

return default
Expand Down Expand Up @@ -498,7 +498,7 @@ def get_pim_interface(device, interface):
get_data = result['ins_api']['outputs']['output']['body'].get(
'TABLE_iod')['ROW_iod']

if isinstance(get_data.get('dr-priority'), unicode) or \
if isinstance(get_data.get('dr-priority'), str) or \
isinstance(get_data.get('dr-priority'), str):
pim_interface['dr_prio'] = get_data.get('dr-priority')
else:
Expand Down Expand Up @@ -527,7 +527,7 @@ def get_pim_interface(device, interface):
if jp_in_policy == 'none configured':
pim_interface['jp_policy_in'] = None

if isinstance(get_data.get('jp-out-policy-name'), unicode) or \
if isinstance(get_data.get('jp-out-policy-name'), str) or \
isinstance(get_data.get('jp-out-policy-name'), str):
pim_interface['jp_policy_out'] = get_data.get('jp-out-policy-name')
else:
Expand Down Expand Up @@ -655,7 +655,7 @@ def config_pim_interface(delta, existing, jp_bidir, isauth):
if command:
commands.append(command)

for k, v in delta.iteritems():
for k, v in delta.items():
if k in ['dr_prio', 'hello_interval', 'hello_auth_key', 'border',
'sparse']:
if v:
Expand Down Expand Up @@ -733,7 +733,7 @@ def get_pim_interface_defaults():
hello_interval=hello_interval,
hello_auth_key=hello_auth_key)

default = dict((param, value) for (param, value) in args.iteritems()
default = dict((param, value) for (param, value) in args.items()
if value is not None)

return default
Expand Down Expand Up @@ -765,7 +765,7 @@ def default_pim_interface_policies(existing, jp_bidir):

elif not jp_bidir:
command = None
for k, v in existing.iteritems():
for k, v in existing.items():
if k == 'jp_policy_in':
if existing.get('jp_policy_in'):
if existing.get('jp_type_in') == 'prefix':
Expand Down Expand Up @@ -816,8 +816,8 @@ def config_pim_interface_defaults(existing, jp_bidir, isauth):

# returns a dict
defaults = get_pim_interface_defaults()
delta = dict(set(defaults.iteritems()).difference(
existing.iteritems()))
delta = dict(set(defaults.items()).difference(
iter(existing.items())))
if delta:
# returns a list
command = config_pim_interface(delta, existing,
Expand Down
Loading