def test_expand(self): data = { "@context": [{ "hbp": "http://localhost:8080/vocab/hbp/core/celloptimization/", }], "imports": [ "{{scheme}}://{{host}}/{{prefix}}/schemas/nexus/core/datadownload/v1.0.0" ], "hbp:contributors": "Homer Simpson" } qualified_data = Entity.fully_qualify(data) expected_data = {'http://localhost:8080/vocab/hbp/core/celloptimization/contributors': 'Homer Simpson'} assert_that(qualified_data, equal_to(expected_data))
def create_instance_by_file(self, file_path, fully_qualify=False): """Create a new instance for the provided data Arguments: file_path -- path to the location of the file to be uploaded as instance fully_qualify -- if True, prefixes are resolved and the JSON-LD to be uploaded will be interpretable as JSON (but with non-human-friendly, fully qualified keys) """ with open(os.path.abspath(file_path)) as metadata_file: file_content = metadata_file.read() raw_json = self.__resolve_entities(file_content) raw_json = self.__fill_placeholders(raw_json) if fully_qualify: final_json = Entity.fully_qualify(json.loads(raw_json)) else: final_json = json.loads(raw_json) if type( raw_json) is not dict else raw_json schema_data = SchemaOrContextData.by_filepath( file_path, final_json) schema_identifier = "http://schema.org/identifier" if self._upload_fully_qualified: raw_json = final_json instance = Instance.create_new(schema_data.organization, schema_data.domain, schema_data.name, schema_data.version, raw_json) if schema_identifier in final_json: checksum = instance.get_checksum() checksum_file = "{}.{}.chksum".format(file_path, checksum) if os.path.exists(checksum_file): LOGGER.debug("{} is unchanged - no upload required".format( file_path)) return identifier = final_json.get(schema_identifier) if type(identifier) is list: identifier = identifier[0] found_instances = self._client.instances.find_by_field( instance.id, schema_identifier, identifier) if found_instances and len(found_instances.results) > 0: instance.path = found_instances.results[0].self_link instance.id = Instance.extract_id_from_url( instance.path, instance.root_path) result = self._client.instances.update(instance) with open(checksum_file, 'a') as checksum_file: checksum_file.close() return result return self._client.instances.create( Instance.create_new(schema_data.organization, schema_data.domain, schema_data.name, schema_data.version, raw_json))
def create_instance(self, data, schema_data, fail_if_linked_instance_is_missing=True): """Create a new instance for the provided data Arguments: file_path -- path to the location of the file to be uploaded as instance fully_qualify -- if True, prefixes are resolved and the JSON-LD to be uploaded will be interpretable as JSON (but with non-human-friendly, fully qualified keys) """ raw_json = self.resolve_entities( data if not isinstance(data, dict) else json.dumps(data), fail_if_linked_instance_is_missing) raw_json = self._fill_placeholders(raw_json) fully_qualified_json = Entity.fully_qualify(json.loads(raw_json)) if not self._upload_fully_qualified: final_json = json.loads(raw_json) if not isinstance( raw_json, dict) else raw_json else: final_json = fully_qualified_json schema_identifier = "http://schema.org/identifier" hashcode_field = "http://hbp.eu/internal#hashcode" if self._upload_fully_qualified: raw_json = final_json instance = Instance.create_new(schema_data.organization, schema_data.domain, schema_data.name, schema_data.version, raw_json) if hashcode_field not in fully_qualified_json: current_hashcode = Entity.do_get_checksum(fully_qualified_json) fully_qualified_json[hashcode_field] = current_hashcode instance.data[hashcode_field] = current_hashcode else: current_hashcode = fully_qualified_json[hashcode_field] if schema_identifier in fully_qualified_json: identifier = fully_qualified_json.get(schema_identifier) result = self.handle_known_schema_identifier( schema_identifier, instance, hashcode_field, current_hashcode, identifier) if result is not None: return result return self._client.instances.create( Instance.create_new(schema_data.organization, schema_data.domain, schema_data.name, schema_data.version, raw_json))