From 7a81b5613850b775528f0d05b1dd2d50b04e0068 Mon Sep 17 00:00:00 2001 From: Francesco Salamida Date: Thu, 3 Jul 2014 12:26:35 +0200 Subject: [PATCH] improved getitems --- pynetworking/features/ats_interface.py | 14 +++++++++++--- pynetworking/features/awp_interface.py | 14 +++++++++++--- pynetworking/features/awp_vlan.py | 17 ++++++++++++----- setup.py | 2 +- tests/test_interface.py | 2 +- tests/test_interface_ats.py | 1 + tests/test_vlan.py | 1 + 7 files changed, 38 insertions(+), 13 deletions(-) diff --git a/pynetworking/features/ats_interface.py b/pynetworking/features/ats_interface.py index 64c13fc..1c40711 100644 --- a/pynetworking/features/ats_interface.py +++ b/pynetworking/features/ats_interface.py @@ -111,10 +111,18 @@ def __str__(self): __repr__ = __str__ #pragma: no cover def __getitem__(self, ifn): + if isinstance(ifn, str) or isinstance(vid, unicode): + self._update_interface() + if ifn in self._interface: + return self._interface[ifn] + raise KeyError('interface {0} does not exist'.format(ifn)) + else: + raise TypeError, "Invalid argument type." + + def __iter__(self): self._update_interface() - if ifn in self._interface.keys(): - return self._interface[ifn] - raise IndexError + for interface in self._interface: + yield interface def _update_interface(self): self._d.log_info("_update_interface") diff --git a/pynetworking/features/awp_interface.py b/pynetworking/features/awp_interface.py index 25f7d5f..6090de5 100644 --- a/pynetworking/features/awp_interface.py +++ b/pynetworking/features/awp_interface.py @@ -68,10 +68,18 @@ def __str__(self): __repr__ = __str__ #pragma: no cover def __getitem__(self, ifn): + if isinstance(ifn, str) or isinstance(vid, unicode): + self._update_interface() + if ifn in self._interface: + return self._interface[ifn] + raise KeyError('interface {0} does not exist'.format(ifn)) + else: + raise TypeError, "Invalid argument type." + + def __iter__(self): self._update_interface() - if ifn in self._interface.keys(): - return self._interface[ifn] - raise KeyError('{0} key does not exist'.format(key)) + for interface in self._interface: + yield interface def _get_interface_ns(self, ifn): ifn = str(ifn) diff --git a/pynetworking/features/awp_vlan.py b/pynetworking/features/awp_vlan.py index 3d037f4..ae164b6 100644 --- a/pynetworking/features/awp_vlan.py +++ b/pynetworking/features/awp_vlan.py @@ -117,7 +117,6 @@ def add_interface(self, vid, ifn, tagged=False): self._device.cmd(cmds, cache=False, flush_cache=True) self._device.load_system() - def delete_interface(self, vid, ifn): self._d.log_info("delete_interface {0} ifn={1}".format(vid, ifn)) self._update_vlan() @@ -168,11 +167,19 @@ def __str__(self): __repr__ = __str__ #pragma: no cover def __getitem__(self, vid): + if isinstance(vid, str) or isinstance(vid, int) or isinstance(vid, unicode): + self._update_vlan() + vid = str(vid) + if vid in self._vlan: + return self._vlan[vid] + raise KeyError('vlan id {0} does not exist'.format(vid)) + else: + raise TypeError, "Invalid argument type." + + def __iter__(self): self._update_vlan() - vid = str(vid) - if vid in self._vlan: - return self._vlan[vid] - raise KeyError('vlan id {0} does not exist'.format(vid)) + for vlan in self._vlan: + yield vlan def _get_vlan_ids(self, vlan_id): vlan_id = str(vlan_id) diff --git a/setup.py b/setup.py index 1e3f5a6..a460b43 100755 --- a/setup.py +++ b/setup.py @@ -122,7 +122,7 @@ def run(self): 'Jinja2', 'ply', 'pyasn1', - 'pyzmq', + 'pyzmq>=14.0.0', 'ordereddict' ], setup_requires = [ 'sphinx', diff --git a/tests/test_interface.py b/tests/test_interface.py index 3603af3..39dc587 100644 --- a/tests/test_interface.py +++ b/tests/test_interface.py @@ -245,7 +245,6 @@ def test_description(dut, log_level): d.interface.update('1.0.10',description='camera_1') assert d.interface['1.0.10']['description'] == 'camera_1' d.interface.update('1.0.10',description='cam one') - # assert d.interface['1.0.10']['description'] == 'cam one' d.close() def test_unexisting_interface(dut, log_level): @@ -272,4 +271,5 @@ def test_unexisting_interface(dut, log_level): with pytest.raises(ValueError) as excinfo: d.interface.update(max_if_name,enable=False) assert max_if_str in excinfo.value + assert '1.0.10' in d.interface d.close() diff --git a/tests/test_interface_ats.py b/tests/test_interface_ats.py index bc99b9d..c44fd11 100644 --- a/tests/test_interface_ats.py +++ b/tests/test_interface_ats.py @@ -925,6 +925,7 @@ def test_update_interface_48(dut, log_level): """]}) d=Device(host=dut.host,port=dut.port,protocol=dut.protocol, log_level=log_level) d.open() + assert '1.0.10' in d.interface assert d.interface['1.0.24']['enable'] == False d.interface.update('1.0.18',description='test description') assert d.interface['1.0.18']['description'] == 'test description' diff --git a/tests/test_vlan.py b/tests/test_vlan.py index 13d38c1..11896c0 100644 --- a/tests/test_vlan.py +++ b/tests/test_vlan.py @@ -182,6 +182,7 @@ def test_create_vlan_1(dut, log_level): d=Device(host=dut.host,port=dut.port,protocol=dut.protocol, log_level=log_level) d.open() d.vlan.create(20, name='admin', mtu=1300) + assert '20' in d.vlan assert d.vlan[20]['state'] == 'enable' assert d.vlan[20]['name'] == 'admin' assert d.vlan[20]['mtu'] == 1300