#!/usr/bin/env python

"""
Access a Galaxy instance and get data for Datasets.
"""
import os
import sys
import pprint

import setup
import common

RESOURCE_URL = '/api/datasets'

# ----------------------------------------------------------------------------- functions
def get_datasets():
    """
    Return a list of dictionaries that describe datasets.
    """
    apikey = setup.get_apikey()
    full_url = setup.get_base_url() + RESOURCE_URL
    return common.get( apikey, full_url )

def get_dataset( dataset_id ):
    """
    Return a dictionary of detailed information for a specific dataset.
    :param dataset_id: the id of the dataset which we want to query.
    """
    apikey = setup.get_apikey()
    resource_url = setup.get_base_url() + RESOURCE_URL

    full_url = resource_url + '/' + dataset_id
    return common.get( apikey, full_url )

def get_dataset_column( dataset_id, column ):
    """
    Return a dictionary of detailed information for a specific dataset.
    :param dataset_id: the id of the dataset which we want to query.
    """
    # here, we'll pass some extra arguments into the URL to get what we want - column data from the dataset's file
    # first, build the base url for the dataset resource (in this case, datasets.show):
    apikey = setup.get_apikey()
    resource_url = setup.get_base_url() + RESOURCE_URL
    datasets_show_url = resource_url + '/' + dataset_id

    # arguments to the api can be added in two different ways:
    #   1. on the query string (you can see query string arguments when you do a google search -
    #        they appear in your address bar separated by '&', '=', etc. - all after a '?')
    #   2. post data - data that gets attached (gen. in JSON format) to your request
    #       without adding it directly to the url

    # something to keep in mind when using GET API functions:
    #   every argument needs to be a well formed string
    # if you have problems calling a particular GET API function, one thing to check is how your arguments are
    #   being converted to strings

    # our arguments to the datasets API function will be:
    # 1. the type of data we want: raw_data
    data_type = 'raw_data'
    # 2. which provider we want - in this case we need a 'column' data provider
    provider = 'column'
    # 3. which column we want in JSON array format - we'll also make sure it's a string
    #   (you can use a csv string to get more than one column but we'll keep things simple here)
    columns = '[' + str( column ) + ']'

    # here we'll use common.make_url - this is a convenience function that will
    #   place all the arguments we need into the /api/datasets URL in the proper GET style
    # we'll need to bundle them into a python list of tuples first: [ ( arg1_name, arg1_value ), ... ]
    arg_list = [ ( 'data_type', data_type ), ( 'provider', provider ), ( 'columns', columns ) ]
    full_url = common.make_url( apikey, datasets_show_url, args=arg_list )

    # (let's also print the final url to see what make_url did:)
    print 'get_dataset_column, full_url:', full_url

    return common.get( apikey, full_url )


# ----------------------------------------------------------------------------- main
if __name__ == '__main__':
    if len( sys.argv ) <= 2:
        print 'USAGE: datasets_3.py <dataset_id> <0-based column index>'
        sys.exit( 1 )

    dataset_id = sys.argv[1]
    column = sys.argv[2]
    returned = get_dataset_column( dataset_id, column )
    pprint.pprint( returned, indent=2 )
