Locked History Actions

attachment:upload_to_history.py of Events/GCC2013/TrainingDay/API

Attachment 'upload_to_history.py'

Download

   1 #!/usr/bin/env python
   2 """
   3 Upload a file to the desired history.
   4 """
   5 _USAGE = ( "history_upload.py <api key> <galaxy base url> <history id> <filepath to upload>\n"
   6          + "  (where galaxy base url is just the root url where your Galaxy is served; e.g. 'localhost:8080')" )
   7 
   8 import os, sys, json, pprint
   9 #sys.path.insert( 0, os.path.dirname( __file__ ) )
  10 
  11 try:
  12     import requests
  13 except ImportError, imp_err:
  14     log.error( "Could not import the requests module. See http://docs.python-requests.org/en/latest/ or "
  15              + "install with 'pip install requests'" )
  16     raise
  17 
  18 
  19 # -----------------------------------------------------------------------------
  20 def upload_file( base_url, api_key, history_id, filepath, **kwargs ):
  21     full_url = base_url + '/api/tools'
  22 
  23     payload = {
  24         'key'           : api_key,
  25         'tool_id'       : 'upload1',
  26         'history_id'    : history_id,
  27     }
  28     inputs = {
  29         'files_0|NAME'  : kwargs.get( 'filename', os.path.basename( filepath ) ),
  30         'files_0|type'  : 'upload_dataset',
  31         #TODO: the following doesn't work with tools.py
  32         #'dbkey'         : kwargs.get( 'dbkey', '?' ),
  33         'dbkey'         : '?',
  34         'file_type'     : kwargs.get( 'file_type', 'auto' ),
  35         'ajax_upload'   : u'true',
  36         #'async_datasets': '1',
  37     }
  38     payload[ 'inputs' ] = json.dumps( inputs )
  39 
  40     response = None
  41     with open( filepath, 'rb' ) as file_to_upload:
  42         files = { 'files_0|file_data' : file_to_upload }
  43         response = requests.post( full_url, data=payload, files=files )
  44     return response.json()
  45     
  46 
  47 # -----------------------------------------------------------------------------
  48 if __name__ == '__main__':
  49 
  50     if len( sys.argv ) < 5:
  51         print _USAGE
  52         sys.exit( 1 )
  53 
  54     api_key, base_url, history_id, filepath = sys.argv[1:5]
  55     kwargs = dict([ kwarg.split('=', 1) for kwarg in sys.argv[5:]])
  56 
  57     response = upload_file( base_url, api_key, history_id, filepath, **kwargs )
  58     print >> sys.stderr, response
  59     print response.content

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2013-06-24 16:01:09, 8.2 KB) [[attachment:Galaxy-Workflow-Joined_Solexa_QC.ga]]
  • [get | view] (2013-06-24 16:46:36, 14.0 KB) [[attachment:all-scripts.tar.gz]]
  • [get | view] (2013-06-24 16:01:23, 5.2 KB) [[attachment:common.py]]
  • [get | view] (2013-06-24 16:04:07, 0.9 KB) [[attachment:datasets_1.py]]
  • [get | view] (2013-06-24 16:04:15, 1.2 KB) [[attachment:datasets_2.py]]
  • [get | view] (2013-06-24 16:04:22, 3.4 KB) [[attachment:datasets_3.py]]
  • [get | view] (2013-06-24 16:04:33, 1.5 KB) [[attachment:hdas_1.py]]
  • [get | view] (2013-06-24 16:04:50, 1.7 KB) [[attachment:hdas_2.py]]
  • [get | view] (2013-06-24 16:05:07, 2.2 KB) [[attachment:hdas_3.py]]
  • [get | view] (2013-06-24 16:03:38, 0.9 KB) [[attachment:histories_1.py]]
  • [get | view] (2013-06-24 16:03:52, 2.1 KB) [[attachment:histories_2.py]]
  • [get | view] (2013-06-24 16:05:25, 2.2 KB) [[attachment:histories_3.py]]
  • [get | view] (2013-06-24 16:05:34, 3.3 KB) [[attachment:lddas_1.py]]
  • [get | view] (2013-06-24 16:05:41, 1.3 KB) [[attachment:libraries_1.py]]
  • [get | view] (2013-06-24 16:01:36, 0.8 KB) [[attachment:setup.py]]
  • [get | view] (2013-06-24 16:02:45, 1.1 KB) [[attachment:step_1.py]]
  • [get | view] (2013-06-24 16:07:15, 9.9 KB) [[attachment:step_10.py]]
  • [get | view] (2013-06-24 16:03:28, 1.4 KB) [[attachment:step_2.py]]
  • [get | view] (2013-06-24 16:06:01, 1.6 KB) [[attachment:step_3.py]]
  • [get | view] (2013-06-24 16:06:11, 1.1 KB) [[attachment:step_4.py]]
  • [get | view] (2013-06-24 16:06:20, 1.9 KB) [[attachment:step_5.py]]
  • [get | view] (2013-06-24 16:06:44, 2.5 KB) [[attachment:step_6.py]]
  • [get | view] (2013-06-24 16:06:53, 4.9 KB) [[attachment:step_7.py]]
  • [get | view] (2013-06-24 16:07:05, 5.9 KB) [[attachment:step_8.py]]
  • [get | view] (2013-06-30 13:51:46, 8.0 KB) [[attachment:step_9.py]]
  • [get | view] (2013-06-24 16:41:51, 1.8 KB) [[attachment:tools_1.py]]
  • [get | view] (2013-06-24 16:44:32, 2.0 KB) [[attachment:upload_to_history.py]]
  • [get | view] (2013-06-24 16:03:03, 1.3 KB) [[attachment:users_1.py]]
  • [get | view] (2013-06-24 16:44:44, 2.9 KB) [[attachment:workflows_1.py]]
 All files | Selected Files: delete move to page

You are not allowed to attach a file to this page.