def upload_csv_to_omero(ctx, file, tablename, target_id, target_type="Project"): """Upload the CSV file and attach it to the specified object""" print file print file.name svc = gateway.getFacility(DataManagerFacility) file_size = os.path.getsize(file.name) original_file = OriginalFileI() original_file.setName(rstring(tablename)) original_file.setPath(rstring(file.name)) original_file.setSize(rlong(file_size)) checksum_algorithm = ChecksumAlgorithmI() checksum_algorithm.setValue(rstring(ChecksumAlgorithmSHA1160.value)) original_file.setHasher(checksum_algorithm) original_file.setMimetype(rstring("text/csv")) original_file = svc.saveAndReturnObject(ctx, original_file) store = gateway.getRawFileService(ctx) # Open file and read stream store.setFileId(original_file.getId().getValue()) print original_file.getId().getValue() try: store.setFileId(original_file.getId().getValue()) with open(file.name, 'rb') as stream: buf = 10000 for pos in range(0, long(file_size), buf): block = None if file_size - pos < buf: block_size = file_size - pos else: block_size = buf stream.seek(pos) block = stream.read(block_size) store.write(block, pos, block_size) original_file = store.save() finally: store.close() # create the file annotation namespace = "training.demo" fa = FileAnnotationI() fa.setFile(original_file) fa.setNs(rstring(namespace)) if target_type == "Project": target_obj = ProjectData(ProjectI(target_id, False)) elif target_type == "Dataset": target_obj = DatasetData(DatasetI(target_id, False)) elif target_type == "Image": target_obj = ImageData(ImageI(target_id, False)) svc.attachAnnotation(ctx, FileAnnotationData(fa), target_obj)
def make_file_annotation(self, name=None, binary=None, format=None, client=None, ns=None): """ Creates a new DatasetI instance and returns the persisted object. If no name has been provided, a UUID string shall be used. :param name: the name of the project :param client: The client to use to create the object :param ns: The namespace for the annotation """ if client is None: client = self.client update = client.sf.getUpdateService() # file if format is None: format = "application/octet-stream" if binary is None: binary = "12345678910" if name is None: name = str(self.uuid()) oFile = OriginalFileI() oFile.setName(rstring(name)) oFile.setPath(rstring(str(self.uuid()))) oFile.setSize(rlong(len(binary))) oFile.hasher = ChecksumAlgorithmI() oFile.hasher.value = rstring("SHA1-160") oFile.setMimetype(rstring(str(format))) oFile = update.saveAndReturnObject(oFile) # save binary store = client.sf.createRawFileStore() store.setFileId(oFile.id.val) store.write(binary, 0, 0) oFile = store.save() # See ticket:1501 store.close() fa = FileAnnotationI() fa.setFile(oFile) if ns is not None: fa.setNs(rstring(ns)) return update.saveAndReturnObject(fa)