various formatting, including mixes of tab and spaces detected in py3
[nepi.git] / src / nepi / util / netgraph.py
index 744a95b..dfa0897 100644 (file)
@@ -16,7 +16,7 @@
 #
 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
 
-import ipaddr
+import ipaddress
 import networkx
 import math
 import random
@@ -72,14 +72,14 @@ class NetGraph(object):
             :param assign_st: Select source and target nodes on the graph.
             :type assign_st: bool
 
-           :param sources_targets: dictionary with the list of sources (key =
+            :param sources_targets: dictionary with the list of sources (key =
             "sources") and list of targets (key = "targets") if defined, ignore
             assign_st
-           :type sources_targets: dictionary of lists
+            :type sources_targets: dictionary of lists
 
-           :param leaf_source: if True, random sources will be selected only 
+            :param leaf_source: if True, random sources will be selected only 
             from leaf nodes.
-           :type leaf_source: bool
+            :type leaf_source: bool
 
         NOTE: Only point-to-point like network topologies are supported for now.
                 (Wireless and Ethernet networks were several nodes share the same
@@ -107,11 +107,11 @@ class NetGraph(object):
             self.assign_p2p_ips(network = network, prefix = prefix, 
                     version = version)
 
-       sources_targets = kwargs.get("sources_targets")
-       if sources_targets:
+        sources_targets = kwargs.get("sources_targets")
+        if sources_targets:
             [self.set_source(n) for n in sources_targets["sources"]]
-           [self.set_target(n) for n in sources_targets["targets"]]
-       elif kwargs.get("assign_st"):
+            [self.set_target(n) for n in sources_targets["targets"]]
+        elif kwargs.get("assign_st"):
             self.select_target_zero()
             self.select_random_source(is_leaf = kwargs.get("leaf_source"))
 
@@ -155,9 +155,9 @@ class NetGraph(object):
             nodesinbranch = (node_count - 1)/ BRANCHES
             c = 1
 
-            for i in xrange(BRANCHES):
+            for i in range(BRANCHES):
                 prev = 0
-                for n in xrange(1, nodesinbranch + 1):
+                for n in range(1, nodesinbranch + 1):
                     graph.add_node(c)
                     graph.add_edge(prev, c)
                     prev = c
@@ -188,7 +188,7 @@ class NetGraph(object):
     def annotate_node(self, nid, name, value):
         if not isinstance(value, str) and not isinstance(value, int) and \
                 not isinstance(value, float) and not isinstance(value, bool):
-            raise RuntimeError, "Non-serializable annotation"
+            raise RuntimeError("Non-serializable annotation")
 
         self.topology.node[nid][name] = value
     
@@ -196,7 +196,7 @@ class NetGraph(object):
         return self.topology.node[nid].get(name)
 
     def node_annotations(self, nid):
-        return self.topology.node[nid].keys()
+        return list(self.topology.node[nid].keys())
     
     def del_node_annotation(self, nid, name):
         del self.topology.node[nid][name]
@@ -204,7 +204,7 @@ class NetGraph(object):
     def annotate_edge(self, nid1, nid2, name, value):
         if not isinstance(value, str) and not isinstance(value, int) and \
                 not isinstance(value, float) and not isinstance(value, bool):
-            raise RuntimeError, "Non-serializable annotation"
+            raise RuntimeError("Non-serializable annotation")
 
         self.topology.edge[nid1][nid2][name] = value
    
@@ -224,7 +224,7 @@ class NetGraph(object):
         return self.topology.edge[nid1][nid2].get(name)
  
     def edge_annotations(self, nid1, nid2):
-        return self.topology.edge[nid1][nid2].keys()
+        return list(self.topology.edge[nid1][nid2].keys())
     
     def del_edge_annotation(self, nid1, nid2, name):
         del self.topology.edge[nid1][nid2][name]
@@ -250,13 +250,13 @@ class NetGraph(object):
         # Assign IP addresses to host
         netblock = "%s/%d" % (network, prefix)
         if version == 4:
-            net = ipaddr.IPv4Network(netblock)
+            net = ipaddress.ip_network(netblock)
             new_prefix = 30
         elif version == 6:
-            net = ipaddr.IPv6Network(netblock)
+            net = ipaddress.ip_network(netblock)
             new_prefix = 30
         else:
-            raise RuntimeError, "Invalid IP version %d" % version
+            raise RuntimeError("Invalid IP version %d" % version)
         
         ## Clear all previusly assigned IPs
         for nid in self.topology.nodes():
@@ -269,15 +269,15 @@ class NetGraph(object):
             #### Compute subnets for each link
             
             # get a subnet of base_add with prefix /30
-            subnet = sub_itr.next()
+            subnet = next(sub_itr)
             mask = subnet.netmask.exploded
             network = subnet.network.exploded
             prefixlen = subnet.prefixlen
 
             # get host addresses in that subnet
             i = subnet.iterhosts()
-            addr1 = i.next()
-            addr2 = i.next()
+            addr1 = next(i)
+            addr2 = next(i)
 
             ip1 = addr1.exploded
             ip2 = addr2.exploded
@@ -333,7 +333,7 @@ class NetGraph(object):
             source = leaves.pop(random.randint(0, len(leaves) - 1))
         else:
             # options must not be already sources or targets
-            options = [ k for k,v in self.topology.degree().iteritems() \
+            options = [ k for k,v in self.topology.degree().items() \
                     if (not kwargs.get("is_leaf") or v == 1)  \
                         and not self.topology.node[k].get("source") \
                         and not self.topology.node[k].get("target")]