def addDataFieldTo( self, entity, fieldName, fieldType="default", value=None, date=None, number=None, unit=None, filepath=None, extraParams={}, ): if filepath is not None: fileId = fileRepository.newFile(entity.__user__, filepath).id else: fileId = None params = { "metadata_thread_id": entity.metadata_thread["id"], "type": fieldType, "label": handleString(fieldName), "value": handleString(value), "date": handleDate(date), "number": number, "unit": unit, "file_id": fileId, **extraParams, } dataField = entityRepository.newEntity( entity.__user__, ProtocolDataField, params) dataField.protocol_id = entity.id return dataField
def addFile(self, filepath=None, rawData=None): """ Add a file to an Experiment Protocol. Parameters ---------- filepath (str) The path to the file to upload. rawData (bytes) Raw data to upload as a file. Returns ------- :class:`~labstep.file.File` The newly added file entity. Example ------- :: experiment = user.getExperiment(17000) experiment_protocol = experiment.getProtocols()[0] file = experiment_protocol.addFile(filepath='./my_file.csv') """ params = {'experiment_id': self.id} return fileRepository.newFile(self.__user__, filepath=filepath, rawData=rawData, extraParams=params)
def addFile(self, filepath=None, rawData=None): """ Add a file to a Protocol. Parameters ---------- filepath (str) The path to the file to upload. Returns ------- :class:`~labstep.file.File` The newly added file entity. Example ------- :: protocol = user.getProtocol(17000) protocol.addFile(filepath='./my_file.csv') """ params = {'protocol_id': self.last_version['id']} return fileRepository.newFile(self.__user__, filepath=filepath, rawData=rawData, extraParams=params)
def addCommentWithFile(self, entity, body, filepath, extraParams={}): if filepath is not None: fileId = fileRepository.newFile(entity.__user__, filepath).id else: fileId = None return self.addComment(entity, body, fileId=fileId, extraParams=extraParams)
def addMetadataTo( self, entity, fieldName, fieldType="default", value=None, date=None, number=None, unit=None, filepath=None, extraParams={}, ): if filepath is not None: fileId = fileRepository.newFile(entity.__user__, filepath).id else: fileId = None if fieldType not in FIELDS: msg = "Not a supported metadata type '{}'".format(fieldType) raise ValueError(msg) allowedFieldsForType = set(ALLOWED_FIELDS[fieldType]) fields = { "value": value, "date": date, "number": number, "unit": unit, "file_id": fileId, } fields = {k: v for k, v in fields.items() if v} fields = set(fields.keys()) violations = fields - allowedFieldsForType if violations: msg = "Unallowed fields [{}] for type {}".format( ",".join(violations), fieldType) raise ValueError(msg) params = { "metadata_thread_id": entity.metadata_thread["id"], "type": fieldType, "label": handleString(fieldName), "value": handleString(value), "date": handleDate(date), "number": number, "unit": unit, "file_id": fileId, **extraParams, } return entityRepository.newEntity(entity.__user__, Metadata, params)
def newFile(self, filepath, rawData=None, extraParams={}): """ Upload a file to the Labstep entity Data. Parameters ---------- filepath (str) The filepath to the file to attach. Example ------- :: entity = user.newFile('./structure_of_aspirin.png') """ from labstep.entities.file.repository import fileRepository return fileRepository.newFile(self, filepath, rawData=rawData, extraParams=extraParams)
def addData( self, fieldName, fieldType="text", text=None, number=None, unit=None, filepath=None, extraParams={}, ): """ Send new data from the Device. Parameters ---------- fieldName (str) The name of the data field being sent. fieldType (str) The type of data being sent. Options are: "text", "numeric", or "file". text (str) The text for a field of type 'text'. number (float) The number for a field of type 'numeric'. unit (str) The unit for a field of type 'numeric'. filepath (str) Local path to the file to upload for type 'file'. Returns ------- :class:`~labstep.entities.device.model.DeviceData` An object representing the new Device Data. Example ------- :: device = user.getDevice(17000) data = device.addData("Temperature","numeric", number=173, unit='K') """ from labstep.generic.entity.repository import entityRepository from labstep.entities.file.repository import fileRepository if fieldType not in FIELD_TYPES: msg = "Not a supported data type '{}'".format(fieldType) raise ValueError(msg) if filepath is not None: file_id = fileRepository.newFile(self.__user__, filepath).id else: file_id = None allowedFieldsForType = set(ALLOWED_FIELDS[FIELD_TYPES[fieldType]]) fields = {"value": text, "number": number, "unit": unit, "file_id": file_id} fields = {k: v for k, v in fields.items() if v} fields = set(fields.keys()) violations = fields - allowedFieldsForType if violations: msg = "Unallowed fields [{}] for type {}".format( ",".join(violations), fieldType ) raise ValueError(msg) params = { "device_id": self.id, "type": FIELD_TYPES[fieldType], "name": fieldName, "value": text, "number": number, "unit": unit, "file_id": file_id, **extraParams, } return entityRepository.newEntity(self.__user__, DeviceData, params)