#!/usr/bin/env python

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

import setup
import common

# the REST URL for the user resource
RESOURCE_URL = '/api/users'

# ----------------------------------------------------------------------------- functions
def get_users():
    """
    Return a list of dictionaries that describe users.

    If the current user (accrd. to the api key) is an Admin, all registered
    users will be returned.
    If not an admin, only the current user will be returned.
    """
    # common.get needs the api key and a url - we can get these from the setup module
    apikey = setup.get_apikey()
    # attach the resource url to the base url that Galaxy is being served at
    full_url = setup.get_base_url() + RESOURCE_URL
    # common.get will send our info to galaxy, get galaxy's response,
    #   and parse it (from JSON) into a python list of dictionaries (one for each user)
    # we'll return that here
    return common.get( apikey, full_url )


# ----------------------------------------------------------------------------- main
if __name__ == '__main__':
    # get the resource data using the function we defined above
    returned = get_users()
    # pretty print the list of dictionaries that Galaxy sent us in response
    pprint.pprint( returned, indent=2 )
