- 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 ] )
- def load_from_dict (self, d):
- for (k,v) in d.iteritems():
- # experimental
- if isinstance(v, StringTypes) and v.lower() in ['true']: v=True
- if isinstance(v, StringTypes) and v.lower() in ['false']: v=False
- setattr(self,k,v)
-
- # in addition we provide convenience for converting to and from xml records
- # for this purpose only, we need the subclasses to define 'fields' as either
- # a list or a dictionary
- def xml_fields (self):
- fields=self.fields
- if isinstance(fields,dict): fields=fields.keys()
- return fields
-
- def save_as_xml (self):
- # xxx not sure about the scope here
- input_dict = dict( [ (key, getattr(self.key), ) for key in self.xml_fields() if getattr(self,key,None) ] )
- xml_record=XML("<record />")
- xml_record.parse_dict (input_dict)
- return xml_record.toxml()
-
- def dump(self, format=None, dump_parents=False):
- if not format:
- format = 'text'
- else:
- format = format.lower()
- if format == 'text':
- self.dump_text(dump_parents)
- elif format == 'xml':
- print self.save_to_string()
- elif format == 'simple':
- print self.dump_simple()
- else:
- raise Exception, "Invalid format %s" % format
-
- def dump_text(self, dump_parents=False):
- # print core fields in this order
- core_fields = ['hrn', 'type', 'authority', 'gid', 'date_created', 'last_updated']
- print "".join(['=' for i in range(40)])
- print "RECORD"
- print " hrn:", self.hrn
- print " type:", self.type
- print " authority:", self.authority
- date_created = utcparse(datetime_to_string(self.date_created))
- print " date created:", date_created
- last_updated = utcparse(datetime_to_string(self.last_updated))
- print " last updated:", last_updated
- print " gid:"
- print "\t\t", self.get_gid_object().dump_string(8, dump_parents)
-
- # print remaining fields
- for attrib_name in dir(self):
- # skip core fields
- if attrib_name in core_fields:
- continue
- # skik callables
- attrib = getattr(self, attrib_name)
- if callable(attrib):
- continue
- print " %s: %s" % (attrib_name, attrib)
-
- def dump_simple(self):
- return "Record(record_id=%s, hrn=%s, type=%s, authority=%s, pointer=%s)" % \
- (self.record_id, self.hrn, self.type, self.authority, self.pointer)
-