def uploadnii(server,token,nii_path,subject_id) : # subject_id may be none : in this case, succeed only if nii already in database common.lout('Asked to upload nii with path '+nii_path+' and parent '+str(subject_id) +'\n') if not common.determine_file_type(nii_path)=='nii' : raise Exception('Invalid nii path '+nii_path+' given') niihashvalue=niihash(nii_path) nii_parsed_xml=checkniiexistence(server,token,niihashvalue) if not nii_parsed_xml is None : common.lout('nii with path '+nii_path+' in database, not uploading') return nii_parsed_xml else : common.lout('nii with path '+nii_path+' not in database, uploading') try : tmpdname=tempfile.mkdtemp() zipfilename=os.sep.join([tmpdname,'nii.zip']) zfile=zipnii(nii_path,zipfilename) niidoc=niidocument(nii_path,subject_id,zfile.filename) client=poauth.OAuthClient(token) response=client.post(server+'/object.xml', data={'object':niidoc.toxml()}, files={'file': open(zfile.filename,'rb')} ) return common.parse_xml_from_server(response.text) finally: shutil.rmtree(tmpdname,ignore_errors=True)
def upload_image_from_path(server,token,image_path,subject_id=None) : """path may be dicom, nii, or other type of path. If it is DICOM and no subject is specified, infer subject""" obj_xml=None if os.path.isfile(image_path) : # special case each supported image type ft=common.determine_file_type(image_path) if ft == 'nii' : obj_xml=niiobject.uploadnii(server,token,image_path,parent_id=subject_id) else : print 'Warning : cannot upload image ' + image_path if os.path.isdir(image_path) : if isvaliddicom(image_path) : if not subject_id is None : print 'Warning : subject_id passed when using upload_image_from_path with DICOM image' obj_xml=uploaddicom(server,token,image_path) return obj_xml