def create(self, entity): result = self._http_client.post(entity.path, entity.data) if result is None: raise ValueError("Entity was not created") entity.data = result entity.id = Instance.extract_id_from_url(result.get("@id"), self.path) entity.build_path() return entity
def instance_from_full_uri(self, uri, cls=None, use_cache=True, deprecated=False, api="query", scope="released", resolved=False): # 'deprecated=True' means 'returns an instance even if that instance is deprecated' # should perhaps be called 'show_deprecated' or 'include_deprecated' logger.debug( "Retrieving instance from {}, api='{}' use_cache={}".format( uri, api, use_cache)) if use_cache and uri in self.cache: logger.debug("Retrieving instance {} from cache".format(uri)) instance = self.cache[uri] elif api == "nexus": instance = Instance(Instance.extract_id_from_url( uri, self._instance_repo.path), data=self._instance_repo._http_client.get(uri), root_path=Instance.path) if instance and instance.data and "@id" in instance.data: if deprecated is False and instance.data["nxv:deprecated"]: instance = None logger.debug("Not returning deprecated instance") else: self.cache[instance.data["@id"]] = instance logger.debug("Retrieved instance from KG Nexus" + str(instance.data)) else: instance = None elif api == "query": if cls and hasattr(cls, "query_id") and cls.query_id is not None: if resolved: query_id = cls.query_id_resolved else: query_id = cls.query_id response = self._kg_query_client.get( "{}/{}/instances?databaseScope={}&id={}".format( cls.path, query_id, SCOPE_MAP[scope], uri)) if response and len(response["results"]) > 0: instance = Instance(cls.path, response["results"][0], Instance.path) self.cache[instance.data["@id"]] = instance logger.debug("Retrieved instance from KG Query" + str(instance.data)) else: logger.warning( "Instance not found at {} using KG Query API".format( uri)) instance = None else: raise NotImplementedError( "No query id available: cls={}".format(str(cls))) else: raise ValueError("'api' must be either 'nexus' or 'query'") return instance
def instance_from_full_uri(self, uri): if uri in self.cache: return self.cache[uri] else: instance = Instance(Instance.extract_id_from_url( uri, self._instance_repo.path), data=self._instance_repo._http_client.get(uri), root_path=Instance.path) self.cache[instance.data["@id"]] = instance return instance
def create(self, entity): print("entitypath", entity.path) print("entitydata", entity.data) result = self._http_client.post(entity.path, entity.data) if result is None: raise ValueError("Entity was not created") else: self.logger.info("Instance created: %s", entity.path) entity.data = result entity.id = Instance.extract_id_from_url(result.get("@id"), self.path) entity.build_path() return entity
def instance_from_full_uri(self, uri, use_cache=True): if use_cache and uri in self.cache: logger.debug("Retrieving instance from cache") return self.cache[uri] else: instance = Instance(Instance.extract_id_from_url( uri, self._instance_repo.path), data=self._instance_repo._http_client.get(uri), root_path=Instance.path) self.cache[instance.data["@id"]] = instance logger.debug("Retrieved instance from KG " + str(instance.data)) return instance
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 handle_known_schema_identifier(self, schema_identifier, instance, hashcode_field, current_hashcode, identifier): if isinstance(identifier, list): identifier = identifier[0] found_instances = self._client.instances.find_by_field(instance.id, schema_identifier, identifier, resolved=True) if found_instances and found_instances.results: found_instance = found_instances.results[0] existing_hashcode = found_instance.data[hashcode_field] if hashcode_field in found_instance.data else None instance.path = found_instance.get_self_link() instance.id = Instance.extract_id_from_url(instance.path, instance.root_path) if existing_hashcode is None or existing_hashcode != current_hashcode: result = self._client.instances.update(instance) else: LOGGER.info("Skipping instance %s because it already exists", instance.path) result = instance return result return None
def unrelease(self, uri): """Unrelease the node with the given uri""" path = Instance.extract_id_from_url(uri, self._instance_repo.path) response = self._release_client.delete(path) if response.status_code not in (200, 204): raise Exception("Can't unrelease node with id {}".format(uri))
def is_released(self, uri): """Release status of the node""" path = Instance.extract_id_from_url(uri, self._instance_repo.path) response = self._release_client.get(path) return response.json()["status"] == "RELEASED"