- # @param pointer is an integer containing the ID of a PLC record
-
- def set_pointer(self, pointer):
- """
- Set the pointer of the record
- """
- self.pointer = pointer
- self['pointer'] = pointer
- self.dirty = True
-
-
- def set_peer_auth(self, peer_authority):
- self.peer_authority = peer_authority
- self['peer_authority'] = peer_authority
- self.dirty = True
-
- ##
- # Return the name (HRN) of the record
-
- def get_name(self):
- """
- Return the name (HRN) of the record
- """
- return self.hrn
-
- ##
- # Return the type of the record
-
- def get_type(self):
- """
- Return the type of the record
- """
- return self.type
-
- ##
- # Return the pointer of the record. The pointer is an integer that may be
- # used to look up the record in the PLC database. The evaluation of pointer
- # depends on the type of the record
-
- def get_pointer(self):
- """
- Return the pointer of the record. The pointer is an integer that may be
- used to look up the record in the PLC database. The evaluation of pointer
- depends on the type of the record
- """
- return self.pointer
-
- ##
- # Return the GID of the record, in the form of a GID object
- # TODO: not the best name for the function, because we have things called
- # gidObjects in the Cred
-
- def get_gid_object(self):
- """
- Return the GID of the record, in the form of a GID object
- """
- return GID(string=self.gid)
-
- ##
- # Returns the value of a field
-
- def get_field(self, fieldname, default=None):
- # sometimes records act like classes, and sometimes they act like dicts
- try:
- return getattr(self, fieldname)
- except AttributeError:
- try:
- return self[fieldname]
- except KeyError:
- if default != None:
- return default
- else:
- raise
-
- ##
- # Returns a list of field names in this record.
-
- def get_field_names(self):
- """
- Returns a list of field names in this record.
- """
- return self.fields.keys()
-
- ##
- # Given a field name ("hrn", "gid", ...) return the value of that field.
- #
- # @param fieldname is the name of field to be returned
-
- def get_field_value_string(self, fieldname):
- """
- Given a field name ("hrn", "gid", ...) return the value of that field.
- """
- if fieldname == "authority":
- val = get_authority(self['hrn'])
- else:
- try:
- val = getattr(self, fieldname)
- except:
- val = self[fieldname]
- if isinstance(val, str):
- return "'" + str(val) + "'"
- else:
- return str(val)
-
- ##
- # Given a list of field names, return a list of values for those public.
- #
- # @param fieldnames is a list of field names
-
- def get_field_value_strings(self, fieldnames):
- """
- Given a list of field names, return a list of values for those public.
- """
- return [ self.get_field_value_string (fieldname) for fieldname in fieldnames ]
-
- ##
- # Return the record in the form of a dictionary
-
- def as_dict(self):
- """
- Return the record in the form of a dictionary
- """
- return dict(self)
-
- ##
- # Load the record from a dictionary
- #
- # @param dict dictionary to load record public from
-
- def load_from_dict(self, dict):
- """
- Load the record from a dictionary
- """
-
- self.set_name(dict['hrn'])
- gidstr = dict.get("gid", None)
- if gidstr:
- self.set_gid(dict['gid'])
-
- if "pointer" in dict:
- self.set_pointer(dict['pointer'])
-
- self.set_type(dict['type'])
- self.update(dict)
-
- ##
- # Save the record to a string. The string contains an XML representation of
- # the record.
-
- def save_to_string(self):
- """
- Save the record to a string. The string contains an XML representation of
- the record.
- """
- recorddict = self.as_dict()
- filteredDict = dict([(key, val) for (key, val) in recorddict.iteritems() if key in self.fields.keys()])
- xml_record = XML('<record/>')
- xml_record.parse_dict(filteredDict)
- str = xml_record.toxml()
- return str
-
- ##
- # Load the record from a string. The string is assumed to contain an XML
- # representation of the record.
-
- def load_from_string(self, str):
- """
- Load the record from a string. The string is assumed to contain an XML
- representation of the record.
- """
- #dict = xmlrpclib.loads(str)[0][0]
-
- xml_record = XML(str)
- self.load_from_dict(xml_record.todict())
-
- ##
- # Dump the record to stdout
- #
- # @param dump_parents if true, then the parents of the GID will be dumped
-
- def dump_text(self, dump_parents=False):
- """
- Walk tree and dump records.
- """
- # print core fields in this order
- print "".join(['=' for i in range(40)])
- print "RECORD"
- print " hrn:", self.get('hrn')
- print " type:", self.get('type')
- print " authority:", self.get('authority')
- date_created = utcparse(datetime_to_string(self.get('date_created')))
- print " date created:", date_created
- last_updated = utcparse(datetime_to_string(self.get('last_updated')))
- print " last updated:", last_updated
- print " gid:"
- print "\t\t", self.get_gid_object().dump_string(8, dump_parents)
-
- # print remaining fields
- all_fields = set(UserRecord.fields.keys() +
- AuthorityRecord.fields.keys() +
- SliceRecord.fields.keys() +
- NodeRecord.fields.keys())
- for field in self:
- # dont print core fields
- if field in all_fields and field not in SfaRecord.fields:
- print " %s: %s" % (field, self[field])
-
- def dump(self, format=None, dump_parents=False):
+ 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 list(d.items()) if not exclude(k, v)]
+ return {k: d[k] for k in keys}
+
+ def toxml(self):
+ return self.save_as_xml()
+
+ def load_from_dict(self, d):
+ for (k, v) in d.items():
+ # experimental
+ if isinstance(v, StringType) and v.lower() in ['true']:
+ v = True
+ if isinstance(v, StringType) 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 fields(self):
+ fields = list(self.__dict__.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.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, sort=False):