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 test_create_turtle_instance(self): organization = self.client.organizations.read("test") if organization is None: self.client.organizations.create( Organization.create_new("test", "An organization for tests")) domain = self.client.domains.read("test", "core") if domain is None: self.client.domains.create( Domain.create_new("test", "core", "A domain for tests")) schema = self.client.schemas.read("test", "core", "turtle", "v0.0.4") if schema is None: schema = self.client.schemas.create( Schema.create_new("test", "core", "turtle", "v0.0.4", self.test_turtle_schema, is_turtle=True)) if not schema.is_published(): self.client.schemas.publish(schema, True) instance = self.client.instances.create( Instance.create_new("test", "core", "turtle", "v0.0.4", self.test_turtle_instance, is_turtle=True)) instance = self.client.instances.read(instance.get_organization(), instance.get_domain(), instance.get_schema(), instance.get_version(), instance.get_id()) print instance
def test_create_and_deprecate(self): entity = Instance.create_new(self.default_prefix, "core", "schematest", "v0.0.1", self.test_instance) result = self.repository.create(entity) assert_that(result, equal_to(entity)) self._assert_valid_default_entity(result, entity.id) assert_that(result.get_revision(), equal_to(1)) self._test_deprecate(entity)
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))
def create_instance(self, schema_name: str, schema_version: str, data: JSON) -> Optional[Instance]: if not self.is_schema_created(schema_name, schema_version): print("<error> Schema does not exist!") return None # TODO Check if the instance has already been created when Nexus v1. local = Instance.create_new(self.organization, self.domain, schema_name, schema_version, data) # Note: Even if NexusClient.logger.hasHandlers() is False and # NexusClient.logger.propagate is set to False, the logging message is # displayed in the IPython notebook. A new logger is then needed. # Using NexusClient.instances.create() outside the current method will # display the logging message as without the following modification. stream = StringIO() stream_handler = logging.StreamHandler(stream=stream) logger = logging.getLogger() logger.addHandler(stream_handler) try: return self.client.instances.create(local) except HTTPError: # Note: repr(HTTPError) is not informative on what really happened. # Note: The 'violations' key is two times in the string. message = stream.getvalue() match = re.search("""{"violations":[^}]+}$""", message) print("<error>") if match: errors = match.group(0) prettify(json.loads(errors)) else: print("--- was not able to parse the error message ---") print(message) print("<data>") prettify(data) return None finally: # Note: StringIO.close() flushes and closes the stream. stream.close() logger.removeHandler(stream_handler)
def create_instance(self, schema_name: str, schema_version: str, data: JSON) -> Optional[Instance]: if not self.is_schema_created(schema_name, schema_version): print("<error> Schema does not exist!") return None # TODO Check if the instance has already been created when Nexus v1. local = Instance.create_new(self.organization, self.domain, schema_name, schema_version, data) # Note: Even if NexusClient.logger.hasHandlers() is False and # NexusClient.logger.propagate is set to False, the logging message is # displayed in the IPython notebook. A new logger is then needed. # Using NexusClient.instances.create() outside the current method will # display the logging message as without the following modification. stream = StringIO() stream_handler = logging.StreamHandler(stream=stream) logger = logging.getLogger() logger.addHandler(stream_handler) try: return self.client.instances.create(local) except HTTPError: # Note: repr(HTTPError) is not informative on what really happened. # Note: The 'violations' key is two times in the string. message = stream.getvalue() match = re.search("""{"violations":[^}]+}$""", message) print("<error>") if match: errors = match.group(0) prettify(json.loads(errors)) else: print("--- was not able to parse the error message ---") print(message) print("<data>") prettify(data) return None finally: # Note: StringIO.close() flushes and closes the stream. stream.close() logger.removeHandler(stream_handler)
example_person_data = { "@context": { "Person": "http://schema.org/Person", "givenName": "http://schema.org/givenName", "familyName": "http://schema.org/familyName" }, "@type": [ "Person" ], "familyName": "Nexus", "givenName": "Brian" } instance = Instance.create_new(organization=organization_name, domain=domain_name, schema=schema_name, version=schema_version, content=example_person_data) client.instances.create(instance) example_person_data_id = instance.data["@id"] example_person_data_rev = instance.get_revision() filepath = "https://docs.google.com/uc?id=1V8-hGYNMVqlCIrvlTKTxseMfZSGEqbHl" # Provide the address of the file you want to attach here r = requests.get(filepath) file = r.content url = "{}/attachment?rev={}".format(example_person_data_id, example_person_data_rev) file_attachment = {'file': file} response = requests.put(url, files=file_attachment)
test_schema_obj = Schema.create_new(organisation_name, domain_name, schema_name, version, test_schema) schema_repo = SchemaRepository(nar_client) schema_repo.create(test_schema_obj) schema_repo.publish(test_schema_obj, True) # # Read a schema # testschema_read = schema_repo.read(organisation_name, domain_name, schema_name, version) print(testschema_read) # # 5. Create a test instance. (test0) # TODO test_instance = {} # from pyxus.resources.repository import InstanceRepository from pyxus.resources.entity import Instance # Create an instance of naro/tests/testschema (v0.0.2) instance_repo = InstanceRepository(nar_client) test0 = Instance.create_new(organisation_name, domain_name, schema_name, version, test_instance) instance_repo.create(test0) # get uuid single_result = instance_repo.list(subpath=fpath, size=1, deprecated=None) instance = single_result.results[0] InstanceRepository._extract_uuid(instance.result_id)