fix bugs
[sfa.git] / geni / util / record.py
index 086723d..523b8d5 100644 (file)
@@ -5,8 +5,9 @@
 ##
 
 import report
+from types import StringTypes
 from gid import *
-
+from geni.util.rspec import *
 ##
 # The GeniRecord class implements a Geni Record. A GeniRecord is a tuple
 # (Name, GID, Type, Info).
@@ -28,7 +29,7 @@ from gid import *
 # of different types. For example, planetlab.us.arizona may have both an SA
 # and a MA record, but cannot have two SA records.
 
-class GeniRecord():
+class GeniRecord:
 
     ##
     # Create a Geni Record
@@ -75,7 +76,7 @@ class GeniRecord():
     # @param gid is a GID object or the string representation of a GID object
 
     def set_gid(self, gid):
-        if isinstance(gid, str):
+        if isinstance(gid, StringTypes):
             self.gid = gid
         else:
             self.gid = gid.save_to_string(save_parents=True)
@@ -229,7 +230,10 @@ class GeniRecord():
 
     def load_from_dict(self, dict):
         self.set_name(dict['name'])
-        self.set_gid(dict['gid'])
+        gidstr = dict.get("gid", None)
+        if gidstr:
+            self.set_gid(dict['gid'])
+
         self.set_type(dict['type'])
         self.set_pointer(dict['pointer'])
         if "pl_info" in dict:
@@ -242,8 +246,12 @@ class GeniRecord():
     # the record.
 
     def save_to_string(self):
+
         dict = self.as_dict()
-        str = xmlrpclib.dumps((dict,), allow_none=True)
+        record = RecordSpec()
+        record.parseDict(dict)
+        str = record.toxml()
+        #str = xmlrpclib.dumps((dict,), allow_none=True)
         return str
 
     ##
@@ -251,8 +259,13 @@ class GeniRecord():
     # representation of the record.
 
     def load_from_string(self, str):
-        dict = xmlrpclib.loads(str)[0][0]
-        self.load_from_dict(dict)
+        #dict = xmlrpclib.loads(str)[0][0]
+        
+        record = RecordSpec()
+        record.parseString(str)
+        record_dict = record.toDict()
+        geni_dict = record_dict['record']
+        self.load_from_dict(geni_dict)
 
     ##
     # Dump the record to stdout
@@ -279,7 +292,13 @@ class GeniRecord():
         print "    pl_info:"
         pl_info = getattr(self, "pl_info", {})
         if pl_info:
+
             for key in pl_info.keys():
                 print "       ", key, ":", pl_info[key]
 
 
+    def getdict(self):
+        info = {'hrn': self.name, 'type': self.type, 'gid': self.gid}
+        info.update(getattr(self, "geni_info", {}))
+        info.update(getattr(self, "pl_info", {}))
+        return info