dect
/
libnl
Archived
13
0
Fork 0

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
This commit is contained in:
Antoni S. Puimedon 2012-10-04 16:36:55 -04:00
parent ce72837c90
commit 72c2cb9e29
1 changed files with 9 additions and 0 deletions

View File

@ -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))