def sync_metadata(self, resource: Resource, result: Dict) -> None: metadata = {"id": resource.id} keys = sorted(self.metadata_context.terms.keys()) only_meta = {k: v for k, v in result.items() if k in keys} metadata.update( _remove_ld_keys(only_meta, self.metadata_context, False)) resource._store_metadata = wrap_dict(metadata)
def test_from_resource(config, person, organization, store_metadata_value): forge = KnowledgeGraphForge(config) data = { 'id': 'c51f4e4e-2b30-41f4-8f7c-aced85632b03', 'type': ['Person', 'Agent'], 'name': 'Jami Booth' } assert isinstance(person, Resource) dataset = Dataset.from_resource(forge, person) assert isinstance(dataset, Dataset) assert forge.as_json(dataset) == forge.as_json(person) assert forge.as_json(dataset) == data assert dataset._store_metadata is None person_with_store_metadata = Resource(**forge.as_json(person)) person_with_store_metadata._store_metadata = store_metadata_value dataset = Dataset.from_resource(forge, person_with_store_metadata, store_metadata=True) assert isinstance(dataset, Dataset) person_with_store_metadata_json = forge.as_json(person_with_store_metadata) assert forge.as_json(dataset) == person_with_store_metadata_json assert forge.as_json(dataset) == data assert dataset._store_metadata == person_with_store_metadata._store_metadata assert forge.as_json(dataset, store_metadata=False) != forge.as_json( person_with_store_metadata, store_metadata=True) assert forge.as_json(dataset, store_metadata=True) == forge.as_json( person_with_store_metadata, store_metadata=True) assert isinstance(organization, Resource) dataset = Dataset.from_resource(forge, [person, organization]) assert isinstance(dataset, List) assert len(dataset) == 2
def _make_registered(r: Resource, metadata, base=None): if base: r.id = f"{urljoin('file:', pathname2url(os.getcwd()))}/{str(uuid4())}" else: r.id = str(uuid4()) if metadata: metadata["id"] = r.id r._store_metadata = wrap_dict(metadata) return r
def _deprecate_one(self, resource: Resource) -> None: rid = resource.id try: record = self.service.deprecate(rid) except StoreLibrary.RecordMissing: raise DeprecationError("resource not found") except StoreLibrary.RecordDeprecated: raise DeprecationError("resource already deprecated") else: resource._store_metadata = wrap_dict(record["metadata"])
def _register_one(self, resource: Resource, schema_id: str) -> None: data = as_json(resource, expanded=False, store_metadata=False, model_context=None, metadata_context=None, context_resolver=None) try: record = self.service.create(data) except StoreLibrary.RecordExists: raise RegistrationError("resource already exists") else: resource.id = record["data"]["id"] resource._store_metadata = wrap_dict(record["metadata"])
def _update_one(self, resource: Resource) -> None: data = as_json(resource, expanded=False, store_metadata=False, model_context=None, metadata_context=None, context_resolver=None) try: record = self.service.update(data) except StoreLibrary.RecordMissing: raise UpdatingError("resource not found") except StoreLibrary.RecordDeprecated: raise UpdatingError("resource is deprecated") else: resource._store_metadata = wrap_dict(record["metadata"])
def test_freeze(config, store_metadata_value): forge = KnowledgeGraphForge(config, debug=True) derivation1 = Dataset(forge, type="Dataset", name="A derivation dataset") derivation1.id = "http://derivation1" derivation1._store_metadata = wrap_dict(store_metadata_value) generation1 = Dataset(forge, type="Dataset", name="A generation dataset") generation1.id = "http://generation1" generation1._store_metadata = wrap_dict(store_metadata_value) invalidation1 = Dataset(forge, type="Activity", name="An invalidation activity") invalidation1.id = "http://invalidation1" invalidation1._store_metadata = wrap_dict(store_metadata_value) contribution1 = Resource(type="Person", name="A contributor") contribution1.id = "http://contribution1" contribution1._store_metadata = wrap_dict(store_metadata_value) dataset = Dataset(forge, type="Dataset", name="A dataset") dataset._store_metadata = wrap_dict(store_metadata_value) dataset.add_derivation(derivation1, versioned=False) dataset.add_generation(generation1, versioned=False) dataset.add_invalidation(invalidation1, versioned=False) dataset.add_contribution(contribution1, versioned=False) expected_derivation = json.loads(json.dumps({"type":"Derivation", "entity":{"id": "http://derivation1", "type":"Dataset", "name":"A derivation dataset"}})) assert forge.as_json(dataset.derivation) == expected_derivation expected_generation = json.loads(json.dumps({"type": "Generation", "activity": {"id": "http://generation1", "type": "Dataset"}})) assert forge.as_json(dataset.generation) == expected_generation expected_contribution = json.loads(json.dumps({"type": "Contribution", "agent": {"id": "http://contribution1", "type": "Person"}})) assert forge.as_json(dataset.contribution) == expected_contribution expected_invalidation = json.loads(json.dumps({"type": "Invalidation", "activity": {"id": "http://invalidation1", "type": "Activity"}})) assert forge.as_json(dataset.invalidation) == expected_invalidation dataset.id = "http://dataset" dataset._synchronized = True forge._store.freeze(dataset) assert dataset.id == "http://dataset?_version=1" assert dataset.derivation.entity.id == "http://derivation1?_version=1" assert dataset.generation.activity.id == "http://generation1?_version=1" assert dataset.contribution.agent.id == "http://contribution1?_version=1" assert dataset.invalidation.activity.id == "http://invalidation1?_version=1"
def add_metadata(resource: Resource): metadata = { "_self": resource.id, "_constrainedBy": "https://bluebrain.github.io/nexus/schemas/unconstrained.json", "_project": "https://nexus/org/prj", "_rev": 1, "_deprecated": False, "_createdAt": "2019-03-28T13:40:38.934Z", "_createdBy": "https://nexus/u1", "_updatedAt": "2019-03-28T13:40:38.934Z", "_updatedBy": "https://nexus/u1", "_incoming": "https:/nexus/incoming", "_outgoing": "https://nexux/outgoing" } resource._synchronized = True resource._validated = True resource._store_metadata = wrap_dict(metadata)