import MySQLdb
import sys
import time


class opts:
    slen = 0

def progress(str):
        #@todo: rainbow asii animated progress nyan!
        str = " %s" % str

        while len(str) < opts.slen:
            str = '%s ' % str
        opts.slen = len(str)
        sys.stdout.write(str + '\r')
        sys.stdout.flush()


def main():
    conn = MySQLdb.connect( read_default_file = '~/.my.cnf', db='password_audit')
    curs = conn.cursor()

    wc = 0
    print 'Please wait getting count'
    f = open(sys.argv[1])
    for i in f:
        wc += 1

    print 'I will check %d words and insert those missing only' % wc

    f.seek(0)
    dc = 0
    pc = 0
    interval = 100000
    last_interval = time.time()
    for i in f:
        pc += 1
        i = i.rstrip('\n')
        #curs.execute('select count(*) from wordlist where word = %s',(i,))
        #res = curs.fetchall()
        #if res[0][0] == 0:
        dc += 1
        curs.execute('insert into wordlist (word) VALUES (%s) on duplicate key update word=word',(i,))
        if (pc % interval) == 0:
            ctime = time.time()
            eta = ((ctime - last_interval) * (wc/interval))
            m, s = divmod(eta, 60)
            h, m = divmod(m, 60)
            etastr = "%d:%02d:%02d" % (h, m, s)
            rate = (interval / (ctime - last_interval))
            #display progress every 100000 words,
            #you can of course change this but then it wastes cpu cycles rendering progress.
            progress('Progress %d/%d (%.2f%%) %d new words ETA %s @ %d words/s' % (
                pc,
                wc,
                (100.00 * ((1.00*pc)/(1.00*wc))),
                dc,
                etastr,
                rate
            ))
            conn.commit() #MySQLdb does not autocommit, lets not loose too much progress if we have to abort
            last_interval = time.time()
                
    conn.commit()
    conn.close()

if __name__ == '__main__':
    main()
