from contextlib import closing
import os
from datetime import datetime
from ..util import regions, get_cnxn
from ..segment import Segment

def run(**opt):
    try:
        segment = opt['segment']
        del opt['segment']
        opt['limit'] = 1
        opt['out_format'] = "mysql"
        s = Segment(segment, 'datadump', **opt)
        s.load_metadata_variables()
        _created = False
        _regions = [str(region) for region in regions if str(region).isdigit()]
        with closing(get_cnxn(opt['db_info']['name'],opt)) as cnx:
            with closing(cnx.cursor()) as c:
                for region in _regions:
                    file = "/".join([opt['in_dir'],"process","".join(["a" if str(segment)=="arrest" else "c","state_",region]),".".join([segment,"txt"])])
                    print "Create temp: " + str(datetime.now())
                    with closing(open(str(file),"rb")) as tempfile:
                        newfile = str(file).replace(".txt","_temp.txt")
                        newfileopen = open(newfile,"w")
                        for line in tempfile.readlines():
                            line = line.split("|")
                            for i in range(0,len(line)):
                                line[i]=str(line[i]).replace("\\","\\\\")
                                if line[i]=="": line[i]="\N"
                            newfileopen.write(str("|".join(line)))
                        newfileopen.close()
                    print "Load file: " + str(datetime.now())
                    if os.path.exists(newfile):
                        if not _created:
                            c.execute('SHOW TABLES FROM %s' % opt['db_info']['name'])
                            if segment not in [table[0] for table in c.fetchall()]:
                                s.load_staged_data()
                                s.save()
                            _created = True
                        c.execute('''LOAD DATA LOCAL INFILE '%s' 
                                     REPLACE INTO TABLE %s 
                                     FIELDS TERMINATED BY %s 
                                     ESCAPED BY '\\\\'
                                     LINES TERMINATED BY '\r\n'
                                     IGNORE 1 LINES
                                  ''' % (newfile, segment,
                                         "'%s'" % opt.get('delimiter', '|')
                                         )
                                  )
                        os.unlink(newfile)
                        cnx.commit()
                    print "Finished loading file: " + str(datetime.now())
                    #break
        return 1
    except:
        if opt['verbose']:
            raise
        return (format_exc(), format_list(extract_stack()))