def get_resource_schema(resource_type): try: #Validate requesting user and expiry and add governance headers ion_actor_id, expiry = get_governance_info_from_request() ion_actor_id, expiry = validate_request(ion_actor_id, expiry) schema_info = dict() #Prepare the dict entry for schema information including all of the internal object types schema_info['schemas'] = dict() #ION Objects are not registered as UNICODE names ion_object_name = str(resource_type) ret_obj = IonObject(ion_object_name, {}) # If it's an op input param or response message object. # Walk param list instantiating any params that were marked None as default. if hasattr(ret_obj, "_svc_name"): schema = ret_obj._schema for field in ret_obj._schema: if schema[field]["default"] is None: try: value = IonObject(schema[field]["type"], {}) except NotFound: # TODO # Some other non-IonObject type. Just use None as default for now. value = None setattr(ret_obj, field, value) #Add schema information for sub object types schema_info['schemas'][ion_object_name] = ret_obj._schema for field in ret_obj._schema: obj_type = ret_obj._schema[field]['type'] #First look for ION objects if is_ion_object(obj_type): try: value = IonObject(obj_type, {}) schema_info['schemas'][obj_type] = value._schema except NotFound: pass #Next look for ION Enums elif ret_obj._schema[field].has_key('enum_type'): if isenum(ret_obj._schema[field]['enum_type']): value = IonObject(ret_obj._schema[field]['enum_type'], {}) schema_info['schemas'][ret_obj._schema[field]['enum_type']] = value._str_map #Add an instance of the resource type object schema_info['object'] = ret_obj return gateway_json_response(schema_info) except Exception, e: return build_error_response(e)
def get_resource_schema(resource_type): try: #Validate requesting user and expiry and add governance headers ion_actor_id, expiry = get_governance_info_from_request() ion_actor_id, expiry = validate_request(ion_actor_id, expiry) schema_info = dict() #Prepare the dict entry for schema information including all of the internal object types schema_info['schemas'] = dict() #ION Objects are not registered as UNICODE names ion_object_name = str(resource_type) ret_obj = IonObject(ion_object_name, {}) # If it's an op input param or response message object. # Walk param list instantiating any params that were marked None as default. if hasattr(ret_obj, "_svc_name"): schema = ret_obj._schema for field in ret_obj._schema: if schema[field]["default"] is None: try: value = IonObject(schema[field]["type"], {}) except NotFound: # TODO # Some other non-IonObject type. Just use None as default for now. value = None setattr(ret_obj, field, value) #Add schema information for sub object types schema_info['schemas'][ion_object_name] = ret_obj._schema for field in ret_obj._schema: obj_type = ret_obj._schema[field]['type'] #First look for ION objects if is_ion_object(obj_type): try: value = IonObject(obj_type, {}) schema_info['schemas'][obj_type] = value._schema except NotFound: pass #Next look for ION Enums elif ret_obj._schema[field].has_key('enum_type'): if isenum(ret_obj._schema[field]['enum_type']): value = IonObject(ret_obj._schema[field]['enum_type'], {}) schema_info['schemas'][ret_obj._schema[field] ['enum_type']] = value._str_map #Add an instance of the resource type object schema_info['object'] = ret_obj return gateway_json_response(schema_info) except Exception, e: return build_error_response(e)
def get_object_schema(resource_type): schema_info = dict() #Prepare the dict entry for schema information including all of the internal object types schema_info['schemas'] = dict() #ION Objects are not registered as UNICODE names ion_object_name = str(resource_type) ret_obj = IonObject(ion_object_name, {}) # If it's an op input param or response message object. # Walk param list instantiating any params that were marked None as default. if hasattr(ret_obj, "_svc_name"): schema = ret_obj._schema for field in ret_obj._schema: if schema[field]["default"] is None: try: value = IonObject(schema[field]["type"], {}) except NotFound: # TODO # Some other non-IonObject type. Just use None as default for now. value = None setattr(ret_obj, field, value) #Add schema information for sub object types schema_info['schemas'][ion_object_name] = ret_obj._schema for field in ret_obj._schema: obj_type = ret_obj._schema[field]['type'] #First look for ION objects if is_ion_object(obj_type): try: value = IonObject(obj_type, {}) schema_info['schemas'][obj_type] = value._schema except NotFound: pass #Next look for ION Enums elif ret_obj._schema[field].has_key('enum_type'): if isenum(ret_obj._schema[field]['enum_type']): value = IonObject(ret_obj._schema[field]['enum_type'], {}) schema_info['schemas'][ret_obj._schema[field]['enum_type']] = value._str_map schema_info['object'] = ret_obj return schema_info
def export_objects(self, filename=None): sub_types = {} for obj_type in OT.keys(): ot_class = getattr(interface.objects, obj_type) base_type = ot_class.__mro__[1].__name__ sub_types.setdefault(base_type, set()).add(obj_type) res_rows = [] for obj_type in sorted(OT.keys()): # Compute type level info is_leaf = str(not bool(sub_types.get(obj_type, None))) ot_class = getattr(interface.objects, obj_type) if not hasattr(ot_class, "_schema"): continue base_types_list = [ bc.__name__ for bc in reversed(self.get_extends(ot_class)) ] base_type = base_types_list[-1] if base_types_list else "" base_types = "/".join(base_types_list[:-1]) oschema = ot_class._schema if obj_type in RT: group = "resource" elif isenum(obj_type): group = "enum" elif "origin" in oschema and "base_types" in oschema: # Not all events end in Event group = "event" else: group = "object" res_rows.append([ "type", base_types, base_type, obj_type, "", group, is_leaf, "True", "", "", ot_class._class_info.get("docstring", "").replace("\\", "").strip(), "" ]) # Add a few default attributes #res_rows.append([group, base_types, obj_type, "type_", "str", is_leaf, "False" if base_types else "True", "", "", "Resource type"]) if group == "resource" or group == "object": res_rows.append([ group, base_types, base_type, obj_type, "__", "first", is_leaf, "True", "", "", "", "" ]) # List attributes for att in sorted(oschema.keys()): if group == "resource" and att in { "addl", "alt_ids", "_id", "name", "description", "lcstate", "availability", "ts_created", "ts_updated" }: continue att_obj = oschema[att] ot, odef, odeco, odesc = att_obj["type"], att_obj[ "default"], att_obj["decorators"], att_obj["description"] if group == "resource" and att == "name": odesc = "Human readable long name of the resource, displayable in lists" att_def = obj_type leaf_attr = True for base_class in self.get_extends(ot_class): if hasattr(base_class, "_schema") and att in base_class._schema: att_def = base_class.__name__ leaf_attr = False is_internal = "" attr_desc = odesc.replace("\\", "").strip() if "(SYS)" in attr_desc: is_internal = "System" res_rows.append([ group, base_types, base_type, obj_type, att, ot, is_leaf, leaf_attr, att_def, odef, attr_desc, is_internal ]) if not filename: csvfile_name = os.path.join("interface", 'object_model.csv') try: os.unlink(csvfile_name) except: pass else: csvfile_name = filename print " Writing object model csv to '" + csvfile_name + "'" csvfile = csv.writer(open(csvfile_name, 'wb'), delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL) csvfile.writerow([ "Group", "Base Types", "Base Type", "Type Name", "Attribute", "Attr Type", "Leaf Type", "Leaf Attr", "Definition Type", "Default", "Description", "Internal" ]) csvfile.writerows(res_rows)
def export_objects(self, filename=None): sub_types = {} for obj_type in OT.keys(): ot_class = getattr(interface.objects, obj_type) base_type = ot_class.__mro__[1].__name__ sub_types.setdefault(base_type, set()).add(obj_type) res_rows = [] for obj_type in sorted(OT.keys()): # Compute type level info is_leaf = str(not bool(sub_types.get(obj_type, None))) ot_class = getattr(interface.objects, obj_type) if not hasattr(ot_class, "_schema"): continue base_types_list = [bc.__name__ for bc in reversed(self.get_extends(ot_class))] base_type = base_types_list[-1] if base_types_list else "" base_types = "/".join(base_types_list[:-1]) oschema = ot_class._schema if obj_type in RT: group = "resource" elif isenum(obj_type): group = "enum" elif "origin" in oschema and "base_types" in oschema: # Not all events end in Event group = "event" else: group = "object" res_rows.append(["type", base_types, base_type, obj_type, "", group, is_leaf, "True", "", "", ot_class._class_info.get("docstring", "").replace("\\", "").strip(), ""]) # Add a few default attributes #res_rows.append([group, base_types, obj_type, "type_", "str", is_leaf, "False" if base_types else "True", "", "", "Resource type"]) if group == "resource" or group == "object": res_rows.append([group, base_types, base_type, obj_type, "__", "first", is_leaf, "True", "", "", "", ""]) # List attributes for att in sorted(oschema.keys()): if group == "resource" and att in {"addl", "alt_ids", "_id", "name", "description", "lcstate", "availability", "ts_created", "ts_updated"}: continue att_obj = oschema[att] ot, odef, odeco, odesc = att_obj["type"], att_obj["default"], att_obj["decorators"], att_obj["description"] if group == "resource" and att == "name": odesc = "Human readable long name of the resource, displayable in lists" att_def = obj_type leaf_attr = True for base_class in self.get_extends(ot_class): if hasattr(base_class, "_schema") and att in base_class._schema: att_def = base_class.__name__ leaf_attr = False is_internal = "" attr_desc = odesc.replace("\\", "").strip() if "(SYS)" in attr_desc: is_internal = "System" res_rows.append([group, base_types, base_type, obj_type, att, ot, is_leaf, leaf_attr, att_def, odef, attr_desc, is_internal]) if not filename: csvfile_name = os.path.join("interface", 'object_model.csv') try: os.unlink(csvfile_name) except: pass else: csvfile_name = filename print " Writing object model csv to '" + csvfile_name + "'" csvfile = csv.writer(open(csvfile_name, 'wb'), delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL) csvfile.writerow(["Group", "Base Types", "Base Type", "Type Name", "Attribute", "Attr Type", "Leaf Type", "Leaf Attr", "Definition Type", "Default", "Description", "Internal"]) csvfile.writerows(res_rows)
def get_object_schema(resource_type): """ This function returns the schema for a given resource_type @param resource_type name of an object type @return schema dict """ schema_info = dict() #Prepare the dict entry for schema information including all of the internal object types schema_info['schemas'] = dict() #ION Objects are not registered as UNICODE names ion_object_name = str(resource_type) ret_obj = IonObject(ion_object_name, {}) # If it's an op input param or response message object. # Walk param list instantiating any params that were marked None as default. if hasattr(ret_obj, "_svc_name"): schema = ret_obj._schema for field in ret_obj._schema: if schema[field]["default"] is None: try: value = IonObject(schema[field]["type"], {}) except NotFound: # TODO # Some other non-IonObject type. Just use None as default for now. value = None setattr(ret_obj, field, value) #Add schema information for sub object types if hasattr(ret_obj, '_schema'): schema_info['schemas'][ion_object_name] = ret_obj._schema for field in ret_obj._schema: obj_type = ret_obj._schema[field]['type'] #First look for ION objects if is_ion_object(obj_type): try: value = IonObject(obj_type, {}) schema_info['schemas'][obj_type] = value._schema except NotFound: pass #Next look for ION Enums elif ret_obj._schema[field].has_key('enum_type'): if isenum(ret_obj._schema[field]['enum_type']): value = IonObject(ret_obj._schema[field]['enum_type'], {}) schema_info['schemas'][ret_obj._schema[field] ['enum_type']] = value._str_map schema_info['object'] = ret_obj elif isenum(resource_type): schema_info['schemas'][resource_type] = {} schema_info['schemas'][resource_type] = ret_obj._str_map else: raise ('%s is not an ION Object', resource_type) return schema_info