summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntoni S. Puimedon <asegurap@redhat.com>2012-10-04 16:36:55 -0400
committerAntoni S. Puimedon <asegurap@redhat.com>2012-10-04 16:36:55 -0400
commit72c2cb9e299b0510ae76e90b0b4fcc040ab485c7 (patch)
tree2fb082021fea92173ae4b3b950ce2355d1e40742
parentce72837c90a6adf750bee9e2cb2cb30e65ee3fb1 (diff)
downloadlibnl-72c2cb9e299b0510ae76e90b0b4fcc040ab485c7.tar.gz
Enabled the use of Links as context managers.
With this change you can still set do modifications of Links and then to change to pass the changes to the kernel. But it additionally enables you to interact with this part of libnl-python in a more pythonic way. Instead of: eth0 = links['eth0'] eth0.mtu = 5000 eth0.change() you can do: with links['eth0'] as eth0: eth0.mtu = 5000
-rw-r--r--python/netlink/route/link.py9
1 files changed, 9 insertions, 0 deletions
diff --git a/python/netlink/route/link.py b/python/netlink/route/link.py
index ab28f37..36d0e9d 100644
--- a/python/netlink/route/link.py
+++ b/python/netlink/route/link.py
@@ -152,6 +152,15 @@ class Link(netlink.Object):
self.inet = inet.InetLink(self)
self.af = {'inet' : self.inet }
+ def __enter__(self):
+ return self
+
+ def __exit__(self, exc_type, exc_value, tb):
+ if exc_type is None:
+ self.change()
+ else:
+ return false
+
@classmethod
def from_capi(cls, obj):
return cls(capi.link2obj(obj))