Esempio n. 1
0
    def test_do_get_checksum(self):
        data1 = {"foo": "bar"}
        os.environ[ENV_VAR_HASHCODE_NAMESPACE] = "bar"
        os.environ[ENV_VAR_NEXUS_NAMESPACE] = "foo"
        data2 = {"bar": "bar"}
        checksum1 = Entity.do_get_checksum(data1)
        checksum2 = Entity.do_get_checksum(data2)

        self.assertEqual(checksum1, checksum2)
Esempio n. 2
0
 def _wrap_with_entity(self, search_result):
     identifier = Entity.extract_id_from_url(search_result.self_link,
                                             self.path)
     return self.constructor(
         identifier, search_result.data["source"], self.path) if type(
             search_result.data
         ) is dict and "source" in search_result.data else None
Esempio n. 3
0
    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))
Esempio n. 4
0
 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))
Esempio n. 5
0
    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))
Esempio n. 6
0
 def test_extract_id_from_url_with_wrong_entity(self):
     with self.assertRaises(ValueError):
         Entity.extract_id_from_url(
             "http://kg:8080/v0/organizations/hbp/core/celloptimization/v0.0.1",
             Schema.path)
Esempio n. 7
0
 def test_extract_id_from_url_with_slash_at_the_end(self):
     result = Entity.extract_id_from_url(
         "http://kg:8080/v0/schemas/hbp/core/celloptimization/v0.0.1/",
         Schema.path)
     self.assertEqual(result, "hbp/core/celloptimization/v0.0.1")
Esempio n. 8
0
 def test_extract_id_from_url_with_anchor(self):
     result = Entity.extract_id_from_url(
         "http://kg:8080/v0/schemas/hbp/core/celloptimization/v0.0.1#hello_world",
         Schema.path)
     self.assertEqual(result, "hbp/core/celloptimization/v0.0.1")
Esempio n. 9
0
 def test_extract_id_from_url_with_param(self):
     result = Entity.extract_id_from_url(
         "http://kg:8080/v0/schemas/hbp/core/celloptimization/v0.0.1?hello_world", Schema.path)
     assert result == "hbp/core/celloptimization/v0.0.1"
Esempio n. 10
0
 def resolve(self, search_result):
     identifier = Entity.extract_id_from_url(search_result.self_link,
                                             self.path)
     data = self._read(identifier)
     return Context(identifier, data,
                    self.path) if data is not None else None
Esempio n. 11
0
 def test_extract_id_from_url(self):
     result = Entity.extract_id_from_url("http://kg:8080/v0/schemas/hbp/core/celloptimization/v0.0.1", Schema.path)
     assert_that(result, equal_to("hbp/core/celloptimization/v0.0.1"))