commit/galaxy-central: dannon: Tweaks to scripts/api/common.py to fix JSONDecodeError removal and clean up code.
1 new commit in galaxy-central: https://bitbucket.org/galaxy/galaxy-central/commits/d2f73445ce7a/ Changeset: d2f73445ce7a User: dannon Date: 2014-01-15 15:05:38 Summary: Tweaks to scripts/api/common.py to fix JSONDecodeError removal and clean up code. Affected #: 1 file diff -r d0302907cbbfbfbc8561bd3b2fe4a667161e67ff -r d2f73445ce7ada2f13947aa7c7c8506743d40d3f scripts/api/common.py --- a/scripts/api/common.py +++ b/scripts/api/common.py @@ -1,3 +1,6 @@ +""" +Common methods used by the API sample scripts. +""" import json import logging import os @@ -9,17 +12,16 @@ sys.path = new_path from galaxy import eggs -import pkg_resources +eggs.require( "pycrypto" ) -pkg_resources.require( "pycrypto" ) from Crypto.Cipher import Blowfish -from Crypto.Util.randpool import RandomPool -from Crypto.Util import number log = logging.getLogger( __name__ ) def make_url( api_key, url, args=None ): - # Adds the API Key to the URL if it's not already there. + """ + Adds the API Key to the URL if it's not already there. + """ if args is None: args = [] argsep = '&' @@ -30,29 +32,37 @@ return url + argsep + '&'.join( [ '='.join( t ) for t in args ] ) def get( api_key, url ): - # Do the actual GET. + """ + Do the actual GET. + """ url = make_url( api_key, url ) try: return json.loads( urllib2.urlopen( url ).read() ) - except json.decoder.JSONDecodeError, e: - print "URL did not return JSON data" + except ValueError, e: + print "URL did not return JSON data: %s" % e sys.exit(1) def post( api_key, url, data ): - # Do the actual POST. + """ + 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() ) def put( api_key, url, data ): - # Do the actual PUT + """ + Do the actual PUT + """ url = make_url( api_key, url ) req = urllib2.Request( url, headers = { 'Content-Type': 'application/json' }, data = json.dumps( data )) req.get_method = lambda: 'PUT' return json.loads( urllib2.urlopen( req ).read() ) def __del( api_key, url, data ): - # Do the actual DELETE + """ + Do the actual DELETE + """ url = make_url( api_key, url ) req = urllib2.Request( url, headers = { 'Content-Type': 'application/json' }, data = json.dumps( data )) req.get_method = lambda: 'DELETE' @@ -60,7 +70,9 @@ def display( api_key, url, return_formatted=True ): - # Sends an API GET request and acts as a generic formatter for the JSON response. + """ + Sends an API GET request and acts as a generic formatter for the JSON response. + """ try: r = get( api_key, url ) except urllib2.HTTPError, e: @@ -97,8 +109,10 @@ print 'response is unknown type: %s' % type( r ) def submit( api_key, url, data, return_formatted=True ): - # Sends an API POST request and acts as a generic formatter for the JSON response. - # 'data' will become the JSON payload read by Galaxy. + """ + Sends an API POST request and acts as a generic formatter for the JSON response. + 'data' will become the JSON payload read by Galaxy. + """ try: r = post( api_key, url, data ) except urllib2.HTTPError, e: @@ -131,8 +145,10 @@ print r def update( api_key, url, data, return_formatted=True ): - # Sends an API PUT request and acts as a generic formatter for the JSON response. - # 'data' will become the JSON payload read by Galaxy. + """ + Sends an API PUT request and acts as a generic formatter for the JSON response. + 'data' will become the JSON payload read by Galaxy. + """ try: r = put( api_key, url, data ) except urllib2.HTTPError, e: @@ -147,10 +163,12 @@ print 'Response' print '--------' print r - + def delete( api_key, url, data, return_formatted=True ): - # Sends an API DELETE request and acts as a generic formatter for the JSON response. - # 'data' will become the JSON payload read by Galaxy. + """ + Sends an API DELETE request and acts as a generic formatter for the JSON response. + 'data' will become the JSON payload read by Galaxy. + """ try: r = __del( api_key, url, data ) except urllib2.HTTPError, e: @@ -166,12 +184,14 @@ print '--------' print r -# utility method to encode ID's def encode_id( config_id_secret, obj_id ): + """ + utility method to encode ID's + """ id_cipher = Blowfish.new( config_id_secret ) # Convert to string s = str( obj_id ) - # Pad to a multiple of 8 with leading "!" + # Pad to a multiple of 8 with leading "!" s = ( "!" * ( 8 - len(s) % 8 ) ) + s # Encrypt return id_cipher.encrypt( s ).encode( 'hex' ) Repository URL: https://bitbucket.org/galaxy/galaxy-central/ -- This is a commit notification from bitbucket.org. You are receiving this because you have the service enabled, addressing the recipient of this email.
participants (1)
-
commits-noreply@bitbucket.org