def post(self): barcodes = self.get_argument('barcodes').split(',') if self.get_argument('blanks'): blanks = self.get_argument('blanks').split(',') else: blanks = [] if self.get_argument('external'): external = self.get_argument('external').split(',') else: external = [] # Get metadata and create zip file metadata, failures = db.pulldown(barcodes, blanks, external) meta_zip = InMemoryZip() failed = '\n'.join(['\t'.join(bc) for bc in viewitems(failures)]) failtext = ("The following barcodes were not retrieved " "for any survey:\n%s" % failed) meta_zip.append("failures.txt", failtext) for survey, meta in viewitems(metadata): meta_zip.append('survey_%s_md.txt' % survey, meta) # write out zip file self.add_header('Content-type', 'application/octet-stream') self.add_header('Content-Transfer-Encoding', 'binary') self.add_header('Accept-Ranges', 'bytes') self.add_header('Content-Encoding', 'none') self.add_header('Content-Disposition', 'attachment; filename=metadata.zip') self.write(meta_zip.write_to_buffer()) self.flush() self.finish()
def test_append(self): exp_contents = 'argh' mem = InMemoryZip() mem2 = mem.append(self.test_fname, exp_contents) self.assertIn( 'test.zip', mem2.in_memory_data.getvalue(), )
def test_write_to_buffer(self): exp_contents = 'argh' mem = InMemoryZip() mem2 = mem.append(self.test_fname, exp_contents) res = mem2.write_to_buffer() # http://stackoverflow.com/a/34162395/1167475 zhandle = zipfile.ZipFile(io.BytesIO(res)) res_contents = zhandle.read(self.test_fname) self.assertEqual(res_contents, exp_contents)
def test_writetofile(self): exp_contents = 'argh' mem = InMemoryZip() mem2 = mem.append(self.test_fname, exp_contents) mem2.writetofile(self.test_out_fname) self.assertTrue(os.path.exists(self.test_out_fname)) zhandle = zipfile.ZipFile(self.test_out_fname, 'r') res_contents = zhandle.read(self.test_fname) self.assertEqual(res_contents, exp_contents)
def post(self): participants = db.participant_names() participants = '\n'.join(['\t'.join(r) for r in participants]) meta_zip = InMemoryZip() meta_zip.append('participants.txt', participants) # write out zip file self.add_header('Content-type', 'application/octet-stream') self.add_header('Content-Transfer-Encoding', 'binary') self.add_header('Accept-Ranges', 'bytes') self.add_header('Content-Encoding', 'none') self.add_header('Content-Disposition', 'attachment; filename=participants.zip') self.write(meta_zip.write_to_buffer()) self.flush() self.finish()
def post(self): kitinfo = loads(self.get_argument('kitinfo')) fields = self.get_argument('fields').split(',') table = ['\t'.join(fields)] table.extend(['\t'.join(map(str, kit)) for kit in kitinfo]) kit_zip = InMemoryZip() kit_zip.append('kit_printouts.txt', get_printout_data(kitinfo)).append( 'kit_table.txt', '\n'.join(table)) # write out zip file self.add_header('Content-type', 'application/octet-stream') self.add_header('Content-Transfer-Encoding', 'binary') self.add_header('Accept-Ranges', 'bytes') self.add_header('Content-Encoding', 'none') self.add_header('Content-Disposition', 'attachment; filename=kitinfo.zip') self.write(kit_zip.write_to_buffer()) self.flush() self.finish()
def post(self): barcodes = listify(self.get_arguments('barcodes')) blanks = listify(self.get_arguments('blanks')) # query which surveys have been selected by the user selected_ag_surveys = listify( self.get_arguments('selected_ag_surveys')) external = listify(self.get_arguments('external')) selected_ag_surveys = list(map(int, selected_ag_surveys)) # Get metadata and create zip file metadata, failures = db.pulldown(barcodes, blanks, external) meta_zip = InMemoryZip() failed = '\n'.join(['\t'.join(bc) for bc in viewitems(failures)]) failtext = ("The following barcodes were not retrieved " "for any survey:\n%s" % failed) meta_zip.append("failures.txt", failtext) # check database about what surveys are available available_agsurveys = {} for (_id, name, _) in db.list_ag_surveys(): available_agsurveys[_id] = name.replace(' ', '_') results_as_pd = [] for survey, meta in viewitems(metadata): # only create files for those surveys that have been selected by # the user. Note that ids from the DB are negative, in metadata # they are positive! # Currently, I (Stefan Janssen) don't have test data for external # surveys, thus I don't know their 'survey' value. I expect it to # be the name of the external survey. In order to not block their # pulldown I check that a skipped survey ID must be in the set of # all available surveys. survey = -1 * survey if (survey in selected_ag_surveys) or \ (survey not in available_agsurveys): meta_zip.append( 'survey_%s_md.txt' % available_agsurveys[survey], meta) # transform each survey into a pandas dataframe for later merge # read all columns as string to avoid unintened conversions, # like cutting leading zeros of barcodes pd_meta = pd.read_csv(StringIO(meta), sep="\t", dtype=str) # reset the index to barcodes = here sample_name pd_meta.set_index('sample_name', inplace=True) results_as_pd.append(pd_meta) # add the merged table of all selected surveys to the zip archive if self.get_argument('merged', default='False') == 'True': pd_all = pd.DataFrame() if len(results_as_pd) > 0: pd_all = pd.concat(results_as_pd, join='outer', axis=1) meta_zip.append( 'surveys_merged_md.txt', pd_all.to_csv(sep='\t', index_label='sample_name')) # write out zip file self.add_header('Content-type', 'application/octet-stream') self.add_header('Content-Transfer-Encoding', 'binary') self.add_header('Accept-Ranges', 'bytes') self.add_header('Content-Encoding', 'none') self.add_header('Content-Disposition', 'attachment; filename=metadata.zip') self.write(meta_zip.write_to_buffer()) self.flush() self.finish()
def test_constructor(self): mem = InMemoryZip() self.assertTrue(mem.in_memory_data is not None) self.assertTrue(mem.in_memory_zip is not None) self.assertEqual(mem.in_memory_zip.debug, 3)