def field_description_from_view_field(collection, field, view_context=None, group_ids_seen=[]): """ Returns a field description value created using information from a field reference in a view description record (i.e. a dictionary containing a field id value and optional field property URI and placement values. (The optional values, if not provided, are obtained from the referenced field description) collection is a collection from which data is being rendered. field is a dictionary with the field description from a view or list description, containing a field id and placement values. view_context is a dictionary of additional values that may be used in assembling values to be used when rendering the field. In particular, a copy of the view description record provides context for some enumeration type selections. group_ids_seen group ids expanded so far, to check for recursive reference. """ #@@TODO: for resilience, revert this when all tests pass? # field_id = field.get(ANNAL.CURIE.field_id, "Field_id_missing") # Field ID slug in URI #@@ field_id = extract_entity_id(field[ANNAL.CURIE.field_id]) recordfield = RecordField.load(collection, field_id, altscope="all") if recordfield is None: log.warning("Can't retrieve definition for field %s" % (field_id)) recordfield = RecordField.load(collection, "Field_missing", altscope="all") # If field references group, pull in group details group_ref = extract_entity_id(recordfield.get(ANNAL.CURIE.group_ref, None)) if group_ref: group_view = RecordGroup.load(collection, group_ref, altscope="all") if not group_view: log.error("Group %s used in field %s" % (group_ref, field_id)) # log.error("".join(traceback.format_stack())) # ex_type, ex, tb = sys.exc_info() # traceback.print_tb(tb) # raise EntityNotFound_Error("Group %s used in field %s"%(group_ref, field_id)) else: group_view = None # If present, `field_property` and `field_placement` override values in the field dexcription return FieldDescription(collection, recordfield, view_context=view_context, field_property=field.get(ANNAL.CURIE.property_uri, None), field_placement=field.get( ANNAL.CURIE.field_placement, None), group_view=group_view, group_ids_seen=group_ids_seen)
def field_description_from_view_field(collection, field, view_context=None): """ Returns a field description value created using information from a field reference in a view description record (i.e. a dictionary containing a field id value and optional field property URI and placement values. (The optional values, if not provided, are obtained from the referenced field descriptionb) collection is a collection from which data is being rendered. field is a dictionary with the field description from a view or list description, containing a field id and placement values. view_context is a dictionary of additional values that may be used in assembling values to be used when rendering the field. In particular, a copy of the view description record provides context for some enumeration type selections. """ #@@TODO: for resilience, revert this when all tests pass? # field_id = field.get(ANNAL.CURIE.field_id, "Field_id_missing") # Field ID slug in URI #@@ field_id = field[ANNAL.CURIE.field_id] recordfield = RecordField.load(collection, field_id, collection._parentsite) if recordfield is None: log.warning("Can't retrieve definition for field %s" % (field_id)) recordfield = RecordField.load(collection, "Field_missing", collection._parentsite) field_property = (field.get(ANNAL.CURIE.property_uri, None) or recordfield.get(ANNAL.CURIE.property_uri, "")) field_placement = get_placement_classes( field.get(ANNAL.CURIE.field_placement, None) or recordfield.get(ANNAL.CURIE.field_placement, "")) # If field references group, pull in field details group_ref = recordfield.get(ANNAL.CURIE.group_ref, None) if group_ref: group_view = RecordGroup.load(collection, group_ref, collection._parentsite) if not group_view: raise EntityNotFound_Error("Group %s used in field %s" % (group_ref, field_id)) else: group_view = None return FieldDescription(collection, recordfield, view_context=view_context, field_property=field.get(ANNAL.CURIE.property_uri, None), field_placement=field.get( ANNAL.CURIE.field_placement, None), group_view=group_view)
def field_description_from_view_field(collection, field, view_context=None, group_ids_seen=[]): """ Returns a field description value created using information from a field reference in a view description record (i.e. a dictionary containing a field id value and optional field property URI and placement values. (The optional values, if not provided, are obtained from the referenced field description) collection is a collection from which data is being rendered. field is a dictionary with the field description from a view or list description, containing a field id and placement values. view_context is a dictionary of additional values that may be used in assembling values to be used when rendering the field. In particular, a copy of the view description record provides context for some enumeration type selections. group_ids_seen group ids expanded so far, to check for recursive reference. """ #@@TODO: for resilience, revert this when all tests pass? # field_id = field.get(ANNAL.CURIE.field_id, "Field_id_missing") # Field ID slug in URI #@@ field_id = extract_entity_id(field[ANNAL.CURIE.field_id]) recordfield = RecordField.load(collection, field_id, altscope="all") if recordfield is None: log.warning("Can't retrieve definition for field %s"%(field_id)) recordfield = RecordField.load(collection, "Field_missing", altscope="all") # If field references group, pull in group details group_ref = extract_entity_id(recordfield.get(ANNAL.CURIE.group_ref, None)) if group_ref: group_view = RecordGroup.load(collection, group_ref, altscope="all") if not group_view: log.error("Group %s used in field %s"%(group_ref, field_id)) # log.error("".join(traceback.format_stack())) # ex_type, ex, tb = sys.exc_info() # traceback.print_tb(tb) # raise EntityNotFound_Error("Group %s used in field %s"%(group_ref, field_id)) else: group_view = None # If present, `field_property` and `field_placement` override values in the field dexcription return FieldDescription( collection, recordfield, view_context=view_context, field_property=field.get(ANNAL.CURIE.property_uri, None), field_placement=field.get(ANNAL.CURIE.field_placement, None), group_view=group_view, group_ids_seen=group_ids_seen )
def get_field_uri_jsonld_context(self, fid, get_field_context): """ Access field description, and return field property URI and appropriate property description for JSON-LD context. If there is no corresponding field description, returns (None, None) If no context should be generated for the field URI, returns (uri, None) """ f = RecordField.load(self, fid, altscope="all") if f is None: return (None, None) # @@debugging@@ # if fid in ["Entity_id", "List_id"]: # print "@@ %s field %r"%(fid, f) # print "@@ %s context %r"%(fid, get_field_context(f)) return (f[ANNAL.CURIE.property_uri], get_field_context(f))
def cache_get_field(self, field_id): """ Retrieve field from cache. Returns field entity if found, otherwise None. """ #@@ field_cache.get_field(self, field_id) t = field_cache.get_field(self, field_id) # Was it previously created but not cached? if not t and RecordField.exists(self, field_id, altscope="all"): msg = ( "Collection.get_field %s present but not cached for collection %s"% (field_id, self.get_id()) ) log.warning(msg) t = RecordField.load(self, field_id, altscope="all") field_cache.set_field(self, t) return t
def cache_get_field(self, field_id): """ Retrieve field from cache. Returns field entity if found, otherwise None. """ field_cache.get_field(self, field_id) t = field_cache.get_field(self, field_id) # Was it previously created but not cached? if not t and RecordField.exists(self, field_id, altscope="all"): msg = ( "Collection.get_field %s present but not cached for collection %s"% (field_id, self.get_id()) ) log.warning(msg) t = RecordField.load(self, field_id, altscope="all") field_cache.set_field(self, t) return t
def get_field_uri_jsonld_context(self, fid, get_field_context): """ Access field description, and return field property URI and appropriate property description for JSON-LD context. Returns a triple consisting of the field property URI, the context information to be generated for the field, and a list of any field references contained directly within the field definition (as opposed to a field group reference) If there is no corresponding field description, returns (None, None, None) If no context should be generated for the field URI, returns (uri, None, field_list) The field list returned is 'None' if there is no contained list of fields. """ f = RecordField.load(self, fid, altscope="all") if f is None: return (None, None, None) field_list = f.get(ANNAL.CURIE.field_fields, None) return (f[ANNAL.CURIE.property_uri], get_field_context(f), field_list)
def coll_field(coll, field_id): """ Return identified field in collection, or None """ return RecordField.load(coll, field_id, altscope="all")
def coll_type(coll, type_id): """ Return identified type in collection, or None """ return RecordField.load(coll, field_id, altscope="all")