Skip to content

Commit

Permalink
Merge pull request #143 from vnitinv/master
Browse files Browse the repository at this point in the history
Fix: Python 2.6 support
  • Loading branch information
leopoul committed Jul 5, 2016
2 parents 3575ba5 + f1fabea commit e09d370
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 100 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: python
python:
- '2.6'
- '2.7'
- '3.4'
install:
Expand Down
3 changes: 1 addition & 2 deletions ncclient/transport/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import socket
import getpass
from binascii import hexlify
import sys
from six import StringIO
from io import BytesIO
from lxml import etree
Expand Down Expand Up @@ -63,7 +62,7 @@ def _colonify(fp):
finga += ":" + fp[idx:idx+2]
return finga

if sys.version_info.major < 3:
if sys.version < '3':
def textify(buf):
return buf
else:
Expand Down
5 changes: 4 additions & 1 deletion ncclient/transport/third_party/junos/ioproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
else:
from io import StringIO
from select import select
from subprocess import Popen, check_output, PIPE, STDOUT
if sys.version>='2.7':
from subprocess import Popen, check_output, PIPE, STDOUT
else:
from subprocess import Popen, PIPE, STDOUT

from ncclient.transport.errors import SessionCloseError, TransportError, PermissionError
from ncclient.transport.ssh import SSHSession
Expand Down
36 changes: 18 additions & 18 deletions test/unit/operations/test_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def test_edit_config(self, mock_request):
node = new_ele("edit-config")
node.append(util.datastore_or_url("target", "candidate"))
node.append(validated_element(root, ("config", qualify("config"))))
xml = ElementTree.tostring(node, method='xml')
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call, method='xml')
call = ElementTree.tostring(call)
self.assertEqual(call, xml)

@patch('ncclient.operations.edit.RPC._request')
Expand All @@ -61,9 +61,9 @@ def test_edit_config_2(self, mock_request):
sub_ele(node, "default-operation").text = "default"
config_text = sub_ele(node, "config-text")
sub_ele(config_text, "configuration-text").text = root
xml = ElementTree.tostring(node, method='xml')
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call, method='xml')
call = ElementTree.tostring(call)
self.assertEqual(call, xml)

@patch('ncclient.operations.RPC._request')
Expand All @@ -76,9 +76,9 @@ def test_delete_config(self, mock_request):
obj.request("candidate")
node = new_ele("delete-config")
node.append(util.datastore_or_url("target", "candidate"))
xml = ElementTree.tostring(node, method='xml')
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call, method='xml')
call = ElementTree.tostring(call)
self.assertEqual(call, xml)

@patch('ncclient.operations.RPC._request')
Expand All @@ -92,9 +92,9 @@ def test_copy_config(self, mock_request):
node = new_ele("copy-config")
node.append(util.datastore_or_url("target", "running"))
node.append(util.datastore_or_url("source", "candidate"))
xml = ElementTree.tostring(node, method='xml')
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call, method='xml')
call = ElementTree.tostring(call)
self.assertEqual(call, xml)

@patch('ncclient.operations.RPC._request')
Expand All @@ -109,9 +109,9 @@ def test_validate_config(self, mock_request):
src = sub_ele(node, "source")
cfg = sub_ele(src, "config")
sub_ele(cfg, "data")
xml = ElementTree.tostring(node, method='xml')
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call, method='xml')
call = ElementTree.tostring(call)
self.assertEqual(call, xml)

@patch('ncclient.operations.RPC._request')
Expand All @@ -123,9 +123,9 @@ def test_validate_datastore(self, mock_request):
node = new_ele("validate")
src = sub_ele(node, "source")
sub_ele(src, "candidate")
xml = ElementTree.tostring(node, method='xml')
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call, method='xml')
call = ElementTree.tostring(call)
self.assertEqual(call, xml)

@patch('ncclient.operations.RPC._request')
Expand All @@ -137,18 +137,18 @@ def test_commit(self, mock_request):
node = new_ele("commit")
sub_ele(node, "confirmed")
sub_ele(node, "confirm-timeout").text = "0"
xml = ElementTree.tostring(node, method='xml')
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call, method='xml')
call = ElementTree.tostring(call)
self.assertEqual(call, xml)

@patch('ncclient.operations.RPC._request')
def test_commit_exception(self, mock_request):
session = ncclient.transport.SSHSession(self.device_handler)
session._server_capabilities = [':candidate']
obj = Commit(session, self.device_handler, raise_mode=RaiseMode.ALL)
with self.assertRaises(MissingCapabilityError):
obj.request(confirmed=True, timeout="0")
self.assertRaises(MissingCapabilityError,
obj.request, confirmed=True, timeout="0")

@patch('ncclient.operations.RPC._request')
def test_discard_changes(self, mock_request):
Expand All @@ -160,7 +160,7 @@ def test_discard_changes(self, mock_request):
raise_mode=RaiseMode.ALL)
obj.request()
node = new_ele("discard-changes")
xml = ElementTree.tostring(node, method='xml')
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call, method='xml')
call = ElementTree.tostring(call)
self.assertEqual(call, xml)
24 changes: 12 additions & 12 deletions test/unit/operations/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def test_lock_default_param(self, mock_request, mock_session):
obj.request()
node = new_ele("lock")
sub_ele(sub_ele(node, "target"), "candidate")
xml = ElementTree.tostring(node, method='xml')
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call, method='xml')
call = ElementTree.tostring(call)
self.assertEqual(call, xml)

@patch('ncclient.transport.SSHSession')
Expand All @@ -35,9 +35,9 @@ def test_lock(self, mock_request, mock_session):
obj.request(target="running")
node = new_ele("lock")
sub_ele(sub_ele(node, "target"), "running")
xml = ElementTree.tostring(node, method='xml')
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call, method='xml')
call = ElementTree.tostring(call)
self.assertEqual(call, xml)

@patch('ncclient.transport.SSHSession')
Expand All @@ -48,9 +48,9 @@ def test_unlock_default_param(self, mock_request, mock_session):
obj.request()
node = new_ele("unlock")
sub_ele(sub_ele(node, "target"), "candidate")
xml = ElementTree.tostring(node, method='xml')
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call, method='xml')
call = ElementTree.tostring(call)
self.assertEqual(call, xml)

@patch('ncclient.transport.SSHSession')
Expand All @@ -61,9 +61,9 @@ def test_unlock(self, mock_request, mock_session):
obj.request(target="running")
node = new_ele("unlock")
sub_ele(sub_ele(node, "target"), "running")
xml = ElementTree.tostring(node, method='xml')
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call, method='xml')
call = ElementTree.tostring(call)
self.assertEqual(call, xml)

@patch('ncclient.transport.SSHSession')
Expand All @@ -74,9 +74,9 @@ def test_lock_context_enter(self, mock_request, mock_session):
self.assertEqual(obj.__enter__(), obj)
node = new_ele("lock")
sub_ele(sub_ele(node, "target"), "candidate")
xml = ElementTree.tostring(node, method='xml')
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call, method='xml')
call = ElementTree.tostring(call)
self.assertEqual(call, xml)

@patch('ncclient.transport.SSHSession')
Expand All @@ -87,7 +87,7 @@ def test_lock_context_exit(self, mock_request, mock_session):
self.assertFalse(obj.__exit__())
node = new_ele("unlock")
sub_ele(sub_ele(node, "target"), "running")
xml = ElementTree.tostring(node, method='xml')
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call, method='xml')
call = ElementTree.tostring(call)
self.assertEqual(call, xml)
20 changes: 10 additions & 10 deletions test/unit/operations/test_retrieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def test_get(self, mock_request):
obj.request(copy.deepcopy(root_filter))
node = new_ele("get")
node.append(util.build_filter(root_filter))
xml = ElementTree.tostring(node, method='xml')
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call, method='xml')
call = ElementTree.tostring(call)
self.assertEqual(call, xml)

@patch('ncclient.operations.retrieve.RPC._request')
Expand All @@ -38,9 +38,9 @@ def test_get_config(self, mock_request):
obj.request(source)
node = new_ele("get-config")
node.append(util.datastore_or_url("source", source))
xml = ElementTree.tostring(node, method='xml')
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call, method='xml')
call = ElementTree.tostring(call)
self.assertEqual(call, xml)

@patch('ncclient.operations.retrieve.RPC._request')
Expand All @@ -60,9 +60,9 @@ def test_get_schema(self, mock_request):
formt = etree.SubElement(node,
qualify("format", NETCONF_MONITORING_NS))
formt.text = reqformat
xml = ElementTree.tostring(node, method='xml')
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call, method='xml')
call = ElementTree.tostring(call)
self.assertEqual(call, xml)

@patch('ncclient.operations.retrieve.RPC._request')
Expand All @@ -80,9 +80,9 @@ def test_dispatch(self, mock_request):
node = new_ele(rpc)
node.append(util.datastore_or_url("source", source))
node.append(util.build_filter(root_filter))
xml = ElementTree.tostring(node, method='xml')
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call, method='xml')
call = ElementTree.tostring(call)
self.assertEqual(call, xml)

@patch('ncclient.operations.retrieve.RPC._request')
Expand All @@ -99,7 +99,7 @@ def test_dispatch_2(self, mock_request):
obj.request(node, source=source, filter=a)
node.append(util.datastore_or_url("source", source))
node.append(util.build_filter(root_filter))
xml = ElementTree.tostring(node, method='xml')
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call, method='xml')
call = ElementTree.tostring(call)
self.assertEqual(call, xml)
15 changes: 7 additions & 8 deletions test/unit/operations/test_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ def test_rpc_timeout_error(self, mock_thread, mock_send):
node = new_ele("commit")
sub_ele(node, "confirmed")
mock_thread.return_value = False
with self.assertRaises(TimeoutExpiredError):
obj._request(node)
self.assertRaises(TimeoutExpiredError, obj._request, node)

@patch('ncclient.transport.Session.send')
@patch(patch_str)
Expand All @@ -161,10 +160,10 @@ def test_rpc_rpcerror(self, mock_thread, mock_send):
obj._reply = reply
node = new_ele("commit")
sub_ele(node, "confirmed")
with self.assertRaises(RPCError):
err = RPCError(to_ele(xml2))
obj.deliver_error(err)
obj._request(node)

err = RPCError(to_ele(xml2))
obj.deliver_error(err)
self.assertRaises(RPCError, obj._request, node)

@patch('ncclient.transport.Session.send')
@patch(patch_str)
Expand All @@ -175,5 +174,5 @@ def test_rpc_capability_error(self, mock_thread, mock_send):
session._server_capabilities = [':running']
obj = RPC(session, device_handler, raise_mode=RaiseMode.ALL, timeout=0)
obj._assert(':running')
with self.assertRaises(MissingCapabilityError):
obj._assert(':candidate')
self.assertRaises(MissingCapabilityError,
obj._assert, ':candidate')
8 changes: 4 additions & 4 deletions test/unit/operations/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ def test_close_session(self, mock_request, mock_session):
raise_mode=RaiseMode.ALL)
obj.request()
node = new_ele("close-session")
xml = ElementTree.tostring(node, method='xml')
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call, method='xml')
call = ElementTree.tostring(call)
self.assertEqual(call, xml)

@patch('ncclient.transport.SSHSession')
Expand All @@ -40,7 +40,7 @@ def test_kill_session(self, mock_request, mock_session):
obj.request("100")
node = new_ele("kill-session")
sub_ele(node, "session-id").text = "100"
xml = ElementTree.tostring(node, method='xml')
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call, method='xml')
call = ElementTree.tostring(call)
self.assertEqual(call, xml)
Loading

0 comments on commit e09d370

Please sign in to comment.