#!/usr/bin/env python
"""
exhibit_agg

Aggregation tool for exhibits. Can be used to merge and filter exhibits

"""

import re
import sys
import os
import glob
import time
from itertools import islice

from amara.thirdparty import httplib2, json
#from amara.lib.iri import relativize, absolutize

#from amara.lib.util import coroutine
from datachef import exhibit


def run(inputs=None, filt=None, match=None, out=None, fixup=False):
    '''
    See the command line help
    '''
    template = json.load(inputs[0])
    outdata = template.copy()
    outdata['items'] = []
    criteria = lambda x: True
    #FIXME: if match & filter are both used, only match will take effect
    if filt:
        def criteria(item):
            context = {'item': item}
            return not not eval(filt, context, context)

    if match:
        matchdata = json.load(match)
        match_ids = set(( i.get('id', i.get('label')) for i in matchdata['items'] ))
        #The above logic will use an ID of None for any item with neither id nor label
        match_ids.discard(None)
        criteria = lambda item: item.get('id', item.get('label')) in match_ids

    for inp in inputs:
        if template:
            indata = template
            template = None
        else:
            indata = json.load(inp)
        out_items = outdata['items']
        if fixup:
            legend = {}
        for item in indata['items']:
            if criteria(item):
                out_items.append(item)
            if fixup:
                exhibit.fixup(item, legend)
    json.dump(outdata, out, indent=4)

    return
    try:
        pass #print >> sys.stderr, '...'
    except (KeyboardInterrupt, SystemExit):
        raise
    except Exception as e:
        print >> sys.stderr, 'An exception here might mean you do not have Akara running at %s:'%moinid, e
    return


# Handle the command-line arguments

from akara.thirdparty import argparse #Sorry PEP 8 ;)

#import signal
#import shutil

if __name__ == '__main__':
    #parser = argparse.ArgumentParser(prog="bootstrap", add_help=False)
    parser = argparse.ArgumentParser()
    #parser.add_argument('-o', '--output')
    parser.add_argument('inputs', type=argparse.FileType('r'), metavar='inputs', nargs='+',
                        help='One or more input exhibit to combine/trim. If there are more than one, the first one will become the template (including data types) and items from the rest will be merged in')
    parser.add_argument('-o', '--out', type=argparse.FileType('w'), default=sys.stdout,
        help='file where output should be written '
             '(default: write to stdout)')
    parser.add_argument('-c', '--cmatch', type=argparse.FileType('r'),
        help='cross-match: a separate exhibit used to match IDs for the input. Only IDs in the input that appear in the match exhibit will be filtered through')
    parser.add_argument('-f', '--filter', metavar="PYTHON_EXPR", dest="filt",
        help='filter: a python expression evaluated for each input item. If it evaluates to True the item is copied to the output')
    parser.add_argument('--fixup', action='store_true',
        help='whether to fix up common problems in the exhibit file format')
    #
    args = parser.parse_args()

    run(inputs=args.inputs, filt=args.filt, match=args.cmatch, out=args.out, fixup=args.fixup)
    args.out.close()

