diff --git a/.ci/py3_sources.txt b/.ci/py3_sources.txt index 1852fbd39b3..75869f8cb23 100644 --- a/.ci/py3_sources.txt +++ b/.ci/py3_sources.txt @@ -12,3 +12,6 @@ lib/galaxy/tools/toolbox/ lib/galaxy/tools/parser/ lib/galaxy/jobs/metrics/ lib/galaxy/objectstore/ +scripts/api/common.py +scripts/api/display.py +scripts/api/workflow_execute_parameters.py diff --git a/scripts/api/common.py b/scripts/api/common.py index 8a054b6bc89..63ad99d0a19 100644 --- a/scripts/api/common.py +++ b/scripts/api/common.py @@ -1,12 +1,15 @@ """ Common methods used by the API sample scripts. """ +from __future__ import print_function + import json import logging import sys -import urllib2 from Crypto.Cipher import Blowfish +from six.moves.urllib.error import HTTPError +from six.moves.urllib.request import Request, urlopen log = logging.getLogger( __name__ ) @@ -31,9 +34,9 @@ def get( api_key, url ): """ url = make_url( api_key, url ) try: - return json.loads( urllib2.urlopen( url ).read() ) - except ValueError, e: - print "URL did not return JSON data: %s" % e + return json.loads( urlopen( url ).read() ) + except ValueError as e: + print("URL did not return JSON data: %s" % e) sys.exit(1) @@ -42,8 +45,8 @@ def post( api_key, url, data ): Do the actual POST. """ url = make_url( api_key, url ) - req = urllib2.Request( url, headers={ 'Content-Type': 'application/json' }, data=json.dumps( data ) ) - return json.loads( urllib2.urlopen( req ).read() ) + req = Request( url, headers={ 'Content-Type': 'application/json' }, data=json.dumps( data ) ) + return json.loads( urlopen( req ).read() ) def put( api_key, url, data ): @@ -51,9 +54,9 @@ def put( api_key, url, data ): Do the actual PUT """ url = make_url( api_key, url ) - req = urllib2.Request( url, headers={ 'Content-Type': 'application/json' }, data=json.dumps( data )) + req = Request( url, headers={ 'Content-Type': 'application/json' }, data=json.dumps( data )) req.get_method = lambda: 'PUT' - return json.loads( urllib2.urlopen( req ).read() ) + return json.loads( urlopen( req ).read() ) def __del( api_key, url, data ): @@ -61,9 +64,9 @@ def __del( api_key, url, data ): Do the actual DELETE """ url = make_url( api_key, url ) - req = urllib2.Request( url, headers={ 'Content-Type': 'application/json' }, data=json.dumps( data )) + req = Request( url, headers={ 'Content-Type': 'application/json' }, data=json.dumps( data )) req.get_method = lambda: 'DELETE' - return json.loads( urllib2.urlopen( req ).read() ) + return json.loads( urlopen( req ).read() ) def display( api_key, url, return_formatted=True ): @@ -72,40 +75,37 @@ def display( api_key, url, return_formatted=True ): """ try: r = get( api_key, url ) - except urllib2.HTTPError, e: - print e - print e.read( 1024 ) # Only return the first 1K of errors. + except HTTPError as e: + print(e) + print(e.read( 1024 )) # Only return the first 1K of errors. sys.exit( 1 ) - if type( r ) == unicode: - print 'error: %s' % r - return None if not return_formatted: return r elif type( r ) == list: # Response is a collection as defined in the REST style. - print 'Collection Members' - print '------------------' + print('Collection Members') + print('------------------') for n, i in enumerate(r): # All collection members should have a name in the response. # url is optional if 'url' in i: - print '#%d: %s' % (n + 1, i.pop( 'url' ) ) + print('#%d: %s' % (n + 1, i.pop( 'url' ) )) if 'name' in i: - print ' name: %s' % i.pop( 'name' ) + print(' name: %s' % i.pop( 'name' )) for k, v in i.items(): - print ' %s: %s' % ( k, v ) - print '' - print '%d element(s) in collection' % len( r ) + print(' %s: %s' % ( k, v )) + print('') + print('%d element(s) in collection' % len( r )) elif type( r ) == dict: # Response is an element as defined in the REST style. - print 'Member Information' - print '------------------' + print('Member Information') + print('------------------') for k, v in r.items(): - print '%s: %s' % ( k, v ) + print('%s: %s' % ( k, v )) elif type( r ) == str: - print r + print(r) else: - print 'response is unknown type: %s' % type( r ) + print('response is unknown type: %s' % type( r )) def submit( api_key, url, data, return_formatted=True ): @@ -115,34 +115,34 @@ def submit( api_key, url, data, return_formatted=True ): """ try: r = post( api_key, url, data ) - except urllib2.HTTPError, e: + except HTTPError as e: if return_formatted: - print e - print e.read( 1024 ) + print(e) + print(e.read( 1024 )) sys.exit( 1 ) else: return 'Error. ' + str( e.read( 1024 ) ) if not return_formatted: return r - print 'Response' - print '--------' + print('Response') + print('--------') if type( r ) == list: # Currently the only implemented responses are lists of dicts, because # submission creates some number of collection elements. for i in r: if type( i ) == dict: if 'url' in i: - print i.pop( 'url' ) + print(i.pop( 'url' )) else: - print '----' + print('----') if 'name' in i: - print ' name: %s' % i.pop( 'name' ) + print(' name: %s' % i.pop( 'name' )) for k, v in i.items(): - print ' %s: %s' % ( k, v ) + print(' %s: %s' % ( k, v )) else: - print i + print(i) else: - print r + print(r) def update( api_key, url, data, return_formatted=True ): @@ -152,18 +152,18 @@ def update( api_key, url, data, return_formatted=True ): """ try: r = put( api_key, url, data ) - except urllib2.HTTPError, e: + except HTTPError as e: if return_formatted: - print e - print e.read( 1024 ) + print(e) + print(e.read( 1024 )) sys.exit( 1 ) else: return 'Error. ' + str( e.read( 1024 ) ) if not return_formatted: return r - print 'Response' - print '--------' - print r + print('Response') + print('--------') + print(r) def delete( api_key, url, data, return_formatted=True ): @@ -173,18 +173,18 @@ def delete( api_key, url, data, return_formatted=True ): """ try: r = __del( api_key, url, data ) - except urllib2.HTTPError, e: + except HTTPError as e: if return_formatted: - print e - print e.read( 1024 ) + print(e) + print(e.read( 1024 )) sys.exit( 1 ) else: return 'Error. ' + str( e.read( 1024 ) ) if not return_formatted: return r - print 'Response' - print '--------' - print r + print('Response') + print('--------') + print(r) def encode_id( config_id_secret, obj_id ): diff --git a/scripts/api/display.py b/scripts/api/display.py index 621e8f75d21..641f58c2428 100755 --- a/scripts/api/display.py +++ b/scripts/api/display.py @@ -1,7 +1,10 @@ #!/usr/bin/env python +from __future__ import print_function + import os import sys -import urllib2 + +from six.moves.urllib.error import URLError sys.path.insert( 0, os.path.dirname( __file__ ) ) @@ -9,9 +12,10 @@ from common import display try: display( *sys.argv[1:3] ) -except TypeError: - print 'usage: %s key url' % os.path.basename( sys.argv[0] ) +except TypeError as e: + print('usage: %s key url' % os.path.basename( sys.argv[0] )) + print(e) sys.exit( 1 ) -except urllib2.URLError, e: - print str(e) +except URLError as e: + print(e) sys.exit( 1 ) diff --git a/scripts/api/workflow_execute_parameters.py b/scripts/api/workflow_execute_parameters.py index 06963fd0115..205f45f7e6d 100644 --- a/scripts/api/workflow_execute_parameters.py +++ b/scripts/api/workflow_execute_parameters.py @@ -9,6 +9,7 @@ Example calls: python workflow_execute.py /api/workflows 'hist_id=' '38=hda=' 'param=tool=name=value' python workflow_execute_parameters.py http://localhost:8080/api/workflows 1cd8e2f6b131e891 'Test API' '69=ld=a799d38679e985db' '70=ld=33b43b4e7093c91f' 'param=peakcalling_spp=aligner=bowtie' 'param=bowtie_wrapper=suppressHeader=True' 'param=peakcalling_spp=window_size=1000' """ +from __future__ import print_function import os import sys @@ -47,7 +48,7 @@ def main(): print("TOOL ID ERROR:") except IndexError: - print 'usage: %s key url workflow_id history step=src=dataset_id' % os.path.basename(sys.argv[0]) + print('usage: %s key url workflow_id history step=src=dataset_id' % os.path.basename(sys.argv[0])) sys.exit(1) submit( sys.argv[1], sys.argv[2], data )