Record.todict behaves suspisciously
[sfa.git] / sfa / storage / record.py
index f3aa8ee..5931765 100644 (file)
@@ -4,14 +4,16 @@ from datetime import datetime
 from sfa.util.xml import XML
 from sfa.trust.gid import GID
 
+from sfa.util.sfalogging import logger
+
 class Record:
 
-    def __init__(self, dict=None, xml=None):
+    def __init__(self, dict=None, xml_str=None):
         if dict:
             self.load_from_dict(dict)
-        elif xml:
-            xml_record = XML(xml)
-            xml_dict = xml_record.todict()
+        elif xml_str:
+            xml = XML(xml_str)
+            xml_dict = xml.todict()
             self.load_from_dict(xml_dict)  
 
 
@@ -33,10 +35,18 @@ class Record:
         # fallback
         return "** undef_datetime **"
     
-    def todict (self):
-        d=self.__dict__
-        keys=[k for k in d.keys() if not k.startswith('_')]
-        return dict ( [ (k,d[k]) for k in keys ] )
+    #
+    # need to filter out results, esp. wrt relationships
+    # exclude_types must be a tuple so we can use isinstance
+    # 
+    def record_to_dict (self, exclude_types=None):
+        if exclude_types is None:
+            exclude_types = ()
+        d = self.__dict__
+        def exclude (k, v):
+            return k.startswith('_') or isinstance (v, exclude_types)
+        keys = [ k for k, v in d.items() if not exclude(k, v) ]
+        return { k : d[k] for k in keys }
     
     def toxml(self):
         return self.save_as_xml()
@@ -91,7 +101,7 @@ class Record:
             # handle gid 
             if attrib_name == 'gid':
                 print "    gid:"      
-                print GID(attrib).dump_string(8, dump_parents)
+                print GID(string=attrib).dump_string(8, dump_parents)
             elif attrib_name in ['date created', 'last updated']:
                 print "    %s: %s" % (attrib_name, self.date_repr(attrib_name))
             else: