import re, os, sys, time, threading

from util import *
from configuration import config
from recid_rules_db.control import run_transform
import MySQLdb

__all__ = ['TransformThread']
                
def _get_ext(path):
    return re.search('.sav|.txt|.xlsx|.xls', path).group()

class TransformThread(threading.Thread):
    def __init__(self, queue, db_info, file_name, arrest_del_queue, sentence_del_queue):
        threading.Thread.__init__(self)
        self.queue = queue
        self.db = MySQLdb.connect(host=config.db_host, user=db_info['user'], passwd=db_info['pass'], 
                           charset='utf8', db=db_info['name'], port=config.db_port, use_unicode=True)
        self.arrest_file = open(os.path.join(config.tmp_out_dir, 'transform', 'arrest', file_name), 'wb')
        self.sentence_file = open(os.path.join(config.tmp_out_dir, 'transform', 'sentence', file_name), 'wb')
        self.started = 0
        self.arrest_del_queue = arrest_del_queue
        self.sentence_del_queue = sentence_del_queue
        
    def run(self):
        while True:
            transform_options = self.queue.get()
            run_transform(self.db, self.arrest_file, self.sentence_file, self.arrest_del_queue, self.sentence_del_queue, **transform_options)
            self.queue.task_done()
        