def binpid2zip(pid, outfile, log_callback=None): def log(msg): if log_callback is not None: log_callback(msg) """Generate a zip file given a canonical pid""" parsed = parse_pid(pid) bin_pid = ''.join([parsed[NAMESPACE], parsed[BIN_LID]]) timestamp = iso8601(strptime(parsed[TIMESTAMP], parsed[TIMESTAMP_FORMAT])) log('copying raw data for %s to temp files ...' % bin_pid) with tempfile.NamedTemporaryFile() as hdr_tmp: hdr_path = hdr_tmp.name drain(UrlSource(bin_pid+'.hdr'), LocalFileSink(hdr_path)) hdr = parse_hdr_file(hdr_path) with tempfile.NamedTemporaryFile() as adc_tmp: adc_path = adc_tmp.name drain(UrlSource(bin_pid+'.adc'), LocalFileSink(adc_path)) adc = Adc(adc_path, parsed[SCHEMA_VERSION]) unstitched_targets = add_pids(adc.get_targets(), bin_pid) stitched_targets = list_stitched_targets(unstitched_targets) with tempfile.NamedTemporaryFile() as roi_tmp: roi_path = roi_tmp.name drain(UrlSource(bin_pid+'.roi'), LocalFileSink(roi_path)) canonical_pid = bin_pid log('copied raw data for %s' % canonical_pid) """*parsed_pid - result of parsing pid *canonical_pid - canonicalized with URL prefix *targets - list of (stitched) targets *hdr - result of parsing header file *timestamp - timestamp (FIXME in what format?) *roi_path - path to ROI file outfile - where to write resulting zip file""" log('creating zip file for %s' % bin_pid) with open(outfile,'wb') as fout: return bin2zip(parsed,bin_pid,stitched_targets,hdr,timestamp,roi_path,fout)
def serve_pid(pid): req = DashboardRequest(pid, request) try: paths = get_fileset(req.parsed) except NotFound: abort(404) hdr_path = paths['hdr_path'] adc_path = paths['adc_path'] roi_path = paths['roi_path'] adc = Adc(adc_path, req.schema_version) heft = 'full' # heft is either short, medium, or full if 'extension' in req.parsed: extension = req.parsed['extension'] if 'target' in req.parsed: canonical_bin_pid = canonicalize(req.url_root, req.time_series, req.bin_lid) target_no = int(req.parsed['target']) # pull three targets, then find any stitched pair targets = adc.get_some_targets(target_no-1, 3) targets = list_stitched_targets(targets) for t in targets: if t[TARGET_NUMBER] == target_no: target = t add_pid(target, canonical_bin_pid) # check for image mimetype = mimetypes.types_map['.' + extension] if mimetype.startswith('image/'): if req.product=='raw': img = get_target_image(req.parsed, target, roi_path) return Response(as_bytes(img,mimetype),mimetype=mimetype) if req.product=='blob': return serve_blob_image(req.parsed, mimetype) if req.product=='blob_outline': img = get_target_image(req.parsed, target, roi_path) return serve_blob_image(req.parsed, mimetype, outline=True, target_img=img) # not an image, so get more metadata targets = get_targets(adc, canonical_bin_pid) # not an image, check for JSON if extension == 'json': return Response(json.dumps(target),mimetype=MIME_JSON) target = get_target_metadata(target,targets) # more metadata representations. we'll need the header hdr = parse_hdr_file(hdr_path) if extension == 'xml': return Response(target2xml(req.canonical_pid, target, req.timestamp, canonical_bin_pid), mimetype='text/xml') if extension == 'rdf': return Response(target2rdf(req.canonical_pid, target, req.timestamp, canonical_bin_pid), mimetype='text/xml') if extension in ['html', 'htm']: template = { 'static': STATIC, 'target_pid': req.canonical_pid, 'bin_pid': canonical_bin_pid, 'properties': target, 'target': target.items(), # FIXME use order_keys 'date': req.timestamp } return template_response('target.html',**template) else: # bin if req.extension in ['hdr', 'adc', 'roi']: path = dict(hdr=hdr_path, adc=adc_path, roi=roi_path)[req.extension] mimetype = dict(hdr='text/plain', adc='text/csv', roi='application/octet-stream')[req.extension] return Response(file(path,'rb'), direct_passthrough=True, mimetype=mimetype) try: if req.product in ['blobs','blob']: # accept old url pattern return serve_blob_bin(req.parsed) if req.product=='features': return serve_features_bin(req.parsed) if req.product=='class_scores': return serve_class_scores_bin(req.parsed) except NotFound: abort(404) # gonna need targets unless heft is medium or below targets = [] if req.product != 'short': targets = get_targets(adc, req.canonical_pid) # end of views # not a special view, handle representations of targets if req.extension=='csv': adc_cols = req.parsed[ADC_COLS].split(' ') lines = targets2csv(targets,adc_cols) return Response('\n'.join(lines)+'\n',mimetype='text/csv') # we'll need the header for the other representations hdr = parse_hdr_file(hdr_path) if req.extension in ['html', 'htm']: targets = list(targets) context, props = split_hdr(hdr) template = { 'static': STATIC, 'bin_pid': req.canonical_pid, 'time_series': req.time_series, 'context': context, 'properties': props, 'targets': targets, 'target_pids': [t['pid'] for t in targets], 'date': req.timestamp, 'files': get_files(req.parsed,check=True) # note: ORM call! } print get_files(req.parsed) return template_response('bin.html', **template) if req.extension=='json': if req.product=='short': return Response(bin2json_short(req.canonical_pid,hdr,req.timestamp),mimetype=MIME_JSON) if req.product=='medium': return Response(bin2json_medium(req.canonical_pid,hdr,targets,req.timestamp),mimetype=MIME_JSON) return Response(bin2json(req.canonical_pid,hdr,targets,req.timestamp),mimetype=MIME_JSON) if req.extension=='xml': return Response(bin2xml(req.canonical_pid,hdr,targets,req.timestamp),mimetype='text/xml') if req.extension=='rdf': return Response(bin2rdf(req.canonical_pid,hdr,targets,req.timestamp),mimetype='text/xml') if req.extension=='zip': # look to see if the zipfile is resolvable try: zip_path = get_product_file(req.parsed, 'binzip') if os.path.exists(zip_path): return Response(file(zip_path), direct_passthrough=True, mimetype='application/zip') except NotFound: pass except: raise buffer = BytesIO() bin2zip(req.parsed,req.canonical_pid,targets,hdr,req.timestamp,roi_path,buffer) return Response(buffer.getvalue(), mimetype='application/zip') return 'unimplemented'