import files that are relative to the source as nepi.full.path
[nepi.git] / src / nepi / resources / ns3 / resource_manager_generator.py
index 3350d77..6bd9b89 100644 (file)
@@ -29,6 +29,8 @@
 #  PYTHONPATH=$PYTHONPATH:~/repos/nepi/src python src/nepi/resources/ns3/resource_manager_generator.py
 #
 
+from __future__ import print_function
+
 # Force the load of ns3 libraries
 from nepi.resources.ns3.ns3wrapper import load_ns3_module
 
@@ -94,7 +96,7 @@ def create_ns3_rms():
     base = type_id.LookupByName("ns3::Object")
 
     # Create a .py file using the ns-3 RM template for each ns-3 TypeId
-    for i in xrange(tid_count):
+    for i in range(tid_count):
         tid = type_id.GetRegistered(i)
         
         (base_class_import, base_class) = select_base_class(ns3, tid)
@@ -125,9 +127,8 @@ def create_ns3_rms():
         short_rtype = uncamm_rtype.replace("::","-")
 
         d = os.path.dirname(os.path.realpath(__file__))
-        ftemp = open(os.path.join(d, "templates", "resource_manager_template.txt"), "r")
-        template = ftemp.read()
-        ftemp.close()
+        with open(os.path.join(d, "templates", "resource_manager_template.txt"), "r") as ftemp:
+            template = ftemp.read()
 
         template = template. \
                 replace("<CLASS_NAME>", classname). \
@@ -142,22 +143,20 @@ def create_ns3_rms():
                 replace('::', ''). \
                 replace("-","_").lower() + ".py"
 
-        f = open(os.path.join(d, "classes", fname), "w")
-        print os.path.join(d, fname)
-        print template
-        f.write(template)
-        f.close()
+        with open(os.path.join(d, "classes", fname), "w") as f:
+            print(os.path.join(d, fname))
+            print(template)
+            f.write(template)
 
 def template_attributes(ns3, tid): 
     d = os.path.dirname(os.path.realpath(__file__))
-    ftemp = open(os.path.join(d, "templates", "attribute_template.txt"), "r")
-    template = ftemp.read()
-    ftemp.close()
+    with open(os.path.join(d, "templates", "attribute_template.txt"), "r") as ftemp:
+        template = ftemp.read()
 
     attributes = ""
 
     attr_count = tid.GetAttributeN()
-    for i in xrange(attr_count):
+    for i in range(attr_count):
         attr_info = tid.GetAttribute(i)
         if not attr_info.accessor.HasGetter():
             continue
@@ -193,7 +192,7 @@ def template_attributes(ns3, tid):
         elif isinstance(value, ns3.EnumValue):
             attr_type = "Types.Enumerate"
             allowed = checker.GetUnderlyingTypeInformation().split("|")
-            attr_allowed = "[%s]" % ",".join(map(lambda x: "\"%s\"" % x, allowed))
+            attr_allowed = "[%s]" % ",".join(["\"%s\"" % x for x in allowed])
         elif isinstance(value, ns3.DoubleValue):
             attr_type = "Types.Double"
             # TODO: range
@@ -215,14 +214,13 @@ def template_attributes(ns3, tid):
 
 def template_traces(ns3, tid): 
     d = os.path.dirname(os.path.realpath(__file__))
-    ftemp = open(os.path.join(d, "templates", "trace_template.txt"), "r")
-    template = ftemp.read()
-    ftemp.close()
+    with open(os.path.join(d, "templates", "trace_template.txt"), "r") as ftemp:
+        template = ftemp.read()
 
     traces = ""
 
     trace_count = tid.GetTraceSourceN()
-    for i in xrange(trace_count):
+    for i in range(trace_count):
         trace_info = tid.GetTraceSource(i)
         trace_name = trace_info.name
         trace_help = trace_info.help.replace('"', '\\"').replace("'", "\\'")