def try_update(self, data): """ Overwrites the metadata with the data provided. The only caveat is that if you try to set metadata that is immutable it will be ignored. Args: data(schemas/metadata.json) Returns: pyshelf.metadata.result.Result """ data = self.container.mapper.from_response(data) old_meta = copy.deepcopy(self.metadata) for key, val in old_meta.iteritems(): new_meta = data.get(key) if new_meta: if not self.metadata.is_immutable(key): self.metadata[key] = new_meta data.pop(key) else: if not self.metadata.is_immutable(key): del self.metadata[key] if len(data) > 0: self.metadata.update(data) # assuming success if it hasn't thrown an exception self.write() return Result()
def try_delete_property(self, key): """ Deletes a single metadata property. Args: key(string): Name of the metadata property Returns: pyshelf.metadata.result.Result """ result = Result() if self.metadata.is_immutable(key): result.add_error(ErrorCode.IMMUTABLE) else: del self.metadata[key] self.write() return result
def try_create_property(self, key, value): """ Creates a single metadata property. Will error if the property already exists. Args: key(string) value(schemas/metadata-property.json) Returns: pyshelf.metadata.result.Result """ result = Result() if self.metadata.get(key): result.add_error(ErrorCode.DUPLICATE) else: result = self._try_update_property_with_result(key, value, result) return result
def try_update_property(self, key, value): """ Updates a single metadata property Args: key(string) value(schemas/metadata-property.json) Returns: pyshelf.metadata.result.Result """ result = Result() result = self._try_update_property_with_result(key, value, result) return result