#!/usr/bin/env python

"""
Access a Galaxy instance and get data for Tools.

(You definitely can get quite a bit of tool info currently from the API, but
for the purposes of this workshop we won't fill this module out except for the
upload_hda function.)

Allows us to create an HDA by uploading a file.
"""
import os
import sys
import pprint

import setup
import common

# This script is from scripts/api in your galaxy installation and (unfortunately) relies
#   on the requests module (available from See http://docs.python-requests.org/en/latest/)
# It does most of the heavy lifting for getting a file into the API call and - for the
#   purposes of the workshop - this section won't be explained in full
import upload_to_history

RESOURCE_URL = '/api/tools'

# ----------------------------------------------------------------------------- functions
def upload_hda( history_id, filepath ):
    """
    Use the upload_to_history script to upload the file at `filepath`
    to the history with the id `history_id`.
    :param history_id: the id of the history where the new HDA will be created
    :param filepath: the local OS path and filename of the file to upload
    """
    apikey = setup.get_apikey()
    # upload_to_history builds it's own url - so we only need to send in the base
    base_url = setup.get_base_url()
    return upload_to_history.upload_file( base_url, apikey, history_id, filepath )


# ----------------------------------------------------------------------------- main
if __name__ == '__main__':
    # this time two arguments:
    if len( sys.argv ) <= 2:
        print 'USAGE: hdas_1.py <history id> <filepath to upload>'
        sys.exit( 1 )

    history_id = sys.argv[1]
    filepath   = sys.argv[2]
    returned = upload_hda( history_id, filepath )
    pprint.pprint( returned, indent=2 )
