#python
import sys
from os.path import join
from datetime import datetime
from traceback import format_exc, extract_stack, format_list
#third-party
import cardsharp as cs
#src
from ..configuration import config
from ..util import get_max_review_round
from ..compare import compare
from ..report import Report

def run(**kw):
    try:
        print 'Starting %s compare...' % kw['segment']
        _src = join(config.review_dir, 'set_%s' % kw.get('set'), kw.get('segment'), kw.get('region'))
        _round = get_max_review_round(kw['segment'], kw['region'], kw['set'])
        comp_round = str(kw['round'])
        
        _filename = '%s.sav' % kw['segment'] if kw['segment'] != 'summary' else 'criminal_history_summary.sav'
        
        ds1 = cs.load(source=join(_src, 'round%s' % ((_round - 1) if comp_round == '-1' else comp_round), _filename), format='spss')
        cs.wait()
        ds2 = cs.load(source=join(_src, 'round%s' % _round, _filename), format='spss')
        cs.wait()
        compare(ds1, ds2, **kw)
        cs.wait()
        report = Report(join(_src, 'round%s' % _round), **kw)
        report.create_reports()
    
        print '...%s dataset compared at %s.' % (kw['segment'], datetime.now().isoformat(' '))
        return 1
    except:
        if kw['verbose']:
            raise
        return (format_exc(), format_list(extract_stack())) 