Drop Python 3.4 and switch to async / await syntax for asyncio. Fixes #1425

This commit is contained in:
grossmj committed 2018-10-15 17:05:49 +07:00
1 parent 8baa480b79
commit 76af98404a
119 files changed
+2432 -3084

No files matched your search

+21 -31
View File
@@ -48,53 +48,48 @@ class NIO:
self._output_filter_options = None # no output filter options by default
self._dynamips_direction = {"in": 0, "out": 1, "both": 2}
@asyncio.coroutine
def list(self):
async def list(self):
"""
Returns all NIOs.
:returns: NIO list
"""
nio_list = yield from self._hypervisor.send("nio list")
nio_list = await self._hypervisor.send("nio list")
return nio_list
@asyncio.coroutine
def delete(self):
async def delete(self):
"""
Deletes this NIO.
"""
if self._input_filter or self._output_filter:
yield from self.unbind_filter("both")
yield from self._hypervisor.send("nio delete {}".format(self._name))
await self.unbind_filter("both")
await self._hypervisor.send("nio delete {}".format(self._name))
log.info("NIO {name} has been deleted".format(name=self._name))
@asyncio.coroutine
def rename(self, new_name):
async def rename(self, new_name):
"""
Renames this NIO
:param new_name: new NIO name
"""
yield from self._hypervisor.send("nio rename {name} {new_name}".format(name=self._name, new_name=new_name))
await self._hypervisor.send("nio rename {name} {new_name}".format(name=self._name, new_name=new_name))
log.info("NIO {name} renamed to {new_name}".format(name=self._name, new_name=new_name))
self._name = new_name
@asyncio.coroutine
def debug(self, debug):
async def debug(self, debug):
"""
Enables/Disables debugging for this NIO.
:param debug: debug value (0 = disable, enable = 1)
"""
yield from self._hypervisor.send("nio set_debug {name} {debug}".format(name=self._name, debug=debug))
await self._hypervisor.send("nio set_debug {name} {debug}".format(name=self._name, debug=debug))
@asyncio.coroutine
def bind_filter(self, direction, filter_name):
async def bind_filter(self, direction, filter_name):
"""
Adds a packet filter to this NIO.
Filter "freq_drop" drops packets.
@@ -108,7 +103,7 @@ class NIO:
raise DynamipsError("Unknown direction {} to bind filter {}:".format(direction, filter_name))
dynamips_direction = self._dynamips_direction[direction]
yield from self._hypervisor.send("nio bind_filter {name} {direction} {filter}".format(name=self._name,
await self._hypervisor.send("nio bind_filter {name} {direction} {filter}".format(name=self._name,
direction=dynamips_direction,
filter=filter_name))
@@ -120,8 +115,7 @@ class NIO:
self._input_filter = filter_name
self._output_filter = filter_name
@asyncio.coroutine
def unbind_filter(self, direction):
async def unbind_filter(self, direction):
"""
Removes packet filter for this NIO.
@@ -132,7 +126,7 @@ class NIO:
raise DynamipsError("Unknown direction {} to unbind filter:".format(direction))
dynamips_direction = self._dynamips_direction[direction]
yield from self._hypervisor.send("nio unbind_filter {name} {direction}".format(name=self._name,
await self._hypervisor.send("nio unbind_filter {name} {direction}".format(name=self._name,
direction=dynamips_direction))
if direction == "in":
@@ -143,8 +137,7 @@ class NIO:
self._input_filter = None
self._output_filter = None
@asyncio.coroutine
def setup_filter(self, direction, options):
async def setup_filter(self, direction, options):
"""
Setups a packet filter bound with this NIO.
@@ -166,7 +159,7 @@ class NIO:
raise DynamipsError("Unknown direction {} to setup filter:".format(direction))
dynamips_direction = self._dynamips_direction[direction]
yield from self._hypervisor.send("nio setup_filter {name} {direction} {options}".format(name=self._name,
await self._hypervisor.send("nio setup_filter {name} {direction} {options}".format(name=self._name,
direction=dynamips_direction,
options=options))
@@ -198,24 +191,22 @@ class NIO:
return self._output_filter, self._output_filter_options
@asyncio.coroutine
def get_stats(self):
async def get_stats(self):
"""
Gets statistics for this NIO.
:returns: NIO statistics (string with packets in, packets out, bytes in, bytes out)
"""
stats = yield from self._hypervisor.send("nio get_stats {}".format(self._name))
stats = await self._hypervisor.send("nio get_stats {}".format(self._name))
return stats[0]
@asyncio.coroutine
def reset_stats(self):
async def reset_stats(self):
"""
Resets statistics for this NIO.
"""
yield from self._hypervisor.send("nio reset_stats {}".format(self._name))
await self._hypervisor.send("nio reset_stats {}".format(self._name))
@property
def bandwidth(self):
@@ -227,15 +218,14 @@ class NIO:
return self._bandwidth
@asyncio.coroutine
def set_bandwidth(self, bandwidth):
async def set_bandwidth(self, bandwidth):
"""
Sets bandwidth constraint.
:param bandwidth: bandwidth integer value (in Kb/s)
"""
yield from self._hypervisor.send("nio set_bandwidth {name} {bandwidth}".format(name=self._name, bandwidth=bandwidth))
await self._hypervisor.send("nio set_bandwidth {name} {bandwidth}".format(name=self._name, bandwidth=bandwidth))
self._bandwidth = bandwidth
@property
@@ -43,10 +43,9 @@ class NIOGenericEthernet(NIO):
self._ethernet_device = ethernet_device
super().__init__(name, hypervisor)
@asyncio.coroutine
def create(self):
async def create(self):
yield from self._hypervisor.send("nio create_gen_eth {name} {eth_device}".format(name=self._name,
await self._hypervisor.send("nio create_gen_eth {name} {eth_device}".format(name=self._name,
eth_device=self._ethernet_device))
log.info("NIO Generic Ethernet {name} created with device {device}".format(name=self._name,
@@ -42,10 +42,9 @@ class NIOLinuxEthernet(NIO):
self._ethernet_device = ethernet_device
super().__init__(name, hypervisor)
@asyncio.coroutine
def create(self):
async def create(self):
yield from self._hypervisor.send("nio create_linux_eth {name} {eth_device}".format(name=self._name,
await self._hypervisor.send("nio create_linux_eth {name} {eth_device}".format(name=self._name,
eth_device=self._ethernet_device))
log.info("NIO Linux Ethernet {name} created with device {device}".format(name=self._name,
+2 -3
View File
@@ -41,10 +41,9 @@ class NIONull(NIO):
name = 'null-{}'.format(uuid.uuid4())
super().__init__(name, hypervisor)
@asyncio.coroutine
def create(self):
async def create(self):
yield from self._hypervisor.send("nio create_null {}".format(self._name))
await self._hypervisor.send("nio create_null {}".format(self._name))
log.info("NIO NULL {name} created.".format(name=self._name))
def __json__(self):
+2 -3
View File
@@ -43,10 +43,9 @@ class NIOTAP(NIO):
self._tap_device = tap_device
super().__init__(name, hypervisor)
@asyncio.coroutine
def create(self):
async def create(self):
yield from self._hypervisor.send("nio create_tap {name} {tap}".format(name=self._name, tap=self._tap_device))
await self._hypervisor.send("nio create_tap {name} {tap}".format(name=self._name, tap=self._tap_device))
log.info("NIO TAP {name} created with device {device}".format(name=self._name, device=self._tap_device))
@property
+8 -11
View File
@@ -53,13 +53,12 @@ class NIOUDP(NIO):
self._node = node
super().__init__(name, node.hypervisor)
@asyncio.coroutine
def create(self):
async def create(self):
if not self._hypervisor:
return
# Ubridge is not supported
if not hasattr(self._node, "add_ubridge_udp_connection"):
yield from self._hypervisor.send("nio create_udp {name} {lport} {rhost} {rport}".format(name=self._name,
await self._hypervisor.send("nio create_udp {name} {lport} {rhost} {rport}".format(name=self._name,
lport=self._lport,
rhost=self._rhost,
rport=self._rport))
@@ -67,7 +66,7 @@ class NIOUDP(NIO):
self._local_tunnel_lport = self._node.manager.port_manager.get_free_udp_port(self._node.project)
self._local_tunnel_rport = self._node.manager.port_manager.get_free_udp_port(self._node.project)
self._bridge_name = 'DYNAMIPS-{}-{}'.format(self._local_tunnel_lport, self._local_tunnel_rport)
yield from self._hypervisor.send("nio create_udp {name} {lport} {rhost} {rport}".format(name=self._name,
await self._hypervisor.send("nio create_udp {name} {lport} {rhost} {rport}".format(name=self._name,
lport=self._local_tunnel_lport,
rhost='127.0.0.1',
rport=self._local_tunnel_rport))
@@ -84,24 +83,22 @@ class NIOUDP(NIO):
self._rhost,
self._rport)
self._destination_nio.filters = self._filters
yield from self._node.add_ubridge_udp_connection(
await self._node.add_ubridge_udp_connection(
self._bridge_name,
self._source_nio,
self._destination_nio
)
@asyncio.coroutine
def update(self):
async def update(self):
self._destination_nio.filters = self._filters
yield from self._node.update_ubridge_udp_connection(
await self._node.update_ubridge_udp_connection(
self._bridge_name,
self._source_nio,
self._destination_nio)
@asyncio.coroutine
def close(self):
async def close(self):
if self._local_tunnel_lport:
yield from self._node.ubridge_delete_bridge(self._bridge_name)
await self._node.ubridge_delete_bridge(self._bridge_name)
self._node.manager.port_manager.release_udp_port(self._local_tunnel_lport, self ._node.project)
if self._local_tunnel_rport:
self._node.manager.port_manager.release_udp_port(self._local_tunnel_rport, self._node.project)
+2 -3
View File
@@ -45,10 +45,9 @@ class NIOUNIX(NIO):
self._remote_file = remote_file
super().__init__(name, hypervisor)
@asyncio.coroutine
def create(self):
async def create(self):
yield from self._hypervisor.send("nio create_unix {name} {local} {remote}".format(name=self._name,
await self._hypervisor.send("nio create_unix {name} {local} {remote}".format(name=self._name,
local=self._local_file,
remote=self._remote_file))
+2 -3
View File
@@ -45,10 +45,9 @@ class NIOVDE(NIO):
self._local_file = local_file
super().__init__(name, hypervisor)
@asyncio.coroutine
def create(self):
async def create(self):
yield from self._hypervisor.send("nio create_vde {name} {control} {local}".format(name=self._name,
await self._hypervisor.send("nio create_vde {name} {control} {local}".format(name=self._name,
control=self._control_file,
local=self._local_file))