Locked History Actions

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

Attachment 'common.py'

Download

   1 import os
   2 import sys
   3 import urllib2
   4 
   5 #new_path = [ os.path.join( os.path.dirname( __file__ ), '..', '..', 'lib' ) ]
   6 #new_path.extend( sys.path[1:] )
   7 #sys.path = new_path
   8 
   9 import json
  10 
  11 import logging
  12 log = logging.getLogger( __name__ )
  13 
  14 def make_url( api_key, url, args=None ):
  15     # Adds the API Key to the URL if it's not already there.
  16     if args is None:
  17         args = []
  18     argsep = '&'
  19     if '?' not in url:
  20         argsep = '?'
  21     if '?key=' not in url and '&key=' not in url:
  22         args.insert( 0, ( 'key', api_key ) )
  23     return url + argsep + '&'.join( [ '='.join( t ) for t in args ] )
  24 
  25 def get( api_key, url ):
  26     # Do the actual GET.
  27     url = make_url( api_key, url )
  28     #try:
  29     return json.loads( urllib2.urlopen( url ).read() )
  30     #except simplejson.decoder.JSONDecodeError, e:
  31     #    print "URL did not return JSON data"
  32     #    sys.exit(1)
  33 
  34 def post( api_key, url, data ):
  35     # Do the actual POST.
  36     url = make_url( api_key, url )
  37     req = urllib2.Request( url, headers = { 'Content-Type': 'application/json' }, data = json.dumps( data ) )
  38     return json.loads( urllib2.urlopen( req ).read() )
  39 
  40 def put( api_key, url, data ):
  41     # Do the actual PUT
  42     url = make_url( api_key, url )
  43     req = urllib2.Request( url, headers = { 'Content-Type': 'application/json' }, data = json.dumps( data ))
  44     req.get_method = lambda: 'PUT'
  45     return json.loads( urllib2.urlopen( req ).read() )
  46 
  47 def __del( api_key, url, data ):
  48     # Do the actual DELETE
  49     url = make_url( api_key, url )
  50     req = urllib2.Request( url, headers = { 'Content-Type': 'application/json' }, data = json.dumps( data ))
  51     req.get_method = lambda: 'DELETE'
  52     return json.loads( urllib2.urlopen( req ).read() )
  53 
  54 
  55 def display( api_key, url, return_formatted=True ):
  56     # Sends an API GET request and acts as a generic formatter for the JSON response.
  57     try:
  58         r = get( api_key, url )
  59     except urllib2.HTTPError, e:
  60         print e
  61         print e.read( 1024 ) # Only return the first 1K of errors.
  62         sys.exit( 1 )
  63     if type( r ) == unicode:
  64         print 'error: %s' % r
  65         return None
  66     if not return_formatted:
  67         return r
  68     elif type( r ) == list:
  69         # Response is a collection as defined in the REST style.
  70         print 'Collection Members'
  71         print '------------------'
  72         for n, i in enumerate(r):
  73             # All collection members should have a name and url in the response.
  74             print '#%d: %s' % (n+1, i.pop( 'url' ) )
  75             if 'name' in i:
  76                 print '  name: %s' % i.pop( 'name' )
  77             for k, v in i.items():
  78                 print '  %s: %s' % ( k, v )
  79         print ''
  80         print '%d element(s) in collection' % len( r )
  81     elif type( r ) == dict:
  82         # Response is an element as defined in the REST style.
  83         print 'Member Information'
  84         print '------------------'
  85         for k, v in r.items():
  86             print '%s: %s' % ( k, v )
  87     elif type( r ) == str:
  88         print r
  89     else:
  90         print 'response is unknown type: %s' % type( r )
  91 
  92 def submit( api_key, url, data, return_formatted=True ):
  93     # Sends an API POST request and acts as a generic formatter for the JSON response.
  94     # 'data' will become the JSON payload read by Galaxy.
  95     try:
  96         r = post( api_key, url, data )
  97     except urllib2.HTTPError, e:
  98         if return_formatted:
  99             print e
 100             print e.read( 1024 )
 101             sys.exit( 1 )
 102         else:
 103             return 'Error. '+ str( e.read( 1024 ) )
 104     if not return_formatted:
 105         return r
 106     print 'Response'
 107     print '--------'
 108     if type( r ) == list:
 109         # Currently the only implemented responses are lists of dicts, because
 110         # submission creates some number of collection elements.
 111         for i in r:
 112             if type( i ) == dict:
 113                 if 'url' in i:
 114                     print i.pop( 'url' )
 115                 else:
 116                     print '----'
 117                 if 'name' in i:
 118                     print '  name: %s' % i.pop( 'name' )
 119                 for k, v in i.items():
 120                     print '  %s: %s' % ( k, v )
 121             else:
 122                 print i
 123     else:
 124         print r
 125 
 126 def update( api_key, url, data, return_formatted=True ):
 127     # Sends an API PUT request and acts as a generic formatter for the JSON response.
 128     # 'data' will become the JSON payload read by Galaxy.
 129     try:
 130         r = put( api_key, url, data )
 131     except urllib2.HTTPError, e:
 132         if return_formatted:
 133             print e
 134             print e.read( 1024 )
 135             sys.exit( 1 )
 136         else:
 137             return 'Error. '+ str( e.read( 1024 ) )
 138     if not return_formatted:
 139         return r
 140     print 'Response'
 141     print '--------'
 142     print r
 143         
 144 def delete( api_key, url, data, return_formatted=True ):
 145     # Sends an API DELETE request and acts as a generic formatter for the JSON response.
 146     # 'data' will become the JSON payload read by Galaxy.
 147     try:
 148         r = __del( api_key, url, data )
 149     except urllib2.HTTPError, e:
 150         if return_formatted:
 151             print e
 152             print e.read( 1024 )
 153             sys.exit( 1 )
 154         else:
 155             return 'Error. '+ str( e.read( 1024 ) )
 156     if not return_formatted:
 157         return r
 158     print 'Response'
 159     print '--------'
 160     print r

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.