root/galaxy-central/scripts/api/common.py

リビジョン 2, 3.3 KB (コミッタ: hatakeyama, 14 年 前)

import galaxy-central

行番号 
1import os, sys, urllib, urllib2
2
3new_path = [ os.path.join( os.path.dirname( __file__ ), '..', '..', 'lib' ) ]
4new_path.extend( sys.path[1:] )
5sys.path = new_path
6
7from galaxy import eggs
8import pkg_resources
9
10pkg_resources.require( "simplejson" )
11import simplejson
12
13def make_url( api_key, url, args=None ):
14    # Adds the API Key to the URL if it's not already there.
15    if args is None:
16        args = []
17    argsep = '&'
18    if '?' not in url:
19        argsep = '?'
20    if '?key=' not in url and '&key=' not in url:
21        args.insert( 0, ( 'key', api_key ) )
22    return url + argsep + '&'.join( [ '='.join( t ) for t in args ] )
23
24def get( api_key, url ):
25    # Do the actual GET.
26    url = make_url( api_key, url )
27    try:
28        return simplejson.loads( urllib2.urlopen( url ).read() )
29    except simplejson.decoder.JSONDecodeError, e:
30        print "URL did not return JSON data"
31        sys.exit(1)
32
33def post( api_key, url, data ):
34    # Do the actual POST.
35    url = make_url( api_key, url )
36    req = urllib2.Request( url, headers = { 'Content-Type': 'application/json' }, data = simplejson.dumps( data ) )
37    return simplejson.loads( urllib2.urlopen( req ).read() )
38
39def display( api_key, url ):
40    # Sends an API GET request and acts as a generic formatter for the JSON response.
41    try:
42        r = get( api_key, url )
43    except urllib2.HTTPError, e:
44        print e
45        print e.read( 1024 ) # Only return the first 1K of errors.
46        sys.exit( 1 )
47    if type( r ) == unicode:
48        print 'error: %s' % r
49        return None
50    elif type( r ) == list:
51        # Response is a collection as defined in the REST style.
52        print 'Collection Members'
53        print '------------------'
54        for n, i in enumerate(r):
55            # All collection members should have a name and url in the response.
56            print '#%d: %s' % (n+1, i.pop( 'url' ) )
57            print '  name: %s' % i.pop( 'name' )
58            for k, v in i.items():
59                print '  %s: %s' % ( k, v )
60        print ''
61        print '%d element(s) in collection' % len( r )
62    elif type( r ) == dict:
63        # Response is an element as defined in the REST style.
64        print 'Member Information'
65        print '------------------'
66        for k, v in r.items():
67            print '%s: %s' % ( k, v )
68    else:
69        print 'response is unknown type: %s' % type( r )
70
71def submit( api_key, url, data ):
72    # Sends an API POST request and acts as a generic formatter for the JSON response.
73    # 'data' will become the JSON payload read by Galaxy.
74    try:
75        r = post( api_key, url, data )
76    except urllib2.HTTPError, e:
77        print e
78        print e.read( 1024 )
79        sys.exit( 1 )
80    print 'Response'
81    print '--------'
82    if type( r ) == list:
83        # Currently the only implemented responses are lists of dicts, because
84        # submission creates some number of collection elements.
85        for i in r:
86            if type( i ) == dict:
87                if 'url' in i:
88                    print i.pop( 'url' )
89                else:
90                    print '----'
91                if 'name' in i:
92                    print '  name: %s' % i.pop( 'name' )
93                for k, v in i.items():
94                    print '  %s: %s' % ( k, v )
95            else:
96                print i
97    else:
98        print r
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。