#!/usr/bin/env python
"""
Lists XNAT objects.
"""
import os
import sys
import imp
import argparse
from qiutil.command import configure_log
import qixnat
from qixnat import command
from qixnat.helpers import (xnat_name, standardize_experiment_child_hierarchy)


class SessionNotFoundError(Exception):
    pass


def main(argv=sys.argv):
    # Parse the command line arguments.
    path_str, opts = _parse_arguments()
    # The XNAT configuration.
    config = opts.pop('config', None)
    # Configure the logger.
    configure_log('qils', opts)

    # Parse the XNAT hierarchy argument.
    path_items = path_str.split('/')
    if len(path_items) < 2:
        raise ValueError('The search path must include at least two items,'
                         ' e.g. Breast003/Session01')
    # The XNAT project (default 'QIN').
    prj = opts.pop('project', qixnat.project())
    # The subject/session from the command line path argument.
    sbj, sess = path_items[0:2]
    # The rest of the command line path argument.
    child_path = standardize_experiment_child_hierarchy(path_items[2:])
    # Print the XNAT object names specified by the path. 
    with qixnat.connection.connect(config) as xnat:
        sess_obj = xnat.get_session(prj, sbj, sess)
        if not xnat.exists(sess_obj):
            raise SessionNotFoundError("No such XNAT session: %s %s %s" %
                                       (prj, sbj, sess))
        for child in xnat.expand_child_hierarchy(sess_obj, child_path):
            print xnat_name(child)

    return 0


def _parse_arguments():
    """Parses the command line arguments."""
    parser = argparse.ArgumentParser()

    # The log options.
    qixnat.command.add_options(parser)

    # The input XNAT hierarchy path.
    parser.add_argument('path', help='the target XNAT object path')

    args = vars(parser.parse_args())
    nonempty_args = dict((k, v) for k, v in args.iteritems() if v != None)

    return nonempty_args.pop('path'), nonempty_args


if __name__ == '__main__':
    sys.exit(main())
