import os
from datetime import datetime
from traceback import format_exc, extract_stack, format_list

import cardsharp as cs

from ..configuration import segment_info, config
from util import *
from ..util import set_LOOKUPS
from ..transform import *
from ..segment import Segment

_segments = {}
for s in segment_info.itervalues():
    _segments[s['label']] = {'label': s['label'], 
                             'id': s['id'], 
                             'region_var': s['region_var'], 
                             'priority': s['priority']}
def run(**opt):
    """Apply BJS processing rules."""    
    try:
        #TODO add for only checking validation
        set_LOOKUPS() #set the lookup table values
        segment = _segments[opt['segment']]
        del(opt['segment'])
        opt['segments'] = get_segments('pre_process', **opt) #used to create the crosswalk variables
            
        """Apply BJS processing rules."""
        opt['out_dir'] = os.path.join(opt['out_dir'], 'pre_process')
        
        #TODO drop subject from segment_list before processing
        
        opt['log_dir'] = os.path.join(config.log_dir, 'set_%i' % config.current_set, 'pre_process', segment.get('label'), opt['in_where'] if opt.get('in_where') else 'all')
        if not os.path.isdir(os.path.normpath(opt['log_dir'])):
            os.makedirs(os.path.normpath(opt['log_dir']))
        
        rules = get_rules(['1', '2', '3'], segment, **opt)
        
        print "Starting %s processing at %s:" % (segment.get('label'), datetime.now().isoformat(' '))
        
        print "Begin data load and variable formatting..."
        
        s = Segment(segment.get('label'), 'pre_process', **opt)
        s.load_metadata_variables()
        s.load_staged_data()
        print "load and formatting complete."
        
        run_rules(['1', '2', '3'], rules, s, s.name)
        
        cs.wait()
        
        print "Saving and creating reports..."
        s.save()
    
    #    time = sub(':', '-', datetime.now().isoformat('_'))
    #    report = ReportNew('pre_process', opt['db_info'])
    #    report.create_reports()
        cs.wait()
        print "save complete at %s." % datetime.now().isoformat(' ')
        return 1
    except:
        if opt['verbose']:
            raise
        return (format_exc(), format_list(extract_stack())) 
                      