python: Use builtin len() function for network interfaces

Use our own len() function for network interfaces as doing
len(container.get_config_item("lxc.network")) will fail when the
list is empty.

Signed-off-by: Stéphane Graber <stgraber@ubuntu.com>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
This commit is contained in:
Stéphane Graber 2012-11-22 18:01:40 -05:00
parent bde1853954
commit 2cdb945b13

View File

@ -115,23 +115,27 @@ class ContainerNetworkList():
self.container = container
def __getitem__(self, index):
count = len(self.container.get_config_item("lxc.network"))
if index >= count:
if index >= len(self):
raise IndexError("list index out of range")
return ContainerNetwork(self.container, index)
def __len__(self):
return len(self.container.get_config_item("lxc.network"))
values = self.container.get_config_item("lxc.network")
if values:
return len(values)
else:
return 0
def add(self, network_type):
index = len(self.container.get_config_item("lxc.network"))
index = len(self)
return self.container.set_config_item("lxc.network.%s.type" % index,
network_type)
def remove(self, index):
count = len(self.container.get_config_item("lxc.network"))
count = len(self)
if index >= count:
raise IndexError("list index out of range")